Coverage Report

Created: 2024-05-20 06:11

/src/FreeRDP/libfreerdp/cache/brush.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Brush Cache
4
 *
5
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
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
36
typedef struct
37
{
38
  UINT32 bpp;
39
  void* entry;
40
} BRUSH_ENTRY;
41
42
struct 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
59
static BOOL update_gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
60
0
{
61
0
  BYTE style = 0;
62
0
  BOOL ret = TRUE;
63
0
  rdpBrush* brush = NULL;
64
0
  const rdpCache* cache = NULL;
65
66
0
  WINPR_ASSERT(context);
67
0
  WINPR_ASSERT(patblt);
68
69
0
  cache = context->cache;
70
0
  WINPR_ASSERT(cache);
71
72
0
  brush = &patblt->brush;
73
0
  style = brush->style;
74
75
0
  if (brush->style & CACHED_BRUSH)
76
0
  {
77
0
    brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
78
0
    brush->style = 0x03;
79
0
  }
80
81
0
  WINPR_ASSERT(cache->brush);
82
0
  IFCALLRET(cache->brush->PatBlt, ret, context, patblt);
83
0
  brush->style = style;
84
0
  return ret;
85
0
}
86
87
static BOOL update_gdi_polygon_sc(rdpContext* context, const POLYGON_SC_ORDER* polygon_sc)
88
0
{
89
0
  rdpCache* cache = NULL;
90
0
  WINPR_ASSERT(context);
91
0
  cache = context->cache;
92
0
  WINPR_ASSERT(cache);
93
0
  WINPR_ASSERT(cache->brush);
94
0
  return IFCALLRESULT(TRUE, cache->brush->PolygonSC, context, polygon_sc);
95
0
}
96
97
static BOOL update_gdi_polygon_cb(rdpContext* context, POLYGON_CB_ORDER* polygon_cb)
98
0
{
99
0
  BYTE style = 0;
100
0
  rdpBrush* brush = NULL;
101
0
  rdpCache* cache = NULL;
102
0
  BOOL ret = TRUE;
103
104
0
  WINPR_ASSERT(context);
105
0
  WINPR_ASSERT(polygon_cb);
106
107
0
  cache = context->cache;
108
0
  WINPR_ASSERT(cache);
109
110
0
  brush = &polygon_cb->brush;
111
0
  style = brush->style;
112
113
0
  if (brush->style & CACHED_BRUSH)
114
0
  {
115
0
    brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
116
0
    brush->style = 0x03;
117
0
  }
118
119
0
  WINPR_ASSERT(cache->brush);
120
0
  IFCALLRET(cache->brush->PolygonCB, ret, context, polygon_cb);
121
0
  brush->style = style;
122
0
  return ret;
123
0
}
124
125
static BOOL update_gdi_cache_brush(rdpContext* context, const CACHE_BRUSH_ORDER* cacheBrush)
126
0
{
127
0
  UINT32 length = 0;
128
0
  void* data = NULL;
129
0
  rdpCache* cache = NULL;
130
131
0
  WINPR_ASSERT(context);
132
0
  WINPR_ASSERT(cacheBrush);
133
134
0
  cache = context->cache;
135
0
  WINPR_ASSERT(cache);
136
137
0
  length = cacheBrush->bpp * 64 / 8;
138
0
  data = malloc(length);
139
140
0
  if (!data)
141
0
    return FALSE;
142
143
0
  CopyMemory(data, cacheBrush->data, length);
144
0
  brush_cache_put(cache->brush, cacheBrush->index, data, cacheBrush->bpp);
145
0
  return TRUE;
146
0
}
147
148
void* brush_cache_get(rdpBrushCache* brushCache, UINT32 index, UINT32* bpp)
149
0
{
150
0
  void* entry = NULL;
151
152
0
  if (!brushCache)
153
0
    return NULL;
154
155
0
  if (!bpp)
156
0
    return NULL;
157
158
0
  if (*bpp == 1)
159
0
  {
160
0
    if (index >= brushCache->maxMonoEntries)
161
0
    {
162
0
      WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
163
0
      return NULL;
164
0
    }
165
166
0
    *bpp = brushCache->monoEntries[index].bpp;
167
0
    entry = brushCache->monoEntries[index].entry;
168
0
  }
169
0
  else
170
0
  {
171
0
    if (index >= brushCache->maxEntries)
172
0
    {
173
0
      WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", *bpp, index);
174
0
      return NULL;
175
0
    }
176
177
0
    *bpp = brushCache->entries[index].bpp;
178
0
    entry = brushCache->entries[index].entry;
179
0
  }
180
181
0
  if (entry == NULL)
182
0
  {
183
0
    WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) at index: 0x%08" PRIX32 "", *bpp, index);
184
0
    return NULL;
185
0
  }
186
187
0
  return entry;
188
0
}
189
190
void brush_cache_put(rdpBrushCache* brushCache, UINT32 index, void* entry, UINT32 bpp)
191
0
{
192
0
  WINPR_ASSERT(brushCache);
193
194
0
  if (bpp == 1)
195
0
  {
196
0
    if (index >= brushCache->maxMonoEntries)
197
0
    {
198
0
      WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
199
0
      free(entry);
200
0
      return;
201
0
    }
202
203
0
    WINPR_ASSERT(brushCache->monoEntries);
204
0
    free(brushCache->monoEntries[index].entry);
205
0
    brushCache->monoEntries[index].bpp = bpp;
206
0
    brushCache->monoEntries[index].entry = entry;
207
0
  }
208
0
  else
209
0
  {
210
0
    if (index >= brushCache->maxEntries)
211
0
    {
212
0
      WLog_ERR(TAG, "invalid brush (%" PRIu32 " bpp) index: 0x%08" PRIX32 "", bpp, index);
213
0
      free(entry);
214
0
      return;
215
0
    }
216
217
0
    WINPR_ASSERT(brushCache->entries);
218
0
    free(brushCache->entries[index].entry);
219
0
    brushCache->entries[index].bpp = bpp;
220
0
    brushCache->entries[index].entry = entry;
221
0
  }
222
0
}
223
224
void brush_cache_register_callbacks(rdpUpdate* update)
225
0
{
226
0
  WINPR_ASSERT(update);
227
0
  WINPR_ASSERT(update->context);
228
0
  WINPR_ASSERT(update->primary);
229
0
  WINPR_ASSERT(update->secondary);
230
231
0
  if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
232
0
  {
233
0
    rdpCache* cache = update->context->cache;
234
0
    WINPR_ASSERT(cache);
235
0
    WINPR_ASSERT(cache->brush);
236
237
0
    cache->brush->PatBlt = update->primary->PatBlt;
238
0
    cache->brush->PolygonSC = update->primary->PolygonSC;
239
0
    cache->brush->PolygonCB = update->primary->PolygonCB;
240
0
    update->primary->PatBlt = update_gdi_patblt;
241
0
    update->primary->PolygonSC = update_gdi_polygon_sc;
242
0
    update->primary->PolygonCB = update_gdi_polygon_cb;
243
0
    update->secondary->CacheBrush = update_gdi_cache_brush;
244
0
  }
245
0
}
246
247
rdpBrushCache* brush_cache_new(rdpContext* context)
248
0
{
249
0
  rdpBrushCache* brushCache = NULL;
250
251
0
  WINPR_ASSERT(context);
252
253
0
  brushCache = (rdpBrushCache*)calloc(1, sizeof(rdpBrushCache));
254
255
0
  if (!brushCache)
256
0
    return NULL;
257
258
0
  brushCache->context = context;
259
0
  brushCache->maxEntries = 64;
260
0
  brushCache->maxMonoEntries = 64;
261
0
  brushCache->entries = (BRUSH_ENTRY*)calloc(brushCache->maxEntries, sizeof(BRUSH_ENTRY));
262
263
0
  if (!brushCache->entries)
264
0
    goto fail;
265
266
0
  brushCache->monoEntries = (BRUSH_ENTRY*)calloc(brushCache->maxMonoEntries, sizeof(BRUSH_ENTRY));
267
268
0
  if (!brushCache->monoEntries)
269
0
    goto fail;
270
271
0
  return brushCache;
272
0
fail:
273
0
  WINPR_PRAGMA_DIAG_PUSH
274
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
275
0
  brush_cache_free(brushCache);
276
0
  WINPR_PRAGMA_DIAG_POP
277
0
  return NULL;
278
0
}
279
280
void brush_cache_free(rdpBrushCache* brushCache)
281
0
{
282
0
  if (brushCache)
283
0
  {
284
0
    if (brushCache->entries)
285
0
    {
286
0
      for (size_t i = 0; i < brushCache->maxEntries; i++)
287
0
        free(brushCache->entries[i].entry);
288
289
0
      free(brushCache->entries);
290
0
    }
291
292
0
    if (brushCache->monoEntries)
293
0
    {
294
0
      for (size_t i = 0; i < brushCache->maxMonoEntries; i++)
295
0
        free(brushCache->monoEntries[i].entry);
296
297
0
      free(brushCache->monoEntries);
298
0
    }
299
300
0
    free(brushCache);
301
0
  }
302
0
}
303
304
void free_cache_brush_order(rdpContext* context, CACHE_BRUSH_ORDER* order)
305
439
{
306
439
  WINPR_UNUSED(context);
307
439
  free(order);
308
439
}
309
310
CACHE_BRUSH_ORDER* copy_cache_brush_order(rdpContext* context, const CACHE_BRUSH_ORDER* order)
311
0
{
312
0
  CACHE_BRUSH_ORDER* dst = NULL;
313
314
0
  WINPR_ASSERT(context);
315
316
0
  dst = calloc(1, sizeof(CACHE_BRUSH_ORDER));
317
318
0
  if (!dst || !order)
319
0
    goto fail;
320
321
0
  *dst = *order;
322
0
  return dst;
323
0
fail:
324
0
  free_cache_brush_order(context, dst);
325
0
  return NULL;
326
0
}