/src/FreeRDP/libfreerdp/cache/bitmap.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Bitmap Cache V2 |
4 | | * |
5 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
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 | | |
43 | | static rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index); |
44 | | static BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, |
45 | | rdpBitmap* bitmap); |
46 | | |
47 | | static BOOL update_gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt) |
48 | 0 | { |
49 | 0 | rdpBitmap* bitmap = nullptr; |
50 | 0 | rdpCache* cache = nullptr; |
51 | |
|
52 | 0 | cache = context->cache; |
53 | |
|
54 | 0 | if (memblt->cacheId == 0xFF) |
55 | 0 | bitmap = offscreen_cache_get(cache->offscreen, memblt->cacheIndex); |
56 | 0 | else |
57 | 0 | 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 | 0 | if (bitmap == nullptr) |
61 | 0 | return TRUE; |
62 | | |
63 | 0 | memblt->bitmap = bitmap; |
64 | 0 | return IFCALLRESULT(TRUE, cache->bitmap->MemBlt, context, memblt); |
65 | 0 | } |
66 | | |
67 | | static BOOL update_gdi_mem3blt(rdpContext* context, MEM3BLT_ORDER* mem3blt) |
68 | 0 | { |
69 | 0 | rdpBitmap* bitmap = nullptr; |
70 | 0 | rdpCache* cache = context->cache; |
71 | 0 | rdpBrush* brush = &mem3blt->brush; |
72 | 0 | BOOL ret = TRUE; |
73 | |
|
74 | 0 | if (mem3blt->cacheId == 0xFF) |
75 | 0 | bitmap = offscreen_cache_get(cache->offscreen, mem3blt->cacheIndex); |
76 | 0 | else |
77 | 0 | 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 | 0 | if (!bitmap) |
81 | 0 | return TRUE; |
82 | | |
83 | 0 | const BYTE style = WINPR_ASSERTING_INT_CAST(UINT8, brush->style); |
84 | 0 | BYTE* data = brush->data; |
85 | |
|
86 | 0 | if (brush->style & CACHED_BRUSH) |
87 | 0 | { |
88 | 0 | brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp); |
89 | |
|
90 | 0 | if (!brush->data) |
91 | 0 | return FALSE; |
92 | | |
93 | 0 | brush->style = 0x03; |
94 | 0 | } |
95 | | |
96 | 0 | mem3blt->bitmap = bitmap; |
97 | 0 | IFCALLRET(cache->bitmap->Mem3Blt, ret, context, mem3blt); |
98 | 0 | brush->style = style; |
99 | 0 | brush->data = data; |
100 | 0 | return ret; |
101 | 0 | } |
102 | | |
103 | | static BOOL update_gdi_cache_bitmap(rdpContext* context, const CACHE_BITMAP_ORDER* cacheBitmap) |
104 | 0 | { |
105 | 0 | rdpBitmap* bitmap = nullptr; |
106 | 0 | rdpBitmap* prevBitmap = nullptr; |
107 | 0 | rdpCache* cache = context->cache; |
108 | 0 | bitmap = Bitmap_Alloc(context); |
109 | |
|
110 | 0 | if (!bitmap) |
111 | 0 | return FALSE; |
112 | | |
113 | 0 | if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmap->bitmapWidth), |
114 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmap->bitmapHeight))) |
115 | 0 | goto fail; |
116 | | |
117 | 0 | if (!bitmap->Decompress(context, bitmap, cacheBitmap->bitmapDataStream, |
118 | 0 | cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight, |
119 | 0 | cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength, |
120 | 0 | cacheBitmap->compressed, RDP_CODEC_ID_NONE)) |
121 | 0 | goto fail; |
122 | | |
123 | 0 | if (!bitmap->New(context, bitmap)) |
124 | 0 | goto fail; |
125 | | |
126 | 0 | prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex); |
127 | 0 | Bitmap_Free(context, prevBitmap); |
128 | 0 | if (!bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap)) |
129 | 0 | goto fail; |
130 | 0 | return TRUE; |
131 | | |
132 | 0 | fail: |
133 | 0 | Bitmap_Free(context, bitmap); |
134 | 0 | return FALSE; |
135 | 0 | } |
136 | | |
137 | | static BOOL update_gdi_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cacheBitmapV2) |
138 | | |
139 | 0 | { |
140 | 0 | rdpBitmap* prevBitmap = nullptr; |
141 | 0 | rdpCache* cache = context->cache; |
142 | 0 | rdpSettings* settings = context->settings; |
143 | 0 | rdpBitmap* bitmap = Bitmap_Alloc(context); |
144 | |
|
145 | 0 | if (!bitmap) |
146 | 0 | return FALSE; |
147 | | |
148 | 0 | const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth); |
149 | 0 | bitmap->key64 = ((UINT64)cacheBitmapV2->key1 | (((UINT64)cacheBitmapV2->key2) << 32)); |
150 | |
|
151 | 0 | if (!cacheBitmapV2->bitmapBpp) |
152 | 0 | cacheBitmapV2->bitmapBpp = ColorDepth; |
153 | |
|
154 | 0 | if ((ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16)) |
155 | 0 | cacheBitmapV2->bitmapBpp = ColorDepth; |
156 | |
|
157 | 0 | if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapWidth), |
158 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapHeight))) |
159 | 0 | goto fail; |
160 | | |
161 | 0 | if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream, |
162 | 0 | cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight, |
163 | 0 | cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength, |
164 | 0 | cacheBitmapV2->compressed, RDP_CODEC_ID_NONE)) |
165 | 0 | goto fail; |
166 | | |
167 | 0 | prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex); |
168 | |
|
169 | 0 | if (!bitmap->New(context, bitmap)) |
170 | 0 | goto fail; |
171 | | |
172 | 0 | Bitmap_Free(context, prevBitmap); |
173 | 0 | if (!bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex, bitmap)) |
174 | 0 | goto fail; |
175 | 0 | return TRUE; |
176 | | |
177 | 0 | fail: |
178 | 0 | Bitmap_Free(context, bitmap); |
179 | 0 | return FALSE; |
180 | 0 | } |
181 | | |
182 | | static BOOL update_gdi_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cacheBitmapV3) |
183 | 0 | { |
184 | 0 | rdpBitmap* bitmap = nullptr; |
185 | 0 | rdpBitmap* prevBitmap = nullptr; |
186 | 0 | BOOL compressed = TRUE; |
187 | 0 | rdpCache* cache = context->cache; |
188 | 0 | rdpSettings* settings = context->settings; |
189 | 0 | BITMAP_DATA_EX* bitmapData = &cacheBitmapV3->bitmapData; |
190 | 0 | bitmap = Bitmap_Alloc(context); |
191 | |
|
192 | 0 | if (!bitmap) |
193 | 0 | return FALSE; |
194 | | |
195 | 0 | const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth); |
196 | 0 | bitmap->key64 = ((UINT64)cacheBitmapV3->key1 | (((UINT64)cacheBitmapV3->key2) << 32)); |
197 | |
|
198 | 0 | if (!cacheBitmapV3->bpp) |
199 | 0 | cacheBitmapV3->bpp = ColorDepth; |
200 | |
|
201 | 0 | compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE); |
202 | |
|
203 | 0 | if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->width), |
204 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->height))) |
205 | 0 | goto fail; |
206 | | |
207 | 0 | if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width, |
208 | 0 | bitmapData->height, bitmapData->bpp, bitmapData->length, compressed, |
209 | 0 | bitmapData->codecID)) |
210 | 0 | goto fail; |
211 | | |
212 | 0 | if (!bitmap->New(context, bitmap)) |
213 | 0 | goto fail; |
214 | | |
215 | 0 | prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex); |
216 | 0 | Bitmap_Free(context, prevBitmap); |
217 | 0 | if (!bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex, bitmap)) |
218 | 0 | goto fail; |
219 | 0 | return TRUE; |
220 | | |
221 | 0 | fail: |
222 | 0 | Bitmap_Free(context, bitmap); |
223 | 0 | return FALSE; |
224 | 0 | } |
225 | | |
226 | | rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index) |
227 | 0 | { |
228 | 0 | rdpBitmap* bitmap = nullptr; |
229 | |
|
230 | 0 | if (id >= bitmapCache->maxCells) |
231 | 0 | { |
232 | 0 | WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id); |
233 | 0 | return nullptr; |
234 | 0 | } |
235 | | |
236 | 0 | if (index == BITMAP_CACHE_WAITING_LIST_INDEX) |
237 | 0 | { |
238 | 0 | index = bitmapCache->cells[id].number; |
239 | 0 | } |
240 | 0 | else if (index > bitmapCache->cells[id].number) |
241 | 0 | { |
242 | 0 | WLog_ERR(TAG, "get invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id); |
243 | 0 | return nullptr; |
244 | 0 | } |
245 | | |
246 | 0 | bitmap = bitmapCache->cells[id].entries[index]; |
247 | 0 | return bitmap; |
248 | 0 | } |
249 | | |
250 | | BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, rdpBitmap* bitmap) |
251 | 0 | { |
252 | 0 | WINPR_ASSERT(bitmapCache); |
253 | 0 | if (id > bitmapCache->maxCells) |
254 | 0 | { |
255 | 0 | WLog_ERR(TAG, "put invalid bitmap cell id: %" PRIu32 "", id); |
256 | 0 | return FALSE; |
257 | 0 | } |
258 | | |
259 | 0 | if (index == BITMAP_CACHE_WAITING_LIST_INDEX) |
260 | 0 | { |
261 | 0 | index = bitmapCache->cells[id].number; |
262 | 0 | } |
263 | 0 | else if (index > bitmapCache->cells[id].number) |
264 | 0 | { |
265 | 0 | WLog_ERR(TAG, "put invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id); |
266 | 0 | return FALSE; |
267 | 0 | } |
268 | | |
269 | 0 | bitmapCache->cells[id].entries[index] = bitmap; |
270 | 0 | return TRUE; |
271 | 0 | } |
272 | | |
273 | | void bitmap_cache_register_callbacks(rdpUpdate* update) |
274 | 0 | { |
275 | 0 | rdpCache* cache = nullptr; |
276 | |
|
277 | 0 | WINPR_ASSERT(update); |
278 | 0 | WINPR_ASSERT(update->context); |
279 | 0 | WINPR_ASSERT(update->context->cache); |
280 | |
|
281 | 0 | cache = update->context->cache; |
282 | 0 | WINPR_ASSERT(cache); |
283 | |
|
284 | 0 | if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding)) |
285 | 0 | { |
286 | 0 | cache->bitmap->MemBlt = update->primary->MemBlt; |
287 | 0 | cache->bitmap->Mem3Blt = update->primary->Mem3Blt; |
288 | 0 | update->primary->MemBlt = update_gdi_memblt; |
289 | 0 | update->primary->Mem3Blt = update_gdi_mem3blt; |
290 | 0 | update->secondary->CacheBitmap = update_gdi_cache_bitmap; |
291 | 0 | update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2; |
292 | 0 | update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3; |
293 | 0 | update->BitmapUpdate = gdi_bitmap_update; |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | | static int bitmap_cache_save_persistent(rdpBitmapCache* bitmapCache) |
298 | 0 | { |
299 | 0 | WINPR_ASSERT(bitmapCache); |
300 | |
|
301 | 0 | rdpContext* context = bitmapCache->context; |
302 | 0 | rdpSettings* settings = context->settings; |
303 | |
|
304 | 0 | const UINT32 version = freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheVersion); |
305 | |
|
306 | 0 | if (version != 2) |
307 | 0 | return 0; /* persistent bitmap cache already saved in egfx channel */ |
308 | | |
309 | 0 | if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled)) |
310 | 0 | return 0; |
311 | | |
312 | 0 | const char* BitmapCachePersistFile = |
313 | 0 | freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile); |
314 | 0 | if (!BitmapCachePersistFile) |
315 | 0 | return 0; |
316 | | |
317 | 0 | rdpPersistentCache* persistent = persistent_cache_new(); |
318 | |
|
319 | 0 | if (!persistent) |
320 | 0 | return -1; |
321 | | |
322 | 0 | int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version); |
323 | |
|
324 | 0 | if (status < 1) |
325 | 0 | goto end; |
326 | | |
327 | 0 | if (bitmapCache->cells) |
328 | 0 | { |
329 | 0 | for (UINT32 i = 0; i < bitmapCache->maxCells; i++) |
330 | 0 | { |
331 | 0 | BITMAP_V2_CELL* cell = &bitmapCache->cells[i]; |
332 | 0 | for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++) |
333 | 0 | { |
334 | 0 | PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT; |
335 | 0 | rdpBitmap* bitmap = cell->entries[j]; |
336 | |
|
337 | 0 | if (!bitmap || !bitmap->key64) |
338 | 0 | continue; |
339 | | |
340 | 0 | cacheEntry.key64 = bitmap->key64; |
341 | |
|
342 | 0 | cacheEntry.width = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width); |
343 | 0 | cacheEntry.height = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height); |
344 | 0 | const UINT64 size = 4ULL * bitmap->width * bitmap->height; |
345 | 0 | if (size > UINT32_MAX) |
346 | 0 | continue; |
347 | 0 | cacheEntry.size = (UINT32)size; |
348 | 0 | cacheEntry.flags = 0; |
349 | 0 | cacheEntry.data = bitmap->data; |
350 | |
|
351 | 0 | if (persistent_cache_write_entry(persistent, &cacheEntry) < 1) |
352 | 0 | { |
353 | 0 | status = -1; |
354 | 0 | goto end; |
355 | 0 | } |
356 | 0 | } |
357 | 0 | } |
358 | 0 | } |
359 | | |
360 | 0 | status = 1; |
361 | |
|
362 | 0 | end: |
363 | 0 | persistent_cache_free(persistent); |
364 | 0 | return status; |
365 | 0 | } |
366 | | |
367 | | static void bitmap_cache_cell_free(rdpBitmapCache* bitmapCache) |
368 | 0 | { |
369 | 0 | WINPR_ASSERT(bitmapCache); |
370 | 0 | if (!bitmapCache->cells) |
371 | 0 | return; |
372 | | |
373 | | /* iterate through maxCells + 1 to also free the overallocated extra slot */ |
374 | 0 | for (UINT32 i = 0; i <= bitmapCache->maxCells; i++) |
375 | 0 | { |
376 | 0 | UINT32 j = 0; |
377 | 0 | BITMAP_V2_CELL* cell = &bitmapCache->cells[i]; |
378 | |
|
379 | 0 | if (!cell->entries) |
380 | 0 | continue; |
381 | | |
382 | 0 | for (j = 0; j < cell->number + 1; j++) |
383 | 0 | { |
384 | 0 | rdpBitmap* bitmap = cell->entries[j]; |
385 | 0 | Bitmap_Free(bitmapCache->context, bitmap); |
386 | 0 | } |
387 | |
|
388 | 0 | free((void*)cell->entries); |
389 | 0 | } |
390 | |
|
391 | 0 | free(bitmapCache->cells); |
392 | |
|
393 | 0 | bitmapCache->cells = nullptr; |
394 | 0 | } |
395 | | |
396 | | BOOL bitmap_cache_resize(rdpBitmapCache* bitmapCache) |
397 | 0 | { |
398 | 0 | if (!bitmapCache) |
399 | 0 | return FALSE; |
400 | | |
401 | 0 | rdpContext* context = bitmapCache->context; |
402 | 0 | WINPR_ASSERT(context); |
403 | |
|
404 | 0 | rdpSettings* settings = context->settings; |
405 | 0 | WINPR_ASSERT(settings); |
406 | |
|
407 | 0 | const UINT32 BitmapCacheV2NumCells = |
408 | 0 | freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells); |
409 | |
|
410 | 0 | if (BitmapCacheV2NumCells == bitmapCache->maxCells) |
411 | 0 | return TRUE; |
412 | | |
413 | 0 | bitmap_cache_cell_free(bitmapCache); |
414 | | |
415 | | /* overallocate by 1. older RDP servers do send a off by 1 cache index. */ |
416 | 0 | bitmapCache->cells = |
417 | 0 | (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells + 1ull, sizeof(BITMAP_V2_CELL)); |
418 | |
|
419 | 0 | if (!bitmapCache->cells) |
420 | 0 | return FALSE; |
421 | 0 | bitmapCache->maxCells = BitmapCacheV2NumCells; |
422 | |
|
423 | 0 | for (UINT32 i = 0; i < bitmapCache->maxCells; i++) |
424 | 0 | { |
425 | 0 | const BITMAP_CACHE_V2_CELL_INFO* info = |
426 | 0 | freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i); |
427 | 0 | if (!info) |
428 | 0 | return FALSE; |
429 | | |
430 | 0 | BITMAP_V2_CELL* cell = &bitmapCache->cells[i]; |
431 | 0 | UINT32 nr = info->numEntries; |
432 | | /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */ |
433 | 0 | cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*)); |
434 | |
|
435 | 0 | if (!cell->entries) |
436 | 0 | return FALSE; |
437 | 0 | cell->number = nr; |
438 | 0 | } |
439 | | |
440 | | /* initialize the overallocated extra slot for old RDP servers that send |
441 | | * cacheId == maxCells; use a minimal allocation since no protocol-negotiated |
442 | | * capacity exists for this slot */ |
443 | 0 | { |
444 | 0 | BITMAP_V2_CELL* extra = &bitmapCache->cells[bitmapCache->maxCells]; |
445 | | /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */ |
446 | 0 | extra->entries = (rdpBitmap**)calloc(1, sizeof(rdpBitmap*)); |
447 | |
|
448 | 0 | if (!extra->entries) |
449 | 0 | return FALSE; |
450 | 0 | extra->number = 0; |
451 | 0 | } |
452 | | |
453 | 0 | return TRUE; |
454 | 0 | } |
455 | | |
456 | | rdpBitmapCache* bitmap_cache_new(rdpContext* context) |
457 | 0 | { |
458 | 0 | WINPR_ASSERT(context); |
459 | |
|
460 | 0 | rdpBitmapCache* bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache)); |
461 | |
|
462 | 0 | if (!bitmapCache) |
463 | 0 | return nullptr; |
464 | | |
465 | 0 | bitmapCache->context = context; |
466 | |
|
467 | 0 | if (!bitmap_cache_resize(bitmapCache)) |
468 | 0 | goto fail; |
469 | | |
470 | 0 | return bitmapCache; |
471 | 0 | fail: |
472 | 0 | WINPR_PRAGMA_DIAG_PUSH |
473 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
474 | 0 | bitmap_cache_free(bitmapCache); |
475 | 0 | WINPR_PRAGMA_DIAG_POP |
476 | 0 | return nullptr; |
477 | 0 | } |
478 | | |
479 | | void bitmap_cache_free(rdpBitmapCache* bitmapCache) |
480 | 0 | { |
481 | 0 | if (!bitmapCache) |
482 | 0 | return; |
483 | | |
484 | 0 | bitmap_cache_save_persistent(bitmapCache); |
485 | |
|
486 | 0 | bitmap_cache_cell_free(bitmapCache); |
487 | |
|
488 | 0 | persistent_cache_free(bitmapCache->persistent); |
489 | |
|
490 | 0 | free(bitmapCache); |
491 | 0 | } |
492 | | |
493 | | static void free_bitmap_data(BITMAP_DATA* data, size_t count) |
494 | 0 | { |
495 | 0 | if (!data) |
496 | 0 | return; |
497 | | |
498 | 0 | for (size_t x = 0; x < count; x++) |
499 | 0 | free(data[x].bitmapDataStream); |
500 | |
|
501 | 0 | free(data); |
502 | 0 | } |
503 | | |
504 | | static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count) |
505 | 0 | { |
506 | 0 | BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA)); |
507 | |
|
508 | 0 | if (!dst) |
509 | 0 | goto fail; |
510 | | |
511 | 0 | for (size_t x = 0; x < count; x++) |
512 | 0 | { |
513 | 0 | dst[x] = data[x]; |
514 | |
|
515 | 0 | if (data[x].bitmapLength > 0) |
516 | 0 | { |
517 | 0 | dst[x].bitmapDataStream = malloc(data[x].bitmapLength); |
518 | |
|
519 | 0 | if (!dst[x].bitmapDataStream) |
520 | 0 | goto fail; |
521 | | |
522 | 0 | memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength); |
523 | 0 | } |
524 | 0 | } |
525 | | |
526 | 0 | return dst; |
527 | 0 | fail: |
528 | 0 | free_bitmap_data(dst, count); |
529 | 0 | return nullptr; |
530 | 0 | } |
531 | | |
532 | | void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context, |
533 | | WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer) |
534 | 0 | { |
535 | 0 | if (!pointer) |
536 | 0 | return; |
537 | | |
538 | 0 | free_bitmap_data(pointer->rectangles, pointer->number); |
539 | 0 | free(pointer); |
540 | 0 | } |
541 | | |
542 | | BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer) |
543 | 0 | { |
544 | 0 | BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE)); |
545 | |
|
546 | 0 | if (!dst || !pointer) |
547 | 0 | goto fail; |
548 | | |
549 | 0 | *dst = *pointer; |
550 | 0 | dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number); |
551 | |
|
552 | 0 | if (!dst->rectangles) |
553 | 0 | goto fail; |
554 | | |
555 | 0 | return dst; |
556 | 0 | fail: |
557 | 0 | WINPR_PRAGMA_DIAG_PUSH |
558 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
559 | 0 | free_bitmap_update(context, dst); |
560 | 0 | WINPR_PRAGMA_DIAG_POP |
561 | 0 | return nullptr; |
562 | 0 | } |
563 | | |
564 | | CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order) |
565 | 0 | { |
566 | 0 | CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER)); |
567 | |
|
568 | 0 | if (!dst || !order) |
569 | 0 | goto fail; |
570 | | |
571 | 0 | *dst = *order; |
572 | |
|
573 | 0 | if (order->bitmapLength > 0) |
574 | 0 | { |
575 | 0 | dst->bitmapDataStream = malloc(order->bitmapLength); |
576 | |
|
577 | 0 | if (!dst->bitmapDataStream) |
578 | 0 | goto fail; |
579 | | |
580 | 0 | memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength); |
581 | 0 | } |
582 | | |
583 | 0 | return dst; |
584 | 0 | fail: |
585 | 0 | WINPR_PRAGMA_DIAG_PUSH |
586 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
587 | 0 | free_cache_bitmap_order(context, dst); |
588 | 0 | WINPR_PRAGMA_DIAG_POP |
589 | 0 | return nullptr; |
590 | 0 | } |
591 | | |
592 | | void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order) |
593 | 0 | { |
594 | 0 | if (order) |
595 | 0 | free(order->bitmapDataStream); |
596 | |
|
597 | 0 | free(order); |
598 | 0 | } |
599 | | |
600 | | CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context, |
601 | | const CACHE_BITMAP_V2_ORDER* order) |
602 | 0 | { |
603 | 0 | CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER)); |
604 | |
|
605 | 0 | if (!dst || !order) |
606 | 0 | goto fail; |
607 | | |
608 | 0 | *dst = *order; |
609 | |
|
610 | 0 | if (order->bitmapLength > 0) |
611 | 0 | { |
612 | 0 | dst->bitmapDataStream = malloc(order->bitmapLength); |
613 | |
|
614 | 0 | if (!dst->bitmapDataStream) |
615 | 0 | goto fail; |
616 | | |
617 | 0 | memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength); |
618 | 0 | } |
619 | | |
620 | 0 | return dst; |
621 | 0 | fail: |
622 | 0 | WINPR_PRAGMA_DIAG_PUSH |
623 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
624 | 0 | free_cache_bitmap_v2_order(context, dst); |
625 | 0 | WINPR_PRAGMA_DIAG_POP |
626 | 0 | return nullptr; |
627 | 0 | } |
628 | | |
629 | | void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context, |
630 | | WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order) |
631 | 0 | { |
632 | 0 | if (order) |
633 | 0 | free(order->bitmapDataStream); |
634 | |
|
635 | 0 | free(order); |
636 | 0 | } |
637 | | |
638 | | CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context, |
639 | | const CACHE_BITMAP_V3_ORDER* order) |
640 | 0 | { |
641 | 0 | CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER)); |
642 | |
|
643 | 0 | if (!dst || !order) |
644 | 0 | goto fail; |
645 | | |
646 | 0 | *dst = *order; |
647 | |
|
648 | 0 | if (order->bitmapData.length > 0) |
649 | 0 | { |
650 | 0 | dst->bitmapData.data = malloc(order->bitmapData.length); |
651 | |
|
652 | 0 | if (!dst->bitmapData.data) |
653 | 0 | goto fail; |
654 | | |
655 | 0 | memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length); |
656 | 0 | } |
657 | | |
658 | 0 | return dst; |
659 | 0 | fail: |
660 | 0 | WINPR_PRAGMA_DIAG_PUSH |
661 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
662 | 0 | free_cache_bitmap_v3_order(context, dst); |
663 | 0 | WINPR_PRAGMA_DIAG_POP |
664 | 0 | return nullptr; |
665 | 0 | } |
666 | | |
667 | | void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order) |
668 | 0 | { |
669 | 0 | if (order) |
670 | 0 | free(order->bitmapData.data); |
671 | |
|
672 | 0 | free(order); |
673 | 0 | } |