mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
Refactored order updates
Unified order creation/copy/delete to avoid memory leaks.
This commit is contained in:
245
libfreerdp/cache/bitmap.c
vendored
245
libfreerdp/cache/bitmap.c
vendored
@@ -36,15 +36,17 @@
|
||||
#include "../gdi/gdi.h"
|
||||
#include "../core/graphics.h"
|
||||
|
||||
#include "bitmap.h"
|
||||
|
||||
#define TAG FREERDP_TAG("cache.bitmap")
|
||||
|
||||
static rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id,
|
||||
UINT32 index);
|
||||
UINT32 index);
|
||||
static BOOL bitmap_cache_put(rdpBitmapCache* bitmap_cache, UINT32 id,
|
||||
UINT32 index, rdpBitmap* bitmap);
|
||||
UINT32 index, rdpBitmap* bitmap);
|
||||
|
||||
static BOOL update_gdi_memblt(rdpContext* context,
|
||||
MEMBLT_ORDER* memblt)
|
||||
MEMBLT_ORDER* memblt)
|
||||
{
|
||||
rdpBitmap* bitmap;
|
||||
rdpCache* cache = context->cache;
|
||||
@@ -53,7 +55,7 @@ static BOOL update_gdi_memblt(rdpContext* context,
|
||||
bitmap = offscreen_cache_get(cache->offscreen, memblt->cacheIndex);
|
||||
else
|
||||
bitmap = bitmap_cache_get(cache->bitmap, (BYTE) memblt->cacheId,
|
||||
memblt->cacheIndex);
|
||||
memblt->cacheIndex);
|
||||
|
||||
/* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
|
||||
if (bitmap == NULL)
|
||||
@@ -64,7 +66,7 @@ static BOOL update_gdi_memblt(rdpContext* context,
|
||||
}
|
||||
|
||||
static BOOL update_gdi_mem3blt(rdpContext* context,
|
||||
MEM3BLT_ORDER* mem3blt)
|
||||
MEM3BLT_ORDER* mem3blt)
|
||||
{
|
||||
BYTE style;
|
||||
rdpBitmap* bitmap;
|
||||
@@ -76,7 +78,7 @@ static BOOL update_gdi_mem3blt(rdpContext* context,
|
||||
bitmap = offscreen_cache_get(cache->offscreen, mem3blt->cacheIndex);
|
||||
else
|
||||
bitmap = bitmap_cache_get(cache->bitmap, (BYTE) mem3blt->cacheId,
|
||||
mem3blt->cacheIndex);
|
||||
mem3blt->cacheIndex);
|
||||
|
||||
/* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
|
||||
if (!bitmap)
|
||||
@@ -101,7 +103,7 @@ static BOOL update_gdi_mem3blt(rdpContext* context,
|
||||
}
|
||||
|
||||
static BOOL update_gdi_cache_bitmap(rdpContext* context,
|
||||
const CACHE_BITMAP_ORDER* cacheBitmap)
|
||||
const CACHE_BITMAP_ORDER* cacheBitmap)
|
||||
{
|
||||
rdpBitmap* bitmap;
|
||||
rdpBitmap* prevBitmap;
|
||||
@@ -112,13 +114,13 @@ static BOOL update_gdi_cache_bitmap(rdpContext* context,
|
||||
return FALSE;
|
||||
|
||||
Bitmap_SetDimensions(bitmap, cacheBitmap->bitmapWidth,
|
||||
cacheBitmap->bitmapHeight);
|
||||
cacheBitmap->bitmapHeight);
|
||||
|
||||
if (!bitmap->Decompress(context, bitmap,
|
||||
cacheBitmap->bitmapDataStream, cacheBitmap->bitmapWidth,
|
||||
cacheBitmap->bitmapHeight,
|
||||
cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength,
|
||||
cacheBitmap->compressed, RDP_CODEC_ID_NONE))
|
||||
cacheBitmap->bitmapDataStream, cacheBitmap->bitmapWidth,
|
||||
cacheBitmap->bitmapHeight,
|
||||
cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength,
|
||||
cacheBitmap->compressed, RDP_CODEC_ID_NONE))
|
||||
{
|
||||
Bitmap_Free(context, bitmap);
|
||||
return FALSE;
|
||||
@@ -131,15 +133,15 @@ static BOOL update_gdi_cache_bitmap(rdpContext* context,
|
||||
}
|
||||
|
||||
prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmap->cacheId,
|
||||
cacheBitmap->cacheIndex);
|
||||
cacheBitmap->cacheIndex);
|
||||
Bitmap_Free(context, prevBitmap);
|
||||
return bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId,
|
||||
cacheBitmap->cacheIndex,
|
||||
bitmap);
|
||||
cacheBitmap->cacheIndex,
|
||||
bitmap);
|
||||
}
|
||||
|
||||
static BOOL update_gdi_cache_bitmap_v2(rdpContext* context,
|
||||
CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
|
||||
CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
|
||||
|
||||
{
|
||||
rdpBitmap* bitmap;
|
||||
@@ -158,23 +160,23 @@ static BOOL update_gdi_cache_bitmap_v2(rdpContext* context,
|
||||
cacheBitmapV2->bitmapBpp = settings->ColorDepth;
|
||||
|
||||
Bitmap_SetDimensions(bitmap, cacheBitmapV2->bitmapWidth,
|
||||
cacheBitmapV2->bitmapHeight);
|
||||
cacheBitmapV2->bitmapHeight);
|
||||
|
||||
if (!bitmap->Decompress(context, bitmap,
|
||||
cacheBitmapV2->bitmapDataStream,
|
||||
cacheBitmapV2->bitmapWidth,
|
||||
cacheBitmapV2->bitmapHeight,
|
||||
cacheBitmapV2->bitmapBpp,
|
||||
cacheBitmapV2->bitmapLength,
|
||||
cacheBitmapV2->compressed,
|
||||
RDP_CODEC_ID_NONE))
|
||||
cacheBitmapV2->bitmapDataStream,
|
||||
cacheBitmapV2->bitmapWidth,
|
||||
cacheBitmapV2->bitmapHeight,
|
||||
cacheBitmapV2->bitmapBpp,
|
||||
cacheBitmapV2->bitmapLength,
|
||||
cacheBitmapV2->compressed,
|
||||
RDP_CODEC_ID_NONE))
|
||||
{
|
||||
Bitmap_Free(context, bitmap);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId,
|
||||
cacheBitmapV2->cacheIndex);
|
||||
cacheBitmapV2->cacheIndex);
|
||||
|
||||
if (!bitmap->New(context, bitmap))
|
||||
{
|
||||
@@ -184,11 +186,11 @@ static BOOL update_gdi_cache_bitmap_v2(rdpContext* context,
|
||||
|
||||
Bitmap_Free(context, prevBitmap);
|
||||
return bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId,
|
||||
cacheBitmapV2->cacheIndex, bitmap);
|
||||
cacheBitmapV2->cacheIndex, bitmap);
|
||||
}
|
||||
|
||||
static BOOL update_gdi_cache_bitmap_v3(rdpContext* context,
|
||||
CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
|
||||
CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
|
||||
{
|
||||
rdpBitmap* bitmap;
|
||||
rdpBitmap* prevBitmap;
|
||||
@@ -208,9 +210,9 @@ static BOOL update_gdi_cache_bitmap_v3(rdpContext* context,
|
||||
Bitmap_SetDimensions(bitmap, bitmapData->width, bitmapData->height);
|
||||
|
||||
if (!bitmap->Decompress(context, bitmap,
|
||||
bitmapData->data, bitmapData->width, bitmapData->height,
|
||||
bitmapData->bpp, bitmapData->length, compressed,
|
||||
bitmapData->codecID))
|
||||
bitmapData->data, bitmapData->width, bitmapData->height,
|
||||
bitmapData->bpp, bitmapData->length, compressed,
|
||||
bitmapData->codecID))
|
||||
{
|
||||
Bitmap_Free(context, bitmap);
|
||||
return FALSE;
|
||||
@@ -223,14 +225,14 @@ static BOOL update_gdi_cache_bitmap_v3(rdpContext* context,
|
||||
}
|
||||
|
||||
prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId,
|
||||
cacheBitmapV3->cacheIndex);
|
||||
cacheBitmapV3->cacheIndex);
|
||||
Bitmap_Free(context, prevBitmap);
|
||||
return bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId,
|
||||
cacheBitmapV3->cacheIndex, bitmap);
|
||||
cacheBitmapV3->cacheIndex, bitmap);
|
||||
}
|
||||
|
||||
rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id,
|
||||
UINT32 index)
|
||||
UINT32 index)
|
||||
{
|
||||
rdpBitmap* bitmap;
|
||||
|
||||
@@ -255,7 +257,7 @@ rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id,
|
||||
}
|
||||
|
||||
BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index,
|
||||
rdpBitmap* bitmap)
|
||||
rdpBitmap* bitmap)
|
||||
{
|
||||
if (id > bitmapCache->maxCells)
|
||||
{
|
||||
@@ -304,7 +306,7 @@ rdpBitmapCache* bitmap_cache_new(rdpSettings* settings)
|
||||
bitmapCache->context = bitmapCache->update->context;
|
||||
bitmapCache->maxCells = settings->BitmapCacheV2NumCells;
|
||||
bitmapCache->cells = (BITMAP_V2_CELL*) calloc(bitmapCache->maxCells,
|
||||
sizeof(BITMAP_V2_CELL));
|
||||
sizeof(BITMAP_V2_CELL));
|
||||
|
||||
if (!bitmapCache->cells)
|
||||
goto fail;
|
||||
@@ -314,7 +316,7 @@ rdpBitmapCache* bitmap_cache_new(rdpSettings* settings)
|
||||
bitmapCache->cells[i].number = settings->BitmapCacheV2CellInfo[i].numEntries;
|
||||
/* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
|
||||
bitmapCache->cells[i].entries = (rdpBitmap**) calloc((
|
||||
bitmapCache->cells[i].number + 1), sizeof(rdpBitmap*));
|
||||
bitmapCache->cells[i].number + 1), sizeof(rdpBitmap*));
|
||||
|
||||
if (!bitmapCache->cells[i].entries)
|
||||
goto fail;
|
||||
@@ -355,3 +357,174 @@ void bitmap_cache_free(rdpBitmapCache* bitmapCache)
|
||||
free(bitmapCache);
|
||||
}
|
||||
}
|
||||
|
||||
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
|
||||
{
|
||||
size_t x;
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
for (x = 0; x < count; x++)
|
||||
free(data[x].bitmapDataStream);
|
||||
|
||||
free(data);
|
||||
}
|
||||
|
||||
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
|
||||
{
|
||||
size_t x;
|
||||
BITMAP_DATA* dst = (BITMAP_DATA*) calloc(count, sizeof(BITMAP_DATA));
|
||||
|
||||
if (!dst)
|
||||
goto fail;
|
||||
|
||||
for (x = 0; x < count; x++)
|
||||
{
|
||||
dst[x] = data[x];
|
||||
|
||||
if (data[x].bitmapLength > 0)
|
||||
{
|
||||
dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
|
||||
|
||||
if (!dst[x].bitmapDataStream)
|
||||
goto fail;
|
||||
|
||||
memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
fail:
|
||||
free_bitmap_data(dst, count);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void free_bitmap_update(rdpContext* context, BITMAP_UPDATE* pointer)
|
||||
{
|
||||
if (!pointer)
|
||||
return;
|
||||
|
||||
free_bitmap_data(pointer->rectangles, pointer->number);
|
||||
free(pointer);
|
||||
}
|
||||
|
||||
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
|
||||
{
|
||||
BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
|
||||
|
||||
if (!dst || !pointer)
|
||||
goto fail;
|
||||
|
||||
*dst = *pointer;
|
||||
dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
|
||||
|
||||
if (!dst->rectangles)
|
||||
goto fail;
|
||||
|
||||
return dst;
|
||||
fail:
|
||||
free_bitmap_update(context, dst);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
|
||||
{
|
||||
CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
|
||||
|
||||
if (!dst || !order)
|
||||
goto fail;
|
||||
|
||||
*dst = *order;
|
||||
|
||||
if (order->bitmapLength > 0)
|
||||
{
|
||||
dst->bitmapDataStream = malloc(order->bitmapLength);
|
||||
|
||||
if (!dst->bitmapDataStream)
|
||||
goto fail;
|
||||
|
||||
memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
|
||||
}
|
||||
|
||||
return dst;
|
||||
fail:
|
||||
free_cache_bitmap_order(context, dst);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void free_cache_bitmap_order(rdpContext* context, CACHE_BITMAP_ORDER* order)
|
||||
{
|
||||
if (order)
|
||||
free(order->bitmapDataStream);
|
||||
|
||||
free(order);
|
||||
}
|
||||
|
||||
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
|
||||
const CACHE_BITMAP_V2_ORDER* order)
|
||||
{
|
||||
CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
|
||||
|
||||
if (!dst || !order)
|
||||
goto fail;
|
||||
|
||||
*dst = *order;
|
||||
|
||||
if (order->bitmapLength > 0)
|
||||
{
|
||||
dst->bitmapDataStream = malloc(order->bitmapLength);
|
||||
|
||||
if (!dst->bitmapDataStream)
|
||||
goto fail;
|
||||
|
||||
memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
|
||||
}
|
||||
|
||||
return dst;
|
||||
fail:
|
||||
free_cache_bitmap_v2_order(context, dst);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void free_cache_bitmap_v2_order(rdpContext* context, CACHE_BITMAP_V2_ORDER* order)
|
||||
{
|
||||
if (order)
|
||||
free(order->bitmapDataStream);
|
||||
|
||||
free(order);
|
||||
}
|
||||
|
||||
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
|
||||
const CACHE_BITMAP_V3_ORDER* order)
|
||||
{
|
||||
CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
|
||||
|
||||
if (!dst || !order)
|
||||
goto fail;
|
||||
|
||||
*dst = *order;
|
||||
|
||||
if (order->bitmapData.length > 0)
|
||||
{
|
||||
dst->bitmapData.data = malloc(order->bitmapData.length);
|
||||
|
||||
if (!dst->bitmapData.data)
|
||||
goto fail;
|
||||
|
||||
memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
|
||||
}
|
||||
|
||||
return dst;
|
||||
fail:
|
||||
free_cache_bitmap_v3_order(context, dst);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void free_cache_bitmap_v3_order(rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
|
||||
{
|
||||
if (order)
|
||||
free(order->bitmapData.data);
|
||||
|
||||
free(order);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user