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 = nullptr;
50 rdpCache* cache = nullptr;
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 == nullptr)
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 = nullptr;
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 = nullptr;
104 rdpBitmap* prevBitmap = nullptr;
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 if (!bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap))
127 goto fail;
128 return TRUE;
129
130fail:
131 Bitmap_Free(context, bitmap);
132 return FALSE;
133}
134
135static BOOL update_gdi_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
136
137{
138 rdpBitmap* prevBitmap = nullptr;
139 rdpCache* cache = context->cache;
140 rdpSettings* settings = context->settings;
141 rdpBitmap* bitmap = Bitmap_Alloc(context);
142
143 if (!bitmap)
144 return FALSE;
145
146 const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
147 bitmap->key64 = ((UINT64)cacheBitmapV2->key1 | (((UINT64)cacheBitmapV2->key2) << 32));
148
149 if (!cacheBitmapV2->bitmapBpp)
150 cacheBitmapV2->bitmapBpp = ColorDepth;
151
152 if ((ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16))
153 cacheBitmapV2->bitmapBpp = ColorDepth;
154
155 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapWidth),
156 WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapHeight)))
157 goto fail;
158
159 if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream,
160 cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight,
161 cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength,
162 cacheBitmapV2->compressed, RDP_CODEC_ID_NONE))
163 goto fail;
164
165 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex);
166
167 if (!bitmap->New(context, bitmap))
168 goto fail;
169
170 Bitmap_Free(context, prevBitmap);
171 if (!bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex, bitmap))
172 goto fail;
173 return TRUE;
174
175fail:
176 Bitmap_Free(context, bitmap);
177 return FALSE;
178}
179
180static BOOL update_gdi_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
181{
182 rdpBitmap* bitmap = nullptr;
183 rdpBitmap* prevBitmap = nullptr;
184 BOOL compressed = TRUE;
185 rdpCache* cache = context->cache;
186 rdpSettings* settings = context->settings;
187 BITMAP_DATA_EX* bitmapData = &cacheBitmapV3->bitmapData;
188 bitmap = Bitmap_Alloc(context);
189
190 if (!bitmap)
191 return FALSE;
192
193 const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
194 bitmap->key64 = ((UINT64)cacheBitmapV3->key1 | (((UINT64)cacheBitmapV3->key2) << 32));
195
196 if (!cacheBitmapV3->bpp)
197 cacheBitmapV3->bpp = ColorDepth;
198
199 compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE);
200
201 if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->width),
202 WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->height)))
203 goto fail;
204
205 if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width,
206 bitmapData->height, bitmapData->bpp, bitmapData->length, compressed,
207 bitmapData->codecID))
208 goto fail;
209
210 if (!bitmap->New(context, bitmap))
211 goto fail;
212
213 prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex);
214 Bitmap_Free(context, prevBitmap);
215 if (!bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex, bitmap))
216 goto fail;
217 return TRUE;
218
219fail:
220 Bitmap_Free(context, bitmap);
221 return FALSE;
222}
223
224rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index)
225{
226 rdpBitmap* bitmap = nullptr;
227
228 if (id >= bitmapCache->maxCells)
229 {
230 WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
231 return nullptr;
232 }
233
234 if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
235 {
236 index = bitmapCache->cells[id].number;
237 }
238 else if (index > bitmapCache->cells[id].number)
239 {
240 WLog_ERR(TAG, "get invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
241 return nullptr;
242 }
243
244 bitmap = bitmapCache->cells[id].entries[index];
245 return bitmap;
246}
247
248BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, rdpBitmap* bitmap)
249{
250 WINPR_ASSERT(bitmapCache);
251 if (id > bitmapCache->maxCells)
252 {
253 WLog_ERR(TAG, "put invalid bitmap cell id: %" PRIu32 "", id);
254 return FALSE;
255 }
256
257 if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
258 {
259 index = bitmapCache->cells[id].number;
260 }
261 else if (index > bitmapCache->cells[id].number)
262 {
263 WLog_ERR(TAG, "put invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
264 return FALSE;
265 }
266
267 bitmapCache->cells[id].entries[index] = bitmap;
268 return TRUE;
269}
270
271void bitmap_cache_register_callbacks(rdpUpdate* update)
272{
273 rdpCache* cache = nullptr;
274
275 WINPR_ASSERT(update);
276 WINPR_ASSERT(update->context);
277 WINPR_ASSERT(update->context->cache);
278
279 cache = update->context->cache;
280 WINPR_ASSERT(cache);
281
282 if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
283 {
284 cache->bitmap->MemBlt = update->primary->MemBlt;
285 cache->bitmap->Mem3Blt = update->primary->Mem3Blt;
286 update->primary->MemBlt = update_gdi_memblt;
287 update->primary->Mem3Blt = update_gdi_mem3blt;
288 update->secondary->CacheBitmap = update_gdi_cache_bitmap;
289 update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2;
290 update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3;
291 update->BitmapUpdate = gdi_bitmap_update;
292 }
293}
294
295static int bitmap_cache_save_persistent(rdpBitmapCache* bitmapCache)
296{
297 rdpContext* context = bitmapCache->context;
298 rdpSettings* settings = context->settings;
299
300 const UINT32 version = freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheVersion);
301
302 if (version != 2)
303 return 0; /* persistent bitmap cache already saved in egfx channel */
304
305 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
306 return 0;
307
308 const char* BitmapCachePersistFile =
309 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
310 if (!BitmapCachePersistFile)
311 return 0;
312
313 rdpPersistentCache* persistent = persistent_cache_new();
314
315 if (!persistent)
316 return -1;
317
318 int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version);
319
320 if (status < 1)
321 goto end;
322
323 if (bitmapCache->cells)
324 {
325 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
326 {
327 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
328 for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++)
329 {
330 PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
331 rdpBitmap* bitmap = cell->entries[j];
332
333 if (!bitmap || !bitmap->key64)
334 continue;
335
336 cacheEntry.key64 = bitmap->key64;
337
338 cacheEntry.width = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width);
339 cacheEntry.height = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height);
340 const UINT64 size = 4ULL * bitmap->width * bitmap->height;
341 if (size > UINT32_MAX)
342 continue;
343 cacheEntry.size = (UINT32)size;
344 cacheEntry.flags = 0;
345 cacheEntry.data = bitmap->data;
346
347 if (persistent_cache_write_entry(persistent, &cacheEntry) < 1)
348 {
349 status = -1;
350 goto end;
351 }
352 }
353 }
354 }
355
356 status = 1;
357
358end:
359 persistent_cache_free(persistent);
360 return status;
361}
362
363rdpBitmapCache* bitmap_cache_new(rdpContext* context)
364{
365 rdpSettings* settings = nullptr;
366 rdpBitmapCache* bitmapCache = nullptr;
367
368 WINPR_ASSERT(context);
369
370 settings = context->settings;
371 WINPR_ASSERT(settings);
372
373 bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
374
375 if (!bitmapCache)
376 return nullptr;
377
378 const UINT32 BitmapCacheV2NumCells =
379 freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
380 bitmapCache->context = context;
381
382 /* overallocate by 1. older RDP servers do send a off by 1 cache index. */
383 bitmapCache->cells =
384 (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells + 1ull, sizeof(BITMAP_V2_CELL));
385
386 if (!bitmapCache->cells)
387 goto fail;
388 bitmapCache->maxCells = BitmapCacheV2NumCells;
389
390 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
391 {
392 const BITMAP_CACHE_V2_CELL_INFO* info =
393 freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
394 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
395 UINT32 nr = info->numEntries;
396 /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
397 cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
398
399 if (!cell->entries)
400 goto fail;
401 cell->number = nr;
402 }
403
404 /* initialize the overallocated extra slot for old RDP servers that send
405 * cacheId == maxCells; use a minimal allocation since no protocol-negotiated
406 * capacity exists for this slot */
407 {
408 BITMAP_V2_CELL* extra = &bitmapCache->cells[bitmapCache->maxCells];
409 /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
410 extra->entries = (rdpBitmap**)calloc(1, sizeof(rdpBitmap*));
411
412 if (!extra->entries)
413 goto fail;
414 extra->number = 0;
415 }
416
417 return bitmapCache;
418fail:
419 WINPR_PRAGMA_DIAG_PUSH
420 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
421 bitmap_cache_free(bitmapCache);
422 WINPR_PRAGMA_DIAG_POP
423 return nullptr;
424}
425
426void bitmap_cache_free(rdpBitmapCache* bitmapCache)
427{
428 if (!bitmapCache)
429 return;
430
431 bitmap_cache_save_persistent(bitmapCache);
432
433 if (bitmapCache->cells)
434 {
435 /* iterate through maxCells + 1 to also free the overallocated extra slot */
436 for (UINT32 i = 0; i <= bitmapCache->maxCells; i++)
437 {
438 UINT32 j = 0;
439 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
440
441 if (!cell->entries)
442 continue;
443
444 for (j = 0; j < cell->number + 1; j++)
445 {
446 rdpBitmap* bitmap = cell->entries[j];
447 Bitmap_Free(bitmapCache->context, bitmap);
448 }
449
450 free((void*)cell->entries);
451 }
452
453 free(bitmapCache->cells);
454 }
455
456 persistent_cache_free(bitmapCache->persistent);
457
458 free(bitmapCache);
459}
460
461static void free_bitmap_data(BITMAP_DATA* data, size_t count)
462{
463 if (!data)
464 return;
465
466 for (size_t x = 0; x < count; x++)
467 free(data[x].bitmapDataStream);
468
469 free(data);
470}
471
472static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
473{
474 BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
475
476 if (!dst)
477 goto fail;
478
479 for (size_t x = 0; x < count; x++)
480 {
481 dst[x] = data[x];
482
483 if (data[x].bitmapLength > 0)
484 {
485 dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
486
487 if (!dst[x].bitmapDataStream)
488 goto fail;
489
490 memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
491 }
492 }
493
494 return dst;
495fail:
496 free_bitmap_data(dst, count);
497 return nullptr;
498}
499
500void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
501 WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
502{
503 if (!pointer)
504 return;
505
506 free_bitmap_data(pointer->rectangles, pointer->number);
507 free(pointer);
508}
509
510BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
511{
512 BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
513
514 if (!dst || !pointer)
515 goto fail;
516
517 *dst = *pointer;
518 dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
519
520 if (!dst->rectangles)
521 goto fail;
522
523 return dst;
524fail:
525 WINPR_PRAGMA_DIAG_PUSH
526 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
527 free_bitmap_update(context, dst);
528 WINPR_PRAGMA_DIAG_POP
529 return nullptr;
530}
531
532CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
533{
534 CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
535
536 if (!dst || !order)
537 goto fail;
538
539 *dst = *order;
540
541 if (order->bitmapLength > 0)
542 {
543 dst->bitmapDataStream = malloc(order->bitmapLength);
544
545 if (!dst->bitmapDataStream)
546 goto fail;
547
548 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
549 }
550
551 return dst;
552fail:
553 WINPR_PRAGMA_DIAG_PUSH
554 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
555 free_cache_bitmap_order(context, dst);
556 WINPR_PRAGMA_DIAG_POP
557 return nullptr;
558}
559
560void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
561{
562 if (order)
563 free(order->bitmapDataStream);
564
565 free(order);
566}
567
568CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
569 const CACHE_BITMAP_V2_ORDER* order)
570{
571 CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
572
573 if (!dst || !order)
574 goto fail;
575
576 *dst = *order;
577
578 if (order->bitmapLength > 0)
579 {
580 dst->bitmapDataStream = malloc(order->bitmapLength);
581
582 if (!dst->bitmapDataStream)
583 goto fail;
584
585 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
586 }
587
588 return dst;
589fail:
590 WINPR_PRAGMA_DIAG_PUSH
591 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
592 free_cache_bitmap_v2_order(context, dst);
593 WINPR_PRAGMA_DIAG_POP
594 return nullptr;
595}
596
597void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
598 WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
599{
600 if (order)
601 free(order->bitmapDataStream);
602
603 free(order);
604}
605
606CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
607 const CACHE_BITMAP_V3_ORDER* order)
608{
609 CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
610
611 if (!dst || !order)
612 goto fail;
613
614 *dst = *order;
615
616 if (order->bitmapData.length > 0)
617 {
618 dst->bitmapData.data = malloc(order->bitmapData.length);
619
620 if (!dst->bitmapData.data)
621 goto fail;
622
623 memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
624 }
625
626 return dst;
627fail:
628 WINPR_PRAGMA_DIAG_PUSH
629 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
630 free_cache_bitmap_v3_order(context, dst);
631 WINPR_PRAGMA_DIAG_POP
632 return nullptr;
633}
634
635void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
636{
637 if (order)
638 free(order->bitmapData.data);
639
640 free(order);
641}
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
Definition persistent.h:70