summaryrefslogtreecommitdiffstats
path: root/libfreerdp/cache/brush.c
blob: 94900764c167d9d51942593ec8d416046d93a86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * 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.
 */

#include <freerdp/config.h>

#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/assert.h>

#include <freerdp/log.h>
#include <freerdp/update.h>
#include <freerdp/freerdp.h>
#include <winpr/stream.h>

#include "brush.h"
#include "cache.h"

#define TAG FREERDP_TAG("cache.brush")

typedef struct
{
	UINT32 bpp;
	void* entry;
} BRUSH_ENTRY;

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 */

	rdpContext* context;
};

static BOOL update_gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
{
	BYTE style = 0;
	BOOL ret = TRUE;
	rdpBrush* brush = NULL;
	const rdpCache* cache = NULL;

	WINPR_ASSERT(context);
	WINPR_ASSERT(patblt);

	cache = context->cache;
	WINPR_ASSERT(cache);

	brush = &patblt->brush;
	style = brush->style;

	if (brush->style & CACHED_BRUSH)
	{
		brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
		brush->style = 0x03;
	}

	WINPR_ASSERT(cache->brush);
	IFCALLRET(cache->brush->PatBlt, ret, context, patblt);
	brush->style = style;
	return ret;
}

static BOOL update_gdi_polygon_sc(rdpContext* context, const POLYGON_SC_ORDER* polygon_sc)
{
	rdpCache* cache = NULL;
	WINPR_ASSERT(context);
	cache = context->cache;
	WINPR_ASSERT(cache);
	WINPR_ASSERT(cache->brush);
	return IFCALLRESULT(TRUE, cache->brush->PolygonSC, context, polygon_sc);
}

static BOOL update_gdi_polygon_cb(rdpContext* context, POLYGON_CB_ORDER* polygon_cb)
{
	BYTE style = 0;
	rdpBrush* brush = NULL;
	rdpCache* cache = NULL;
	BOOL ret = TRUE;

	WINPR_ASSERT(context);
	WINPR_ASSERT(polygon_cb);

	cache = context->cache;
	WINPR_ASSERT(cache);

	brush = &polygon_cb->brush;
	style = brush->style;

	if (brush->style & CACHED_BRUSH)
	{
		brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
		brush->style = 0x03;
	}

	WINPR_ASSERT(cache->brush);
	IFCALLRET(cache->brush->PolygonCB, ret, context, polygon_cb);
	brush->style = style;
	return ret;
}

static BOOL update_gdi_cache_brush(rdpContext* context, const CACHE_BRUSH_ORDER* cacheBrush)
{
	UINT32 length = 0;
	void* data = NULL;
	rdpCache* cache = NULL;

	WINPR_ASSERT(context);
	WINPR_ASSERT(cacheBrush);

	cache = context->cache;
	WINPR_ASSERT(cache);

	length = cacheBrush->bpp * 64 / 8;
	data = malloc(length);

	if (!data)
		return FALSE;

	CopyMemory(data, cacheBrush->data, length);
	brush_cache_put(cache->brush, cacheBrush->index, data, cacheBrush->bpp);
	return TRUE;
}

void* brush_cache_get(rdpBrushCache* brushCache, UINT32 index, UINT32* bpp)
{
	void* entry = NULL;

	if (!brushCache)
		return NULL;

	if (!bpp)
		return NULL;

	if (*bpp == 1)
	{
		if (index >= brushCache->maxMonoEntries)
		{
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
			return NULL;
		}

		*bpp = brushCache->monoEntries[index].bpp;
		entry = brushCache->monoEntries[index].entry;
	}
	else
	{
		if (index >= brushCache->maxEntries)
		{
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
			return NULL;
		}

		*bpp = brushCache->entries[index].bpp;
		entry = brushCache->entries[index].entry;
	}

	if (entry == NULL)
	{
		WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) at index: 0x%08" PRIX32 "", *bpp, index);
		return NULL;
	}

	return entry;
}

void brush_cache_put(rdpBrushCache* brushCache, UINT32 index, void* entry, UINT32 bpp)
{
	WINPR_ASSERT(brushCache);

	if (bpp == 1)
	{
		if (index >= brushCache->maxMonoEntries)
		{
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
			free(entry);
			return;
		}

		WINPR_ASSERT(brushCache->monoEntries);
		free(brushCache->monoEntries[index].entry);
		brushCache->monoEntries[index].bpp = bpp;
		brushCache->monoEntries[index].entry = entry;
	}
	else
	{
		if (index >= brushCache->maxEntries)
		{
			WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
			free(entry);
			return;
		}

		WINPR_ASSERT(brushCache->entries);
		free(brushCache->entries[index].entry);
		brushCache->entries[index].bpp = bpp;
		brushCache->entries[index].entry = entry;
	}
}

void brush_cache_register_callbacks(rdpUpdate* update)
{
	WINPR_ASSERT(update);
	WINPR_ASSERT(update->context);
	WINPR_ASSERT(update->primary);
	WINPR_ASSERT(update->secondary);

	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;
	}
}

rdpBrushCache* brush_cache_new(rdpContext* context)
{
	rdpBrushCache* brushCache = NULL;

	WINPR_ASSERT(context);

	brushCache = (rdpBrushCache*)calloc(1, sizeof(rdpBrushCache));

	if (!brushCache)
		return NULL;

	brushCache->context = context;
	brushCache->maxEntries = 64;
	brushCache->maxMonoEntries = 64;
	brushCache->entries = (BRUSH_ENTRY*)calloc(brushCache->maxEntries, sizeof(BRUSH_ENTRY));

	if (!brushCache->entries)
		goto fail;

	brushCache->monoEntries = (BRUSH_ENTRY*)calloc(brushCache->maxMonoEntries, sizeof(BRUSH_ENTRY));

	if (!brushCache->monoEntries)
		goto fail;

	return brushCache;
fail:
	WINPR_PRAGMA_DIAG_PUSH
	WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
	brush_cache_free(brushCache);
	WINPR_PRAGMA_DIAG_POP
	return NULL;
}

void brush_cache_free(rdpBrushCache* brushCache)
{
	if (brushCache)
	{
		if (brushCache->entries)
		{
			for (size_t i = 0; i < brushCache->maxEntries; i++)
				free(brushCache->entries[i].entry);

			free(brushCache->entries);
		}

		if (brushCache->monoEntries)
		{
			for (size_t i = 0; i < brushCache->maxMonoEntries; i++)
				free(brushCache->monoEntries[i].entry);

			free(brushCache->monoEntries);
		}

		free(brushCache);
	}
}

void free_cache_brush_order(rdpContext* context, CACHE_BRUSH_ORDER* order)
{
	WINPR_UNUSED(context);
	free(order);
}

CACHE_BRUSH_ORDER* copy_cache_brush_order(rdpContext* context, const CACHE_BRUSH_ORDER* order)
{
	CACHE_BRUSH_ORDER* dst = NULL;

	WINPR_ASSERT(context);

	dst = calloc(1, sizeof(CACHE_BRUSH_ORDER));

	if (!dst || !order)
		goto fail;

	*dst = *order;
	return dst;
fail:
	free_cache_brush_order(context, dst);
	return NULL;
}