20 #include <freerdp/config.h>
24 #include <winpr/crt.h>
25 #include <winpr/assert.h>
27 #include <freerdp/freerdp.h>
28 #include <freerdp/constants.h>
29 #include <winpr/stream.h>
31 #include <freerdp/log.h>
32 #include <freerdp/gdi/bitmap.h>
34 #include "../gdi/gdi.h"
35 #include "../core/graphics.h"
40 #define TAG FREERDP_TAG("cache.bitmap")
42 static rdpBitmap* bitmap_cache_get(
rdpBitmapCache* bitmapCache, UINT32
id, UINT32 index);
43 static BOOL bitmap_cache_put(
rdpBitmapCache* bitmapCache, UINT32
id, UINT32 index,
46 static BOOL update_gdi_memblt(rdpContext* context,
MEMBLT_ORDER* memblt)
48 rdpBitmap* bitmap = NULL;
49 rdpCache* cache = NULL;
51 cache = context->cache;
53 if (memblt->cacheId == 0xFF)
54 bitmap = offscreen_cache_get(cache->offscreen, memblt->cacheIndex);
56 bitmap = bitmap_cache_get(cache->bitmap, (BYTE)memblt->cacheId, memblt->cacheIndex);
62 memblt->bitmap = bitmap;
63 return IFCALLRESULT(TRUE, cache->bitmap->MemBlt, context, memblt);
66 static BOOL update_gdi_mem3blt(rdpContext* context,
MEM3BLT_ORDER* mem3blt)
69 rdpBitmap* bitmap = NULL;
70 rdpCache* cache = context->cache;
71 rdpBrush* brush = &mem3blt->brush;
74 if (mem3blt->cacheId == 0xFF)
75 bitmap = offscreen_cache_get(cache->offscreen, mem3blt->cacheIndex);
77 bitmap = bitmap_cache_get(cache->bitmap, (BYTE)mem3blt->cacheId, mem3blt->cacheIndex);
85 if (brush->style & CACHED_BRUSH)
87 brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
95 mem3blt->bitmap = bitmap;
96 IFCALLRET(cache->bitmap->Mem3Blt, ret, context, mem3blt);
101 static BOOL update_gdi_cache_bitmap(rdpContext* context,
const CACHE_BITMAP_ORDER* cacheBitmap)
103 rdpBitmap* bitmap = NULL;
104 rdpBitmap* prevBitmap = NULL;
105 rdpCache* cache = context->cache;
106 bitmap = Bitmap_Alloc(context);
111 Bitmap_SetDimensions(bitmap, cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight);
113 if (!bitmap->Decompress(context, bitmap, cacheBitmap->bitmapDataStream,
114 cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight,
115 cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength,
116 cacheBitmap->compressed, RDP_CODEC_ID_NONE))
118 Bitmap_Free(context, bitmap);
122 if (!bitmap->New(context, bitmap))
124 Bitmap_Free(context, bitmap);
128 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex);
129 Bitmap_Free(context, prevBitmap);
130 return bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap);
136 rdpBitmap* prevBitmap = NULL;
137 rdpCache* cache = context->cache;
138 rdpSettings* settings = context->settings;
139 rdpBitmap* bitmap = Bitmap_Alloc(context);
145 bitmap->key64 = ((UINT64)cacheBitmapV2->key1 | (((UINT64)cacheBitmapV2->key2) << 32));
147 if (!cacheBitmapV2->bitmapBpp)
148 cacheBitmapV2->bitmapBpp = ColorDepth;
150 if ((ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16))
151 cacheBitmapV2->bitmapBpp = ColorDepth;
153 Bitmap_SetDimensions(bitmap, cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight);
155 if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream,
156 cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight,
157 cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength,
158 cacheBitmapV2->compressed, RDP_CODEC_ID_NONE))
161 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex);
163 if (!bitmap->New(context, bitmap))
166 Bitmap_Free(context, prevBitmap);
167 return bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex,
171 Bitmap_Free(context, bitmap);
177 rdpBitmap* bitmap = NULL;
178 rdpBitmap* prevBitmap = NULL;
179 BOOL compressed = TRUE;
180 rdpCache* cache = context->cache;
181 rdpSettings* settings = context->settings;
183 bitmap = Bitmap_Alloc(context);
189 bitmap->key64 = ((UINT64)cacheBitmapV3->key1 | (((UINT64)cacheBitmapV3->key2) << 32));
191 if (!cacheBitmapV3->bpp)
192 cacheBitmapV3->bpp = ColorDepth;
194 compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE);
195 Bitmap_SetDimensions(bitmap, bitmapData->width, bitmapData->height);
197 if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width,
198 bitmapData->height, bitmapData->bpp, bitmapData->length, compressed,
199 bitmapData->codecID))
202 if (!bitmap->New(context, bitmap))
205 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex);
206 Bitmap_Free(context, prevBitmap);
207 return bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex,
211 Bitmap_Free(context, bitmap);
215 rdpBitmap* bitmap_cache_get(
rdpBitmapCache* bitmapCache, UINT32
id, UINT32 index)
217 rdpBitmap* bitmap = NULL;
219 if (
id >= bitmapCache->maxCells)
221 WLog_ERR(TAG,
"get invalid bitmap cell id: %" PRIu32
"",
id);
225 if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
227 index = bitmapCache->cells[id].number;
229 else if (index > bitmapCache->cells[
id].number)
231 WLog_ERR(TAG,
"get invalid bitmap index %" PRIu32
" in cell id: %" PRIu32
"", index,
id);
235 bitmap = bitmapCache->cells[id].entries[index];
239 BOOL bitmap_cache_put(
rdpBitmapCache* bitmapCache, UINT32
id, UINT32 index, rdpBitmap* bitmap)
241 if (
id > bitmapCache->maxCells)
243 WLog_ERR(TAG,
"put invalid bitmap cell id: %" PRIu32
"",
id);
247 if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
249 index = bitmapCache->cells[id].number;
251 else if (index > bitmapCache->cells[
id].number)
253 WLog_ERR(TAG,
"put invalid bitmap index %" PRIu32
" in cell id: %" PRIu32
"", index,
id);
257 bitmapCache->cells[id].entries[index] = bitmap;
261 void bitmap_cache_register_callbacks(rdpUpdate* update)
263 rdpCache* cache = NULL;
265 WINPR_ASSERT(update);
266 WINPR_ASSERT(update->context);
267 WINPR_ASSERT(update->context->cache);
269 cache = update->context->cache;
274 cache->bitmap->MemBlt = update->primary->MemBlt;
275 cache->bitmap->Mem3Blt = update->primary->Mem3Blt;
276 update->primary->MemBlt = update_gdi_memblt;
277 update->primary->Mem3Blt = update_gdi_mem3blt;
278 update->secondary->CacheBitmap = update_gdi_cache_bitmap;
279 update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2;
280 update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3;
281 update->BitmapUpdate = gdi_bitmap_update;
285 static int bitmap_cache_save_persistent(
rdpBitmapCache* bitmapCache)
287 rdpContext* context = bitmapCache->context;
288 rdpSettings* settings = context->settings;
298 const char* BitmapCachePersistFile =
300 if (!BitmapCachePersistFile)
303 rdpPersistentCache* persistent = persistent_cache_new();
308 int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version);
313 if (bitmapCache->cells)
315 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
318 for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++)
321 rdpBitmap* bitmap = cell->entries[j];
323 if (!bitmap || !bitmap->key64)
326 cacheEntry.key64 = bitmap->key64;
327 cacheEntry.width = bitmap->width;
328 cacheEntry.height = bitmap->height;
329 const UINT64 size = 4ULL * bitmap->width * bitmap->height;
330 if (size > UINT32_MAX)
332 cacheEntry.size = (UINT32)size;
333 cacheEntry.flags = 0;
334 cacheEntry.data = bitmap->data;
336 if (persistent_cache_write_entry(persistent, &cacheEntry) < 1)
348 persistent_cache_free(persistent);
354 rdpSettings* settings = NULL;
357 WINPR_ASSERT(context);
359 settings = context->settings;
360 WINPR_ASSERT(settings);
367 const UINT32 BitmapCacheV2NumCells =
369 bitmapCache->context = context;
372 if (!bitmapCache->cells)
374 bitmapCache->maxCells = BitmapCacheV2NumCells;
376 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
379 freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
381 UINT32 nr = info->numEntries;
383 cell->entries = (rdpBitmap**)calloc((nr + 1),
sizeof(rdpBitmap*));
392 WINPR_PRAGMA_DIAG_PUSH
393 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
394 bitmap_cache_free(bitmapCache);
395 WINPR_PRAGMA_DIAG_POP
404 bitmap_cache_save_persistent(bitmapCache);
406 if (bitmapCache->cells)
408 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
416 for (j = 0; j < cell->number + 1; j++)
418 rdpBitmap* bitmap = cell->entries[j];
419 Bitmap_Free(bitmapCache->context, bitmap);
425 free(bitmapCache->cells);
428 persistent_cache_free(bitmapCache->persistent);
433 static void free_bitmap_data(
BITMAP_DATA* data,
size_t count)
438 for (
size_t x = 0; x < count; x++)
439 free(data[x].bitmapDataStream);
451 for (
size_t x = 0; x < count; x++)
455 if (data[x].bitmapLength > 0)
457 dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
459 if (!dst[x].bitmapDataStream)
462 memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
468 free_bitmap_data(dst, count);
472 void free_bitmap_update(rdpContext* context,
BITMAP_UPDATE* pointer)
477 free_bitmap_data(pointer->rectangles, pointer->number);
485 if (!dst || !pointer)
489 dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
491 if (!dst->rectangles)
496 WINPR_PRAGMA_DIAG_PUSH
497 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
498 free_bitmap_update(context, dst);
499 WINPR_PRAGMA_DIAG_POP
512 if (order->bitmapLength > 0)
514 dst->bitmapDataStream = malloc(order->bitmapLength);
516 if (!dst->bitmapDataStream)
519 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
524 WINPR_PRAGMA_DIAG_PUSH
525 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
526 free_cache_bitmap_order(context, dst);
527 WINPR_PRAGMA_DIAG_POP
534 free(order->bitmapDataStream);
549 if (order->bitmapLength > 0)
551 dst->bitmapDataStream = malloc(order->bitmapLength);
553 if (!dst->bitmapDataStream)
556 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
561 WINPR_PRAGMA_DIAG_PUSH
562 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
563 free_cache_bitmap_v2_order(context, dst);
564 WINPR_PRAGMA_DIAG_POP
571 free(order->bitmapDataStream);
586 if (order->bitmapData.length > 0)
588 dst->bitmapData.data = malloc(order->bitmapData.length);
590 if (!dst->bitmapData.data)
593 memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
598 WINPR_PRAGMA_DIAG_PUSH
599 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
600 free_cache_bitmap_v3_order(context, dst);
601 WINPR_PRAGMA_DIAG_POP
608 free(order->bitmapData.data);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.