Blame libfreerdp/cache/brush.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Brush Cache
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#include <freerdp/update.h>
Packit 1fb8d4
#include <freerdp/freerdp.h>
Packit 1fb8d4
#include <winpr/stream.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/cache/brush.h>
Packit 1fb8d4
Packit 1fb8d4
#include "brush.h"
Packit 1fb8d4
Packit 1fb8d4
#define TAG FREERDP_TAG("cache.brush")
Packit 1fb8d4
Packit Service 5a9772
struct _BRUSH_ENTRY
Packit Service 5a9772
{
Packit Service 5a9772
	UINT32 bpp;
Packit Service 5a9772
	void* entry;
Packit Service 5a9772
};
Packit Service 5a9772
typedef struct _BRUSH_ENTRY BRUSH_ENTRY;
Packit Service 5a9772
Packit Service 5a9772
struct rdp_brush_cache
Packit Service 5a9772
{
Packit Service 5a9772
	pPatBlt PatBlt;          /* 0 */
Packit Service 5a9772
	pCacheBrush CacheBrush;  /* 1 */
Packit Service 5a9772
	pPolygonSC PolygonSC;    /* 2 */
Packit Service 5a9772
	pPolygonCB PolygonCB;    /* 3 */
Packit Service 5a9772
	UINT32 paddingA[16 - 4]; /* 4 */
Packit Service 5a9772
Packit Service 5a9772
	UINT32 maxEntries;        /* 16 */
Packit Service 5a9772
	UINT32 maxMonoEntries;    /* 17 */
Packit Service 5a9772
	BRUSH_ENTRY* entries;     /* 18 */
Packit Service 5a9772
	BRUSH_ENTRY* monoEntries; /* 19 */
Packit Service 5a9772
	UINT32 paddingB[32 - 20]; /* 20 */
Packit Service 5a9772
Packit Service 5a9772
	/* internal */
Packit Service 5a9772
Packit Service 5a9772
	rdpSettings* settings;
Packit Service 5a9772
};
Packit Service 5a9772
Packit Service 5a9772
static BOOL update_gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE style;
Packit 1fb8d4
	BOOL ret = TRUE;
Packit 1fb8d4
	rdpBrush* brush = &patblt->brush;
Packit 1fb8d4
	const rdpCache* cache = context->cache;
Packit 1fb8d4
	style = brush->style;
Packit 1fb8d4
Packit 1fb8d4
	if (brush->style & CACHED_BRUSH)
Packit 1fb8d4
	{
Packit 1fb8d4
		brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
Packit 1fb8d4
		brush->style = 0x03;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	IFCALLRET(cache->brush->PatBlt, ret, context, patblt);
Packit 1fb8d4
	brush->style = style;
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL update_gdi_polygon_sc(rdpContext* context, const POLYGON_SC_ORDER* polygon_sc)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpCache* cache = context->cache;
Packit 1fb8d4
	return IFCALLRESULT(TRUE, cache->brush->PolygonSC, context, polygon_sc);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL update_gdi_polygon_cb(rdpContext* context, POLYGON_CB_ORDER* polygon_cb)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE style;
Packit 1fb8d4
	rdpBrush* brush = &polygon_cb->brush;
Packit 1fb8d4
	rdpCache* cache = context->cache;
Packit 1fb8d4
	BOOL ret = TRUE;
Packit 1fb8d4
	style = brush->style;
Packit 1fb8d4
Packit 1fb8d4
	if (brush->style & CACHED_BRUSH)
Packit 1fb8d4
	{
Packit 1fb8d4
		brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
Packit 1fb8d4
		brush->style = 0x03;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	IFCALLRET(cache->brush->PolygonCB, ret, context, polygon_cb);
Packit 1fb8d4
	brush->style = style;
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL update_gdi_cache_brush(rdpContext* context, const CACHE_BRUSH_ORDER* cacheBrush)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 length;
Packit 1fb8d4
	void* data = NULL;
Packit 1fb8d4
	rdpCache* cache = context->cache;
Packit 1fb8d4
	length = cacheBrush->bpp * 64 / 8;
Packit 1fb8d4
	data = malloc(length);
Packit 1fb8d4
Packit 1fb8d4
	if (!data)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	CopyMemory(data, cacheBrush->data, length);
Packit 1fb8d4
	brush_cache_put(cache->brush, cacheBrush->index, data, cacheBrush->bpp);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void* brush_cache_get(rdpBrushCache* brushCache, UINT32 index, UINT32* bpp)
Packit 1fb8d4
{
Packit 1fb8d4
	void* entry;
Packit 1fb8d4
Packit 1fb8d4
	if (!brushCache)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (!bpp)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (*bpp == 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (index >= brushCache->maxMonoEntries)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		*bpp = brushCache->monoEntries[index].bpp;
Packit 1fb8d4
		entry = brushCache->monoEntries[index].entry;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (index >= brushCache->maxEntries)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		*bpp = brushCache->entries[index].bpp;
Packit 1fb8d4
		entry = brushCache->entries[index].entry;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (entry == NULL)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) at index: 0x%08" PRIX32 "", *bpp, index);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return entry;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void brush_cache_put(rdpBrushCache* brushCache, UINT32 index, void* entry, UINT32 bpp)
Packit 1fb8d4
{
Packit 1fb8d4
	if (bpp == 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (index >= brushCache->maxMonoEntries)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
Packit 1fb8d4
			free(entry);
Packit 1fb8d4
			return;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		free(brushCache->monoEntries[index].entry);
Packit 1fb8d4
		brushCache->monoEntries[index].bpp = bpp;
Packit 1fb8d4
		brushCache->monoEntries[index].entry = entry;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (index >= brushCache->maxEntries)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
Packit 1fb8d4
			free(entry);
Packit 1fb8d4
			return;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		free(brushCache->entries[index].entry);
Packit 1fb8d4
		brushCache->entries[index].bpp = bpp;
Packit 1fb8d4
		brushCache->entries[index].entry = entry;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void brush_cache_register_callbacks(rdpUpdate* update)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpCache* cache = update->context->cache;
Packit 1fb8d4
	cache->brush->PatBlt = update->primary->PatBlt;
Packit 1fb8d4
	cache->brush->PolygonSC = update->primary->PolygonSC;
Packit 1fb8d4
	cache->brush->PolygonCB = update->primary->PolygonCB;
Packit 1fb8d4
	update->primary->PatBlt = update_gdi_patblt;
Packit 1fb8d4
	update->primary->PolygonSC = update_gdi_polygon_sc;
Packit 1fb8d4
	update->primary->PolygonCB = update_gdi_polygon_cb;
Packit 1fb8d4
	update->secondary->CacheBrush = update_gdi_cache_brush;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
rdpBrushCache* brush_cache_new(rdpSettings* settings)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpBrushCache* brushCache;
Packit Service 5a9772
	brushCache = (rdpBrushCache*)calloc(1, sizeof(rdpBrushCache));
Packit 1fb8d4
Packit 1fb8d4
	if (!brushCache)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	brushCache->settings = settings;
Packit 1fb8d4
	brushCache->maxEntries = 64;
Packit 1fb8d4
	brushCache->maxMonoEntries = 64;
Packit 1fb8d4
	brushCache->entries = (BRUSH_ENTRY*)calloc(brushCache->maxEntries, sizeof(BRUSH_ENTRY));
Packit 1fb8d4
Packit 1fb8d4
	if (!brushCache->entries)
Packit 1fb8d4
		goto error_entries;
Packit 1fb8d4
Packit Service 5a9772
	brushCache->monoEntries = (BRUSH_ENTRY*)calloc(brushCache->maxMonoEntries, sizeof(BRUSH_ENTRY));
Packit 1fb8d4
Packit 1fb8d4
	if (!brushCache->monoEntries)
Packit 1fb8d4
		goto error_mono;
Packit 1fb8d4
Packit 1fb8d4
	return brushCache;
Packit 1fb8d4
error_mono:
Packit 1fb8d4
	free(brushCache->entries);
Packit 1fb8d4
error_entries:
Packit 1fb8d4
	free(brushCache);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void brush_cache_free(rdpBrushCache* brushCache)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
Packit 1fb8d4
	if (brushCache)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (brushCache->entries)
Packit 1fb8d4
		{
Packit Service 5a9772
			for (i = 0; i < (int)brushCache->maxEntries; i++)
Packit 1fb8d4
				free(brushCache->entries[i].entry);
Packit 1fb8d4
Packit 1fb8d4
			free(brushCache->entries);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (brushCache->monoEntries)
Packit 1fb8d4
		{
Packit Service 5a9772
			for (i = 0; i < (int)brushCache->maxMonoEntries; i++)
Packit 1fb8d4
				free(brushCache->monoEntries[i].entry);
Packit 1fb8d4
Packit 1fb8d4
			free(brushCache->monoEntries);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		free(brushCache);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void free_cache_brush_order(rdpContext* context, CACHE_BRUSH_ORDER* order)
Packit 1fb8d4
{
Packit 1fb8d4
	free(order);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
CACHE_BRUSH_ORDER* copy_cache_brush_order(rdpContext* context, const CACHE_BRUSH_ORDER* order)
Packit 1fb8d4
{
Packit 1fb8d4
	CACHE_BRUSH_ORDER* dst = calloc(1, sizeof(CACHE_BRUSH_ORDER));
Packit 1fb8d4
Packit 1fb8d4
	if (!dst || !order)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	*dst = *order;
Packit 1fb8d4
	return dst;
Packit 1fb8d4
fail:
Packit 1fb8d4
	free_cache_brush_order(context, dst);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}