Coverage Report

Created: 2023-09-25 06:56

/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;
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
0
  size_t x;
381
382
0
  for (x = 0; x < count; x++)
383
0
  {
384
0
    if (rop3_code_table[x].code == rop)
385
0
      return rop3_code_table[x].name;
386
0
  }
387
388
0
  return "UNKNOWN";
389
0
}
390
391
UINT32 gdi_get_pixel_format(UINT32 bitsPerPixel)
392
0
{
393
0
  UINT32 format;
394
395
0
  switch (bitsPerPixel)
396
0
  {
397
0
    case 32:
398
0
      format = PIXEL_FORMAT_BGRA32;
399
0
      break;
400
401
0
    case 24:
402
0
      format = PIXEL_FORMAT_BGR24;
403
0
      break;
404
405
0
    case 16:
406
0
      format = PIXEL_FORMAT_RGB16;
407
0
      break;
408
409
0
    case 15:
410
0
      format = PIXEL_FORMAT_RGB15;
411
0
      break;
412
413
0
    case 8:
414
0
      format = PIXEL_FORMAT_RGB8;
415
0
      break;
416
417
0
    default:
418
0
      WLog_ERR(TAG, "Unsupported color depth %" PRIu32, bitsPerPixel);
419
0
      format = 0;
420
0
      break;
421
0
  }
422
423
0
  return format;
424
0
}
425
426
gdiBitmap* gdi_bitmap_new_ex(rdpGdi* gdi, int width, int height, int bpp, BYTE* data)
427
0
{
428
0
  gdiBitmap* bitmap;
429
0
  bitmap = (gdiBitmap*)calloc(1, sizeof(gdiBitmap));
430
431
0
  if (!bitmap)
432
0
    goto fail_bitmap;
433
434
0
  if (!(bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc)))
435
0
    goto fail_hdc;
436
437
0
  WLog_Print(gdi->log, WLOG_DEBUG, "gdi_bitmap_new: width:%d height:%d bpp:%d", width, height,
438
0
             bpp);
439
440
0
  if (!data)
441
0
    bitmap->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, width, height);
442
0
  else
443
0
    bitmap->bitmap = gdi_create_bitmap(gdi, width, height, bpp, data);
444
445
0
  if (!bitmap->bitmap)
446
0
    goto fail_bitmap_bitmap;
447
448
0
  gdi_SelectObject(bitmap->hdc, (HGDIOBJECT)bitmap->bitmap);
449
0
  bitmap->org_bitmap = NULL;
450
0
  return bitmap;
451
0
fail_bitmap_bitmap:
452
0
  gdi_DeleteDC(bitmap->hdc);
453
0
fail_hdc:
454
0
  free(bitmap);
455
0
fail_bitmap:
456
0
  return NULL;
457
0
}
458
459
void gdi_bitmap_free_ex(gdiBitmap* bitmap)
460
0
{
461
0
  if (bitmap)
462
0
  {
463
0
    gdi_SelectObject(bitmap->hdc, (HGDIOBJECT)bitmap->org_bitmap);
464
0
    gdi_DeleteObject((HGDIOBJECT)bitmap->bitmap);
465
0
    gdi_DeleteDC(bitmap->hdc);
466
0
    free(bitmap);
467
0
  }
468
0
}
469
470
BOOL gdi_bitmap_update(rdpContext* context, const BITMAP_UPDATE* bitmapUpdate)
471
0
{
472
0
  UINT32 index;
473
474
0
  if (!context || !bitmapUpdate || !context->gdi || !context->codecs)
475
0
  {
476
0
    WLog_ERR(TAG,
477
0
             "Invalid arguments: context=%p, bitmapUpdate=%p, context->gdi=%p, "
478
0
             "context->codecs=%p",
479
0
             context, bitmapUpdate, context->gdi, context->codecs);
480
0
    return FALSE;
481
0
  }
482
483
0
  for (index = 0; index < bitmapUpdate->number; index++)
484
0
  {
485
0
    const BITMAP_DATA* bitmap = &(bitmapUpdate->rectangles[index]);
486
0
    rdpBitmap* bmp = Bitmap_Alloc(context);
487
488
0
    if (!bmp)
489
0
    {
490
0
      WLog_ERR(TAG, "Bitmap_Alloc failed");
491
0
      return FALSE;
492
0
    }
493
494
0
    Bitmap_SetDimensions(bmp, bitmap->width, bitmap->height);
495
0
    Bitmap_SetRectangle(bmp, bitmap->destLeft, bitmap->destTop, bitmap->destRight,
496
0
                        bitmap->destBottom);
497
498
0
    if (!bmp->Decompress(context, bmp, bitmap->bitmapDataStream, bitmap->width, bitmap->height,
499
0
                         bitmap->bitsPerPixel, bitmap->bitmapLength, bitmap->compressed,
500
0
                         RDP_CODEC_ID_NONE))
501
0
    {
502
0
      WLog_ERR(TAG, "bmp->Decompress failed");
503
0
      Bitmap_Free(context, bmp);
504
0
      return FALSE;
505
0
    }
506
507
0
    if (!bmp->New(context, bmp))
508
0
    {
509
0
      WLog_ERR(TAG, "bmp->New failed");
510
0
      Bitmap_Free(context, bmp);
511
0
      return FALSE;
512
0
    }
513
514
0
    if (!bmp->Paint(context, bmp))
515
0
    {
516
0
      WLog_ERR(TAG, "bmp->Paint failed");
517
0
      Bitmap_Free(context, bmp);
518
0
      return FALSE;
519
0
    }
520
521
0
    Bitmap_Free(context, bmp);
522
0
  }
523
524
0
  return TRUE;
525
0
}
526
527
static BOOL gdi_palette_update(rdpContext* context, const PALETTE_UPDATE* palette)
528
0
{
529
0
  UINT32 index;
530
0
  rdpGdi* gdi;
531
532
0
  if (!context || !palette)
533
0
    return FALSE;
534
535
0
  gdi = context->gdi;
536
0
  gdi->palette.format = gdi->dstFormat;
537
538
0
  for (index = 0; index < palette->number; index++)
539
0
  {
540
0
    const PALETTE_ENTRY* pe = &(palette->entries[index]);
541
0
    gdi->palette.palette[index] =
542
0
        FreeRDPGetColor(gdi->dstFormat, pe->red, pe->green, pe->blue, 0xFF);
543
0
  }
544
545
0
  return TRUE;
546
0
}
547
548
static BOOL gdi_set_bounds(rdpContext* context, const rdpBounds* bounds)
549
0
{
550
0
  rdpGdi* gdi;
551
552
0
  if (!context)
553
0
    return FALSE;
554
555
0
  gdi = context->gdi;
556
557
0
  if (bounds)
558
0
  {
559
0
    gdi_SetClipRgn(gdi->drawing->hdc, bounds->left, bounds->top,
560
0
                   bounds->right - bounds->left + 1, bounds->bottom - bounds->top + 1);
561
0
  }
562
0
  else
563
0
    gdi_SetNullClipRgn(gdi->drawing->hdc);
564
565
0
  return TRUE;
566
0
}
567
568
static BOOL gdi_dstblt(rdpContext* context, const DSTBLT_ORDER* dstblt)
569
0
{
570
0
  rdpGdi* gdi;
571
572
0
  if (!context || !dstblt)
573
0
    return FALSE;
574
575
0
  gdi = context->gdi;
576
0
  return gdi_BitBlt(gdi->drawing->hdc, dstblt->nLeftRect, dstblt->nTopRect, dstblt->nWidth,
577
0
                    dstblt->nHeight, NULL, 0, 0, gdi_rop3_code(dstblt->bRop), &gdi->palette);
578
0
}
579
580
static BOOL gdi_patblt(rdpContext* context, PATBLT_ORDER* patblt)
581
0
{
582
0
  const rdpBrush* brush = &patblt->brush;
583
0
  UINT32 foreColor;
584
0
  UINT32 backColor;
585
0
  UINT32 originalColor;
586
0
  HGDI_BRUSH originalBrush, hbrush = NULL;
587
0
  rdpGdi* gdi = context->gdi;
588
0
  BOOL ret = FALSE;
589
0
  const DWORD rop = gdi_rop3_code(patblt->bRop);
590
0
  INT32 nXSrc = 0;
591
0
  INT32 nYSrc = 0;
592
0
  BYTE data[8 * 8 * 4];
593
0
  HGDI_BITMAP hBmp = NULL;
594
595
0
  if (!gdi_decode_color(gdi, patblt->foreColor, &foreColor, NULL))
596
0
    return FALSE;
597
598
0
  if (!gdi_decode_color(gdi, patblt->backColor, &backColor, NULL))
599
0
    return FALSE;
600
601
0
  originalColor = gdi_SetTextColor(gdi->drawing->hdc, foreColor);
602
0
  originalBrush = gdi->drawing->hdc->brush;
603
604
0
  switch (brush->style)
605
0
  {
606
0
    case GDI_BS_SOLID:
607
0
      hbrush = gdi_CreateSolidBrush(foreColor);
608
0
      break;
609
610
0
    case GDI_BS_HATCHED:
611
0
    {
612
0
      const BYTE* hatched;
613
0
      hatched = GDI_BS_HATCHED_PATTERNS + (8 * brush->hatch);
614
615
0
      if (!freerdp_image_copy_from_monochrome(data, gdi->drawing->hdc->format, 0, 0, 0, 8, 8,
616
0
                                              hatched, backColor, foreColor, &gdi->palette))
617
0
        goto out_error;
618
619
0
      hBmp = gdi_CreateBitmapEx(8, 8, gdi->drawing->hdc->format, 0, data, NULL);
620
621
0
      if (!hBmp)
622
0
        goto out_error;
623
624
0
      hbrush = gdi_CreateHatchBrush(hBmp);
625
0
    }
626
0
    break;
627
628
0
    case GDI_BS_PATTERN:
629
0
    {
630
0
      UINT32 brushFormat;
631
632
0
      if (brush->bpp > 1)
633
0
      {
634
0
        UINT32 bpp = brush->bpp;
635
636
0
        if ((bpp == 16) &&
637
0
            (freerdp_settings_get_uint32(context->settings, FreeRDP_ColorDepth) == 15))
638
0
          bpp = 15;
639
640
0
        brushFormat = gdi_get_pixel_format(bpp);
641
642
0
        if (!freerdp_image_copy(data, gdi->drawing->hdc->format, 0, 0, 0, 8, 8, brush->data,
643
0
                                brushFormat, 0, 0, 0, &gdi->palette, FREERDP_FLIP_NONE))
644
0
          goto out_error;
645
0
      }
646
0
      else
647
0
      {
648
0
        if (!freerdp_image_copy_from_monochrome(data, gdi->drawing->hdc->format, 0, 0, 0, 8,
649
0
                                                8, brush->data, backColor, foreColor,
650
0
                                                &gdi->palette))
651
0
          goto out_error;
652
0
      }
653
654
0
      hBmp = gdi_CreateBitmapEx(8, 8, gdi->drawing->hdc->format, 0, data, NULL);
655
656
0
      if (!hBmp)
657
0
        goto out_error;
658
659
0
      hbrush = gdi_CreatePatternBrush(hBmp);
660
0
    }
661
0
    break;
662
663
0
    default:
664
0
      WLog_ERR(TAG, "unimplemented brush style:%" PRIu32 "", brush->style);
665
0
      break;
666
0
  }
667
668
0
  if (hbrush)
669
0
  {
670
0
    hbrush->nXOrg = brush->x;
671
0
    hbrush->nYOrg = brush->y;
672
0
    gdi->drawing->hdc->brush = hbrush;
673
0
    ret = gdi_BitBlt(gdi->drawing->hdc, patblt->nLeftRect, patblt->nTopRect, patblt->nWidth,
674
0
                     patblt->nHeight, gdi->primary->hdc, nXSrc, nYSrc, rop, &gdi->palette);
675
0
  }
676
677
0
out_error:
678
0
  gdi_DeleteObject((HGDIOBJECT)hBmp);
679
0
  gdi_DeleteObject((HGDIOBJECT)hbrush);
680
0
  gdi->drawing->hdc->brush = originalBrush;
681
0
  gdi_SetTextColor(gdi->drawing->hdc, originalColor);
682
0
  return ret;
683
0
}
684
685
static BOOL gdi_scrblt(rdpContext* context, const SCRBLT_ORDER* scrblt)
686
0
{
687
0
  rdpGdi* gdi;
688
689
0
  if (!context || !context->gdi)
690
0
    return FALSE;
691
692
0
  gdi = context->gdi;
693
0
  return gdi_BitBlt(gdi->drawing->hdc, scrblt->nLeftRect, scrblt->nTopRect, scrblt->nWidth,
694
0
                    scrblt->nHeight, gdi->primary->hdc, scrblt->nXSrc, scrblt->nYSrc,
695
0
                    gdi_rop3_code(scrblt->bRop), &gdi->palette);
696
0
}
697
698
static BOOL gdi_opaque_rect(rdpContext* context, const OPAQUE_RECT_ORDER* opaque_rect)
699
0
{
700
0
  GDI_RECT rect;
701
0
  HGDI_BRUSH hBrush;
702
0
  UINT32 brush_color;
703
0
  rdpGdi* gdi = context->gdi;
704
0
  BOOL ret;
705
0
  INT32 x = opaque_rect->nLeftRect;
706
0
  INT32 y = opaque_rect->nTopRect;
707
0
  INT32 w = opaque_rect->nWidth;
708
0
  INT32 h = opaque_rect->nHeight;
709
0
  gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
710
0
  gdi_CRgnToRect(x, y, w, h, &rect);
711
712
0
  if (!gdi_decode_color(gdi, opaque_rect->color, &brush_color, NULL))
713
0
    return FALSE;
714
715
0
  if (!(hBrush = gdi_CreateSolidBrush(brush_color)))
716
0
    return FALSE;
717
718
0
  ret = gdi_FillRect(gdi->drawing->hdc, &rect, hBrush);
719
0
  gdi_DeleteObject((HGDIOBJECT)hBrush);
720
0
  return ret;
721
0
}
722
723
static BOOL gdi_multi_opaque_rect(rdpContext* context,
724
                                  const MULTI_OPAQUE_RECT_ORDER* multi_opaque_rect)
725
0
{
726
0
  UINT32 i;
727
0
  GDI_RECT rect;
728
0
  HGDI_BRUSH hBrush;
729
0
  UINT32 brush_color;
730
0
  rdpGdi* gdi = context->gdi;
731
0
  BOOL ret = TRUE;
732
733
0
  if (!gdi_decode_color(gdi, multi_opaque_rect->color, &brush_color, NULL))
734
0
    return FALSE;
735
736
0
  hBrush = gdi_CreateSolidBrush(brush_color);
737
738
0
  if (!hBrush)
739
0
    return FALSE;
740
741
0
  for (i = 0; i < multi_opaque_rect->numRectangles; i++)
742
0
  {
743
0
    const DELTA_RECT* rectangle = &multi_opaque_rect->rectangles[i];
744
0
    INT32 x = rectangle->left;
745
0
    INT32 y = rectangle->top;
746
0
    INT32 w = rectangle->width;
747
0
    INT32 h = rectangle->height;
748
0
    gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
749
0
    gdi_CRgnToRect(x, y, w, h, &rect);
750
0
    ret = gdi_FillRect(gdi->drawing->hdc, &rect, hBrush);
751
752
0
    if (!ret)
753
0
      break;
754
0
  }
755
756
0
  gdi_DeleteObject((HGDIOBJECT)hBrush);
757
0
  return ret;
758
0
}
759
760
static BOOL gdi_line_to(rdpContext* context, const LINE_TO_ORDER* lineTo)
761
0
{
762
0
  UINT32 color;
763
0
  HGDI_PEN hPen;
764
0
  rdpGdi* gdi = context->gdi;
765
0
  INT32 xStart = lineTo->nXStart;
766
0
  INT32 yStart = lineTo->nYStart;
767
0
  INT32 xEnd = lineTo->nXEnd;
768
0
  INT32 yEnd = lineTo->nYEnd;
769
0
  INT32 w = 0;
770
0
  INT32 h = 0;
771
0
  gdi_ClipCoords(gdi->drawing->hdc, &xStart, &yStart, &w, &h, NULL, NULL);
772
0
  gdi_ClipCoords(gdi->drawing->hdc, &xEnd, &yEnd, &w, &h, NULL, NULL);
773
774
0
  if (!gdi_decode_color(gdi, lineTo->penColor, &color, NULL))
775
0
    return FALSE;
776
777
0
  if (!(hPen = gdi_CreatePen(lineTo->penStyle, lineTo->penWidth, color, gdi->drawing->hdc->format,
778
0
                             &gdi->palette)))
779
0
    return FALSE;
780
781
0
  gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)hPen);
782
0
  gdi_SetROP2(gdi->drawing->hdc, lineTo->bRop2);
783
0
  gdi_MoveToEx(gdi->drawing->hdc, lineTo->nXStart, lineTo->nYStart, NULL);
784
0
  gdi_LineTo(gdi->drawing->hdc, lineTo->nXEnd, lineTo->nYEnd);
785
0
  gdi_DeleteObject((HGDIOBJECT)hPen);
786
0
  return TRUE;
787
0
}
788
789
static BOOL gdi_polyline(rdpContext* context, const POLYLINE_ORDER* polyline)
790
0
{
791
0
  UINT32 i;
792
0
  INT32 x;
793
0
  INT32 y;
794
0
  UINT32 color;
795
0
  HGDI_PEN hPen;
796
0
  DELTA_POINT* points;
797
0
  rdpGdi* gdi = context->gdi;
798
0
  INT32 w = 0, h = 0;
799
800
0
  if (!gdi_decode_color(gdi, polyline->penColor, &color, NULL))
801
0
    return FALSE;
802
803
0
  if (!(hPen = gdi_CreatePen(GDI_PS_SOLID, 1, color, gdi->drawing->hdc->format, &gdi->palette)))
804
0
    return FALSE;
805
806
0
  gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)hPen);
807
0
  gdi_SetROP2(gdi->drawing->hdc, polyline->bRop2);
808
0
  x = polyline->xStart;
809
0
  y = polyline->yStart;
810
0
  gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
811
0
  gdi_MoveToEx(gdi->drawing->hdc, x, y, NULL);
812
0
  points = polyline->points;
813
814
0
  for (i = 0; i < polyline->numDeltaEntries; i++)
815
0
  {
816
0
    x += points[i].x;
817
0
    y += points[i].y;
818
0
    gdi_ClipCoords(gdi->drawing->hdc, &x, &y, &w, &h, NULL, NULL);
819
0
    gdi_LineTo(gdi->drawing->hdc, x, y);
820
0
    gdi_MoveToEx(gdi->drawing->hdc, x, y, NULL);
821
0
  }
822
823
0
  gdi_DeleteObject((HGDIOBJECT)hPen);
824
0
  return TRUE;
825
0
}
826
827
static BOOL gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt)
828
0
{
829
0
  gdiBitmap* bitmap;
830
0
  rdpGdi* gdi;
831
832
0
  if (!context || !memblt || !context->gdi || !memblt->bitmap)
833
0
    return FALSE;
834
835
0
  bitmap = (gdiBitmap*)memblt->bitmap;
836
0
  gdi = context->gdi;
837
0
  return gdi_BitBlt(gdi->drawing->hdc, memblt->nLeftRect, memblt->nTopRect, memblt->nWidth,
838
0
                    memblt->nHeight, bitmap->hdc, memblt->nXSrc, memblt->nYSrc,
839
0
                    gdi_rop3_code(memblt->bRop), &gdi->palette);
840
0
}
841
842
static BOOL gdi_mem3blt(rdpContext* context, MEM3BLT_ORDER* mem3blt)
843
0
{
844
0
  HGDI_BRUSH originalBrush;
845
0
  rdpGdi* gdi = context->gdi;
846
0
  BOOL ret = TRUE;
847
0
  const rdpBrush* brush = &mem3blt->brush;
848
0
  gdiBitmap* bitmap = (gdiBitmap*)mem3blt->bitmap;
849
0
  UINT32 foreColor;
850
0
  UINT32 backColor;
851
0
  UINT32 originalColor;
852
853
0
  if (!gdi_decode_color(gdi, mem3blt->foreColor, &foreColor, NULL))
854
0
    return FALSE;
855
856
0
  if (!gdi_decode_color(gdi, mem3blt->backColor, &backColor, NULL))
857
0
    return FALSE;
858
859
0
  originalColor = gdi_SetTextColor(gdi->drawing->hdc, foreColor);
860
861
0
  switch (brush->style)
862
0
  {
863
0
    case GDI_BS_SOLID:
864
0
      originalBrush = gdi->drawing->hdc->brush;
865
0
      gdi->drawing->hdc->brush = gdi_CreateSolidBrush(foreColor);
866
867
0
      if (!gdi->drawing->hdc->brush)
868
0
      {
869
0
        ret = FALSE;
870
0
        goto out_fail;
871
0
      }
872
873
0
      ret = gdi_BitBlt(gdi->drawing->hdc, mem3blt->nLeftRect, mem3blt->nTopRect,
874
0
                       mem3blt->nWidth, mem3blt->nHeight, bitmap->hdc, mem3blt->nXSrc,
875
0
                       mem3blt->nYSrc, gdi_rop3_code(mem3blt->bRop), &gdi->palette);
876
0
      gdi_DeleteObject((HGDIOBJECT)gdi->drawing->hdc->brush);
877
0
      gdi->drawing->hdc->brush = originalBrush;
878
0
      break;
879
880
0
    case GDI_BS_PATTERN:
881
0
    {
882
0
      HGDI_BITMAP hBmp;
883
0
      UINT32 brushFormat;
884
0
      BYTE* data = (BYTE*)winpr_aligned_malloc(
885
0
          8 * 8 * FreeRDPGetBytesPerPixel(gdi->drawing->hdc->format), 16);
886
887
0
      if (!data)
888
0
      {
889
0
        ret = FALSE;
890
0
        goto out_fail;
891
0
      }
892
893
0
      if (brush->bpp > 1)
894
0
      {
895
0
        UINT32 bpp = brush->bpp;
896
897
0
        const UINT32 ColorDepth =
898
0
            freerdp_settings_get_uint32(gdi->context->settings, FreeRDP_ColorDepth);
899
0
        if ((bpp == 16) && (ColorDepth == 15))
900
0
          bpp = 15;
901
902
0
        brushFormat = gdi_get_pixel_format(bpp);
903
904
0
        if (!freerdp_image_copy(data, gdi->drawing->hdc->format, 0, 0, 0, 8, 8, brush->data,
905
0
                                brushFormat, 0, 0, 0, &gdi->palette, FREERDP_FLIP_NONE))
906
0
        {
907
0
          ret = FALSE;
908
0
          winpr_aligned_free(data);
909
0
          goto out_fail;
910
0
        }
911
0
      }
912
0
      else
913
0
      {
914
0
        if (!freerdp_image_copy_from_monochrome(data, gdi->drawing->hdc->format, 0, 0, 0, 8,
915
0
                                                8, brush->data, backColor, foreColor,
916
0
                                                &gdi->palette))
917
0
        {
918
0
          ret = FALSE;
919
0
          winpr_aligned_free(data);
920
0
          goto out_fail;
921
0
        }
922
0
      }
923
924
0
      hBmp = gdi_CreateBitmap(8, 8, gdi->drawing->hdc->format, data);
925
926
0
      if (!hBmp)
927
0
      {
928
0
        ret = FALSE;
929
0
        winpr_aligned_free(data);
930
0
        goto out_fail;
931
0
      }
932
933
0
      originalBrush = gdi->drawing->hdc->brush;
934
0
      gdi->drawing->hdc->brush = gdi_CreatePatternBrush(hBmp);
935
936
0
      if (!gdi->drawing->hdc->brush)
937
0
      {
938
0
        gdi_DeleteObject((HGDIOBJECT)hBmp);
939
0
        goto out_fail;
940
0
      }
941
942
0
      gdi->drawing->hdc->brush->nXOrg = brush->x;
943
0
      gdi->drawing->hdc->brush->nYOrg = brush->y;
944
0
      ret = gdi_BitBlt(gdi->drawing->hdc, mem3blt->nLeftRect, mem3blt->nTopRect,
945
0
                       mem3blt->nWidth, mem3blt->nHeight, bitmap->hdc, mem3blt->nXSrc,
946
0
                       mem3blt->nYSrc, gdi_rop3_code(mem3blt->bRop), &gdi->palette);
947
0
      gdi_DeleteObject((HGDIOBJECT)gdi->drawing->hdc->brush);
948
0
      gdi_DeleteObject((HGDIOBJECT)hBmp);
949
0
      gdi->drawing->hdc->brush = originalBrush;
950
0
    }
951
0
    break;
952
953
0
    default:
954
0
      WLog_ERR(TAG, "Mem3Blt unimplemented brush style:%" PRIu32 "", brush->style);
955
0
      break;
956
0
  }
957
958
0
out_fail:
959
0
  gdi_SetTextColor(gdi->drawing->hdc, originalColor);
960
0
  return ret;
961
0
}
962
963
static BOOL gdi_polygon_sc(rdpContext* context, const POLYGON_SC_ORDER* polygon_sc)
964
0
{
965
0
  WLog_WARN(TAG, "not implemented");
966
0
  return FALSE;
967
0
}
968
969
static BOOL gdi_polygon_cb(rdpContext* context, POLYGON_CB_ORDER* polygon_cb)
970
0
{
971
0
  WLog_WARN(TAG, "not implemented");
972
0
  return FALSE;
973
0
}
974
975
static BOOL gdi_ellipse_sc(rdpContext* context, const ELLIPSE_SC_ORDER* ellipse_sc)
976
0
{
977
0
  WLog_WARN(TAG, "not implemented");
978
0
  return FALSE;
979
0
}
980
981
static BOOL gdi_ellipse_cb(rdpContext* context, const ELLIPSE_CB_ORDER* ellipse_cb)
982
0
{
983
0
  WLog_WARN(TAG, "not implemented");
984
0
  return FALSE;
985
0
}
986
987
static BOOL gdi_frame_marker(rdpContext* context, const FRAME_MARKER_ORDER* frameMarker)
988
0
{
989
0
  return TRUE;
990
0
}
991
992
static BOOL gdi_surface_frame_marker(rdpContext* context,
993
                                     const SURFACE_FRAME_MARKER* surfaceFrameMarker)
994
0
{
995
0
  WLog_Print(context->gdi->log, WLOG_DEBUG, "frameId %" PRIu32 " frameAction %" PRIu32 "",
996
0
             surfaceFrameMarker->frameId, surfaceFrameMarker->frameAction);
997
998
0
  switch (surfaceFrameMarker->frameAction)
999
0
  {
1000
0
    case SURFACECMD_FRAMEACTION_BEGIN:
1001
0
      break;
1002
1003
0
    case SURFACECMD_FRAMEACTION_END:
1004
0
      if (freerdp_settings_get_uint32(context->settings, FreeRDP_FrameAcknowledge) > 0)
1005
0
      {
1006
0
        IFCALL(context->update->SurfaceFrameAcknowledge, context,
1007
0
               surfaceFrameMarker->frameId);
1008
0
      }
1009
1010
0
      break;
1011
0
  }
1012
1013
0
  return TRUE;
1014
0
}
1015
1016
static BOOL intersect_rect(const rdpGdi* gdi, const SURFACE_BITS_COMMAND* cmd, RECTANGLE_16* prect)
1017
0
{
1018
0
  const UINT32 w = (const UINT32)gdi->width;
1019
0
  const UINT32 h = (const UINT32)gdi->height;
1020
1021
0
  if (cmd->destLeft > w)
1022
0
    return FALSE;
1023
0
  if (cmd->destRight > w)
1024
0
    return FALSE;
1025
0
  if (cmd->destLeft > cmd->destRight)
1026
0
    return FALSE;
1027
0
  if (cmd->destRight > UINT16_MAX)
1028
0
    return FALSE;
1029
1030
0
  if (cmd->destTop > h)
1031
0
    return FALSE;
1032
0
  if (cmd->destBottom > h)
1033
0
    return FALSE;
1034
0
  if (cmd->destTop > cmd->destBottom)
1035
0
    return FALSE;
1036
0
  if (cmd->destBottom > UINT16_MAX)
1037
0
    return FALSE;
1038
1039
0
  prect->left = (const UINT16)cmd->destLeft;
1040
0
  prect->top = (const UINT16)cmd->destTop;
1041
0
  prect->right = MIN((UINT16)cmd->destRight, prect->left + cmd->bmp.width);
1042
0
  prect->bottom = MIN((UINT16)cmd->destBottom, prect->top + cmd->bmp.height);
1043
0
  return TRUE;
1044
0
}
1045
1046
static BOOL gdi_surface_bits(rdpContext* context, const SURFACE_BITS_COMMAND* cmd)
1047
0
{
1048
0
  BOOL result = FALSE;
1049
0
  DWORD format;
1050
0
  rdpGdi* gdi = NULL;
1051
0
  size_t size;
1052
0
  REGION16 region;
1053
0
  RECTANGLE_16 cmdRect = { 0 };
1054
0
  UINT32 nbRects;
1055
0
  const RECTANGLE_16* rects = NULL;
1056
1057
0
  if (!context || !cmd)
1058
0
    return FALSE;
1059
1060
0
  gdi = context->gdi;
1061
0
  WLog_Print(
1062
0
      gdi->log, WLOG_DEBUG,
1063
0
      "destLeft %" PRIu32 " destTop %" PRIu32 " destRight %" PRIu32 " destBottom %" PRIu32 " "
1064
0
      "bpp %" PRIu8 " flags %" PRIx8 " codecID %" PRIu16 " width %" PRIu16 " height %" PRIu16
1065
0
      " length %" PRIu32 "",
1066
0
      cmd->destLeft, cmd->destTop, cmd->destRight, cmd->destBottom, cmd->bmp.bpp, cmd->bmp.flags,
1067
0
      cmd->bmp.codecID, cmd->bmp.width, cmd->bmp.height, cmd->bmp.bitmapDataLength);
1068
0
  region16_init(&region);
1069
1070
0
  if (!intersect_rect(gdi, cmd, &cmdRect))
1071
0
    goto out;
1072
1073
0
  switch (cmd->bmp.codecID)
1074
0
  {
1075
0
    case RDP_CODEC_ID_REMOTEFX:
1076
0
    case RDP_CODEC_ID_IMAGE_REMOTEFX:
1077
0
      if (!rfx_process_message(context->codecs->rfx, cmd->bmp.bitmapData,
1078
0
                               cmd->bmp.bitmapDataLength, cmdRect.left, cmdRect.top,
1079
0
                               gdi->primary_buffer, gdi->dstFormat, gdi->stride, gdi->height,
1080
0
                               &region))
1081
0
      {
1082
0
        WLog_ERR(TAG, "Failed to process RemoteFX message");
1083
0
        goto out;
1084
0
      }
1085
1086
0
      break;
1087
1088
0
    case RDP_CODEC_ID_NSCODEC:
1089
0
      format = gdi->dstFormat;
1090
1091
0
      if (!nsc_process_message(
1092
0
              context->codecs->nsc, cmd->bmp.bpp, cmd->bmp.width, cmd->bmp.height,
1093
0
              cmd->bmp.bitmapData, cmd->bmp.bitmapDataLength, gdi->primary_buffer, format,
1094
0
              gdi->stride, cmdRect.left, cmdRect.top, cmdRect.right - cmdRect.left,
1095
0
              cmdRect.bottom - cmdRect.top, FREERDP_FLIP_VERTICAL))
1096
0
      {
1097
0
        WLog_ERR(TAG, "Failed to process NSCodec message");
1098
0
        goto out;
1099
0
      }
1100
1101
0
      region16_union_rect(&region, &region, &cmdRect);
1102
0
      break;
1103
1104
0
    case RDP_CODEC_ID_NONE:
1105
0
      format = gdi_get_pixel_format(cmd->bmp.bpp);
1106
0
      size = 1ull * cmd->bmp.width * cmd->bmp.height * FreeRDPGetBytesPerPixel(format);
1107
0
      if (size > cmd->bmp.bitmapDataLength)
1108
0
      {
1109
0
        WLog_ERR(TAG, "Short nocodec message: got %" PRIu32 " bytes, require %" PRIuz,
1110
0
                 cmd->bmp.bitmapDataLength, size);
1111
0
        goto out;
1112
0
      }
1113
1114
0
      if (!freerdp_image_copy(gdi->primary_buffer, gdi->dstFormat, gdi->stride, cmdRect.left,
1115
0
                              cmdRect.top, cmdRect.right - cmdRect.left,
1116
0
                              cmdRect.bottom - cmdRect.top, cmd->bmp.bitmapData, format, 0, 0,
1117
0
                              0, &gdi->palette, FREERDP_FLIP_VERTICAL))
1118
0
      {
1119
0
        WLog_ERR(TAG, "Failed to process nocodec message");
1120
0
        goto out;
1121
0
      }
1122
1123
0
      region16_union_rect(&region, &region, &cmdRect);
1124
0
      break;
1125
1126
0
    default:
1127
0
      WLog_ERR(TAG, "Unsupported codecID %" PRIu32 "", cmd->bmp.codecID);
1128
0
      break;
1129
0
  }
1130
1131
0
  if (!(rects = region16_rects(&region, &nbRects)))
1132
0
    goto out;
1133
1134
0
  for (UINT32 i = 0; i < nbRects; i++)
1135
0
  {
1136
0
    UINT32 left = rects[i].left;
1137
0
    UINT32 top = rects[i].top;
1138
0
    UINT32 width = rects[i].right - rects[i].left;
1139
0
    UINT32 height = rects[i].bottom - rects[i].top;
1140
1141
0
    if (!gdi_InvalidateRegion(gdi->primary->hdc, left, top, width, height))
1142
0
    {
1143
0
      WLog_ERR(TAG, "Failed to update invalid region");
1144
0
      goto out;
1145
0
    }
1146
0
  }
1147
1148
0
  result = TRUE;
1149
0
out:
1150
0
  region16_uninit(&region);
1151
0
  return result;
1152
0
}
1153
1154
/**
1155
 * Register GDI callbacks with libfreerdp-core.
1156
 * @param update current instance
1157
 */
1158
1159
static void gdi_register_update_callbacks(rdpUpdate* update)
1160
0
{
1161
0
  rdpPrimaryUpdate* primary;
1162
0
  const rdpSettings* settings;
1163
1164
0
  WINPR_ASSERT(update);
1165
0
  WINPR_ASSERT(update->context);
1166
1167
0
  settings = update->context->settings;
1168
0
  WINPR_ASSERT(settings);
1169
1170
0
  primary = update->primary;
1171
0
  WINPR_ASSERT(primary);
1172
1173
0
  if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
1174
0
    return;
1175
0
  update->Palette = gdi_palette_update;
1176
0
  update->SetBounds = gdi_set_bounds;
1177
0
  primary->DstBlt = gdi_dstblt;
1178
0
  primary->PatBlt = gdi_patblt;
1179
0
  primary->ScrBlt = gdi_scrblt;
1180
0
  primary->OpaqueRect = gdi_opaque_rect;
1181
0
  primary->DrawNineGrid = NULL;
1182
0
  primary->MultiDstBlt = NULL;
1183
0
  primary->MultiPatBlt = NULL;
1184
0
  primary->MultiScrBlt = NULL;
1185
0
  primary->MultiOpaqueRect = gdi_multi_opaque_rect;
1186
0
  primary->MultiDrawNineGrid = NULL;
1187
0
  primary->LineTo = gdi_line_to;
1188
0
  primary->Polyline = gdi_polyline;
1189
0
  primary->MemBlt = gdi_memblt;
1190
0
  primary->Mem3Blt = gdi_mem3blt;
1191
0
  primary->SaveBitmap = NULL;
1192
0
  primary->GlyphIndex = NULL;
1193
0
  primary->FastIndex = NULL;
1194
0
  primary->FastGlyph = NULL;
1195
0
  primary->PolygonSC = gdi_polygon_sc;
1196
0
  primary->PolygonCB = gdi_polygon_cb;
1197
0
  primary->EllipseSC = gdi_ellipse_sc;
1198
0
  primary->EllipseCB = gdi_ellipse_cb;
1199
0
  update->SurfaceBits = gdi_surface_bits;
1200
0
  update->SurfaceFrameMarker = gdi_surface_frame_marker;
1201
0
  update->altsec->FrameMarker = gdi_frame_marker;
1202
0
}
1203
1204
static BOOL gdi_init_primary(rdpGdi* gdi, UINT32 stride, UINT32 format, BYTE* buffer,
1205
                             void (*pfree)(void*), BOOL isLocked)
1206
0
{
1207
0
  WINPR_ASSERT(gdi);
1208
0
  WINPR_ASSERT(gdi->context);
1209
0
  WINPR_ASSERT(gdi->context->update);
1210
0
  if (!isLocked)
1211
0
    rdp_update_lock(gdi->context->update);
1212
1213
0
  gdi->primary = (gdiBitmap*)calloc(1, sizeof(gdiBitmap));
1214
1215
0
  if (format > 0)
1216
0
    gdi->dstFormat = format;
1217
1218
0
  if (stride > 0)
1219
0
    gdi->stride = stride;
1220
0
  else
1221
0
    gdi->stride = gdi->width * FreeRDPGetBytesPerPixel(gdi->dstFormat);
1222
1223
0
  if (!gdi->primary)
1224
0
    goto fail_primary;
1225
1226
0
  if (!(gdi->primary->hdc = gdi_CreateCompatibleDC(gdi->hdc)))
1227
0
    goto fail_hdc;
1228
1229
0
  if (!buffer)
1230
0
  {
1231
0
    gdi->primary->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, gdi->width, gdi->height);
1232
0
  }
1233
0
  else
1234
0
  {
1235
0
    gdi->primary->bitmap =
1236
0
        gdi_CreateBitmapEx(gdi->width, gdi->height, gdi->dstFormat, gdi->stride, buffer, pfree);
1237
0
  }
1238
1239
0
  if (!gdi->primary->bitmap)
1240
0
    goto fail_bitmap;
1241
1242
0
  gdi->stride = gdi->primary->bitmap->scanline;
1243
0
  gdi_SelectObject(gdi->primary->hdc, (HGDIOBJECT)gdi->primary->bitmap);
1244
0
  gdi->primary->org_bitmap = NULL;
1245
0
  gdi->primary_buffer = gdi->primary->bitmap->data;
1246
1247
0
  if (!(gdi->primary->hdc->hwnd = (HGDI_WND)calloc(1, sizeof(GDI_WND))))
1248
0
    goto fail_hwnd;
1249
1250
0
  if (!(gdi->primary->hdc->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0)))
1251
0
    goto fail_hwnd;
1252
1253
0
  gdi->primary->hdc->hwnd->invalid->null = TRUE;
1254
0
  gdi->primary->hdc->hwnd->count = 32;
1255
1256
0
  if (!(gdi->primary->hdc->hwnd->cinvalid =
1257
0
            (HGDI_RGN)calloc(gdi->primary->hdc->hwnd->count, sizeof(GDI_RGN))))
1258
0
    goto fail_hwnd;
1259
1260
0
  gdi->primary->hdc->hwnd->ninvalid = 0;
1261
1262
0
  if (!gdi->drawing)
1263
0
    gdi->drawing = gdi->primary;
1264
1265
0
  rdp_update_unlock(gdi->context->update);
1266
0
  return TRUE;
1267
0
fail_hwnd:
1268
0
  gdi_DeleteObject((HGDIOBJECT)gdi->primary->bitmap);
1269
0
fail_bitmap:
1270
0
  gdi_DeleteDC(gdi->primary->hdc);
1271
0
fail_hdc:
1272
0
  free(gdi->primary);
1273
0
  gdi->primary = NULL;
1274
0
fail_primary:
1275
0
  rdp_update_unlock(gdi->context->update);
1276
0
  return FALSE;
1277
0
}
1278
1279
BOOL gdi_resize(rdpGdi* gdi, UINT32 width, UINT32 height)
1280
0
{
1281
0
  return gdi_resize_ex(gdi, width, height, 0, 0, NULL, NULL);
1282
0
}
1283
1284
BOOL gdi_resize_ex(rdpGdi* gdi, UINT32 width, UINT32 height, UINT32 stride, UINT32 format,
1285
                   BYTE* buffer, void (*pfree)(void*))
1286
0
{
1287
0
  if (!gdi || !gdi->primary)
1288
0
    return FALSE;
1289
1290
0
  if ((width > INT32_MAX) || (height > INT32_MAX))
1291
0
    return FALSE;
1292
1293
0
  if ((gdi->width == (INT32)width) && (gdi->height == (INT32)height) &&
1294
0
      (!buffer || (gdi->primary_buffer == buffer)))
1295
0
    return TRUE;
1296
1297
0
  WINPR_ASSERT(gdi->context);
1298
0
  WINPR_ASSERT(gdi->context->update);
1299
0
  rdp_update_lock(gdi->context->update);
1300
1301
0
  if (gdi->drawing == gdi->primary)
1302
0
    gdi->drawing = NULL;
1303
1304
0
  gdi->width = (INT32)width;
1305
0
  gdi->height = (INT32)height;
1306
0
  gdi_bitmap_free_ex(gdi->primary);
1307
0
  gdi->primary = NULL;
1308
0
  gdi->primary_buffer = NULL;
1309
0
  return gdi_init_primary(gdi, stride, format, buffer, pfree, TRUE);
1310
0
}
1311
1312
/**
1313
 * Initialize GDI
1314
 *
1315
 * @param instance A pointer to the instance to use
1316
 * @param format The color format for the local framebuffer
1317
 * @return \b TRUE for success, \b FALSE for failure
1318
 */
1319
BOOL gdi_init(freerdp* instance, UINT32 format)
1320
0
{
1321
0
  return gdi_init_ex(instance, format, 0, NULL, winpr_aligned_free);
1322
0
}
1323
1324
/**
1325
 * Initialize GDI
1326
 *
1327
 * @param instance A pointer to the instance to use
1328
 * @param format The color format for the local framebuffer
1329
 * @param stride The size of a framebuffer line in bytes
1330
 * @param buffer A pointer to a buffer to be used as framebuffer
1331
 * @param pfree A custom function pointer to use to free the framebuffer
1332
 *
1333
 * @return \b TRUE for success, \b FALSE for failure
1334
 */
1335
BOOL gdi_init_ex(freerdp* instance, UINT32 format, UINT32 stride, BYTE* buffer,
1336
                 void (*pfree)(void*))
1337
0
{
1338
0
  rdpContext* context;
1339
0
  UINT32 SrcFormat;
1340
0
  rdpGdi* gdi;
1341
1342
0
  WINPR_ASSERT(instance);
1343
1344
0
  context = instance->context;
1345
0
  WINPR_ASSERT(context);
1346
0
  WINPR_ASSERT(context->settings);
1347
1348
0
  const UINT32 ColorDepth = freerdp_settings_get_uint32(context->settings, FreeRDP_ColorDepth);
1349
0
  SrcFormat = gdi_get_pixel_format(ColorDepth);
1350
0
  gdi = (rdpGdi*)calloc(1, sizeof(rdpGdi));
1351
1352
0
  if (!gdi)
1353
0
    goto fail;
1354
1355
0
  context->gdi = gdi;
1356
0
  gdi->log = WLog_Get(TAG);
1357
1358
0
  if (!gdi->log)
1359
0
    goto fail;
1360
1361
0
  gdi->context = context;
1362
0
  gdi->width = freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopWidth);
1363
0
  gdi->height = freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopHeight);
1364
0
  gdi->dstFormat = format;
1365
  /* default internal buffer format */
1366
0
  WLog_Print(gdi->log, WLOG_INFO, "Local framebuffer format  %s",
1367
0
             FreeRDPGetColorFormatName(gdi->dstFormat));
1368
0
  WLog_Print(gdi->log, WLOG_INFO, "Remote framebuffer format %s",
1369
0
             FreeRDPGetColorFormatName(SrcFormat));
1370
1371
0
  if (!(gdi->hdc = gdi_GetDC()))
1372
0
    goto fail;
1373
1374
0
  gdi->hdc->format = gdi->dstFormat;
1375
1376
0
  if (!gdi_init_primary(gdi, stride, gdi->dstFormat, buffer, pfree, FALSE))
1377
0
    goto fail;
1378
1379
0
  if (!(context->cache = cache_new(context)))
1380
0
    goto fail;
1381
1382
0
  gdi_register_update_callbacks(context->update);
1383
0
  brush_cache_register_callbacks(context->update);
1384
0
  glyph_cache_register_callbacks(context->update);
1385
0
  bitmap_cache_register_callbacks(context->update);
1386
0
  offscreen_cache_register_callbacks(context->update);
1387
0
  palette_cache_register_callbacks(context->update);
1388
1389
0
  if (!gdi_register_graphics(context->graphics))
1390
0
    goto fail;
1391
1392
0
  return TRUE;
1393
0
fail:
1394
0
  gdi_free(instance);
1395
0
  WLog_ERR(TAG, "failed to initialize gdi");
1396
0
  return FALSE;
1397
0
}
1398
1399
void gdi_free(freerdp* instance)
1400
0
{
1401
0
  rdpGdi* gdi;
1402
0
  rdpContext* context;
1403
1404
0
  if (!instance || !instance->context)
1405
0
    return;
1406
1407
0
  gdi = instance->context->gdi;
1408
1409
0
  if (gdi)
1410
0
  {
1411
0
    gdi_bitmap_free_ex(gdi->primary);
1412
0
    gdi_DeleteDC(gdi->hdc);
1413
0
    free(gdi);
1414
0
  }
1415
1416
0
  context = instance->context;
1417
0
  cache_free(context->cache);
1418
0
  context->cache = NULL;
1419
0
  instance->context->gdi = (rdpGdi*)NULL;
1420
0
}
1421
1422
BOOL gdi_send_suppress_output(rdpGdi* gdi, BOOL suppress)
1423
0
{
1424
0
  RECTANGLE_16 rect;
1425
0
  rdpSettings* settings;
1426
0
  rdpUpdate* update;
1427
1428
0
  if (!gdi || !gdi->context->settings || !gdi->context->update)
1429
0
    return FALSE;
1430
1431
0
  if (gdi->suppressOutput == suppress)
1432
0
    return TRUE;
1433
1434
0
  gdi->suppressOutput = suppress;
1435
0
  settings = gdi->context->settings;
1436
0
  update = gdi->context->update;
1437
0
  rect.left = 0;
1438
0
  rect.top = 0;
1439
0
  rect.right = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
1440
0
  rect.bottom = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
1441
0
  return update->SuppressOutput(gdi->context, !suppress, &rect);
1442
0
}