Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/gdi/gdi.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * GDI Library
4
 *
5
 * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016 Thincast Technologies GmbH
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *   http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
27
#include <winpr/crt.h>
28
#include <winpr/assert.h>
29
#include <winpr/cast.h>
30
31
#include <freerdp/api.h>
32
#include <freerdp/log.h>
33
#include <freerdp/freerdp.h>
34
35
#include <freerdp/gdi/gdi.h>
36
#include <freerdp/gdi/dc.h>
37
#include <freerdp/gdi/pen.h>
38
#include <freerdp/gdi/shape.h>
39
#include <freerdp/gdi/region.h>
40
#include <freerdp/gdi/bitmap.h>
41
42
#include "drawing.h"
43
#include "clipping.h"
44
#include "brush.h"
45
#include "line.h"
46
#include "gdi.h"
47
#include "../core/graphics.h"
48
#include "../core/update.h"
49
#include "../cache/cache.h"
50
51
0
#define TAG FREERDP_TAG("gdi")
52
53
/* Ternary Raster Operation Table */
54
typedef struct
55
{
56
  DWORD code;
57
  const char* name;
58
} rop_table_entry;
59
60
static const rop_table_entry rop3_code_table[] = { { GDI_BLACKNESS, "0" },
61
                                                 { GDI_DPSoon, "DPSoon" },
62
                                                 { GDI_DPSona, "DPSona" },
63
                                                 { GDI_PSon, "PSon" },
64
                                                 { GDI_SDPona, "SDPona" },
65
                                                 { GDI_DPon, "DPon" },
66
                                                 { GDI_PDSxnon, "PDSxnon" },
67
                                                 { GDI_PDSaon, "PDSaon" },
68
                                                 { GDI_SDPnaa, "SDPnaa" },
69
                                                 { GDI_PDSxon, "PDSxon" },
70
                                                 { GDI_DPna, "DPna" },
71
                                                 { GDI_PSDnaon, "PSDnaon" },
72
                                                 { GDI_SPna, "SPna" },
73
                                                 { GDI_PDSnaon, "PDSnaon" },
74
                                                 { GDI_PDSonon, "PDSonon" },
75
                                                 { GDI_Pn, "Pn" },
76
                                                 { GDI_PDSona, "PDSona" },
77
                                                 { GDI_NOTSRCERASE, "DSon" },
78
                                                 { GDI_SDPxnon, "SDPxnon" },
79
                                                 { GDI_SDPaon, "SDPaon" },
80
                                                 { GDI_DPSxnon, "DPSxnon" },
81
                                                 { GDI_DPSaon, "DPSaon" },
82
                                                 { GDI_PSDPSanaxx, "PSDPSanaxx" },
83
                                                 { GDI_SSPxDSxaxn, "SSPxDSxaxn" },
84
                                                 { GDI_SPxPDxa, "SPxPDxa" },
85
                                                 { GDI_SDPSanaxn, "SDPSanaxn" },
86
                                                 { GDI_PDSPaox, "PDSPaox" },
87
                                                 { GDI_SDPSxaxn, "SDPSxaxn" },
88
                                                 { GDI_PSDPaox, "PSDPaox" },
89
                                                 { GDI_DSPDxaxn, "DSPDxaxn" },
90
                                                 { GDI_PDSox, "PDSox" },
91
                                                 { GDI_PDSoan, "PDSoan" },
92
                                                 { GDI_DPSnaa, "DPSnaa" },
93
                                                 { GDI_SDPxon, "SDPxon" },
94
                                                 { GDI_DSna, "DSna" },
95
                                                 { GDI_SPDnaon, "SPDnaon" },
96
                                                 { GDI_SPxDSxa, "SPxDSxa" },
97
                                                 { GDI_PDSPanaxn, "PDSPanaxn" },
98
                                                 { GDI_SDPSaox, "SDPSaox" },
99
                                                 { GDI_SDPSxnox, "SDPSxnox" },
100
                                                 { GDI_DPSxa, "DPSxa" },
101
                                                 { GDI_PSDPSaoxxn, "PSDPSaoxxn" },
102
                                                 { GDI_DPSana, "DPSana" },
103
                                                 { GDI_SSPxPDxaxn, "SSPxPDxaxn" },
104
                                                 { GDI_SPDSoax, "SPDSoax" },
105
                                                 { GDI_PSDnox, "PSDnox" },
106
                                                 { GDI_PSDPxox, "PSDPxox" },
107
                                                 { GDI_PSDnoan, "PSDnoan" },
108
                                                 { GDI_PSna, "PSna" },
109
                                                 { GDI_SDPnaon, "SDPnaon" },
110
                                                 { GDI_SDPSoox, "SDPSoox" },
111
                                                 { GDI_NOTSRCCOPY, "Sn" },
112
                                                 { GDI_SPDSaox, "SPDSaox" },
113
                                                 { GDI_SPDSxnox, "SPDSxnox" },
114
                                                 { GDI_SDPox, "SDPox" },
115
                                                 { GDI_SDPoan, "SDPoan" },
116
                                                 { GDI_PSDPoax, "PSDPoax" },
117
                                                 { GDI_SPDnox, "SPDnox" },
118
                                                 { GDI_SPDSxox, "SPDSxox" },
119
                                                 { GDI_SPDnoan, "SPDnoan" },
120
                                                 { GDI_PSx, "PSx" },
121
                                                 { GDI_SPDSonox, "SPDSonox" },
122
                                                 { GDI_SPDSnaox, "SPDSnaox" },
123
                                                 { GDI_PSan, "PSan" },
124
                                                 { GDI_PSDnaa, "PSDnaa" },
125
                                                 { GDI_DPSxon, "DPSxon" },
126
                                                 { GDI_SDxPDxa, "SDxPDxa" },
127
                                                 { GDI_SPDSanaxn, "SPDSanaxn" },
128
                                                 { GDI_SRCERASE, "SDna" },
129
                                                 { GDI_DPSnaon, "DPSnaon" },
130
                                                 { GDI_DSPDaox, "DSPDaox" },
131
                                                 { GDI_PSDPxaxn, "PSDPxaxn" },
132
                                                 { GDI_SDPxa, "SDPxa" },
133
                                                 { GDI_PDSPDaoxxn, "PDSPDaoxxn" },
134
                                                 { GDI_DPSDoax, "DPSDoax" },
135
                                                 { GDI_PDSnox, "PDSnox" },
136
                                                 { GDI_SDPana, "SDPana" },
137
                                                 { GDI_SSPxDSxoxn, "SSPxDSxoxn" },
138
                                                 { GDI_PDSPxox, "PDSPxox" },
139
                                                 { GDI_PDSnoan, "PDSnoan" },
140
                                                 { GDI_PDna, "PDna" },
141
                                                 { GDI_DSPnaon, "DSPnaon" },
142
                                                 { GDI_DPSDaox, "DPSDaox" },
143
                                                 { GDI_SPDSxaxn, "SPDSxaxn" },
144
                                                 { GDI_DPSonon, "DPSonon" },
145
                                                 { GDI_DSTINVERT, "Dn" },
146
                                                 { GDI_DPSox, "DPSox" },
147
                                                 { GDI_DPSoan, "DPSoan" },
148
                                                 { GDI_PDSPoax, "PDSPoax" },
149
                                                 { GDI_DPSnox, "DPSnox" },
150
                                                 { GDI_PATINVERT, "DPx" },
151
                                                 { GDI_DPSDonox, "DPSDonox" },
152
                                                 { GDI_DPSDxox, "DPSDxox" },
153
                                                 { GDI_DPSnoan, "DPSnoan" },
154
                                                 { GDI_DPSDnaox, "DPSDnaox" },
155
                                                 { GDI_DPan, "DPan" },
156
                                                 { GDI_PDSxa, "PDSxa" },
157
                                                 { GDI_DSPDSaoxxn, "DSPDSaoxxn" },
158
                                                 { GDI_DSPDoax, "DSPDoax" },
159
                                                 { GDI_SDPnox, "SDPnox" },
160
                                                 { GDI_SDPSoax, "SDPSoax" },
161
                                                 { GDI_DSPnox, "DSPnox" },
162
                                                 { GDI_SRCINVERT, "DSx" },
163
                                                 { GDI_SDPSonox, "SDPSonox" },
164
                                                 { GDI_DSPDSonoxxn, "DSPDSonoxxn" },
165
                                                 { GDI_PDSxxn, "PDSxxn" },
166
                                                 { GDI_DPSax, "DPSax" },
167
                                                 { GDI_PSDPSoaxxn, "PSDPSoaxxn" },
168
                                                 { GDI_SDPax, "SDPax" },
169
                                                 { GDI_PDSPDoaxxn, "PDSPDoaxxn" },
170
                                                 { GDI_SDPSnoax, "SDPSnoax" },
171
                                                 { GDI_PDSxnan, "PDSxnan" },
172
                                                 { GDI_PDSana, "PDSana" },
173
                                                 { GDI_SSDxPDxaxn, "SSDxPDxaxn" },
174
                                                 { GDI_SDPSxox, "SDPSxox" },
175
                                                 { GDI_SDPnoan, "SDPnoan" },
176
                                                 { GDI_DSPDxox, "DSPDxox" },
177
                                                 { GDI_DSPnoan, "DSPnoan" },
178
                                                 { GDI_SDPSnaox, "SDPSnaox" },
179
                                                 { GDI_DSan, "DSan" },
180
                                                 { GDI_PDSax, "PDSax" },
181
                                                 { GDI_DSPDSoaxxn, "DSPDSoaxxn" },
182
                                                 { GDI_DPSDnoax, "DPSDnoax" },
183
                                                 { GDI_SDPxnan, "SDPxnan" },
184
                                                 { GDI_SPDSnoax, "SPDSnoax" },
185
                                                 { GDI_DPSxnan, "DPSxnan" },
186
                                                 { GDI_SPxDSxo, "SPxDSxo" },
187
                                                 { GDI_DPSaan, "DPSaan" },
188
                                                 { GDI_DPSaa, "DPSaa" },
189
                                                 { GDI_SPxDSxon, "SPxDSxon" },
190
                                                 { GDI_DPSxna, "DPSxna" },
191
                                                 { GDI_SPDSnoaxn, "SPDSnoaxn" },
192
                                                 { GDI_SDPxna, "SDPxna" },
193
                                                 { GDI_PDSPnoaxn, "PDSPnoaxn" },
194
                                                 { GDI_DSPDSoaxx, "DSPDSoaxx" },
195
                                                 { GDI_PDSaxn, "PDSaxn" },
196
                                                 { GDI_SRCAND, "DSa" },
197
                                                 { GDI_SDPSnaoxn, "SDPSnaoxn" },
198
                                                 { GDI_DSPnoa, "DSPnoa" },
199
                                                 { GDI_DSPDxoxn, "DSPDxoxn" },
200
                                                 { GDI_SDPnoa, "SDPnoa" },
201
                                                 { GDI_SDPSxoxn, "SDPSxoxn" },
202
                                                 { GDI_SSDxPDxax, "SSDxPDxax" },
203
                                                 { GDI_PDSanan, "PDSanan" },
204
                                                 { GDI_PDSxna, "PDSxna" },
205
                                                 { GDI_SDPSnoaxn, "SDPSnoaxn" },
206
                                                 { GDI_DPSDPoaxx, "DPSDPoaxx" },
207
                                                 { GDI_SPDaxn, "SPDaxn" },
208
                                                 { GDI_PSDPSoaxx, "PSDPSoaxx" },
209
                                                 { GDI_DPSaxn, "DPSaxn" },
210
                                                 { GDI_DPSxx, "DPSxx" },
211
                                                 { GDI_PSDPSonoxx, "PSDPSonoxx" },
212
                                                 { GDI_SDPSonoxn, "SDPSonoxn" },
213
                                                 { GDI_DSxn, "DSxn" },
214
                                                 { GDI_DPSnax, "DPSnax" },
215
                                                 { GDI_SDPSoaxn, "SDPSoaxn" },
216
                                                 { GDI_SPDnax, "SPDnax" },
217
                                                 { GDI_DSPDoaxn, "DSPDoaxn" },
218
                                                 { GDI_DSPDSaoxx, "DSPDSaoxx" },
219
                                                 { GDI_PDSxan, "PDSxan" },
220
                                                 { GDI_DPa, "DPa" },
221
                                                 { GDI_PDSPnaoxn, "PDSPnaoxn" },
222
                                                 { GDI_DPSnoa, "DPSnoa" },
223
                                                 { GDI_DPSDxoxn, "DPSDxoxn" },
224
                                                 { GDI_PDSPonoxn, "PDSPonoxn" },
225
                                                 { GDI_PDxn, "PDxn" },
226
                                                 { GDI_DSPnax, "DSPnax" },
227
                                                 { GDI_PDSPoaxn, "PDSPoaxn" },
228
                                                 { GDI_DPSoa, "DPSoa" },
229
                                                 { GDI_DPSoxn, "DPSoxn" },
230
                                                 { GDI_DSTCOPY, "D" },
231
                                                 { GDI_DPSono, "DPSono" },
232
                                                 { GDI_SPDSxax, "SPDSxax" },
233
                                                 { GDI_DPSDaoxn, "DPSDaoxn" },
234
                                                 { GDI_DSPnao, "DSPnao" },
235
                                                 { GDI_DPno, "DPno" },
236
                                                 { GDI_PDSnoa, "PDSnoa" },
237
                                                 { GDI_PDSPxoxn, "PDSPxoxn" },
238
                                                 { GDI_SSPxDSxox, "SSPxDSxox" },
239
                                                 { GDI_SDPanan, "SDPanan" },
240
                                                 { GDI_PSDnax, "PSDnax" },
241
                                                 { GDI_DPSDoaxn, "DPSDoaxn" },
242
                                                 { GDI_DPSDPaoxx, "DPSDPaoxx" },
243
                                                 { GDI_SDPxan, "SDPxan" },
244
                                                 { GDI_PSDPxax, "PSDPxax" },
245
                                                 { GDI_DSPDaoxn, "DSPDaoxn" },
246
                                                 { GDI_DPSnao, "DPSnao" },
247
                                                 { GDI_MERGEPAINT, "DSno" },
248
                                                 { GDI_SPDSanax, "SPDSanax" },
249
                                                 { GDI_SDxPDxan, "SDxPDxan" },
250
                                                 { GDI_DPSxo, "DPSxo" },
251
                                                 { GDI_DPSano, "DPSano" },
252
                                                 { GDI_MERGECOPY, "PSa" },
253
                                                 { GDI_SPDSnaoxn, "SPDSnaoxn" },
254
                                                 { GDI_SPDSonoxn, "SPDSonoxn" },
255
                                                 { GDI_PSxn, "PSxn" },
256
                                                 { GDI_SPDnoa, "SPDnoa" },
257
                                                 { GDI_SPDSxoxn, "SPDSxoxn" },
258
                                                 { GDI_SDPnax, "SDPnax" },
259
                                                 { GDI_PSDPoaxn, "PSDPoaxn" },
260
                                                 { GDI_SDPoa, "SDPoa" },
261
                                                 { GDI_SPDoxn, "SPDoxn" },
262
                                                 { GDI_DPSDxax, "DPSDxax" },
263
                                                 { GDI_SPDSaoxn, "SPDSaoxn" },
264
                                                 { GDI_SRCCOPY, "S" },
265
                                                 { GDI_SDPono, "SDPono" },
266
                                                 { GDI_SDPnao, "SDPnao" },
267
                                                 { GDI_SPno, "SPno" },
268
                                                 { GDI_PSDnoa, "PSDnoa" },
269
                                                 { GDI_PSDPxoxn, "PSDPxoxn" },
270
                                                 { GDI_PDSnax, "PDSnax" },
271
                                                 { GDI_SPDSoaxn, "SPDSoaxn" },
272
                                                 { GDI_SSPxPDxax, "SSPxPDxax" },
273
                                                 { GDI_DPSanan, "DPSanan" },
274
                                                 { GDI_PSDPSaoxx, "PSDPSaoxx" },
275
                                                 { GDI_DPSxan, "DPSxan" },
276
                                                 { GDI_PDSPxax, "PDSPxax" },
277
                                                 { GDI_SDPSaoxn, "SDPSaoxn" },
278
                                                 { GDI_DPSDanax, "DPSDanax" },
279
                                                 { GDI_SPxDSxan, "SPxDSxan" },
280
                                                 { GDI_SPDnao, "SPDnao" },
281
                                                 { GDI_SDno, "SDno" },
282
                                                 { GDI_SDPxo, "SDPxo" },
283
                                                 { GDI_SDPano, "SDPano" },
284
                                                 { GDI_PDSoa, "PDSoa" },
285
                                                 { GDI_PDSoxn, "PDSoxn" },
286
                                                 { GDI_DSPDxax, "DSPDxax" },
287
                                                 { GDI_PSDPaoxn, "PSDPaoxn" },
288
                                                 { GDI_SDPSxax, "SDPSxax" },
289
                                                 { GDI_PDSPaoxn, "PDSPaoxn" },
290
                                                 { GDI_SDPSanax, "SDPSanax" },
291
                                                 { GDI_SPxPDxan, "SPxPDxan" },
292
                                                 { GDI_SSPxDSxax, "SSPxDSxax" },
293
                                                 { GDI_DSPDSanaxxn, "DSPDSanaxxn" },
294
                                                 { GDI_DPSao, "DPSao" },
295
                                                 { GDI_DPSxno, "DPSxno" },
296
                                                 { GDI_SDPao, "SDPao" },
297
                                                 { GDI_SDPxno, "SDPxno" },
298
                                                 { GDI_SRCPAINT, "DSo" },
299
                                                 { GDI_SDPnoo, "SDPnoo" },
300
                                                 { GDI_PATCOPY, "P" },
301
                                                 { GDI_PDSono, "PDSono" },
302
                                                 { GDI_PDSnao, "PDSnao" },
303
                                                 { GDI_PSno, "PSno" },
304
                                                 { GDI_PSDnao, "PSDnao" },
305
                                                 { GDI_PDno, "PDno" },
306
                                                 { GDI_PDSxo, "PDSxo" },
307
                                                 { GDI_PDSano, "PDSano" },
308
                                                 { GDI_PDSao, "PDSao" },
309
                                                 { GDI_PDSxno, "PDSxno" },
310
                                                 { GDI_DPo, "DPo" },
311
                                                 { GDI_PATPAINT, "DPSnoo" },
312
                                                 { GDI_PSo, "PSo" },
313
                                                 { GDI_PSDnoo, "PSDnoo" },
314
                                                 { GDI_DPSoo, "DPSoo" },
315
                                                 { GDI_WHITENESS, "1" } };
316
317
/* Hatch Patterns as monochrome data */
318
static const BYTE GDI_BS_HATCHED_PATTERNS[] = {
319
  0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, /* HS_HORIZONTAL */
320
  0xF7, 0xF7, 0xF7, 0xF7, 0xF7, 0xF7, 0xF7, 0xF7, /* HS_VERTICAL */
321
  0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F, /* HS_FDIAGONAL */
322
  0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE, /* HS_BDIAGONAL */
323
  0xF7, 0xF7, 0xF7, 0x00, 0xF7, 0xF7, 0xF7, 0xF7, /* HS_CROSS */
324
  0x7E, 0xBD, 0xDB, 0xE7, 0xE7, 0xDB, 0xBD, 0x7E  /* HS_DIACROSS */
325
};
326
327
0
#define gdi_rop3_code_checked(code) gdi_rop3_code_checked_int((code), __FILE__, __func__, __LINE__)
328
static inline DWORD gdi_rop3_code_checked_int(UINT32 code, WINPR_ATTR_UNUSED const char* file,
329
                                              WINPR_ATTR_UNUSED const char* fkt,
330
                                              WINPR_ATTR_UNUSED size_t line)
331
0
{
332
0
  WINPR_ASSERT_AT(code <= UINT8_MAX, file, fkt, line);
333
0
  return gdi_rop3_code((UINT8)code);
334
0
}
335
336
BOOL gdi_decode_color(rdpGdi* gdi, UINT32 srcColor, UINT32* color, UINT32* format)
337
0
{
338
0
  UINT32 SrcFormat = 0;
339
340
0
  if (!gdi || !color || !gdi->context || !gdi->context->settings)
341
0
    return FALSE;
342
343
0
  const UINT32 ColorDepth =
344
0
      freerdp_settings_get_uint32(gdi->context->settings, FreeRDP_ColorDepth);
345
346
0
  switch (ColorDepth)
347
0
  {
348
0
    case 32:
349
0
    case 24:
350
0
      SrcFormat = PIXEL_FORMAT_BGR24;
351
0
      break;
352
353
0
    case 16:
354
0
      SrcFormat = PIXEL_FORMAT_RGB16;
355
0
      break;
356
357
0
    case 15:
358
0
      SrcFormat = PIXEL_FORMAT_RGB15;
359
0
      break;
360
361
0
    case 8:
362
0
      SrcFormat = PIXEL_FORMAT_RGB8;
363
0
      break;
364
365
0
    default:
366
0
      return FALSE;
367
0
  }
368
369
0
  if (format)
370
0
    *format = gdi->dstFormat;
371
372
0
  *color = FreeRDPConvertColor(srcColor, SrcFormat, gdi->dstFormat, &gdi->palette);
373
0
  return TRUE;
374
0
}
375
376
/* GDI Helper Functions */
377
DWORD gdi_rop3_code(BYTE code)
378
0
{
379
0
  return rop3_code_table[code].code;
380
0
}
381
382
const char* gdi_rop3_code_string(BYTE code)
383
0
{
384
0
  return rop3_code_table[code].name;
385
0
}
386
387
const char* gdi_rop3_string(DWORD rop)
388
0
{
389
0
  const size_t count = sizeof(rop3_code_table) / sizeof(rop3_code_table[0]);
390
391
0
  for (size_t x = 0; x < count; x++)
392
0
  {
393
0
    if (rop3_code_table[x].code == rop)
394
0
      return rop3_code_table[x].name;
395
0
  }
396
397
0
  return "UNKNOWN";
398
0
}
399
400
UINT32 gdi_get_pixel_format(UINT32 bitsPerPixel)
401
0
{
402
0
  UINT32 format = 0;
403
404
0
  switch (bitsPerPixel)
405
0
  {
406
0
    case 32:
407
0
      format = PIXEL_FORMAT_BGRA32;
408
0
      break;
409
410
0
    case 24:
411
0
      format = PIXEL_FORMAT_BGR24;
412
0
      break;
413
414
0
    case 16:
415
0
      format = PIXEL_FORMAT_RGB16;
416
0
      break;
417
418
0
    case 15:
419
0
      format = PIXEL_FORMAT_RGB15;
420
0
      break;
421
422
0
    case 8:
423
0
      format = PIXEL_FORMAT_RGB8;
424
0
      break;
425
426
0
    default:
427
0
      WLog_ERR(TAG, "Unsupported color depth %" PRIu32, bitsPerPixel);
428
0
      format = 0;
429
0
      break;
430
0
  }
431
432
0
  return format;
433
0
}
434
435
gdiBitmap* gdi_bitmap_new_ex(rdpGdi* gdi, int width, int height, int bpp, BYTE* data)
436
0
{
437
0
  gdiBitmap* bitmap = NULL;
438
0
  bitmap = (gdiBitmap*)calloc(1, sizeof(gdiBitmap));
439
440
0
  if (!bitmap)
441
0
    goto fail_bitmap;
442
443
0
  if (!(bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc)))
444
0
    goto fail_hdc;
445
446
0
  WLog_Print(gdi->log, WLOG_DEBUG, "gdi_bitmap_new: width:%d height:%d bpp:%d", width, height,
447
0
             bpp);
448
449
0
  if (!data)
450
0
    bitmap->bitmap =
451
0
        gdi_CreateCompatibleBitmap(gdi->hdc, WINPR_ASSERTING_INT_CAST(uint32_t, width),
452
0
                                   WINPR_ASSERTING_INT_CAST(uint32_t, height));
453
0
  else
454
0
    bitmap->bitmap = gdi_create_bitmap(gdi, WINPR_ASSERTING_INT_CAST(uint32_t, width),
455
0
                                       WINPR_ASSERTING_INT_CAST(uint32_t, height),
456
0
                                       WINPR_ASSERTING_INT_CAST(uint32_t, bpp), data);
457
458
0
  if (!bitmap->bitmap)
459
0
    goto fail_bitmap_bitmap;
460
461
0
  gdi_SelectObject(bitmap->hdc, (HGDIOBJECT)bitmap->bitmap);
462
0
  bitmap->org_bitmap = NULL;
463
0
  return bitmap;
464
0
fail_bitmap_bitmap:
465
0
  gdi_DeleteDC(bitmap->hdc);
466
0
fail_hdc:
467
0
  free(bitmap);
468
0
fail_bitmap:
469
0
  return NULL;
470
0
}
471
472
void gdi_bitmap_free_ex(gdiBitmap* bitmap)
473
0
{
474
0
  if (bitmap)
475
0
  {
476
0
    gdi_SelectObject(bitmap->hdc, (HGDIOBJECT)bitmap->org_bitmap);
477
0
    gdi_DeleteObject((HGDIOBJECT)bitmap->bitmap);
478
0
    gdi_DeleteDC(bitmap->hdc);
479
0
    free(bitmap);
480
0
  }
481
0
}
482
483
BOOL gdi_bitmap_update(rdpContext* context, const BITMAP_UPDATE* bitmapUpdate)
484
0
{
485
0
  if (!context || !bitmapUpdate || !context->gdi || !context->codecs)
486
0
  {
487
0
    WLog_ERR(TAG,
488
0
             "Invalid arguments: context=%p, bitmapUpdate=%p, context->gdi=%p, "
489
0
             "context->codecs=%p",
490
0
             context, bitmapUpdate, context ? context->gdi : NULL,
491
0
             context ? context->codecs : NULL);
492
0
    return FALSE;
493
0
  }
494
495
0
  for (UINT32 index = 0; index < bitmapUpdate->number; index++)
496
0
  {
497
0
    BOOL rc = FALSE;
498
0
    const BITMAP_DATA* bitmap = &(bitmapUpdate->rectangles[index]);
499
0
    rdpBitmap* bmp = Bitmap_Alloc(context);
500
501
0
    if (!bmp)
502
0
      goto fail;
503
504
0
    if (!Bitmap_SetDimensions(bmp, WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width),
505
0
                              WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height)))
506
0
      goto fail;
507
508
0
    if (!Bitmap_SetRectangle(bmp, WINPR_ASSERTING_INT_CAST(UINT16, bitmap->destLeft),
509
0
                             WINPR_ASSERTING_INT_CAST(UINT16, bitmap->destTop),
510
0
                             WINPR_ASSERTING_INT_CAST(UINT16, bitmap->destRight),
511
0
                             WINPR_ASSERTING_INT_CAST(UINT16, bitmap->destBottom)))
512
0
      goto fail;
513
514
0
    if (!bmp->Decompress(context, bmp, bitmap->bitmapDataStream, bitmap->width, bitmap->height,
515
0
                         bitmap->bitsPerPixel, bitmap->bitmapLength, bitmap->compressed,
516
0
                         RDP_CODEC_ID_NONE))
517
0
      goto fail;
518
519
0
    if (!bmp->New(context, bmp))
520
0
      goto fail;
521
522
0
    if (!bmp->Paint(context, bmp))
523
0
      goto fail;
524
525
0
    rc = TRUE;
526
0
  fail:
527
0
    Bitmap_Free(context, bmp);
528
0
    if (!rc)
529
0
      return FALSE;
530
0
  }
531
532
0
  return TRUE;
533
0
}
534
535
static BOOL gdi_palette_update(rdpContext* context, const PALETTE_UPDATE* palette)
536
0
{
537
0
  rdpGdi* gdi = NULL;
538
539
0
  if (!context || !palette)
540
0
    return FALSE;
541
542
0
  gdi = context->gdi;
543
0
  gdi->palette.format = gdi->dstFormat;
544
545
0
  for (UINT32 index = 0; index < palette->number; index++)
546
0
  {
547
0
    const PALETTE_ENTRY* pe = &(palette->entries[index]);
548
0
    gdi->palette.palette[index] =
549
0
        FreeRDPGetColor(gdi->dstFormat, pe->red, pe->green, pe->blue, 0xFF);
550
0
  }
551
552
0
  return TRUE;
553
0
}
554
555
static BOOL gdi_set_bounds(rdpContext* context, const rdpBounds* bounds)
556
0
{
557
0
  rdpGdi* gdi = NULL;
558
559
0
  if (!context)
560
0
    return FALSE;
561
562
0
  gdi = context->gdi;
563
564
0
  if (bounds)
565
0
  {
566
0
    gdi_SetClipRgn(gdi->drawing->hdc, bounds->left, bounds->top,
567
0
                   bounds->right - bounds->left + 1, bounds->bottom - bounds->top + 1);
568
0
  }
569
0
  else
570
0
    gdi_SetNullClipRgn(gdi->drawing->hdc);
571
572
0
  return TRUE;
573
0
}
574
575
static BOOL gdi_dstblt(rdpContext* context, const DSTBLT_ORDER* dstblt)
576
0
{
577
0
  rdpGdi* gdi = NULL;
578
579
0
  if (!context || !dstblt)
580
0
    return FALSE;
581
582
0
  gdi = context->gdi;
583
0
  return gdi_BitBlt(gdi->drawing->hdc, dstblt->nLeftRect, dstblt->nTopRect, dstblt->nWidth,
584
0
                    dstblt->nHeight, NULL, 0, 0, gdi_rop3_code_checked(dstblt->bRop),
585
0
                    &gdi->palette);
586
0
}
587
588
static BOOL gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
589
0
{
590
0
  const rdpBrush* brush = &patblt->brush;
591
0
  UINT32 foreColor = 0;
592
0
  UINT32 backColor = 0;
593
0
  UINT32 originalColor = 0;
594
0
  HGDI_BRUSH originalBrush = NULL;
595
0
  HGDI_BRUSH hbrush = NULL;
596
0
  rdpGdi* gdi = context->gdi;
597
0
  BOOL ret = FALSE;
598
0
  const DWORD rop = gdi_rop3_code_checked(patblt->bRop);
599
0
  INT32 nXSrc = 0;
600
0
  INT32 nYSrc = 0;
601
0
  BYTE data[8 * 8 * 4];
602
0
  HGDI_BITMAP hBmp = NULL;
603
604
0
  if (!gdi_decode_color(gdi, patblt->foreColor, &foreColor, NULL))
605
0
    return FALSE;
606
607
0
  if (!gdi_decode_color(gdi, patblt->backColor, &backColor, NULL))
608
0
    return FALSE;
609
610
0
  originalColor = gdi_SetTextColor(gdi->drawing->hdc, foreColor);
611
0
  originalBrush = gdi->drawing->hdc->brush;
612
613
0
  switch (brush->style)
614
0
  {
615
0
    case GDI_BS_SOLID:
616
0
      hbrush = gdi_CreateSolidBrush(foreColor);
617
0
      break;
618
619
0
    case GDI_BS_HATCHED:
620
0
    {
621
0
      const BYTE* hatched = NULL;
622
0
      hatched = GDI_BS_HATCHED_PATTERNS + (8ULL * brush->hatch);
623
624
0
      if (!freerdp_image_copy_from_monochrome(data, gdi->drawing->hdc->format, 0, 0, 0, 8, 8,
625
0
                                              hatched, backColor, foreColor, &gdi->palette))
626
0
        goto out_error;
627
628
0
      hBmp = gdi_CreateBitmapEx(8, 8, gdi->drawing->hdc->format, 0, data, NULL);
629
630
0
      if (!hBmp)
631
0
        goto out_error;
632
633
0
      hbrush = gdi_CreateHatchBrush(hBmp);
634
0
    }
635
0
    break;
636
637
0
    case GDI_BS_PATTERN:
638
0
    {
639
0
      UINT32 brushFormat = 0;
640
641
0
      if (brush->bpp > 1)
642
0
      {
643
0
        UINT32 bpp = brush->bpp;
644
645
0
        if ((bpp == 16) &&
646
0
            (freerdp_settings_get_uint32(context->settings, FreeRDP_ColorDepth) == 15))
647
0
          bpp = 15;
648
649
0
        brushFormat = gdi_get_pixel_format(bpp);
650
651
0
        if (!freerdp_image_copy_no_overlap(data, gdi->drawing->hdc->format, 0, 0, 0, 8, 8,
652
0
                                           brush->data, brushFormat, 0, 0, 0, &gdi->palette,
653
0
                                           FREERDP_FLIP_NONE))
654
0
          goto out_error;
655
0
      }
656
0
      else
657
0
      {
658
0
        if (!freerdp_image_copy_from_monochrome(data, gdi->drawing->hdc->format, 0, 0, 0, 8,
659
0
                                                8, brush->data, backColor, foreColor,
660
0
                                                &gdi->palette))
661
0
          goto out_error;
662
0
      }
663
664
0
      hBmp = gdi_CreateBitmapEx(8, 8, gdi->drawing->hdc->format, 0, data, NULL);
665
666
0
      if (!hBmp)
667
0
        goto out_error;
668
669
0
      hbrush = gdi_CreatePatternBrush(hBmp);
670
0
    }
671
0
    break;
672
673
0
    default:
674
0
      WLog_ERR(TAG, "unimplemented brush style:%" PRIu32 "", brush->style);
675
0
      break;
676
0
  }
677
678
0
  if (hbrush)
679
0
  {
680
0
    hbrush->nXOrg = WINPR_ASSERTING_INT_CAST(int32_t, brush->x);
681
0
    hbrush->nYOrg = WINPR_ASSERTING_INT_CAST(int32_t, brush->y);
682
0
    gdi->drawing->hdc->brush = hbrush;
683
0
    ret = gdi_BitBlt(gdi->drawing->hdc, patblt->nLeftRect, patblt->nTopRect, patblt->nWidth,
684
0
                     patblt->nHeight, gdi->primary->hdc, nXSrc, nYSrc, rop, &gdi->palette);
685
0
  }
686
687
0
out_error:
688
0
  gdi_DeleteObject((HGDIOBJECT)hBmp);
689
0
  gdi_DeleteObject((HGDIOBJECT)hbrush);
690
0
  gdi->drawing->hdc->brush = originalBrush;
691
0
  gdi_SetTextColor(gdi->drawing->hdc, originalColor);
692
0
  return ret;
693
0
}
694
695
static BOOL gdi_scrblt(rdpContext* context, const SCRBLT_ORDER* scrblt)
696
0
{
697
0
  rdpGdi* gdi = NULL;
698
699
0
  if (!context || !context->gdi)
700
0
    return FALSE;
701
702
0
  gdi = context->gdi;
703
0
  return gdi_BitBlt(gdi->drawing->hdc, scrblt->nLeftRect, scrblt->nTopRect, scrblt->nWidth,
704
0
                    scrblt->nHeight, gdi->primary->hdc, scrblt->nXSrc, scrblt->nYSrc,
705
0
                    gdi_rop3_code_checked(scrblt->bRop), &gdi->palette);
706
0
}
707
708
static BOOL gdi_opaque_rect(rdpContext* context, const OPAQUE_RECT_ORDER* opaque_rect)
709
0
{
710
0
  GDI_RECT rect;
711
0
  HGDI_BRUSH hBrush = NULL;
712
0
  UINT32 brush_color = 0;
713
0
  rdpGdi* gdi = context->gdi;
714
0
  BOOL ret = 0;
715
0
  INT32 x = opaque_rect->nLeftRect;
716
0
  INT32 y = opaque_rect->nTopRect;
717
0
  INT32 w = opaque_rect->nWidth;
718
0
  INT32 h = opaque_rect->nHeight;
719
0
  gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
720
0
  gdi_CRgnToRect(x, y, w, h, &rect);
721
722
0
  if (!gdi_decode_color(gdi, opaque_rect->color, &brush_color, NULL))
723
0
    return FALSE;
724
725
0
  if (!(hBrush = gdi_CreateSolidBrush(brush_color)))
726
0
    return FALSE;
727
728
0
  ret = gdi_FillRect(gdi->drawing->hdc, &rect, hBrush);
729
0
  gdi_DeleteObject((HGDIOBJECT)hBrush);
730
0
  return ret;
731
0
}
732
733
static BOOL gdi_multi_opaque_rect(rdpContext* context,
734
                                  const MULTI_OPAQUE_RECT_ORDER* multi_opaque_rect)
735
0
{
736
0
  GDI_RECT rect;
737
0
  HGDI_BRUSH hBrush = NULL;
738
0
  UINT32 brush_color = 0;
739
0
  rdpGdi* gdi = context->gdi;
740
0
  BOOL ret = TRUE;
741
742
0
  if (!gdi_decode_color(gdi, multi_opaque_rect->color, &brush_color, NULL))
743
0
    return FALSE;
744
745
0
  hBrush = gdi_CreateSolidBrush(brush_color);
746
747
0
  if (!hBrush)
748
0
    return FALSE;
749
750
0
  for (UINT32 i = 0; i < multi_opaque_rect->numRectangles; i++)
751
0
  {
752
0
    const DELTA_RECT* rectangle = &multi_opaque_rect->rectangles[i];
753
0
    INT32 x = rectangle->left;
754
0
    INT32 y = rectangle->top;
755
0
    INT32 w = rectangle->width;
756
0
    INT32 h = rectangle->height;
757
0
    gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
758
0
    gdi_CRgnToRect(x, y, w, h, &rect);
759
0
    ret = gdi_FillRect(gdi->drawing->hdc, &rect, hBrush);
760
761
0
    if (!ret)
762
0
      break;
763
0
  }
764
765
0
  gdi_DeleteObject((HGDIOBJECT)hBrush);
766
0
  return ret;
767
0
}
768
769
static BOOL gdi_line_to(rdpContext* context, const LINE_TO_ORDER* lineTo)
770
0
{
771
0
  UINT32 color = 0;
772
0
  HGDI_PEN hPen = NULL;
773
0
  rdpGdi* gdi = context->gdi;
774
0
  INT32 xStart = lineTo->nXStart;
775
0
  INT32 yStart = lineTo->nYStart;
776
0
  INT32 xEnd = lineTo->nXEnd;
777
0
  INT32 yEnd = lineTo->nYEnd;
778
0
  INT32 w = 0;
779
0
  INT32 h = 0;
780
0
  gdi_ClipCoords(gdi->drawing->hdc, &xStart, &yStart, &w, &h, NULL, NULL);
781
0
  gdi_ClipCoords(gdi->drawing->hdc, &xEnd, &yEnd, &w, &h, NULL, NULL);
782
783
0
  if (!gdi_decode_color(gdi, lineTo->penColor, &color, NULL))
784
0
    return FALSE;
785
786
0
  if (!(hPen = gdi_CreatePen(lineTo->penStyle, lineTo->penWidth, color, gdi->drawing->hdc->format,
787
0
                             &gdi->palette)))
788
0
    return FALSE;
789
790
0
  gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)hPen);
791
0
  gdi_SetROP2(gdi->drawing->hdc, WINPR_ASSERTING_INT_CAST(int32_t, lineTo->bRop2));
792
0
  gdi_MoveToEx(gdi->drawing->hdc, lineTo->nXStart, lineTo->nYStart, NULL);
793
0
  gdi_LineTo(gdi->drawing->hdc, lineTo->nXEnd, lineTo->nYEnd);
794
0
  gdi_DeleteObject((HGDIOBJECT)hPen);
795
0
  return TRUE;
796
0
}
797
798
static BOOL gdi_polyline(rdpContext* context, const POLYLINE_ORDER* polyline)
799
0
{
800
0
  INT32 x = 0;
801
0
  INT32 y = 0;
802
0
  UINT32 color = 0;
803
0
  HGDI_PEN hPen = NULL;
804
0
  DELTA_POINT* points = NULL;
805
0
  rdpGdi* gdi = context->gdi;
806
0
  INT32 w = 0;
807
0
  INT32 h = 0;
808
809
0
  if (!gdi_decode_color(gdi, polyline->penColor, &color, NULL))
810
0
    return FALSE;
811
812
0
  if (!(hPen = gdi_CreatePen(GDI_PS_SOLID, 1, color, gdi->drawing->hdc->format, &gdi->palette)))
813
0
    return FALSE;
814
815
0
  gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)hPen);
816
0
  gdi_SetROP2(gdi->drawing->hdc, WINPR_ASSERTING_INT_CAST(int32_t, polyline->bRop2));
817
0
  x = polyline->xStart;
818
0
  y = polyline->yStart;
819
0
  gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
820
0
  gdi_MoveToEx(gdi->drawing->hdc, x, y, NULL);
821
0
  points = polyline->points;
822
823
0
  for (UINT32 i = 0; i < polyline->numDeltaEntries; i++)
824
0
  {
825
0
    x += points[i].x;
826
0
    y += points[i].y;
827
0
    gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
828
0
    gdi_LineTo(gdi->drawing->hdc, x, y);
829
0
    gdi_MoveToEx(gdi->drawing->hdc, x, y, NULL);
830
0
  }
831
832
0
  gdi_DeleteObject((HGDIOBJECT)hPen);
833
0
  return TRUE;
834
0
}
835
836
static BOOL gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt)
837
0
{
838
0
  gdiBitmap* bitmap = NULL;
839
0
  rdpGdi* gdi = NULL;
840
841
0
  if (!context || !memblt || !context->gdi || !memblt->bitmap)
842
0
    return FALSE;
843
844
0
  bitmap = (gdiBitmap*)memblt->bitmap;
845
0
  gdi = context->gdi;
846
0
  return gdi_BitBlt(gdi->drawing->hdc, memblt->nLeftRect, memblt->nTopRect, memblt->nWidth,
847
0
                    memblt->nHeight, bitmap->hdc, memblt->nXSrc, memblt->nYSrc,
848
0
                    gdi_rop3_code_checked(memblt->bRop), &gdi->palette);
849
0
}
850
851
static BOOL gdi_mem3blt(rdpContext* context, MEM3BLT_ORDER* mem3blt)
852
0
{
853
0
  HGDI_BRUSH originalBrush = NULL;
854
0
  rdpGdi* gdi = context->gdi;
855
0
  BOOL ret = TRUE;
856
0
  const rdpBrush* brush = &mem3blt->brush;
857
0
  gdiBitmap* bitmap = (gdiBitmap*)mem3blt->bitmap;
858
0
  UINT32 foreColor = 0;
859
0
  UINT32 backColor = 0;
860
0
  UINT32 originalColor = 0;
861
862
0
  if (!gdi_decode_color(gdi, mem3blt->foreColor, &foreColor, NULL))
863
0
    return FALSE;
864
865
0
  if (!gdi_decode_color(gdi, mem3blt->backColor, &backColor, NULL))
866
0
    return FALSE;
867
868
0
  originalColor = gdi_SetTextColor(gdi->drawing->hdc, foreColor);
869
870
0
  switch (brush->style)
871
0
  {
872
0
    case GDI_BS_SOLID:
873
0
      originalBrush = gdi->drawing->hdc->brush;
874
0
      gdi->drawing->hdc->brush = gdi_CreateSolidBrush(foreColor);
875
876
0
      if (!gdi->drawing->hdc->brush)
877
0
      {
878
0
        ret = FALSE;
879
0
        goto out_fail;
880
0
      }
881
882
0
      ret = gdi_BitBlt(gdi->drawing->hdc, mem3blt->nLeftRect, mem3blt->nTopRect,
883
0
                       mem3blt->nWidth, mem3blt->nHeight, bitmap->hdc, mem3blt->nXSrc,
884
0
                       mem3blt->nYSrc, gdi_rop3_code_checked(mem3blt->bRop), &gdi->palette);
885
0
      gdi_DeleteObject((HGDIOBJECT)gdi->drawing->hdc->brush);
886
0
      gdi->drawing->hdc->brush = originalBrush;
887
0
      break;
888
889
0
    case GDI_BS_PATTERN:
890
0
    {
891
0
      HGDI_BITMAP hBmp = NULL;
892
0
      UINT32 brushFormat = 0;
893
0
      BYTE* data = (BYTE*)winpr_aligned_malloc(
894
0
          8ULL * 8ULL * FreeRDPGetBytesPerPixel(gdi->drawing->hdc->format), 16);
895
896
0
      if (!data)
897
0
      {
898
0
        ret = FALSE;
899
0
        goto out_fail;
900
0
      }
901
902
0
      if (brush->bpp > 1)
903
0
      {
904
0
        UINT32 bpp = brush->bpp;
905
906
0
        const UINT32 ColorDepth =
907
0
            freerdp_settings_get_uint32(gdi->context->settings, FreeRDP_ColorDepth);
908
0
        if ((bpp == 16) && (ColorDepth == 15))
909
0
          bpp = 15;
910
911
0
        brushFormat = gdi_get_pixel_format(bpp);
912
913
0
        if (!freerdp_image_copy_no_overlap(data, gdi->drawing->hdc->format, 0, 0, 0, 8, 8,
914
0
                                           brush->data, brushFormat, 0, 0, 0, &gdi->palette,
915
0
                                           FREERDP_FLIP_NONE))
916
0
        {
917
0
          ret = FALSE;
918
0
          winpr_aligned_free(data);
919
0
          goto out_fail;
920
0
        }
921
0
      }
922
0
      else
923
0
      {
924
0
        if (!freerdp_image_copy_from_monochrome(data, gdi->drawing->hdc->format, 0, 0, 0, 8,
925
0
                                                8, brush->data, backColor, foreColor,
926
0
                                                &gdi->palette))
927
0
        {
928
0
          ret = FALSE;
929
0
          winpr_aligned_free(data);
930
0
          goto out_fail;
931
0
        }
932
0
      }
933
934
0
      hBmp = gdi_CreateBitmap(8, 8, gdi->drawing->hdc->format, data);
935
936
0
      if (!hBmp)
937
0
      {
938
0
        ret = FALSE;
939
0
        winpr_aligned_free(data);
940
0
        goto out_fail;
941
0
      }
942
943
0
      originalBrush = gdi->drawing->hdc->brush;
944
0
      gdi->drawing->hdc->brush = gdi_CreatePatternBrush(hBmp);
945
946
0
      if (!gdi->drawing->hdc->brush)
947
0
      {
948
0
        gdi_DeleteObject((HGDIOBJECT)hBmp);
949
0
        goto out_fail;
950
0
      }
951
952
0
      gdi->drawing->hdc->brush->nXOrg = WINPR_ASSERTING_INT_CAST(int32_t, brush->x);
953
0
      gdi->drawing->hdc->brush->nYOrg = WINPR_ASSERTING_INT_CAST(int32_t, brush->y);
954
0
      ret = gdi_BitBlt(gdi->drawing->hdc, mem3blt->nLeftRect, mem3blt->nTopRect,
955
0
                       mem3blt->nWidth, mem3blt->nHeight, bitmap->hdc, mem3blt->nXSrc,
956
0
                       mem3blt->nYSrc, gdi_rop3_code_checked(mem3blt->bRop), &gdi->palette);
957
0
      gdi_DeleteObject((HGDIOBJECT)gdi->drawing->hdc->brush);
958
0
      gdi_DeleteObject((HGDIOBJECT)hBmp);
959
0
      gdi->drawing->hdc->brush = originalBrush;
960
0
    }
961
0
    break;
962
963
0
    default:
964
0
      WLog_ERR(TAG, "Mem3Blt unimplemented brush style:%" PRIu32 "", brush->style);
965
0
      break;
966
0
  }
967
968
0
out_fail:
969
0
  gdi_SetTextColor(gdi->drawing->hdc, originalColor);
970
0
  return ret;
971
0
}
972
973
static BOOL gdi_polygon_sc(WINPR_ATTR_UNUSED rdpContext* context,
974
                           WINPR_ATTR_UNUSED const POLYGON_SC_ORDER* polygon_sc)
975
0
{
976
0
  WLog_WARN(TAG, "not implemented");
977
0
  return FALSE;
978
0
}
979
980
static BOOL gdi_polygon_cb(WINPR_ATTR_UNUSED rdpContext* context,
981
                           WINPR_ATTR_UNUSED POLYGON_CB_ORDER* polygon_cb)
982
0
{
983
0
  WLog_WARN(TAG, "not implemented");
984
0
  return FALSE;
985
0
}
986
987
static BOOL gdi_ellipse_sc(WINPR_ATTR_UNUSED rdpContext* context,
988
                           WINPR_ATTR_UNUSED const ELLIPSE_SC_ORDER* ellipse_sc)
989
0
{
990
0
  WLog_WARN(TAG, "not implemented");
991
0
  return FALSE;
992
0
}
993
994
static BOOL gdi_ellipse_cb(WINPR_ATTR_UNUSED rdpContext* context,
995
                           WINPR_ATTR_UNUSED const ELLIPSE_CB_ORDER* ellipse_cb)
996
0
{
997
0
  WLog_WARN(TAG, "not implemented");
998
0
  return FALSE;
999
0
}
1000
1001
static BOOL gdi_frame_marker(WINPR_ATTR_UNUSED rdpContext* context,
1002
                             WINPR_ATTR_UNUSED const FRAME_MARKER_ORDER* frameMarker)
1003
0
{
1004
0
  return TRUE;
1005
0
}
1006
1007
static BOOL gdi_surface_frame_marker(rdpContext* context,
1008
                                     const SURFACE_FRAME_MARKER* surfaceFrameMarker)
1009
0
{
1010
0
  WLog_Print(context->gdi->log, WLOG_DEBUG, "frameId %" PRIu32 " frameAction %" PRIu32 "",
1011
0
             surfaceFrameMarker->frameId, surfaceFrameMarker->frameAction);
1012
1013
0
  switch (surfaceFrameMarker->frameAction)
1014
0
  {
1015
0
    case SURFACECMD_FRAMEACTION_BEGIN:
1016
0
      break;
1017
1018
0
    case SURFACECMD_FRAMEACTION_END:
1019
0
      if (freerdp_settings_get_uint32(context->settings, FreeRDP_FrameAcknowledge) > 0)
1020
0
      {
1021
0
        IFCALL(context->update->SurfaceFrameAcknowledge, context,
1022
0
               surfaceFrameMarker->frameId);
1023
0
      }
1024
1025
0
      break;
1026
0
    default:
1027
0
      break;
1028
0
  }
1029
1030
0
  return TRUE;
1031
0
}
1032
1033
static BOOL intersect_rect(const rdpGdi* gdi, const SURFACE_BITS_COMMAND* cmd, RECTANGLE_16* prect)
1034
0
{
1035
0
  const UINT32 w = (const UINT32)gdi->width;
1036
0
  const UINT32 h = (const UINT32)gdi->height;
1037
1038
0
  if (cmd->destLeft > w)
1039
0
    return FALSE;
1040
0
  if (cmd->destRight > w)
1041
0
    return FALSE;
1042
0
  if (cmd->destLeft > cmd->destRight)
1043
0
    return FALSE;
1044
0
  if (cmd->destRight > UINT16_MAX)
1045
0
    return FALSE;
1046
1047
0
  if (cmd->destTop > h)
1048
0
    return FALSE;
1049
0
  if (cmd->destBottom > h)
1050
0
    return FALSE;
1051
0
  if (cmd->destTop > cmd->destBottom)
1052
0
    return FALSE;
1053
0
  if (cmd->destBottom > UINT16_MAX)
1054
0
    return FALSE;
1055
1056
0
  prect->left = (const UINT16)cmd->destLeft;
1057
0
  prect->top = (const UINT16)cmd->destTop;
1058
0
  prect->right = MIN((UINT16)cmd->destRight, prect->left + cmd->bmp.width);
1059
0
  prect->bottom = MIN((UINT16)cmd->destBottom, prect->top + cmd->bmp.height);
1060
0
  return TRUE;
1061
0
}
1062
1063
static BOOL gdi_surface_bits(rdpContext* context, const SURFACE_BITS_COMMAND* cmd)
1064
0
{
1065
0
  BOOL result = FALSE;
1066
0
  DWORD format = 0;
1067
0
  rdpGdi* gdi = NULL;
1068
0
  size_t size = 0;
1069
0
  REGION16 region;
1070
0
  RECTANGLE_16 cmdRect = { 0 };
1071
1072
0
  if (!context || !cmd)
1073
0
    return FALSE;
1074
1075
0
  gdi = context->gdi;
1076
0
  WLog_Print(
1077
0
      gdi->log, WLOG_DEBUG,
1078
0
      "destLeft %" PRIu32 " destTop %" PRIu32 " destRight %" PRIu32 " destBottom %" PRIu32 " "
1079
0
      "bpp %" PRIu8 " flags %" PRIx8 " codecID %" PRIu16 " width %" PRIu16 " height %" PRIu16
1080
0
      " length %" PRIu32 "",
1081
0
      cmd->destLeft, cmd->destTop, cmd->destRight, cmd->destBottom, cmd->bmp.bpp, cmd->bmp.flags,
1082
0
      cmd->bmp.codecID, cmd->bmp.width, cmd->bmp.height, cmd->bmp.bitmapDataLength);
1083
0
  region16_init(&region);
1084
1085
0
  if (!intersect_rect(gdi, cmd, &cmdRect))
1086
0
    goto out;
1087
1088
0
  switch (cmd->bmp.codecID)
1089
0
  {
1090
0
    case RDP_CODEC_ID_REMOTEFX:
1091
0
    case RDP_CODEC_ID_IMAGE_REMOTEFX:
1092
0
      if (!rfx_process_message(context->codecs->rfx, cmd->bmp.bitmapData,
1093
0
                               cmd->bmp.bitmapDataLength, cmdRect.left, cmdRect.top,
1094
0
                               gdi->primary_buffer, gdi->dstFormat, gdi->stride,
1095
0
                               WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height), &region))
1096
0
      {
1097
0
        WLog_ERR(TAG, "Failed to process RemoteFX message");
1098
0
        goto out;
1099
0
      }
1100
1101
0
      break;
1102
1103
0
    case RDP_CODEC_ID_NSCODEC:
1104
0
      format = gdi->dstFormat;
1105
1106
0
      if (!nsc_process_message(
1107
0
              context->codecs->nsc, cmd->bmp.bpp, cmd->bmp.width, cmd->bmp.height,
1108
0
              cmd->bmp.bitmapData, cmd->bmp.bitmapDataLength, gdi->primary_buffer, format,
1109
0
              gdi->stride, cmdRect.left, cmdRect.top, cmdRect.right - cmdRect.left,
1110
0
              cmdRect.bottom - cmdRect.top, FREERDP_FLIP_VERTICAL))
1111
0
      {
1112
0
        WLog_ERR(TAG, "Failed to process NSCodec message");
1113
0
        goto out;
1114
0
      }
1115
1116
0
      region16_union_rect(&region, &region, &cmdRect);
1117
0
      break;
1118
1119
0
    case RDP_CODEC_ID_NONE:
1120
0
      format = gdi_get_pixel_format(cmd->bmp.bpp);
1121
0
      size = 1ull * cmd->bmp.width * cmd->bmp.height * FreeRDPGetBytesPerPixel(format);
1122
0
      if (size > cmd->bmp.bitmapDataLength)
1123
0
      {
1124
0
        WLog_ERR(TAG, "Short nocodec message: got %" PRIu32 " bytes, require %" PRIuz,
1125
0
                 cmd->bmp.bitmapDataLength, size);
1126
0
        goto out;
1127
0
      }
1128
1129
0
      if (!freerdp_image_copy_no_overlap(
1130
0
              gdi->primary_buffer, gdi->dstFormat, gdi->stride, cmdRect.left, cmdRect.top,
1131
0
              cmdRect.right - cmdRect.left, cmdRect.bottom - cmdRect.top, cmd->bmp.bitmapData,
1132
0
              format, 0, 0, 0, &gdi->palette, FREERDP_FLIP_VERTICAL))
1133
0
      {
1134
0
        WLog_ERR(TAG, "Failed to process nocodec message");
1135
0
        goto out;
1136
0
      }
1137
1138
0
      region16_union_rect(&region, &region, &cmdRect);
1139
0
      break;
1140
1141
0
    default:
1142
0
      WLog_ERR(TAG, "Unsupported codecID %" PRIu32 "", cmd->bmp.codecID);
1143
0
      break;
1144
0
  }
1145
1146
0
  UINT32 nbRects = 0;
1147
0
  const RECTANGLE_16* rects = region16_rects(&region, &nbRects);
1148
0
  if (!rects && (nbRects > 0))
1149
0
    goto out;
1150
1151
0
  if (nbRects == 0)
1152
0
  {
1153
0
    const int32_t w = cmdRect.right - cmdRect.left;
1154
0
    const int32_t h = cmdRect.bottom - cmdRect.top;
1155
0
    if (!gdi_InvalidateRegion(gdi->primary->hdc, cmdRect.left, cmdRect.top, w, h))
1156
0
      goto out;
1157
0
  }
1158
0
  for (UINT32 i = 0; i < nbRects; i++)
1159
0
  {
1160
0
    const RECTANGLE_16* rect = &rects[i];
1161
1162
0
    UINT32 left = rect->left;
1163
0
    UINT32 top = rect->top;
1164
0
    UINT32 width = rect->right - rect->left;
1165
0
    UINT32 height = rect->bottom - rect->top;
1166
1167
0
    if (!gdi_InvalidateRegion(gdi->primary->hdc, WINPR_ASSERTING_INT_CAST(int32_t, left),
1168
0
                              WINPR_ASSERTING_INT_CAST(int32_t, top),
1169
0
                              WINPR_ASSERTING_INT_CAST(int32_t, width),
1170
0
                              WINPR_ASSERTING_INT_CAST(int32_t, height)))
1171
0
    {
1172
0
      WLog_ERR(TAG, "Failed to update invalid region");
1173
0
      goto out;
1174
0
    }
1175
0
  }
1176
1177
0
  result = TRUE;
1178
0
out:
1179
0
  region16_uninit(&region);
1180
0
  return result;
1181
0
}
1182
1183
/**
1184
 * Register GDI callbacks with libfreerdp-core.
1185
 * @param update current instance
1186
 */
1187
1188
static void gdi_register_update_callbacks(rdpUpdate* update)
1189
0
{
1190
0
  rdpPrimaryUpdate* primary = NULL;
1191
0
  const rdpSettings* settings = NULL;
1192
1193
0
  WINPR_ASSERT(update);
1194
0
  WINPR_ASSERT(update->context);
1195
1196
0
  settings = update->context->settings;
1197
0
  WINPR_ASSERT(settings);
1198
1199
0
  primary = update->primary;
1200
0
  WINPR_ASSERT(primary);
1201
1202
0
  if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
1203
0
    return;
1204
0
  update->Palette = gdi_palette_update;
1205
0
  update->SetBounds = gdi_set_bounds;
1206
0
  primary->DstBlt = gdi_dstblt;
1207
0
  primary->PatBlt = gdi_patblt;
1208
0
  primary->ScrBlt = gdi_scrblt;
1209
0
  primary->OpaqueRect = gdi_opaque_rect;
1210
0
  primary->DrawNineGrid = NULL;
1211
0
  primary->MultiDstBlt = NULL;
1212
0
  primary->MultiPatBlt = NULL;
1213
0
  primary->MultiScrBlt = NULL;
1214
0
  primary->MultiOpaqueRect = gdi_multi_opaque_rect;
1215
0
  primary->MultiDrawNineGrid = NULL;
1216
0
  primary->LineTo = gdi_line_to;
1217
0
  primary->Polyline = gdi_polyline;
1218
0
  primary->MemBlt = gdi_memblt;
1219
0
  primary->Mem3Blt = gdi_mem3blt;
1220
0
  primary->SaveBitmap = NULL;
1221
0
  primary->GlyphIndex = NULL;
1222
0
  primary->FastIndex = NULL;
1223
0
  primary->FastGlyph = NULL;
1224
0
  primary->PolygonSC = gdi_polygon_sc;
1225
0
  primary->PolygonCB = gdi_polygon_cb;
1226
0
  primary->EllipseSC = gdi_ellipse_sc;
1227
0
  primary->EllipseCB = gdi_ellipse_cb;
1228
0
  update->SurfaceBits = gdi_surface_bits;
1229
0
  update->SurfaceFrameMarker = gdi_surface_frame_marker;
1230
0
  update->altsec->FrameMarker = gdi_frame_marker;
1231
0
}
1232
1233
static BOOL gdi_init_primary(rdpGdi* gdi, UINT32 stride, UINT32 format, BYTE* buffer,
1234
                             void (*pfree)(void*), BOOL isLocked)
1235
0
{
1236
0
  WINPR_ASSERT(gdi);
1237
0
  WINPR_ASSERT(gdi->context);
1238
0
  WINPR_ASSERT(gdi->context->update);
1239
0
  if (!isLocked)
1240
0
    rdp_update_lock(gdi->context->update);
1241
1242
0
  gdi->primary = (gdiBitmap*)calloc(1, sizeof(gdiBitmap));
1243
1244
0
  if (format > 0)
1245
0
    gdi->dstFormat = format;
1246
1247
0
  if (stride > 0)
1248
0
    gdi->stride = stride;
1249
0
  else
1250
0
    gdi->stride = WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width) *
1251
0
                  FreeRDPGetBytesPerPixel(gdi->dstFormat);
1252
1253
0
  if (!gdi->primary)
1254
0
    goto fail_primary;
1255
1256
0
  if (!(gdi->primary->hdc = gdi_CreateCompatibleDC(gdi->hdc)))
1257
0
    goto fail_hdc;
1258
1259
0
  if (!buffer)
1260
0
  {
1261
0
    gdi->primary->bitmap =
1262
0
        gdi_CreateCompatibleBitmap(gdi->hdc, WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
1263
0
                                   WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height));
1264
0
  }
1265
0
  else
1266
0
  {
1267
0
    gdi->primary->bitmap = gdi_CreateBitmapEx(WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
1268
0
                                              WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height),
1269
0
                                              gdi->dstFormat, gdi->stride, buffer, pfree);
1270
0
  }
1271
1272
0
  if (!gdi->primary->bitmap)
1273
0
    goto fail_bitmap;
1274
1275
0
  gdi->stride = gdi->primary->bitmap->scanline;
1276
0
  gdi_SelectObject(gdi->primary->hdc, (HGDIOBJECT)gdi->primary->bitmap);
1277
0
  gdi->primary->org_bitmap = NULL;
1278
0
  gdi->primary_buffer = gdi->primary->bitmap->data;
1279
1280
0
  if (!(gdi->primary->hdc->hwnd = (HGDI_WND)calloc(1, sizeof(GDI_WND))))
1281
0
    goto fail_hwnd;
1282
1283
0
  if (!(gdi->primary->hdc->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0)))
1284
0
    goto fail_hwnd;
1285
1286
0
  gdi->primary->hdc->hwnd->invalid->null = TRUE;
1287
0
  gdi->primary->hdc->hwnd->count = 32;
1288
1289
0
  if (!(gdi->primary->hdc->hwnd->cinvalid =
1290
0
            (GDI_RGN*)calloc(gdi->primary->hdc->hwnd->count, sizeof(GDI_RGN))))
1291
0
    goto fail_hwnd;
1292
1293
0
  gdi->primary->hdc->hwnd->ninvalid = 0;
1294
1295
0
  if (!gdi->drawing)
1296
0
    gdi->drawing = gdi->primary;
1297
1298
0
  rdp_update_unlock(gdi->context->update);
1299
0
  return TRUE;
1300
0
fail_hwnd:
1301
0
  gdi_DeleteObject((HGDIOBJECT)gdi->primary->bitmap);
1302
0
fail_bitmap:
1303
0
  gdi_DeleteDC(gdi->primary->hdc);
1304
0
fail_hdc:
1305
0
  free(gdi->primary);
1306
0
  gdi->primary = NULL;
1307
0
fail_primary:
1308
0
  rdp_update_unlock(gdi->context->update);
1309
0
  return FALSE;
1310
0
}
1311
1312
BOOL gdi_resize(rdpGdi* gdi, UINT32 width, UINT32 height)
1313
0
{
1314
0
  return gdi_resize_ex(gdi, width, height, 0, 0, NULL, NULL);
1315
0
}
1316
1317
BOOL gdi_resize_ex(rdpGdi* gdi, UINT32 width, UINT32 height, UINT32 stride, UINT32 format,
1318
                   BYTE* buffer, void (*pfree)(void*))
1319
0
{
1320
0
  if (!gdi || !gdi->primary)
1321
0
    return FALSE;
1322
1323
0
  if ((width > INT32_MAX) || (height > INT32_MAX))
1324
0
    return FALSE;
1325
1326
0
  if ((gdi->width == (INT32)width) && (gdi->height == (INT32)height) &&
1327
0
      (!buffer || (gdi->primary_buffer == buffer)))
1328
0
    return TRUE;
1329
1330
0
  WINPR_ASSERT(gdi->context);
1331
0
  WINPR_ASSERT(gdi->context->update);
1332
1333
  /* EndPaint might not have been called, ensure the update lock is released */
1334
0
  if (!update_end_paint(gdi->context->update))
1335
0
    return FALSE;
1336
0
  rdp_update_lock(gdi->context->update);
1337
1338
0
  if (gdi->drawing == gdi->primary)
1339
0
    gdi->drawing = NULL;
1340
1341
0
  gdi->width = (INT32)width;
1342
0
  gdi->height = (INT32)height;
1343
0
  gdi_bitmap_free_ex(gdi->primary);
1344
0
  gdi->primary = NULL;
1345
0
  gdi->primary_buffer = NULL;
1346
0
  return gdi_init_primary(gdi, stride, format, buffer, pfree, TRUE);
1347
0
}
1348
1349
/**
1350
 * Initialize GDI
1351
 *
1352
 * @param instance A pointer to the instance to use
1353
 * @param format The color format for the local framebuffer
1354
 * @return \b TRUE for success, \b FALSE for failure
1355
 */
1356
BOOL gdi_init(freerdp* instance, UINT32 format)
1357
0
{
1358
0
  return gdi_init_ex(instance, format, 0, NULL, winpr_aligned_free);
1359
0
}
1360
1361
/**
1362
 * Initialize GDI
1363
 *
1364
 * @param instance A pointer to the instance to use
1365
 * @param format The color format for the local framebuffer
1366
 * @param stride The size of a framebuffer line in bytes
1367
 * @param buffer A pointer to a buffer to be used as framebuffer
1368
 * @param pfree A custom function pointer to use to free the framebuffer
1369
 *
1370
 * @return \b TRUE for success, \b FALSE for failure
1371
 */
1372
BOOL gdi_init_ex(freerdp* instance, UINT32 format, UINT32 stride, BYTE* buffer,
1373
                 void (*pfree)(void*))
1374
0
{
1375
0
  rdpContext* context = NULL;
1376
0
  UINT32 SrcFormat = 0;
1377
0
  rdpGdi* gdi = NULL;
1378
1379
0
  WINPR_ASSERT(instance);
1380
1381
0
  context = instance->context;
1382
0
  WINPR_ASSERT(context);
1383
0
  WINPR_ASSERT(context->settings);
1384
1385
0
  const UINT32 ColorDepth = freerdp_settings_get_uint32(context->settings, FreeRDP_ColorDepth);
1386
0
  SrcFormat = gdi_get_pixel_format(ColorDepth);
1387
0
  gdi = (rdpGdi*)calloc(1, sizeof(rdpGdi));
1388
1389
0
  if (!gdi)
1390
0
    goto fail;
1391
1392
0
  context->gdi = gdi;
1393
0
  gdi->log = WLog_Get(TAG);
1394
1395
0
  if (!gdi->log)
1396
0
    goto fail;
1397
1398
0
  gdi->context = context;
1399
0
  gdi->width = WINPR_ASSERTING_INT_CAST(
1400
0
      int32_t, freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopWidth));
1401
0
  gdi->height = WINPR_ASSERTING_INT_CAST(
1402
0
      int32_t, freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopHeight));
1403
0
  gdi->dstFormat = format;
1404
  /* default internal buffer format */
1405
0
  WLog_Print(gdi->log, WLOG_INFO, "Local framebuffer format  %s",
1406
0
             FreeRDPGetColorFormatName(gdi->dstFormat));
1407
0
  WLog_Print(gdi->log, WLOG_INFO, "Remote framebuffer format %s",
1408
0
             FreeRDPGetColorFormatName(SrcFormat));
1409
1410
0
  if (!(gdi->hdc = gdi_GetDC()))
1411
0
    goto fail;
1412
1413
0
  gdi->hdc->format = gdi->dstFormat;
1414
1415
0
  if (!gdi_init_primary(gdi, stride, gdi->dstFormat, buffer, pfree, FALSE))
1416
0
    goto fail;
1417
1418
0
  if (!(context->cache = cache_new(context)))
1419
0
    goto fail;
1420
1421
0
  gdi_register_update_callbacks(context->update);
1422
0
  brush_cache_register_callbacks(context->update);
1423
0
  glyph_cache_register_callbacks(context->update);
1424
0
  bitmap_cache_register_callbacks(context->update);
1425
0
  offscreen_cache_register_callbacks(context->update);
1426
0
  palette_cache_register_callbacks(context->update);
1427
1428
0
  if (!gdi_register_graphics(context->graphics))
1429
0
    goto fail;
1430
1431
0
  return TRUE;
1432
0
fail:
1433
0
  gdi_free(instance);
1434
0
  WLog_ERR(TAG, "failed to initialize gdi");
1435
0
  return FALSE;
1436
0
}
1437
1438
void gdi_free(freerdp* instance)
1439
0
{
1440
0
  rdpGdi* gdi = NULL;
1441
0
  rdpContext* context = NULL;
1442
1443
0
  if (!instance || !instance->context)
1444
0
    return;
1445
1446
0
  gdi = instance->context->gdi;
1447
1448
0
  if (gdi)
1449
0
  {
1450
0
    gdi_bitmap_free_ex(gdi->primary);
1451
0
    gdi_DeleteDC(gdi->hdc);
1452
0
    free(gdi);
1453
0
  }
1454
1455
0
  context = instance->context;
1456
0
  cache_free(context->cache);
1457
0
  context->cache = NULL;
1458
0
  instance->context->gdi = (rdpGdi*)NULL;
1459
0
}
1460
1461
BOOL gdi_send_suppress_output(rdpGdi* gdi, BOOL suppress)
1462
0
{
1463
0
  if (!gdi || !gdi->context)
1464
0
    return FALSE;
1465
1466
0
  if (gdi->suppressOutput == suppress)
1467
0
    return TRUE;
1468
1469
0
  gdi->suppressOutput = suppress;
1470
1471
0
  rdpContext* context = gdi->context;
1472
0
  rdpSettings* settings = context->settings;
1473
0
  WINPR_ASSERT(settings);
1474
1475
0
  rdpUpdate* update = context->update;
1476
0
  WINPR_ASSERT(update);
1477
1478
0
  const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
1479
0
  const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
1480
1481
0
  const RECTANGLE_16 rect = { .left = 0,
1482
0
                            .top = 0,
1483
0
                            .right = WINPR_ASSERTING_INT_CAST(UINT16, w),
1484
0
                            .bottom = WINPR_ASSERTING_INT_CAST(UINT16, h) };
1485
1486
0
  WINPR_ASSERT(update->SuppressOutput);
1487
0
  return update->SuppressOutput(context, !suppress, &rect);
1488
0
}