FreeRDP
Loading...
Searching...
No Matches
cache/brush.c
1
19
20#include <freerdp/config.h>
21
22#include <stdio.h>
23#include <winpr/crt.h>
24#include <winpr/assert.h>
25
26#include <freerdp/log.h>
27#include <freerdp/update.h>
28#include <freerdp/freerdp.h>
29#include <winpr/stream.h>
30
31#include "brush.h"
32#include "cache.h"
33
34#define TAG FREERDP_TAG("cache.brush")
35
36typedef struct
37{
38 UINT32 bpp;
39 void* entry;
40} BRUSH_ENTRY;
41
42struct rdp_brush_cache
43{
44 pPatBlt PatBlt; /* 0 */
45 pCacheBrush CacheBrush; /* 1 */
46 pPolygonSC PolygonSC; /* 2 */
47 pPolygonCB PolygonCB; /* 3 */
48 UINT32 paddingA[16 - 4]; /* 4 */
49
50 UINT32 maxEntries; /* 16 */
51 UINT32 maxMonoEntries; /* 17 */
52 BRUSH_ENTRY* entries; /* 18 */
53 BRUSH_ENTRY* monoEntries; /* 19 */
54 UINT32 paddingB[32 - 20]; /* 20 */
55
56 rdpContext* context;
57};
58
59static BOOL update_gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
60{
61 BOOL ret = TRUE;
62
63 WINPR_ASSERT(context);
64 WINPR_ASSERT(patblt);
65
66 const rdpCache* cache = context->cache;
67 WINPR_ASSERT(cache);
68
69 rdpBrush* brush = &patblt->brush;
70 WINPR_ASSERT(brush->style <= UINT8_MAX);
71 const rdpBrush save = *brush;
72
73 if (brush->style & CACHED_BRUSH)
74 {
75 brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
76 if (!brush->data)
77 return FALSE;
78 brush->style = 0x03;
79 }
80
81 WINPR_ASSERT(cache->brush);
82 IFCALLRET(cache->brush->PatBlt, ret, context, patblt);
83 *brush = save;
84 return ret;
85}
86
87static BOOL update_gdi_polygon_sc(rdpContext* context, const POLYGON_SC_ORDER* polygon_sc)
88{
89 rdpCache* cache = nullptr;
90 WINPR_ASSERT(context);
91 cache = context->cache;
92 WINPR_ASSERT(cache);
93 WINPR_ASSERT(cache->brush);
94 return IFCALLRESULT(TRUE, cache->brush->PolygonSC, context, polygon_sc);
95}
96
97static BOOL update_gdi_polygon_cb(rdpContext* context, POLYGON_CB_ORDER* polygon_cb)
98{
99 BOOL ret = TRUE;
100
101 WINPR_ASSERT(context);
102 WINPR_ASSERT(polygon_cb);
103
104 rdpCache* cache = context->cache;
105 WINPR_ASSERT(cache);
106
107 rdpBrush* brush = &polygon_cb->brush;
108 WINPR_ASSERT(brush->style <= UINT8_MAX);
109 const rdpBrush save = *brush;
110
111 if (brush->style & CACHED_BRUSH)
112 {
113 brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
114 if (!brush->data)
115 return FALSE;
116 brush->style = 0x03;
117 }
118
119 WINPR_ASSERT(cache->brush);
120 IFCALLRET(cache->brush->PolygonCB, ret, context, polygon_cb);
121
122 *brush = save;
123 return ret;
124}
125
126static BOOL update_gdi_cache_brush(rdpContext* context, const CACHE_BRUSH_ORDER* cacheBrush)
127{
128 UINT32 length = 0;
129 void* data = nullptr;
130 rdpCache* cache = nullptr;
131
132 WINPR_ASSERT(context);
133 WINPR_ASSERT(cacheBrush);
134
135 cache = context->cache;
136 WINPR_ASSERT(cache);
137
138 length = cacheBrush->bpp * 64 / 8;
139 data = malloc(length);
140
141 if (!data)
142 return FALSE;
143
144 CopyMemory(data, cacheBrush->data, length);
145 brush_cache_put(cache->brush, cacheBrush->index, data, cacheBrush->bpp);
146 return TRUE;
147}
148
149void* brush_cache_get(rdpBrushCache* brushCache, UINT32 index, UINT32* bpp)
150{
151 void* entry = nullptr;
152
153 if (!brushCache)
154 return nullptr;
155
156 if (!bpp)
157 return nullptr;
158
159 if (*bpp == 1)
160 {
161 if (index >= brushCache->maxMonoEntries)
162 {
163 WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
164 return nullptr;
165 }
166
167 *bpp = brushCache->monoEntries[index].bpp;
168 entry = brushCache->monoEntries[index].entry;
169 }
170 else
171 {
172 if (index >= brushCache->maxEntries)
173 {
174 WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
175 return nullptr;
176 }
177
178 *bpp = brushCache->entries[index].bpp;
179 entry = brushCache->entries[index].entry;
180 }
181
182 if (entry == nullptr)
183 {
184 WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) at index: 0x%08" PRIX32 "", *bpp, index);
185 return nullptr;
186 }
187
188 return entry;
189}
190
191void brush_cache_put(rdpBrushCache* brushCache, UINT32 index, void* entry, UINT32 bpp)
192{
193 WINPR_ASSERT(brushCache);
194
195 if (bpp == 1)
196 {
197 if (index >= brushCache->maxMonoEntries)
198 {
199 WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
200 free(entry);
201 return;
202 }
203
204 WINPR_ASSERT(brushCache->monoEntries);
205 free(brushCache->monoEntries[index].entry);
206 brushCache->monoEntries[index].bpp = bpp;
207 brushCache->monoEntries[index].entry = entry;
208 }
209 else
210 {
211 if (index >= brushCache->maxEntries)
212 {
213 WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
214 free(entry);
215 return;
216 }
217
218 WINPR_ASSERT(brushCache->entries);
219 free(brushCache->entries[index].entry);
220 brushCache->entries[index].bpp = bpp;
221 brushCache->entries[index].entry = entry;
222 }
223}
224
225void brush_cache_register_callbacks(rdpUpdate* update)
226{
227 WINPR_ASSERT(update);
228 WINPR_ASSERT(update->context);
229 WINPR_ASSERT(update->primary);
230 WINPR_ASSERT(update->secondary);
231
232 if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
233 {
234 rdpCache* cache = update->context->cache;
235 WINPR_ASSERT(cache);
236 WINPR_ASSERT(cache->brush);
237
238 cache->brush->PatBlt = update->primary->PatBlt;
239 cache->brush->PolygonSC = update->primary->PolygonSC;
240 cache->brush->PolygonCB = update->primary->PolygonCB;
241 update->primary->PatBlt = update_gdi_patblt;
242 update->primary->PolygonSC = update_gdi_polygon_sc;
243 update->primary->PolygonCB = update_gdi_polygon_cb;
244 update->secondary->CacheBrush = update_gdi_cache_brush;
245 }
246}
247
248rdpBrushCache* brush_cache_new(rdpContext* context)
249{
250 rdpBrushCache* brushCache = nullptr;
251
252 WINPR_ASSERT(context);
253
254 brushCache = (rdpBrushCache*)calloc(1, sizeof(rdpBrushCache));
255
256 if (!brushCache)
257 return nullptr;
258
259 brushCache->context = context;
260 brushCache->maxEntries = 64;
261 brushCache->maxMonoEntries = 64;
262 brushCache->entries = (BRUSH_ENTRY*)calloc(brushCache->maxEntries, sizeof(BRUSH_ENTRY));
263
264 if (!brushCache->entries)
265 goto fail;
266
267 brushCache->monoEntries = (BRUSH_ENTRY*)calloc(brushCache->maxMonoEntries, sizeof(BRUSH_ENTRY));
268
269 if (!brushCache->monoEntries)
270 goto fail;
271
272 return brushCache;
273fail:
274 WINPR_PRAGMA_DIAG_PUSH
275 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
276 brush_cache_free(brushCache);
277 WINPR_PRAGMA_DIAG_POP
278 return nullptr;
279}
280
281void brush_cache_free(rdpBrushCache* brushCache)
282{
283 if (brushCache)
284 {
285 if (brushCache->entries)
286 {
287 for (size_t i = 0; i < brushCache->maxEntries; i++)
288 free(brushCache->entries[i].entry);
289
290 free(brushCache->entries);
291 }
292
293 if (brushCache->monoEntries)
294 {
295 for (size_t i = 0; i < brushCache->maxMonoEntries; i++)
296 free(brushCache->monoEntries[i].entry);
297
298 free(brushCache->monoEntries);
299 }
300
301 free(brushCache);
302 }
303}
304
305void free_cache_brush_order(rdpContext* context, CACHE_BRUSH_ORDER* order)
306{
307 WINPR_UNUSED(context);
308 free(order);
309}
310
311CACHE_BRUSH_ORDER* copy_cache_brush_order(rdpContext* context, const CACHE_BRUSH_ORDER* order)
312{
313 CACHE_BRUSH_ORDER* dst = nullptr;
314
315 WINPR_ASSERT(context);
316
317 dst = calloc(1, sizeof(CACHE_BRUSH_ORDER));
318
319 if (!dst || !order)
320 goto fail;
321
322 *dst = *order;
323 return dst;
324fail:
325 free_cache_brush_order(context, dst);
326 return nullptr;
327}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.