2011-08-05 21:58:46 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-05 21:58:46 +04:00
|
|
|
* Brush Cache
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-09-24 02:41:07 +04:00
|
|
|
#include <stdio.h>
|
2012-11-20 08:49:08 +04:00
|
|
|
#include <winpr/crt.h>
|
2021-09-06 15:00:12 +03:00
|
|
|
#include <winpr/assert.h>
|
2012-11-20 08:49:08 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2011-10-21 07:15:18 +04:00
|
|
|
#include <freerdp/update.h>
|
|
|
|
#include <freerdp/freerdp.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2011-08-05 21:58:46 +04:00
|
|
|
|
2018-03-30 11:43:20 +03:00
|
|
|
#include "brush.h"
|
2023-02-07 17:42:52 +03:00
|
|
|
#include "cache.h"
|
2018-03-30 11:43:20 +03:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("cache.brush")
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2019-11-14 17:10:03 +03:00
|
|
|
{
|
|
|
|
UINT32 bpp;
|
|
|
|
void* entry;
|
2022-02-14 16:59:22 +03:00
|
|
|
} BRUSH_ENTRY;
|
2019-11-14 17:10:03 +03:00
|
|
|
|
|
|
|
struct rdp_brush_cache
|
|
|
|
{
|
|
|
|
pPatBlt PatBlt; /* 0 */
|
|
|
|
pCacheBrush CacheBrush; /* 1 */
|
|
|
|
pPolygonSC PolygonSC; /* 2 */
|
|
|
|
pPolygonCB PolygonCB; /* 3 */
|
|
|
|
UINT32 paddingA[16 - 4]; /* 4 */
|
|
|
|
|
|
|
|
UINT32 maxEntries; /* 16 */
|
|
|
|
UINT32 maxMonoEntries; /* 17 */
|
|
|
|
BRUSH_ENTRY* entries; /* 18 */
|
|
|
|
BRUSH_ENTRY* monoEntries; /* 19 */
|
|
|
|
UINT32 paddingB[32 - 20]; /* 20 */
|
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
rdpContext* context;
|
2019-11-14 17:10:03 +03:00
|
|
|
};
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL update_gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
|
2011-10-21 07:15:18 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE style = 0;
|
2015-04-14 11:14:23 +03:00
|
|
|
BOOL ret = TRUE;
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpBrush* brush = NULL;
|
|
|
|
const rdpCache* cache = NULL;
|
2021-09-06 15:00:12 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(patblt);
|
|
|
|
|
|
|
|
cache = context->cache;
|
|
|
|
WINPR_ASSERT(cache);
|
|
|
|
|
|
|
|
brush = &patblt->brush;
|
2011-12-31 21:06:09 +04:00
|
|
|
style = brush->style;
|
|
|
|
|
2011-10-21 07:15:18 +04:00
|
|
|
if (brush->style & CACHED_BRUSH)
|
|
|
|
{
|
|
|
|
brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
|
|
|
|
brush->style = 0x03;
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_ASSERT(cache->brush);
|
2015-04-14 11:14:23 +03:00
|
|
|
IFCALLRET(cache->brush->PatBlt, ret, context, patblt);
|
2011-12-31 21:06:09 +04:00
|
|
|
brush->style = style;
|
2015-04-14 11:14:23 +03:00
|
|
|
return ret;
|
2011-10-21 07:15:18 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL update_gdi_polygon_sc(rdpContext* context, const POLYGON_SC_ORDER* polygon_sc)
|
2012-02-13 02:14:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpCache* cache = NULL;
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_ASSERT(context);
|
|
|
|
cache = context->cache;
|
|
|
|
WINPR_ASSERT(cache);
|
|
|
|
WINPR_ASSERT(cache->brush);
|
2015-04-17 17:21:55 +03:00
|
|
|
return IFCALLRESULT(TRUE, cache->brush->PolygonSC, context, polygon_sc);
|
2012-02-13 02:14:59 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL update_gdi_polygon_cb(rdpContext* context, POLYGON_CB_ORDER* polygon_cb)
|
2012-02-13 02:14:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE style = 0;
|
|
|
|
rdpBrush* brush = NULL;
|
|
|
|
rdpCache* cache = NULL;
|
2015-04-14 11:14:23 +03:00
|
|
|
BOOL ret = TRUE;
|
2021-09-06 15:00:12 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(polygon_cb);
|
|
|
|
|
|
|
|
cache = context->cache;
|
|
|
|
WINPR_ASSERT(cache);
|
|
|
|
|
|
|
|
brush = &polygon_cb->brush;
|
2012-02-13 02:14:59 +04:00
|
|
|
style = brush->style;
|
|
|
|
|
|
|
|
if (brush->style & CACHED_BRUSH)
|
|
|
|
{
|
|
|
|
brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
|
|
|
|
brush->style = 0x03;
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_ASSERT(cache->brush);
|
2015-04-14 11:14:23 +03:00
|
|
|
IFCALLRET(cache->brush->PolygonCB, ret, context, polygon_cb);
|
2012-02-13 02:14:59 +04:00
|
|
|
brush->style = style;
|
2015-04-14 11:14:23 +03:00
|
|
|
return ret;
|
2012-02-13 02:14:59 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL update_gdi_cache_brush(rdpContext* context, const CACHE_BRUSH_ORDER* cacheBrush)
|
2011-10-21 07:15:18 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT32 length = 0;
|
2013-01-29 03:42:32 +04:00
|
|
|
void* data = NULL;
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpCache* cache = NULL;
|
2021-09-06 15:00:12 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(cacheBrush);
|
|
|
|
|
|
|
|
cache = context->cache;
|
|
|
|
WINPR_ASSERT(cache);
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
length = cacheBrush->bpp * 64 / 8;
|
2013-01-29 03:42:32 +04:00
|
|
|
data = malloc(length);
|
2018-03-30 11:43:20 +03:00
|
|
|
|
2015-04-14 11:14:23 +03:00
|
|
|
if (!data)
|
|
|
|
return FALSE;
|
2013-01-29 03:42:32 +04:00
|
|
|
|
2018-03-30 11:43:20 +03:00
|
|
|
CopyMemory(data, cacheBrush->data, length);
|
2013-11-03 22:51:41 +04:00
|
|
|
brush_cache_put(cache->brush, cacheBrush->index, data, cacheBrush->bpp);
|
2015-04-14 11:14:23 +03:00
|
|
|
return TRUE;
|
2011-10-21 07:15:18 +04:00
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
void* brush_cache_get(rdpBrushCache* brushCache, UINT32 index, UINT32* bpp)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
void* entry = NULL;
|
2011-08-05 21:58:46 +04:00
|
|
|
|
2014-04-21 22:42:24 +04:00
|
|
|
if (!brushCache)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!bpp)
|
|
|
|
return NULL;
|
2013-09-02 12:57:44 +04:00
|
|
|
|
2011-08-08 05:12:36 +04:00
|
|
|
if (*bpp == 1)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2013-11-03 22:51:41 +04:00
|
|
|
if (index >= brushCache->maxMonoEntries)
|
2011-08-08 05:12:36 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
|
2011-08-08 05:12:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
*bpp = brushCache->monoEntries[index].bpp;
|
|
|
|
entry = brushCache->monoEntries[index].entry;
|
2011-08-08 05:12:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-03 22:51:41 +04:00
|
|
|
if (index >= brushCache->maxEntries)
|
2011-08-08 05:12:36 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
|
2011-08-08 05:12:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
*bpp = brushCache->entries[index].bpp;
|
|
|
|
entry = brushCache->entries[index].entry;
|
2011-08-05 21:58:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) at index: 0x%08" PRIX32 "", *bpp, index);
|
2011-08-05 21:58:46 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
void brush_cache_put(rdpBrushCache* brushCache, UINT32 index, void* entry, UINT32 bpp)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_ASSERT(brushCache);
|
|
|
|
|
2011-08-08 05:12:36 +04:00
|
|
|
if (bpp == 1)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2013-11-03 22:51:41 +04:00
|
|
|
if (index >= brushCache->maxMonoEntries)
|
2011-08-08 05:12:36 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(entry);
|
2011-08-08 05:12:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_ASSERT(brushCache->monoEntries);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(brushCache->monoEntries[index].entry);
|
2013-11-03 22:51:41 +04:00
|
|
|
brushCache->monoEntries[index].bpp = bpp;
|
|
|
|
brushCache->monoEntries[index].entry = entry;
|
2011-08-08 05:12:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-03 22:51:41 +04:00
|
|
|
if (index >= brushCache->maxEntries)
|
2011-08-08 05:12:36 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(entry);
|
2011-08-08 05:12:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_ASSERT(brushCache->entries);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(brushCache->entries[index].entry);
|
2013-11-03 22:51:41 +04:00
|
|
|
brushCache->entries[index].bpp = bpp;
|
|
|
|
brushCache->entries[index].entry = entry;
|
2011-08-05 21:58:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-21 07:15:18 +04:00
|
|
|
void brush_cache_register_callbacks(rdpUpdate* update)
|
|
|
|
{
|
2021-09-10 10:06:35 +03:00
|
|
|
WINPR_ASSERT(update);
|
|
|
|
WINPR_ASSERT(update->context);
|
|
|
|
WINPR_ASSERT(update->primary);
|
|
|
|
WINPR_ASSERT(update->secondary);
|
|
|
|
|
2021-09-17 09:59:10 +03:00
|
|
|
if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
|
|
|
|
{
|
|
|
|
rdpCache* cache = update->context->cache;
|
|
|
|
WINPR_ASSERT(cache);
|
|
|
|
WINPR_ASSERT(cache->brush);
|
|
|
|
|
|
|
|
cache->brush->PatBlt = update->primary->PatBlt;
|
|
|
|
cache->brush->PolygonSC = update->primary->PolygonSC;
|
|
|
|
cache->brush->PolygonCB = update->primary->PolygonCB;
|
|
|
|
update->primary->PatBlt = update_gdi_patblt;
|
|
|
|
update->primary->PolygonSC = update_gdi_polygon_sc;
|
|
|
|
update->primary->PolygonCB = update_gdi_polygon_cb;
|
|
|
|
update->secondary->CacheBrush = update_gdi_cache_brush;
|
|
|
|
}
|
2011-10-21 07:15:18 +04:00
|
|
|
}
|
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
rdpBrushCache* brush_cache_new(rdpContext* context)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpBrushCache* brushCache = NULL;
|
2021-09-06 15:00:12 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
brushCache = (rdpBrushCache*)calloc(1, sizeof(rdpBrushCache));
|
2011-08-05 21:58:46 +04:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!brushCache)
|
|
|
|
return NULL;
|
2013-11-03 22:51:41 +04:00
|
|
|
|
2021-09-06 15:00:12 +03:00
|
|
|
brushCache->context = context;
|
2015-06-16 16:42:07 +03:00
|
|
|
brushCache->maxEntries = 64;
|
|
|
|
brushCache->maxMonoEntries = 64;
|
|
|
|
brushCache->entries = (BRUSH_ENTRY*)calloc(brushCache->maxEntries, sizeof(BRUSH_ENTRY));
|
2018-03-30 11:43:20 +03:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!brushCache->entries)
|
2021-09-06 15:00:12 +03:00
|
|
|
goto fail;
|
2012-11-20 08:49:08 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
brushCache->monoEntries = (BRUSH_ENTRY*)calloc(brushCache->maxMonoEntries, sizeof(BRUSH_ENTRY));
|
2018-03-30 11:43:20 +03:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!brushCache->monoEntries)
|
2021-09-06 15:00:12 +03:00
|
|
|
goto fail;
|
2011-08-05 21:58:46 +04:00
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
return brushCache;
|
2021-09-06 15:00:12 +03:00
|
|
|
fail:
|
2024-02-04 13:11:29 +03:00
|
|
|
WINPR_PRAGMA_DIAG_PUSH
|
|
|
|
WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
|
2021-09-06 15:00:12 +03:00
|
|
|
brush_cache_free(brushCache);
|
2024-02-04 13:11:29 +03:00
|
|
|
WINPR_PRAGMA_DIAG_POP
|
2015-06-16 16:42:07 +03:00
|
|
|
return NULL;
|
2011-08-05 21:58:46 +04:00
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
void brush_cache_free(rdpBrushCache* brushCache)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2013-11-03 22:51:41 +04:00
|
|
|
if (brushCache)
|
2011-08-05 21:58:46 +04:00
|
|
|
{
|
2013-11-03 22:51:41 +04:00
|
|
|
if (brushCache->entries)
|
2012-02-02 03:42:20 +04:00
|
|
|
{
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < brushCache->maxEntries; i++)
|
2015-05-11 10:07:39 +03:00
|
|
|
free(brushCache->entries[i].entry);
|
2012-02-02 03:42:20 +04:00
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
free(brushCache->entries);
|
2012-02-02 03:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
if (brushCache->monoEntries)
|
2012-02-02 03:42:20 +04:00
|
|
|
{
|
2024-01-30 12:25:38 +03:00
|
|
|
for (size_t i = 0; i < brushCache->maxMonoEntries; i++)
|
2015-05-11 10:07:39 +03:00
|
|
|
free(brushCache->monoEntries[i].entry);
|
2012-02-02 03:42:20 +04:00
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
free(brushCache->monoEntries);
|
2012-02-02 03:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-11-03 22:51:41 +04:00
|
|
|
free(brushCache);
|
2011-08-05 21:58:46 +04:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 11:43:20 +03:00
|
|
|
|
|
|
|
void free_cache_brush_order(rdpContext* context, CACHE_BRUSH_ORDER* order)
|
|
|
|
{
|
2021-09-06 15:00:12 +03:00
|
|
|
WINPR_UNUSED(context);
|
2018-03-30 11:43:20 +03:00
|
|
|
free(order);
|
|
|
|
}
|
|
|
|
|
|
|
|
CACHE_BRUSH_ORDER* copy_cache_brush_order(rdpContext* context, const CACHE_BRUSH_ORDER* order)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
CACHE_BRUSH_ORDER* dst = NULL;
|
2021-09-06 15:00:12 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
dst = calloc(1, sizeof(CACHE_BRUSH_ORDER));
|
2018-03-30 11:43:20 +03:00
|
|
|
|
|
|
|
if (!dst || !order)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*dst = *order;
|
|
|
|
return dst;
|
|
|
|
fail:
|
|
|
|
free_cache_brush_order(context, dst);
|
|
|
|
return NULL;
|
|
|
|
}
|