FreeRDP/libfreerdp/cache/bitmap.c

505 lines
12 KiB
C
Raw Normal View History

/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* Bitmap Cache V2
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <winpr/crt.h>
#include <freerdp/freerdp.h>
#include <freerdp/constants.h>
#include <winpr/stream.h>
#include <freerdp/log.h>
#include <freerdp/cache/bitmap.h>
2016-04-14 00:10:08 +03:00
#include <freerdp/gdi/bitmap.h>
2016-07-21 11:07:42 +03:00
#include "../gdi/gdi.h"
2016-10-10 11:38:54 +03:00
#include "../core/graphics.h"
2016-07-21 11:07:42 +03:00
#include "bitmap.h"
#define TAG FREERDP_TAG("cache.bitmap")
2019-11-06 17:24:51 +03:00
static rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index);
static BOOL bitmap_cache_put(rdpBitmapCache* bitmap_cache, UINT32 id, UINT32 index,
rdpBitmap* bitmap);
2016-10-10 11:38:54 +03:00
2019-11-06 17:24:51 +03:00
static BOOL update_gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt)
{
rdpBitmap* bitmap;
rdpCache* cache = context->cache;
if (memblt->cacheId == 0xFF)
bitmap = offscreen_cache_get(cache->offscreen, memblt->cacheIndex);
else
2019-11-06 17:24:51 +03:00
bitmap = bitmap_cache_get(cache->bitmap, (BYTE)memblt->cacheId, memblt->cacheIndex);
2016-07-18 15:42:55 +03:00
/* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
if (bitmap == NULL)
return TRUE;
memblt->bitmap = bitmap;
return IFCALLRESULT(TRUE, cache->bitmap->MemBlt, context, memblt);
}
2019-11-06 17:24:51 +03:00
static BOOL update_gdi_mem3blt(rdpContext* context, MEM3BLT_ORDER* mem3blt)
{
BYTE style;
rdpBitmap* bitmap;
rdpCache* cache = context->cache;
2012-02-13 03:12:28 +04:00
rdpBrush* brush = &mem3blt->brush;
BOOL ret = TRUE;
if (mem3blt->cacheId == 0xFF)
bitmap = offscreen_cache_get(cache->offscreen, mem3blt->cacheIndex);
else
2019-11-06 17:24:51 +03:00
bitmap = bitmap_cache_get(cache->bitmap, (BYTE)mem3blt->cacheId, mem3blt->cacheIndex);
2013-11-03 22:51:41 +04:00
/* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
2013-11-03 22:51:41 +04:00
if (!bitmap)
return TRUE;
2012-02-13 03:12:28 +04:00
style = brush->style;
if (brush->style & CACHED_BRUSH)
{
brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
2016-07-18 15:42:55 +03:00
if (!brush->data)
return FALSE;
2016-07-18 15:42:55 +03:00
2012-02-13 03:12:28 +04:00
brush->style = 0x03;
}
mem3blt->bitmap = bitmap;
IFCALLRET(cache->bitmap->Mem3Blt, ret, context, mem3blt);
2012-02-13 03:12:28 +04:00
brush->style = style;
return ret;
}
2019-11-06 17:24:51 +03:00
static BOOL update_gdi_cache_bitmap(rdpContext* context, const CACHE_BITMAP_ORDER* cacheBitmap)
{
rdpBitmap* bitmap;
rdpBitmap* prevBitmap;
rdpCache* cache = context->cache;
bitmap = Bitmap_Alloc(context);
2016-07-18 15:42:55 +03:00
if (!bitmap)
return FALSE;
2019-11-06 17:24:51 +03:00
Bitmap_SetDimensions(bitmap, cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight);
2016-07-20 16:34:06 +03:00
2019-11-06 17:24:51 +03:00
if (!bitmap->Decompress(context, bitmap, cacheBitmap->bitmapDataStream,
cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight,
cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength,
cacheBitmap->compressed, RDP_CODEC_ID_NONE))
{
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, bitmap);
return FALSE;
}
2016-04-14 00:10:08 +03:00
if (!bitmap->New(context, bitmap))
2016-07-19 14:02:08 +03:00
{
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, bitmap);
2016-04-14 00:10:08 +03:00
return FALSE;
2016-07-19 14:02:08 +03:00
}
2019-11-06 17:24:51 +03:00
prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex);
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, prevBitmap);
2019-11-06 17:24:51 +03:00
return bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap);
}
2019-11-06 17:24:51 +03:00
static BOOL update_gdi_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
{
rdpBitmap* bitmap;
rdpBitmap* prevBitmap;
rdpCache* cache = context->cache;
2014-09-12 09:11:40 +04:00
rdpSettings* settings = context->settings;
bitmap = Bitmap_Alloc(context);
2016-07-18 15:42:55 +03:00
if (!bitmap)
return FALSE;
if (!cacheBitmapV2->bitmapBpp)
2014-09-12 09:11:40 +04:00
cacheBitmapV2->bitmapBpp = settings->ColorDepth;
if ((settings->ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16))
cacheBitmapV2->bitmapBpp = settings->ColorDepth;
2019-11-06 17:24:51 +03:00
Bitmap_SetDimensions(bitmap, cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight);
if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream,
cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight,
cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength,
cacheBitmapV2->compressed, RDP_CODEC_ID_NONE))
{
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, bitmap);
return FALSE;
}
2019-11-06 17:24:51 +03:00
prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex);
2016-04-19 22:30:28 +03:00
if (!bitmap->New(context, bitmap))
2016-07-20 16:34:06 +03:00
{
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, bitmap);
2016-04-14 00:10:08 +03:00
return FALSE;
2016-07-20 16:34:06 +03:00
}
2016-04-14 00:10:08 +03:00
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, prevBitmap);
2019-11-06 17:24:51 +03:00
return bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex,
bitmap);
}
2019-11-06 17:24:51 +03:00
static BOOL update_gdi_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
{
rdpBitmap* bitmap;
rdpBitmap* prevBitmap;
2014-09-12 09:11:40 +04:00
BOOL compressed = TRUE;
rdpCache* cache = context->cache;
2014-09-12 09:11:40 +04:00
rdpSettings* settings = context->settings;
2013-11-03 22:51:41 +04:00
BITMAP_DATA_EX* bitmapData = &cacheBitmapV3->bitmapData;
bitmap = Bitmap_Alloc(context);
2016-07-18 15:42:55 +03:00
if (!bitmap)
return FALSE;
if (!cacheBitmapV3->bpp)
2014-09-12 09:11:40 +04:00
cacheBitmapV3->bpp = settings->ColorDepth;
2014-09-12 09:11:40 +04:00
compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE);
2016-07-20 16:34:06 +03:00
Bitmap_SetDimensions(bitmap, bitmapData->width, bitmapData->height);
2019-11-06 17:24:51 +03:00
if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width,
bitmapData->height, bitmapData->bpp, bitmapData->length, compressed,
bitmapData->codecID))
2016-07-20 16:34:06 +03:00
{
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, bitmap);
2016-07-20 16:34:06 +03:00
return FALSE;
}
2016-04-19 22:30:28 +03:00
if (!bitmap->New(context, bitmap))
2016-07-20 16:34:06 +03:00
{
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, bitmap);
2016-04-14 00:10:08 +03:00
return FALSE;
2016-07-20 16:34:06 +03:00
}
2019-11-06 17:24:51 +03:00
prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex);
2016-10-10 11:38:54 +03:00
Bitmap_Free(context, prevBitmap);
2019-11-06 17:24:51 +03:00
return bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex,
bitmap);
}
2019-11-06 17:24:51 +03:00
rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index)
{
rdpBitmap* bitmap;
if (id >= bitmapCache->maxCells)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
return NULL;
}
if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
{
2013-11-03 22:51:41 +04:00
index = bitmapCache->cells[id].number;
}
2013-11-03 22:51:41 +04:00
else if (index > bitmapCache->cells[id].number)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "get invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
return NULL;
}
2013-11-03 22:51:41 +04:00
bitmap = bitmapCache->cells[id].entries[index];
return bitmap;
}
2019-11-06 17:24:51 +03:00
BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, rdpBitmap* bitmap)
{
2013-11-03 22:51:41 +04:00
if (id > bitmapCache->maxCells)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "put invalid bitmap cell id: %" PRIu32 "", id);
2016-10-10 11:38:54 +03:00
return FALSE;
}
if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
{
2013-11-03 22:51:41 +04:00
index = bitmapCache->cells[id].number;
}
2013-11-03 22:51:41 +04:00
else if (index > bitmapCache->cells[id].number)
{
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "put invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
2016-10-10 11:38:54 +03:00
return FALSE;
}
2013-11-03 22:51:41 +04:00
bitmapCache->cells[id].entries[index] = bitmap;
2016-10-10 11:38:54 +03:00
return TRUE;
}
void bitmap_cache_register_callbacks(rdpUpdate* update)
{
rdpCache* cache = update->context->cache;
cache->bitmap->MemBlt = update->primary->MemBlt;
cache->bitmap->Mem3Blt = update->primary->Mem3Blt;
update->primary->MemBlt = update_gdi_memblt;
update->primary->Mem3Blt = update_gdi_mem3blt;
update->secondary->CacheBitmap = update_gdi_cache_bitmap;
update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2;
update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3;
2016-07-21 11:07:42 +03:00
update->BitmapUpdate = gdi_bitmap_update;
}
rdpBitmapCache* bitmap_cache_new(rdpSettings* settings)
{
UINT32 i;
2013-11-03 22:51:41 +04:00
rdpBitmapCache* bitmapCache;
2019-11-06 17:24:51 +03:00
bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
2016-10-04 09:54:42 +03:00
if (!bitmapCache)
return NULL;
bitmapCache->settings = settings;
2019-11-06 17:24:51 +03:00
bitmapCache->update = ((freerdp*)settings->instance)->update;
2016-10-04 09:54:42 +03:00
bitmapCache->context = bitmapCache->update->context;
2020-04-02 11:33:44 +03:00
bitmapCache->cells =
(BITMAP_V2_CELL*)calloc(settings->BitmapCacheV2NumCells, sizeof(BITMAP_V2_CELL));
2016-10-04 09:54:42 +03:00
if (!bitmapCache->cells)
goto fail;
2020-04-02 11:33:44 +03:00
bitmapCache->maxCells = settings->BitmapCacheV2NumCells;
2016-10-04 09:54:42 +03:00
for (i = 0; i < bitmapCache->maxCells; i++)
{
2020-04-02 11:50:16 +03:00
BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
UINT32 nr = settings->BitmapCacheV2CellInfo[i].numEntries;
2016-10-04 09:54:42 +03:00
/* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
2020-04-02 11:50:16 +03:00
cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
2020-04-02 11:50:16 +03:00
if (!cell->entries)
2016-10-04 09:54:42 +03:00
goto fail;
2020-04-02 11:50:16 +03:00
cell->number = nr;
}
2013-11-03 22:51:41 +04:00
return bitmapCache;
2016-10-04 09:54:42 +03:00
fail:
bitmap_cache_free(bitmapCache);
2016-10-04 09:54:42 +03:00
return NULL;
}
2013-11-03 22:51:41 +04:00
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
{
if (bitmapCache)
{
2020-04-02 11:50:16 +03:00
UINT32 i;
for (i = 0; i < bitmapCache->maxCells; i++)
{
2020-04-02 11:50:16 +03:00
UINT32 j;
BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
if (!cell->entries)
continue;
for (j = 0; j < cell->number + 1; j++)
{
2020-04-02 11:50:16 +03:00
rdpBitmap* bitmap = cell->entries[j];
2016-10-10 11:38:54 +03:00
Bitmap_Free(bitmapCache->context, bitmap);
}
2013-11-03 22:51:41 +04:00
free(bitmapCache->cells[i].entries);
}
2013-11-03 22:51:41 +04:00
free(bitmapCache->cells);
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;
2019-11-06 17:24:51 +03:00
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,
2019-11-06 17:24:51 +03:00
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,
2019-11-06 17:24:51 +03:00
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);
}