Coverage Report

Created: 2024-05-20 06:11

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