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 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 = nullptr;
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 = nullptr;
180 rdpBitmap* prevBitmap = nullptr;
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 = nullptr;
223
224 if (id >= bitmapCache->maxCells)
225 {
226 WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
227 return nullptr;
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 nullptr;
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 = nullptr;
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 = WINPR_C_ARRAY_INIT;
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 = nullptr;
361 rdpBitmapCache* bitmapCache = nullptr;
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 nullptr;
372
373 const UINT32 BitmapCacheV2NumCells =
374 freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
375 bitmapCache->context = context;
376
377 /* overallocate by 1. older RDP servers do send a off by 1 cache index. */
378 bitmapCache->cells =
379 (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells + 1ull, sizeof(BITMAP_V2_CELL));
380
381 if (!bitmapCache->cells)
382 goto fail;
383 bitmapCache->maxCells = BitmapCacheV2NumCells;
384
385 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
386 {
387 const BITMAP_CACHE_V2_CELL_INFO* info =
388 freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
389 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
390 UINT32 nr = info->numEntries;
391 /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
392 cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
393
394 if (!cell->entries)
395 goto fail;
396 cell->number = nr;
397 }
398
399 return bitmapCache;
400fail:
401 WINPR_PRAGMA_DIAG_PUSH
402 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
403 bitmap_cache_free(bitmapCache);
404 WINPR_PRAGMA_DIAG_POP
405 return nullptr;
406}
407
408void bitmap_cache_free(rdpBitmapCache* bitmapCache)
409{
410 if (!bitmapCache)
411 return;
412
413 bitmap_cache_save_persistent(bitmapCache);
414
415 if (bitmapCache->cells)
416 {
417 for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
418 {
419 UINT32 j = 0;
420 BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
421
422 if (!cell->entries)
423 continue;
424
425 for (j = 0; j < cell->number + 1; j++)
426 {
427 rdpBitmap* bitmap = cell->entries[j];
428 Bitmap_Free(bitmapCache->context, bitmap);
429 }
430
431 free((void*)cell->entries);
432 }
433
434 free(bitmapCache->cells);
435 }
436
437 persistent_cache_free(bitmapCache->persistent);
438
439 free(bitmapCache);
440}
441
442static void free_bitmap_data(BITMAP_DATA* data, size_t count)
443{
444 if (!data)
445 return;
446
447 for (size_t x = 0; x < count; x++)
448 free(data[x].bitmapDataStream);
449
450 free(data);
451}
452
453static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
454{
455 BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
456
457 if (!dst)
458 goto fail;
459
460 for (size_t x = 0; x < count; x++)
461 {
462 dst[x] = data[x];
463
464 if (data[x].bitmapLength > 0)
465 {
466 dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
467
468 if (!dst[x].bitmapDataStream)
469 goto fail;
470
471 memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
472 }
473 }
474
475 return dst;
476fail:
477 free_bitmap_data(dst, count);
478 return nullptr;
479}
480
481void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
482 WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
483{
484 if (!pointer)
485 return;
486
487 free_bitmap_data(pointer->rectangles, pointer->number);
488 free(pointer);
489}
490
491BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
492{
493 BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
494
495 if (!dst || !pointer)
496 goto fail;
497
498 *dst = *pointer;
499 dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
500
501 if (!dst->rectangles)
502 goto fail;
503
504 return dst;
505fail:
506 WINPR_PRAGMA_DIAG_PUSH
507 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
508 free_bitmap_update(context, dst);
509 WINPR_PRAGMA_DIAG_POP
510 return nullptr;
511}
512
513CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
514{
515 CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
516
517 if (!dst || !order)
518 goto fail;
519
520 *dst = *order;
521
522 if (order->bitmapLength > 0)
523 {
524 dst->bitmapDataStream = malloc(order->bitmapLength);
525
526 if (!dst->bitmapDataStream)
527 goto fail;
528
529 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
530 }
531
532 return dst;
533fail:
534 WINPR_PRAGMA_DIAG_PUSH
535 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
536 free_cache_bitmap_order(context, dst);
537 WINPR_PRAGMA_DIAG_POP
538 return nullptr;
539}
540
541void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
542{
543 if (order)
544 free(order->bitmapDataStream);
545
546 free(order);
547}
548
549CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
550 const CACHE_BITMAP_V2_ORDER* order)
551{
552 CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
553
554 if (!dst || !order)
555 goto fail;
556
557 *dst = *order;
558
559 if (order->bitmapLength > 0)
560 {
561 dst->bitmapDataStream = malloc(order->bitmapLength);
562
563 if (!dst->bitmapDataStream)
564 goto fail;
565
566 memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
567 }
568
569 return dst;
570fail:
571 WINPR_PRAGMA_DIAG_PUSH
572 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
573 free_cache_bitmap_v2_order(context, dst);
574 WINPR_PRAGMA_DIAG_POP
575 return nullptr;
576}
577
578void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
579 WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
580{
581 if (order)
582 free(order->bitmapDataStream);
583
584 free(order);
585}
586
587CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
588 const CACHE_BITMAP_V3_ORDER* order)
589{
590 CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
591
592 if (!dst || !order)
593 goto fail;
594
595 *dst = *order;
596
597 if (order->bitmapData.length > 0)
598 {
599 dst->bitmapData.data = malloc(order->bitmapData.length);
600
601 if (!dst->bitmapData.data)
602 goto fail;
603
604 memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
605 }
606
607 return dst;
608fail:
609 WINPR_PRAGMA_DIAG_PUSH
610 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
611 free_cache_bitmap_v3_order(context, dst);
612 WINPR_PRAGMA_DIAG_POP
613 return nullptr;
614}
615
616void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
617{
618 if (order)
619 free(order->bitmapData.data);
620
621 free(order);
622}
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