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