FreeRDP
Loading...
Searching...
No Matches
gdi/graphics.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25
26#include <freerdp/log.h>
27#include <freerdp/freerdp.h>
28#include <freerdp/gdi/dc.h>
29#include <freerdp/gdi/shape.h>
30#include <freerdp/gdi/region.h>
31#include <freerdp/gdi/bitmap.h>
32
33#include "clipping.h"
34#include "drawing.h"
35#include "brush.h"
36#include "graphics.h"
37
38#define TAG FREERDP_TAG("gdi")
39/* Bitmap Class */
40
41HGDI_BITMAP gdi_create_bitmap(rdpGdi* gdi, UINT32 nWidth, UINT32 nHeight, UINT32 SrcFormat,
42 BYTE* data)
43{
44 UINT32 nSrcStep = 0;
45 UINT32 nDstStep = 0;
46 BYTE* pSrcData = nullptr;
47 BYTE* pDstData = nullptr;
48 HGDI_BITMAP bitmap = nullptr;
49
50 if (!gdi)
51 return nullptr;
52
53 nDstStep = nWidth * FreeRDPGetBytesPerPixel(gdi->dstFormat);
54 pDstData = winpr_aligned_malloc(1ull * nHeight * nDstStep, 16);
55
56 if (!pDstData)
57 return nullptr;
58
59 pSrcData = data;
60 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
61
62 if (!freerdp_image_copy_no_overlap(pDstData, gdi->dstFormat, nDstStep, 0, 0, nWidth, nHeight,
63 pSrcData, SrcFormat, nSrcStep, 0, 0, &gdi->palette,
64 FREERDP_FLIP_NONE))
65 {
66 winpr_aligned_free(pDstData);
67 return nullptr;
68 }
69
70 bitmap = gdi_CreateBitmap(nWidth, nHeight, gdi->dstFormat, pDstData);
71 if (!bitmap)
72 winpr_aligned_free(pDstData);
73 return bitmap;
74}
75
76static BOOL gdi_Bitmap_New(rdpContext* context, rdpBitmap* bitmap)
77{
78 gdiBitmap* gdi_bitmap = nullptr;
79 rdpGdi* gdi = context->gdi;
80 gdi_bitmap = (gdiBitmap*)bitmap;
81 gdi_bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc);
82
83 if (!gdi_bitmap->hdc)
84 return FALSE;
85
86 if (!bitmap->data)
87 gdi_bitmap->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, bitmap->width, bitmap->height);
88 else
89 {
90 UINT32 format = bitmap->format;
91 gdi_bitmap->bitmap =
92 gdi_create_bitmap(gdi, bitmap->width, bitmap->height, format, bitmap->data);
93 }
94
95 if (!gdi_bitmap->bitmap)
96 {
97 gdi_DeleteDC(gdi_bitmap->hdc);
98 gdi_bitmap->hdc = nullptr;
99 return FALSE;
100 }
101
102 gdi_bitmap->hdc->format = gdi_bitmap->bitmap->format;
103 gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->bitmap);
104 gdi_bitmap->org_bitmap = nullptr;
105 return TRUE;
106}
107
108static void gdi_Bitmap_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpBitmap* bitmap)
109{
110 gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
111
112 if (gdi_bitmap)
113 {
114 if (gdi_bitmap->hdc)
115 gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->org_bitmap);
116
117 gdi_DeleteObject((HGDIOBJECT)gdi_bitmap->bitmap);
118 gdi_DeleteDC(gdi_bitmap->hdc);
119 winpr_aligned_free(bitmap->data);
120 }
121
122 free(bitmap);
123}
124
125static BOOL gdi_Bitmap_Paint(rdpContext* context, rdpBitmap* bitmap)
126{
127 gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
128 UINT32 width = bitmap->right - bitmap->left + 1;
129 UINT32 height = bitmap->bottom - bitmap->top + 1;
130 return gdi_BitBlt(context->gdi->primary->hdc, WINPR_ASSERTING_INT_CAST(int, bitmap->left),
131 WINPR_ASSERTING_INT_CAST(int, bitmap->top),
132 WINPR_ASSERTING_INT_CAST(int, width), WINPR_ASSERTING_INT_CAST(int, height),
133 gdi_bitmap->hdc, 0, 0, GDI_SRCCOPY, &context->gdi->palette);
134}
135
136static BOOL gdi_Bitmap_Decompress(rdpContext* context, rdpBitmap* bitmap, const BYTE* pSrcData,
137 UINT32 DstWidth, UINT32 DstHeight, UINT32 bpp, UINT32 length,
138 BOOL compressed, UINT32 codecId)
139{
140 UINT32 SrcSize = length;
141 rdpGdi* gdi = context->gdi;
142 UINT32 size = DstWidth * DstHeight;
143 bitmap->compressed = FALSE;
144 bitmap->format = gdi->dstFormat;
145
146 if ((FreeRDPGetBytesPerPixel(bitmap->format) == 0) || (DstWidth == 0) || (DstHeight == 0) ||
147 (DstWidth > UINT32_MAX / DstHeight) ||
148 (size > (UINT32_MAX / FreeRDPGetBytesPerPixel(bitmap->format))))
149 {
150 WLog_ERR(TAG, "invalid input data");
151 return FALSE;
152 }
153
154 size *= FreeRDPGetBytesPerPixel(bitmap->format);
155 bitmap->length = size;
156 bitmap->data = (BYTE*)winpr_aligned_malloc(bitmap->length, 16);
157
158 if (!bitmap->data)
159 return FALSE;
160
161 if (compressed)
162 {
163 if ((codecId == RDP_CODEC_ID_REMOTEFX) || (codecId == RDP_CODEC_ID_IMAGE_REMOTEFX))
164 {
165 REGION16 invalidRegion = WINPR_C_ARRAY_INIT;
166 region16_init(&invalidRegion);
167
168 const BOOL rc =
169 rfx_process_message(context->codecs->rfx, pSrcData, SrcSize, bitmap->left,
170 bitmap->top, bitmap->data, bitmap->format, gdi->stride,
171 WINPR_ASSERTING_INT_CAST(UINT32, gdi->height), &invalidRegion);
172 region16_uninit(&invalidRegion);
173
174 if (!rc)
175 {
176 WLog_ERR(TAG, "rfx_process_message failed");
177 return FALSE;
178 }
179 }
180 else if (codecId == RDP_CODEC_ID_NSCODEC)
181 {
182 const int status = nsc_process_message(
183 context->codecs->nsc, 32, DstWidth, DstHeight, pSrcData, SrcSize, bitmap->data,
184 bitmap->format, 0, 0, 0, DstWidth, DstHeight, FREERDP_FLIP_VERTICAL);
185
186 if (status < 1)
187 {
188 WLog_ERR(TAG, "nsc_process_message failed");
189 return FALSE;
190 }
191
192 return freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
193 DstHeight, pSrcData, PIXEL_FORMAT_XRGB32, 0, 0, 0,
194 &gdi->palette, FREERDP_FLIP_VERTICAL);
195 }
196 else if (bpp < 32)
197 {
198 if (!interleaved_decompress(context->codecs->interleaved, pSrcData, SrcSize, DstWidth,
199 DstHeight, bpp, bitmap->data, bitmap->format, 0, 0, 0,
200 DstWidth, DstHeight, &gdi->palette))
201 {
202 WLog_ERR(TAG, "interleaved_decompress failed");
203 return FALSE;
204 }
205 }
206 else
207 {
208 const BOOL fidelity =
209 freerdp_settings_get_bool(context->settings, FreeRDP_DrawAllowDynamicColorFidelity);
210 freerdp_planar_switch_bgr(context->codecs->planar, fidelity);
211 if (!freerdp_bitmap_decompress_planar(context->codecs->planar, pSrcData, SrcSize,
212 DstWidth, DstHeight, bitmap->data, bitmap->format,
213 0, 0, 0, DstWidth, DstHeight, TRUE))
214 {
215 WLog_ERR(TAG, "freerdp_bitmap_decompress_planar failed");
216 return FALSE;
217 }
218 }
219 }
220 else
221 {
222 const UINT32 SrcFormat = gdi_get_pixel_format(bpp);
223 const size_t sbpp = FreeRDPGetBytesPerPixel(SrcFormat);
224 const size_t dbpp = FreeRDPGetBytesPerPixel(bitmap->format);
225
226 if ((sbpp == 0) || (dbpp == 0))
227 return FALSE;
228 else
229 {
230 const size_t dstSize = SrcSize * dbpp / sbpp;
231
232 if (dstSize < bitmap->length)
233 {
234 WLog_ERR(TAG, "dstSize %" PRIuz " < bitmap->length %" PRIu32, dstSize,
235 bitmap->length);
236 return FALSE;
237 }
238 }
239
240 if (!freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
241 DstHeight, pSrcData, SrcFormat, 0, 0, 0, &gdi->palette,
242 FREERDP_FLIP_VERTICAL))
243 {
244 WLog_ERR(TAG, "freerdp_image_copy failed");
245 return FALSE;
246 }
247 }
248
249 return TRUE;
250}
251
252static BOOL gdi_Bitmap_SetSurface(rdpContext* context, rdpBitmap* bitmap, BOOL primary)
253{
254 rdpGdi* gdi = nullptr;
255
256 if (!context)
257 return FALSE;
258
259 gdi = context->gdi;
260
261 if (!gdi)
262 return FALSE;
263
264 if (primary)
265 gdi->drawing = gdi->primary;
266 else
267 gdi->drawing = (gdiBitmap*)bitmap;
268
269 return TRUE;
270}
271
272/* Glyph Class */
273static BOOL gdi_Glyph_New(rdpContext* context, rdpGlyph* glyph)
274{
275 if (!context || !glyph)
276 return FALSE;
277
278 gdiGlyph* gdi_glyph = (gdiGlyph*)glyph;
279 gdi_glyph->hdc = gdi_GetDC();
280
281 if (!gdi_glyph->hdc)
282 return FALSE;
283
284 gdi_glyph->hdc->format = PIXEL_FORMAT_MONO;
285 BYTE* data = freerdp_glyph_convert_ex(glyph->cx, glyph->cy, glyph->aj, glyph->cb);
286
287 if (!data)
288 {
289 gdi_DeleteDC(gdi_glyph->hdc);
290 return FALSE;
291 }
292
293 gdi_glyph->bitmap = gdi_CreateBitmap(glyph->cx, glyph->cy, PIXEL_FORMAT_MONO, data);
294
295 if (!gdi_glyph->bitmap)
296 {
297 gdi_DeleteDC(gdi_glyph->hdc);
298 winpr_aligned_free(data);
299 return FALSE;
300 }
301
302 gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->bitmap);
303 gdi_glyph->org_bitmap = nullptr;
304 return TRUE;
305}
306
307static void gdi_Glyph_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpGlyph* glyph)
308{
309 gdiGlyph* gdi_glyph = nullptr;
310 gdi_glyph = (gdiGlyph*)glyph;
311
312 if (gdi_glyph)
313 {
314 gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->org_bitmap);
315 gdi_DeleteObject((HGDIOBJECT)gdi_glyph->bitmap);
316 gdi_DeleteDC(gdi_glyph->hdc);
317 free(glyph->aj);
318 free(glyph);
319 }
320}
321
322static BOOL gdi_Glyph_Draw(rdpContext* context, const rdpGlyph* glyph, INT32 x, INT32 y, INT32 w,
323 INT32 h, INT32 sx, INT32 sy, BOOL fOpRedundant)
324{
325 const gdiGlyph* gdi_glyph = nullptr;
326 rdpGdi* gdi = nullptr;
327 HGDI_BRUSH brush = nullptr;
328 BOOL rc = FALSE;
329
330 if (!context || !glyph)
331 return FALSE;
332
333 gdi = context->gdi;
334 gdi_glyph = (const gdiGlyph*)glyph;
335
336 if (!fOpRedundant)
337 {
338 GDI_RECT rect = WINPR_C_ARRAY_INIT;
339
340 if (x > 0)
341 rect.left = x;
342
343 if (y > 0)
344 rect.top = y;
345
346 if (x + w > 0)
347 rect.right = x + w - 1;
348
349 if (y + h > 0)
350 rect.bottom = y + h - 1;
351
352 if ((rect.left < rect.right) && (rect.top < rect.bottom))
353 {
354 brush = gdi_CreateSolidBrush(gdi->drawing->hdc->bkColor);
355
356 if (!brush)
357 return FALSE;
358
359 const BOOL res = gdi_FillRect(gdi->drawing->hdc, &rect, brush);
360 gdi_DeleteObject((HGDIOBJECT)brush);
361 if (!res)
362 return res;
363 }
364 }
365
366 brush = gdi_CreateSolidBrush(gdi->drawing->hdc->textColor);
367
368 if (!brush)
369 return FALSE;
370
371 gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)brush);
372 rc = gdi_BitBlt(gdi->drawing->hdc, x, y, w, h, gdi_glyph->hdc, sx, sy, GDI_GLYPH_ORDER,
373 &context->gdi->palette);
374 gdi_DeleteObject((HGDIOBJECT)brush);
375 return rc;
376}
377
378static BOOL gdi_Glyph_BeginDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
379 UINT32 bgcolor, UINT32 fgcolor, BOOL fOpRedundant)
380{
381 if (!context || !context->gdi)
382 return FALSE;
383
384 rdpGdi* gdi = context->gdi;
385
386 if (!gdi->drawing || !gdi->drawing->hdc)
387 return FALSE;
388
389 if (!fOpRedundant)
390 {
391 if (!gdi_decode_color(gdi, bgcolor, &bgcolor, nullptr))
392 return FALSE;
393
394 if (!gdi_decode_color(gdi, fgcolor, &fgcolor, nullptr))
395 return FALSE;
396
397 if (!gdi_SetClipRgn(gdi->drawing->hdc, x, y, width, height))
398 return FALSE;
399
400 gdi_SetTextColor(gdi->drawing->hdc, bgcolor);
401 gdi_SetBkColor(gdi->drawing->hdc, fgcolor);
402
403 {
404 GDI_RECT rect = WINPR_C_ARRAY_INIT;
405 HGDI_BRUSH brush = gdi_CreateSolidBrush(fgcolor);
406
407 if (!brush)
408 return FALSE;
409
410 if (x > 0)
411 rect.left = x;
412
413 if (y > 0)
414 rect.top = y;
415
416 rect.right = x + width - 1;
417 rect.bottom = y + height - 1;
418
419 BOOL res = TRUE;
420 if ((x + width > rect.left) && (y + height > rect.top))
421 res = gdi_FillRect(gdi->drawing->hdc, &rect, brush);
422
423 gdi_DeleteObject((HGDIOBJECT)brush);
424 if (!res)
425 return FALSE;
426 }
427
428 return gdi_SetNullClipRgn(gdi->drawing->hdc);
429 }
430
431 return TRUE;
432}
433
434static BOOL gdi_Glyph_EndDraw(rdpContext* context, WINPR_ATTR_UNUSED INT32 x,
435 WINPR_ATTR_UNUSED INT32 y, WINPR_ATTR_UNUSED INT32 width,
436 WINPR_ATTR_UNUSED INT32 height, WINPR_ATTR_UNUSED UINT32 bgcolor,
437 WINPR_ATTR_UNUSED UINT32 fgcolor)
438{
439 rdpGdi* gdi = nullptr;
440
441 if (!context || !context->gdi)
442 return FALSE;
443
444 gdi = context->gdi;
445
446 if (!gdi->drawing || !gdi->drawing->hdc)
447 return FALSE;
448
449 return gdi_SetNullClipRgn(gdi->drawing->hdc);
450}
451
452/* Graphics Module */
453BOOL gdi_register_graphics(rdpGraphics* graphics)
454{
455 rdpBitmap bitmap = WINPR_C_ARRAY_INIT;
456 rdpGlyph glyph = WINPR_C_ARRAY_INIT;
457 bitmap.size = sizeof(gdiBitmap);
458 bitmap.New = gdi_Bitmap_New;
459 bitmap.Free = gdi_Bitmap_Free;
460 bitmap.Paint = gdi_Bitmap_Paint;
461 bitmap.Decompress = gdi_Bitmap_Decompress;
462 bitmap.SetSurface = gdi_Bitmap_SetSurface;
463 graphics_register_bitmap(graphics, &bitmap);
464 glyph.size = sizeof(gdiGlyph);
465 glyph.New = gdi_Glyph_New;
466 glyph.Free = gdi_Glyph_Free;
467 glyph.Draw = gdi_Glyph_Draw;
468 glyph.BeginDraw = gdi_Glyph_BeginDraw;
469 glyph.EndDraw = gdi_Glyph_EndDraw;
470 graphics_register_glyph(graphics, &glyph);
471 return TRUE;
472}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.