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