FreeRDP
Loading...
Searching...
No Matches
cache/bitmap.c
1
20#include <freerdp/config.h>
21
22#include <stdio.h>
23
24#include <winpr/crt.h>
25#include <winpr/assert.h>
26#include <winpr/cast.h>
27
28#include <freerdp/freerdp.h>
29#include <freerdp/constants.h>
30#include <winpr/stream.h>
31
32#include <freerdp/log.h>
33#include <freerdp/gdi/bitmap.h>
34
35#include "../gdi/gdi.h"
36#include "../core/graphics.h"
37
38#include "bitmap.h"
39#include "cache.h"
40
41#define TAG FREERDP_TAG("cache.bitmap")
42
43static rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index);
44static BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index,
45 rdpBitmap* bitmap);
46
47static BOOL update_gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt)
48{
49 rdpBitmap* bitmap = NULL;
50 rdpCache* cache = NULL;
51
52 cache = context->cache;
53
54 if (memblt->cacheId == 0xFF)
55 bitmap = offscreen_cache_get(cache->offscreen, memblt->cacheIndex);
56 else
57 bitmap = bitmap_cache_get(cache->bitmap, (BYTE)memblt->cacheId, memblt->cacheIndex);
58
59 /* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
60 if (bitmap == NULL)
61 return TRUE;
62
63 memblt->bitmap = bitmap;
64 return IFCALLRESULT(TRUE, cache->bitmap->MemBlt, context, memblt);
65}
66
67static BOOL update_gdi_mem3blt(rdpContext* context, MEM3BLT_ORDER* mem3blt)
68{
69 rdpBitmap* bitmap = NULL;
70 rdpCache* cache = context->cache;
71 rdpBrush* brush = &mem3blt->brush;
72 BOOL ret = TRUE;
73
74 if (mem3blt->cacheId == 0xFF)
75 bitmap = offscreen_cache_get(cache->offscreen, mem3blt->cacheIndex);
76 else
77 bitmap = bitmap_cache_get(cache->bitmap, (BYTE)mem3blt->cacheId, mem3blt->cacheIndex);
78
79 /* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
80 if (!bitmap)
81 return TRUE;
82
83 const BYTE style = WINPR_ASSERTING_INT_CAST(UINT8, brush->style);
84
85 if (brush->style & CACHED_BRUSH)
86 {
87 brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
88
89 if (!brush->data)
90 return FALSE;
91
92 brush->style = 0x03;
93 }
94
95 mem3blt->bitmap = bitmap;
96 IFCALLRET(cache->bitmap->Mem3Blt, ret, context, mem3blt);
97 brush->style = style;
98 return ret;
99}
100
101static BOOL update_gdi_cache_bitmap(rdpContext* context, const CACHE_BITMAP_ORDER* cacheBitmap)
102{
103 rdpBitmap* bitmap = NULL;
104 rdpBitmap* prevBitmap = NULL;
105 rdpCache* cache = context->cache;
106 bitmap = Bitmap_Alloc(context);
107
108 if (!bitmap)
109 return FALSE;
110
111 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmap->bitmapWidth),
112 WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmap->bitmapHeight)))
113 goto fail;
114
115 if (!bitmap->Decompress(context, bitmap, cacheBitmap->bitmapDataStream,
116 cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight,
117 cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength,
118 cacheBitmap->compressed, RDP_CODEC_ID_NONE))
119 goto fail;
120
121 if (!bitmap->New(context, bitmap))
122 goto fail;
123
124 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex);
125 Bitmap_Free(context, prevBitmap);
126 return bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap);
127
128fail:
129 Bitmap_Free(context, bitmap);
130 return FALSE;
131}
132
133static BOOL update_gdi_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
134
135{
136 rdpBitmap* prevBitmap = NULL;
137 rdpCache* cache = context->cache;
138 rdpSettings* settings = context->settings;
139 rdpBitmap* bitmap = Bitmap_Alloc(context);
140
141 if (!bitmap)
142 return FALSE;
143
144 const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
145 bitmap->key64 = ((UINT64)cacheBitmapV2->key1 | (((UINT64)cacheBitmapV2->key2) << 32));
146
147 if (!cacheBitmapV2->bitmapBpp)
148 cacheBitmapV2->bitmapBpp = ColorDepth;
149
150 if ((ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16))
151 cacheBitmapV2->bitmapBpp = ColorDepth;
152
153 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapWidth),
154 WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapHeight)))
155 goto fail;
156
157 if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream,
158 cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight,
159 cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength,
160 cacheBitmapV2->compressed, RDP_CODEC_ID_NONE))
161 goto fail;
162
163 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex);
164
165 if (!bitmap->New(context, bitmap))
166 goto fail;
167
168 Bitmap_Free(context, prevBitmap);
169 return bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex,
170 bitmap);
171
172fail:
173 Bitmap_Free(context, bitmap);
174 return FALSE;
175}
176
177static BOOL update_gdi_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
178{
179 rdpBitmap* bitmap = NULL;
180 rdpBitmap* prevBitmap = NULL;
181 BOOL compressed = TRUE;
182 rdpCache* cache = context->cache;
183 rdpSettings* settings = context->settings;
184 BITMAP_DATA_EX* bitmapData = &cacheBitmapV3->bitmapData;
185 bitmap = Bitmap_Alloc(context);
186
187 if (!bitmap)
188 return FALSE;
189
190 const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
191 bitmap->key64 = ((UINT64)cacheBitmapV3->key1 | (((UINT64)cacheBitmapV3->key2) << 32));
192
193 if (!cacheBitmapV3->bpp)
194 cacheBitmapV3->bpp = ColorDepth;
195
196 compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE);
197
198 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->width),
199 WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->height)))
200 goto fail;
201
202 if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width,
203 bitmapData->height, bitmapData->bpp, bitmapData->length, compressed,
204 bitmapData->codecID))
205 goto fail;
206
207 if (!bitmap->New(context, bitmap))
208 goto fail;
209
210 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex);
211 Bitmap_Free(context, prevBitmap);
212 return bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex,
213 bitmap);
214
215fail:
216 Bitmap_Free(context, bitmap);
217 return FALSE;
218}
219
220rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index)
221{
222 rdpBitmap* bitmap = NULL;
223
224 if (id >= bitmapCache->maxCells)
225 {
226 WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
227 return NULL;
228 }
229
230 if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
231 {
232 index = bitmapCache->cells[id].number;
233 }
234 else if (index > bitmapCache->cells[id].number)
235 {
236 WLog_ERR(TAG, "get invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
237 return NULL;
238 }
239
240 bitmap = bitmapCache->cells[id].entries[index];
241 return bitmap;
242}
243
244BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, rdpBitmap* bitmap)
245{
246 if (id > bitmapCache->maxCells)
247 {
248 WLog_ERR(TAG, "put invalid bitmap cell id: %" PRIu32 "", id);
249 return FALSE;
250 }
251
252 if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
253 {
254 index = bitmapCache->cells[id].number;
255 }
256 else if (index > bitmapCache->cells[id].number)
257 {
258 WLog_ERR(TAG, "put invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
259 return FALSE;
260 }
261
262 bitmapCache->cells[id].entries[index] = bitmap;
263 return TRUE;
264}
265
266void bitmap_cache_register_callbacks(rdpUpdate* update)
267{
268 rdpCache* cache = NULL;
269
270 WINPR_ASSERT(update);
271 WINPR_ASSERT(update->context);
272 WINPR_ASSERT(update->context->cache);
273
274 cache = update->context->cache;
275 WINPR_ASSERT(cache);
276
277 if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
278 {
279 cache->bitmap->MemBlt = update->primary->MemBlt;
280 cache->bitmap->Mem3Blt = update->primary->Mem3Blt;
281 update->primary->MemBlt = update_gdi_memblt;
282 update->primary->Mem3Blt = update_gdi_mem3blt;
283 update->secondary->CacheBitmap = update_gdi_cache_bitmap;
284 update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2;
285 update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3;
286 update->BitmapUpdate = gdi_bitmap_update;
287 }
288}
289
290static int bitmap_cache_save_persistent(rdpBitmapCache* bitmapCache)
291{
292 rdpContext* context = bitmapCache->context;
293 rdpSettings* settings = context->settings;
294
295 const UINT32 version = freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheVersion);
296
297 if (version != 2)
298 return 0; /* persistent bitmap cache already saved in egfx channel */
299
300 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
301 return 0;
302
303 const char* BitmapCachePersistFile =
304 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
305 if (!BitmapCachePersistFile)
306 return 0;
307
308 rdpPersistentCache* persistent = persistent_cache_new();
309
310 if (!persistent)
311 return -1;
312
313 int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version);
314
315 if (status < 1)
316 goto end;
317
318 if (bitmapCache->cells)
319 {
320 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
321 {
322 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
323 for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++)
324 {
325 PERSISTENT_CACHE_ENTRY cacheEntry = { 0 };
326 rdpBitmap* bitmap = cell->entries[j];
327
328 if (!bitmap || !bitmap->key64)
329 continue;
330
331 cacheEntry.key64 = bitmap->key64;
332
333 cacheEntry.width = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width);
334 cacheEntry.height = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height);
335 const UINT64 size = 4ULL * bitmap->width * bitmap->height;
336 if (size > UINT32_MAX)
337 continue;
338 cacheEntry.size = (UINT32)size;
339 cacheEntry.flags = 0;
340 cacheEntry.data = bitmap->data;
341
342 if (persistent_cache_write_entry(persistent, &cacheEntry) < 1)
343 {
344 status = -1;
345 goto end;
346 }
347 }
348 }
349 }
350
351 status = 1;
352
353end:
354 persistent_cache_free(persistent);
355 return status;
356}
357
358rdpBitmapCache* bitmap_cache_new(rdpContext* context)
359{
360 rdpSettings* settings = NULL;
361 rdpBitmapCache* bitmapCache = NULL;
362
363 WINPR_ASSERT(context);
364
365 settings = context->settings;
366 WINPR_ASSERT(settings);
367
368 bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
369
370 if (!bitmapCache)
371 return NULL;
372
373 const UINT32 BitmapCacheV2NumCells =
374 freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
375 bitmapCache->context = context;
376 bitmapCache->cells = (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells, sizeof(BITMAP_V2_CELL));
377
378 if (!bitmapCache->cells)
379 goto fail;
380 bitmapCache->maxCells = BitmapCacheV2NumCells;
381
382 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
383 {
384 const BITMAP_CACHE_V2_CELL_INFO* info =
385 freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
386 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
387 UINT32 nr = info->numEntries;
388 /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
389 cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
390
391 if (!cell->entries)
392 goto fail;
393 cell->number = nr;
394 }
395
396 return bitmapCache;
397fail:
398 WINPR_PRAGMA_DIAG_PUSH
399 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
400 bitmap_cache_free(bitmapCache);
401 WINPR_PRAGMA_DIAG_POP
402 return NULL;
403}
404
405void bitmap_cache_free(rdpBitmapCache* bitmapCache)
406{
407 if (!bitmapCache)
408 return;
409
410 bitmap_cache_save_persistent(bitmapCache);
411
412 if (bitmapCache->cells)
413 {
414 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
415 {
416 UINT32 j = 0;
417 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
418
419 if (!cell->entries)
420 continue;
421
422 for (j = 0; j < cell->number + 1; j++)
423 {
424 rdpBitmap* bitmap = cell->entries[j];
425 Bitmap_Free(bitmapCache->context, bitmap);
426 }
427
428 free((void*)cell->entries);
429 }
430
431 free(bitmapCache->cells);
432 }
433
434 persistent_cache_free(bitmapCache->persistent);
435
436 free(bitmapCache);
437}
438
439static void free_bitmap_data(BITMAP_DATA* data, size_t count)
440{
441 if (!data)
442 return;
443
444 for (size_t x = 0; x < count; x++)
445 free(data[x].bitmapDataStream);
446
447 free(data);
448}
449
450static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
451{
452 BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
453
454 if (!dst)
455 goto fail;
456
457 for (size_t x = 0; x < count; x++)
458 {
459 dst[x] = data[x];
460
461 if (data[x].bitmapLength > 0)
462 {
463 dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
464
465 if (!dst[x].bitmapDataStream)
466 goto fail;
467
468 memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
469 }
470 }
471
472 return dst;
473fail:
474 free_bitmap_data(dst, count);
475 return NULL;
476}
477
478void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
479 WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
480{
481 if (!pointer)
482 return;
483
484 free_bitmap_data(pointer->rectangles, pointer->number);
485 free(pointer);
486}
487
488BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
489{
490 BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
491
492 if (!dst || !pointer)
493 goto fail;
494
495 *dst = *pointer;
496 dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
497
498 if (!dst->rectangles)
499 goto fail;
500
501 return dst;
502fail:
503 WINPR_PRAGMA_DIAG_PUSH
504 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
505 free_bitmap_update(context, dst);
506 WINPR_PRAGMA_DIAG_POP
507 return NULL;
508}
509
510CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
511{
512 CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
513
514 if (!dst || !order)
515 goto fail;
516
517 *dst = *order;
518
519 if (order->bitmapLength > 0)
520 {
521 dst->bitmapDataStream = malloc(order->bitmapLength);
522
523 if (!dst->bitmapDataStream)
524 goto fail;
525
526 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
527 }
528
529 return dst;
530fail:
531 WINPR_PRAGMA_DIAG_PUSH
532 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
533 free_cache_bitmap_order(context, dst);
534 WINPR_PRAGMA_DIAG_POP
535 return NULL;
536}
537
538void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
539{
540 if (order)
541 free(order->bitmapDataStream);
542
543 free(order);
544}
545
546CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
547 const CACHE_BITMAP_V2_ORDER* order)
548{
549 CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
550
551 if (!dst || !order)
552 goto fail;
553
554 *dst = *order;
555
556 if (order->bitmapLength > 0)
557 {
558 dst->bitmapDataStream = malloc(order->bitmapLength);
559
560 if (!dst->bitmapDataStream)
561 goto fail;
562
563 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
564 }
565
566 return dst;
567fail:
568 WINPR_PRAGMA_DIAG_PUSH
569 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
570 free_cache_bitmap_v2_order(context, dst);
571 WINPR_PRAGMA_DIAG_POP
572 return NULL;
573}
574
575void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
576 WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
577{
578 if (order)
579 free(order->bitmapDataStream);
580
581 free(order);
582}
583
584CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
585 const CACHE_BITMAP_V3_ORDER* order)
586{
587 CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
588
589 if (!dst || !order)
590 goto fail;
591
592 *dst = *order;
593
594 if (order->bitmapData.length > 0)
595 {
596 dst->bitmapData.data = malloc(order->bitmapData.length);
597
598 if (!dst->bitmapData.data)
599 goto fail;
600
601 memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
602 }
603
604 return dst;
605fail:
606 WINPR_PRAGMA_DIAG_PUSH
607 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
608 free_cache_bitmap_v3_order(context, dst);
609 WINPR_PRAGMA_DIAG_POP
610 return NULL;
611}
612
613void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
614{
615 if (order)
616 free(order->bitmapData.data);
617
618 free(order);
619}
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.
Definition persistent.h:70