FreeRDP
Loading...
Searching...
No Matches
gdi/gfx.c
1
22#include <freerdp/config.h>
23
24#include "../core/update.h"
25
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28
29#include <freerdp/api.h>
30#include <freerdp/log.h>
31#include <freerdp/gdi/gfx.h>
32#include <freerdp/gdi/region.h>
33#include <freerdp/utils/gfx.h>
34#include <math.h>
35
36#define TAG FREERDP_TAG("gdi")
37
38WINPR_ATTR_NODISCARD
39static BOOL is_rect_valid(const RECTANGLE_16* rect, size_t width, size_t height)
40{
41 if (!rect)
42 return FALSE;
43 if ((rect->left > rect->right) || (rect->right > width))
44 return FALSE;
45 if ((rect->top > rect->bottom) || (rect->bottom > height))
46 return FALSE;
47 return TRUE;
48}
49
50WINPR_ATTR_NODISCARD
51static BOOL is_within_surface(const gdiGfxSurface* surface, const RDPGFX_SURFACE_COMMAND* cmd)
52{
53 RECTANGLE_16 rect;
54 if (!surface || !cmd)
55 return FALSE;
56 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
57 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
58 rect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
59 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
60 if (!is_rect_valid(&rect, surface->width, surface->height))
61 {
62 WLog_ERR(TAG,
63 "Command rect %" PRIu32 "x%" PRIu32 "-%" PRIu32 "x%" PRIu32
64 " not within bounds of %" PRIu32 "x%" PRIu32,
65 rect.left, rect.top, cmd->width, cmd->height, surface->width, surface->height);
66 return FALSE;
67 }
68 if (rect.left > surface->width)
69 return FALSE;
70 if (rect.right > surface->width)
71 return FALSE;
72 if (rect.top > surface->height)
73 return FALSE;
74 if (rect.bottom > surface->height)
75 return FALSE;
76
77 return TRUE;
78}
79
80WINPR_ATTR_NODISCARD
81static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
82{
83 const UINT32 align = alignment;
84 const UINT32 pad = align - (widthInBytes % alignment);
85 UINT32 scanline = widthInBytes;
86
87 if (align != pad)
88 scanline += pad;
89
90 return scanline;
91}
92
98WINPR_ATTR_NODISCARD
99static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
100 const RDPGFX_RESET_GRAPHICS_PDU* resetGraphics)
101{
102 UINT rc = ERROR_INTERNAL_ERROR;
103 UINT16 count = 0;
104 UINT16* pSurfaceIds = nullptr;
105
106 WINPR_ASSERT(context);
107 WINPR_ASSERT(resetGraphics);
108
109 rdpGdi* gdi = (rdpGdi*)context->custom;
110 WINPR_ASSERT(gdi);
111
112 rdpUpdate* update = gdi->context->update;
113 WINPR_ASSERT(update);
114
115 rdpSettings* settings = gdi->context->settings;
116 WINPR_ASSERT(settings);
117 EnterCriticalSection(&context->mux);
118 const UINT32 DesktopWidth = resetGraphics->width;
119 const UINT32 DesktopHeight = resetGraphics->height;
120
121 WLog_DBG(TAG, "new size: %" PRIu32 "x%" PRIu32, DesktopWidth, DesktopHeight);
122
123 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, DesktopWidth))
124 goto fail;
125 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, DesktopHeight))
126 goto fail;
127
128 if (update)
129 {
130 WINPR_ASSERT(update->DesktopResize);
131 if (!update->DesktopResize(gdi->context))
132 goto fail;
133 }
134
135 WINPR_ASSERT(context->GetSurfaceIds);
136 const UINT res = context->GetSurfaceIds(context, &pSurfaceIds, &count);
137 if (res != CHANNEL_RC_OK)
138 {
139 rc = res;
140 goto fail;
141 }
142
143 for (UINT32 index = 0; index < count; index++)
144 {
145 WINPR_ASSERT(context->GetSurfaceData);
146 gdiGfxSurface* surface =
147 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
148
149 if (!surface)
150 continue;
151
152 memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);
153 region16_clear(&surface->invalidRegion);
154 }
155
156 free(pSurfaceIds);
157
158 if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
159 {
160 const UINT32 width = (UINT32)MAX(0, gdi->width);
161 const UINT32 height = (UINT32)MAX(0, gdi->height);
162
163 if (!freerdp_client_codecs_reset(
164 context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))
165 {
166 goto fail;
167 }
168 }
169
170 rc = CHANNEL_RC_OK;
171fail:
172 LeaveCriticalSection(&context->mux);
173 return rc;
174}
175
176WINPR_ATTR_NODISCARD
177static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
178{
179 UINT rc = ERROR_INTERNAL_ERROR;
180 UINT32 surfaceX = 0;
181 UINT32 surfaceY = 0;
182 RECTANGLE_16 surfaceRect;
183 const RECTANGLE_16* rects = nullptr;
184 UINT32 nbRects = 0;
185 rdpUpdate* update = nullptr;
186
187 WINPR_ASSERT(gdi);
188 WINPR_ASSERT(gdi->context);
189 WINPR_ASSERT(surface);
190
191 update = gdi->context->update;
192 WINPR_ASSERT(update);
193
194 if (gdi->suppressOutput)
195 return CHANNEL_RC_OK;
196
197 surfaceX = surface->outputOriginX;
198 surfaceY = surface->outputOriginY;
199 surfaceRect.left = 0;
200 surfaceRect.top = 0;
201 surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
202 surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
203 if (!region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion),
204 &surfaceRect))
205 goto fail;
206
207 const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
208 const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
209
210 rects = region16_rects(&surface->invalidRegion, &nbRects);
211 if (!rects || (nbRects == 0))
212 {
213 rc = CHANNEL_RC_OK;
214 goto fail;
215 }
216
217 if (!update_begin_paint(update))
218 goto fail;
219
220 for (UINT32 i = 0; i < nbRects; i++)
221 {
222 const UINT32 nXSrc = rects[i].left;
223 const UINT32 nYSrc = rects[i].top;
224 const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
225 const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
226 const UINT32 swidth = rects[i].right - rects[i].left;
227 const UINT32 sheight = rects[i].bottom - rects[i].top;
228 const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
229 const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
230
231 if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
232 dwidth, dheight, surface->data, surface->format, surface->scanline,
233 nXSrc, nYSrc, swidth, sheight))
234 {
235 rc = CHANNEL_RC_NULL_DATA;
236 goto clear;
237 }
238
239 if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
240 (INT32)dheight))
241 goto clear;
242 }
243
244 rc = CHANNEL_RC_OK;
245clear:
246
247 if (!update_end_paint(update))
248 rc = ERROR_INTERNAL_ERROR;
249
250fail:
251 region16_clear(&(surface->invalidRegion));
252 return rc;
253}
254
255WINPR_ATTR_NODISCARD
256static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
257{
258 WINPR_ASSERT(context);
259 WINPR_ASSERT(surface);
260 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
261}
262
263WINPR_ATTR_NODISCARD
264static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
265{
266 UINT16 count = 0;
267 UINT16* pSurfaceIds = nullptr;
268
269 WINPR_ASSERT(context);
270
271 rdpGdi* gdi = (rdpGdi*)context->custom;
272 WINPR_ASSERT(gdi);
273
274 EnterCriticalSection(&context->mux);
275
276 WINPR_ASSERT(context->GetSurfaceIds);
277 UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
278 if (status != CHANNEL_RC_OK)
279 goto fail;
280
281 for (UINT32 index = 0; index < count; index++)
282 {
283 WINPR_ASSERT(context->GetSurfaceData);
284 gdiGfxSurface* surface =
285 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
286
287 if (!surface)
288 continue;
289
290 /* Already handled in UpdateSurfaceArea callbacks */
291 if (context->UpdateSurfaceArea)
292 {
293 if (surface->handleInUpdateSurfaceArea)
294 continue;
295 }
296
297 if (surface->outputMapped)
298 status = gdi_OutputUpdate(gdi, surface);
299 else if (surface->windowMapped)
300 status = gdi_WindowUpdate(context, surface);
301
302 if (status != CHANNEL_RC_OK)
303 break;
304 }
305
306fail:
307 free(pSurfaceIds);
308 LeaveCriticalSection(&context->mux);
309 return status;
310}
311
317WINPR_ATTR_NODISCARD
318static UINT gdi_StartFrame(RdpgfxClientContext* context, const RDPGFX_START_FRAME_PDU* startFrame)
319{
320 rdpGdi* gdi = nullptr;
321
322 WINPR_ASSERT(context);
323 WINPR_ASSERT(startFrame);
324
325 gdi = (rdpGdi*)context->custom;
326 WINPR_ASSERT(gdi);
327 gdi->inGfxFrame = TRUE;
328 gdi->frameId = startFrame->frameId;
329 return CHANNEL_RC_OK;
330}
331
332WINPR_ATTR_NODISCARD
333static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
334{
335 WINPR_ASSERT(context);
336 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
337}
338
344WINPR_ATTR_NODISCARD
345static UINT gdi_EndFrame(RdpgfxClientContext* context,
346 WINPR_ATTR_UNUSED const RDPGFX_END_FRAME_PDU* endFrame)
347{
348 WINPR_ASSERT(context);
349 WINPR_ASSERT(endFrame);
350
351 rdpGdi* gdi = (rdpGdi*)context->custom;
352 WINPR_ASSERT(gdi);
353 const UINT status = gdi_call_update_surfaces(context);
354 gdi->inGfxFrame = FALSE;
355 return status;
356}
357
358WINPR_ATTR_NODISCARD
359static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
360{
361 WINPR_ASSERT(gdi);
362 UINT status = CHANNEL_RC_OK;
363 if (!gdi->inGfxFrame)
364 status = gdi_call_update_surfaces(context);
365 return status;
366}
367
373WINPR_ATTR_NODISCARD
374static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
375 const RDPGFX_SURFACE_COMMAND* cmd)
376{
377 UINT status = CHANNEL_RC_OK;
378 gdiGfxSurface* surface = nullptr;
379 RECTANGLE_16 invalidRect;
380 DWORD bpp = 0;
381 size_t size = 0;
382 WINPR_ASSERT(gdi);
383 WINPR_ASSERT(context);
384 WINPR_ASSERT(cmd);
385
386 WINPR_ASSERT(context->GetSurfaceData);
387 surface =
388 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
389
390 if (!surface)
391 {
392 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
393 return ERROR_NOT_FOUND;
394 }
395
396 if (!is_within_surface(surface, cmd))
397 return ERROR_INVALID_DATA;
398
399 bpp = FreeRDPGetBytesPerPixel(cmd->format);
400 size = 1ull * bpp * cmd->width * cmd->height;
401 if (cmd->length < size)
402 {
403 WLog_ERR(TAG, "Not enough data, got %" PRIu32 ", expected %" PRIuz, cmd->length, size);
404 return ERROR_INVALID_DATA;
405 }
406
407 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
408 cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
409 0, 0, nullptr, FREERDP_FLIP_NONE))
410 return ERROR_INTERNAL_ERROR;
411
412 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
413 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
414 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
415 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
416 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
417 goto fail;
418 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
419 &invalidRect);
420
421 if (status != CHANNEL_RC_OK)
422 goto fail;
423
424 status = gdi_interFrameUpdate(gdi, context);
425
426fail:
427 return status;
428}
429
430#if defined(WITH_GFX_AV1)
431WINPR_ATTR_NODISCARD
432static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
433 const RDPGFX_SURFACE_COMMAND* cmd)
434{
435 INT32 rc = 0;
436 UINT status = CHANNEL_RC_OK;
437 gdiGfxSurface* surface = nullptr;
438 RDPGFX_H264_METABLOCK* meta = nullptr;
439 RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
440 WINPR_ASSERT(gdi);
441 WINPR_ASSERT(context);
442 WINPR_ASSERT(cmd);
443
444 WINPR_ASSERT(context->GetSurfaceData);
445 surface =
446 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
447
448 if (!surface)
449 {
450 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
451 return ERROR_NOT_FOUND;
452 }
453
454 if (!surface->av1)
455 {
456 surface->av1 = freerdp_av1_context_new(FALSE);
457
458 if (!surface->av1)
459 {
460 WLog_ERR(TAG, "unable to create av1 context");
461 return ERROR_NOT_ENOUGH_MEMORY;
462 }
463
464 if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
465 return ERROR_INTERNAL_ERROR;
466 }
467
468 if (!surface->av1)
469 return ERROR_NOT_SUPPORTED;
470
471 if (!is_within_surface(surface, cmd))
472 return ERROR_INVALID_DATA;
473
474 bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
475
476 if (!bs)
477 return ERROR_INTERNAL_ERROR;
478
479 meta = &(bs->meta);
480 rc = freerdp_av1_decompress(surface->av1, bs->data, bs->length, surface->data, surface->format,
481 surface->scanline, surface->width, surface->height,
482 meta->regionRects, meta->numRegionRects);
483
484 if (rc < 0)
485 {
486 WLog_WARN(TAG, "freerdp_av1_decompress failure: %" PRId32 ", ignoring update.", rc);
487 return CHANNEL_RC_OK;
488 }
489
490 for (UINT32 i = 0; i < meta->numRegionRects; i++)
491 {
492 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
493 &(meta->regionRects[i])))
494 goto fail;
495 }
496
497 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
498 meta->numRegionRects, meta->regionRects);
499
500 if (status != CHANNEL_RC_OK)
501 goto fail;
502
503 status = gdi_interFrameUpdate(gdi, context);
504
505fail:
506 return status;
507}
508#endif
509
515WINPR_ATTR_NODISCARD
516static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
517 const RDPGFX_SURFACE_COMMAND* cmd)
518{
519 UINT status = ERROR_INTERNAL_ERROR;
520 gdiGfxSurface* surface = nullptr;
521 REGION16 invalidRegion;
522 const RECTANGLE_16* rects = nullptr;
523 UINT32 nrRects = 0;
524 WINPR_ASSERT(gdi);
525 WINPR_ASSERT(context);
526 WINPR_ASSERT(cmd);
527
528 WINPR_ASSERT(context->GetSurfaceData);
529 surface =
530 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
531
532 if (!surface)
533 {
534 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
535 return ERROR_NOT_FOUND;
536 }
537
538 if (!is_within_surface(surface, cmd))
539 return ERROR_INVALID_DATA;
540
541 WINPR_ASSERT(surface->codecs);
542 rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
543 region16_init(&invalidRegion);
544
545 if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
546 surface->data, surface->format, surface->scanline, surface->height,
547 &invalidRegion))
548 {
549 WLog_ERR(TAG, "Failed to process RemoteFX message");
550 goto fail;
551 }
552
553 rects = region16_rects(&invalidRegion, &nrRects);
554 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
555 nrRects, rects);
556
557 if (status != CHANNEL_RC_OK)
558 goto fail;
559
560 for (UINT32 x = 0; x < nrRects; x++)
561 {
562 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
563 goto fail;
564 }
565
566 status = gdi_interFrameUpdate(gdi, context);
567
568fail:
569 region16_uninit(&invalidRegion);
570 return status;
571}
572
578WINPR_ATTR_NODISCARD
579static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
580 const RDPGFX_SURFACE_COMMAND* cmd)
581{
582 INT32 rc = 0;
583 UINT status = CHANNEL_RC_OK;
584 gdiGfxSurface* surface = nullptr;
585 RECTANGLE_16 invalidRect;
586 WINPR_ASSERT(gdi);
587 WINPR_ASSERT(context);
588 WINPR_ASSERT(cmd);
589
590 WINPR_ASSERT(context->GetSurfaceData);
591 surface =
592 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
593
594 if (!surface)
595 {
596 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
597 return ERROR_NOT_FOUND;
598 }
599
600 if (!is_within_surface(surface, cmd))
601 return ERROR_INVALID_DATA;
602
603 WINPR_ASSERT(surface->codecs);
604 rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
605 surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
606 surface->width, surface->height, &gdi->palette);
607
608 if (rc < 0)
609 {
610 WLog_ERR(TAG, "clear_decompress failure: %" PRId32 "", rc);
611 return ERROR_INTERNAL_ERROR;
612 }
613
614 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
615 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
616 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
617 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
618 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
619 goto fail;
620 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
621 &invalidRect);
622
623 if (status != CHANNEL_RC_OK)
624 goto fail;
625
626 status = gdi_interFrameUpdate(gdi, context);
627
628fail:
629 return status;
630}
631
637WINPR_ATTR_NODISCARD
638static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
639 const RDPGFX_SURFACE_COMMAND* cmd)
640{
641 UINT status = CHANNEL_RC_OK;
642 BYTE* DstData = nullptr;
643 gdiGfxSurface* surface = nullptr;
644 RECTANGLE_16 invalidRect;
645 WINPR_ASSERT(gdi);
646 WINPR_ASSERT(context);
647 WINPR_ASSERT(cmd);
648
649 WINPR_ASSERT(context->GetSurfaceData);
650 surface =
651 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
652
653 if (!surface)
654 {
655 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
656 return ERROR_NOT_FOUND;
657 }
658
659 DstData = surface->data;
660
661 if (!is_within_surface(surface, cmd))
662 return ERROR_INVALID_DATA;
663
664 if (!freerdp_bitmap_decompress_planar(surface->codecs->planar, cmd->data, cmd->length,
665 cmd->width, cmd->height, DstData, surface->format,
666 surface->scanline, cmd->left, cmd->top, cmd->width,
667 cmd->height, FALSE))
668 return ERROR_INTERNAL_ERROR;
669
670 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
671 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
672 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
673 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
674 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
675 goto fail;
676 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
677 &invalidRect);
678
679 if (status != CHANNEL_RC_OK)
680 goto fail;
681
682 status = gdi_interFrameUpdate(gdi, context);
683
684fail:
685 return status;
686}
687
693WINPR_ATTR_NODISCARD
694static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
695 const RDPGFX_SURFACE_COMMAND* cmd)
696{
697#ifdef WITH_GFX_H264
698 INT32 rc = 0;
699 UINT status = CHANNEL_RC_OK;
700 gdiGfxSurface* surface = nullptr;
701 RDPGFX_H264_METABLOCK* meta = nullptr;
702 RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
703 WINPR_ASSERT(gdi);
704 WINPR_ASSERT(context);
705 WINPR_ASSERT(cmd);
706
707 WINPR_ASSERT(context->GetSurfaceData);
708 surface =
709 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
710
711 if (!surface)
712 {
713 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
714 return ERROR_NOT_FOUND;
715 }
716
717 if (!surface->h264)
718 {
719 surface->h264 = h264_context_new(FALSE);
720
721 if (!surface->h264)
722 {
723 WLog_ERR(TAG, "unable to create h264 context");
724 return ERROR_NOT_ENOUGH_MEMORY;
725 }
726
727 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
728 (UINT32)freerdp_settings_get_bool(gdi->context->settings,
729 FreeRDP_SoftwareGdi) == 0))
730 return ERROR_INTERNAL_ERROR;
731
732 if (!h264_context_reset(surface->h264, surface->width, surface->height))
733 return ERROR_INTERNAL_ERROR;
734 }
735
736 if (!surface->h264)
737 return ERROR_NOT_SUPPORTED;
738
739 if (!is_within_surface(surface, cmd))
740 return ERROR_INVALID_DATA;
741
742 bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
743
744 if (!bs)
745 return ERROR_INTERNAL_ERROR;
746
747 meta = &(bs->meta);
748 rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
749 surface->scanline, surface->width, surface->height, meta->regionRects,
750 meta->numRegionRects);
751
752 if (rc < 0)
753 {
754 WLog_WARN(TAG, "avc420_decompress failure: %" PRId32 ", ignoring update.", rc);
755 return CHANNEL_RC_OK;
756 }
757
758 for (UINT32 i = 0; i < meta->numRegionRects; i++)
759 {
760 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
761 &(meta->regionRects[i])))
762 goto fail;
763 }
764
765 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
766 meta->numRegionRects, meta->regionRects);
767
768 if (status != CHANNEL_RC_OK)
769 goto fail;
770
771 status = gdi_interFrameUpdate(gdi, context);
772
773fail:
774 return status;
775#else
776 return ERROR_NOT_SUPPORTED;
777#endif
778}
779
785WINPR_ATTR_NODISCARD
786static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
787 const RDPGFX_SURFACE_COMMAND* cmd)
788{
789#ifdef WITH_GFX_H264
790 INT32 rc = 0;
791 UINT status = CHANNEL_RC_OK;
792 gdiGfxSurface* surface = nullptr;
793 RDPGFX_AVC444_BITMAP_STREAM* bs = nullptr;
794 RDPGFX_AVC420_BITMAP_STREAM* avc1 = nullptr;
795 RDPGFX_H264_METABLOCK* meta1 = nullptr;
796 RDPGFX_AVC420_BITMAP_STREAM* avc2 = nullptr;
797 RDPGFX_H264_METABLOCK* meta2 = nullptr;
798 WINPR_ASSERT(gdi);
799 WINPR_ASSERT(context);
800 WINPR_ASSERT(cmd);
801
802 WINPR_ASSERT(context->GetSurfaceData);
803 surface =
804 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
805
806 if (!surface)
807 {
808 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
809 return ERROR_NOT_FOUND;
810 }
811
812 if (!surface->h264)
813 {
814 surface->h264 = h264_context_new(FALSE);
815
816 if (!surface->h264)
817 {
818 WLog_ERR(TAG, "unable to create h264 context");
819 return ERROR_NOT_ENOUGH_MEMORY;
820 }
821
822 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
823 (UINT32)freerdp_settings_get_bool(gdi->context->settings,
824 FreeRDP_SoftwareGdi) == 0))
825 return ERROR_INTERNAL_ERROR;
826 if (!h264_context_reset(surface->h264, surface->width, surface->height))
827 return ERROR_INTERNAL_ERROR;
828 }
829
830 if (!surface->h264)
831 return ERROR_NOT_SUPPORTED;
832
833 if (!is_within_surface(surface, cmd))
834 return ERROR_INVALID_DATA;
835
836 bs = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
837
838 if (!bs)
839 return ERROR_INTERNAL_ERROR;
840
841 avc1 = &bs->bitstream[0];
842 avc2 = &bs->bitstream[1];
843 meta1 = &avc1->meta;
844 meta2 = &avc2->meta;
845 rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
846 avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
847 avc2->data, avc2->length, surface->data, surface->format,
848 surface->scanline, surface->width, surface->height, cmd->codecId);
849
850 if (rc < 0)
851 {
852 WLog_WARN(TAG, "avc444_decompress failure: %" PRId32 ", ignoring update.", rc);
853 return CHANNEL_RC_OK;
854 }
855
856 for (UINT32 i = 0; i < meta1->numRegionRects; i++)
857 {
858 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
859 &(meta1->regionRects[i])))
860 goto fail;
861 }
862
863 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
864 meta1->numRegionRects, meta1->regionRects);
865
866 if (status != CHANNEL_RC_OK)
867 goto fail;
868
869 for (UINT32 i = 0; i < meta2->numRegionRects; i++)
870 {
871 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
872 &(meta2->regionRects[i])))
873 goto fail;
874 }
875
876 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
877 meta2->numRegionRects, meta2->regionRects);
878
879 if (status != CHANNEL_RC_OK)
880 goto fail;
881
882 status = gdi_interFrameUpdate(gdi, context);
883
884fail:
885 return status;
886#else
887 return ERROR_NOT_SUPPORTED;
888#endif
889}
890
891WINPR_ATTR_NODISCARD
892static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride, RECTANGLE_16* rect,
893 UINT32 startOffsetX, UINT32 count, BYTE a)
894{
895 UINT32 written = 0;
896 BOOL first = TRUE;
897 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
898 WINPR_ASSERT(rect);
899
900 for (size_t y = rect->top; y < rect->bottom; y++)
901 {
902 BYTE* line = &data[y * stride];
903
904 for (size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
905 {
906 BYTE r = 0;
907 BYTE g = 0;
908 BYTE b = 0;
909
910 if (written == count)
911 return TRUE;
912
913 BYTE* src = &line[x * bpp];
914 UINT32 color = FreeRDPReadColor(src, format);
915 FreeRDPSplitColor(color, format, &r, &g, &b, nullptr, nullptr);
916 color = FreeRDPGetColor(format, r, g, b, a);
917 FreeRDPWriteColor(src, format, color);
918 written++;
919 }
920
921 first = FALSE;
922 }
923
924 return TRUE;
925}
931WINPR_ATTR_NODISCARD
932static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
933 const RDPGFX_SURFACE_COMMAND* cmd)
934{
935 UINT status = CHANNEL_RC_OK;
936 UINT16 alphaSig = 0;
937 UINT16 compressed = 0;
938 RECTANGLE_16 invalidRect;
939 wStream buffer = WINPR_C_ARRAY_INIT;
940 WINPR_ASSERT(gdi);
941 WINPR_ASSERT(context);
942 WINPR_ASSERT(cmd);
943
944 wStream* s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
945
946 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
947 return ERROR_INVALID_DATA;
948
949 WINPR_ASSERT(context->GetSurfaceData);
950 gdiGfxSurface* surface =
951 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
952
953 if (!surface)
954 {
955 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
956 return ERROR_NOT_FOUND;
957 }
958
959 if (!is_within_surface(surface, cmd))
960 return ERROR_INVALID_DATA;
961
962 Stream_Read_UINT16(s, alphaSig);
963 Stream_Read_UINT16(s, compressed);
964
965 if (alphaSig != 0x414C)
966 return ERROR_INVALID_DATA;
967
968 if (compressed == 0)
969 {
970 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
971 return ERROR_INVALID_DATA;
972
973 for (size_t y = cmd->top; y < cmd->top + cmd->height; y++)
974 {
975 BYTE* line = &surface->data[y * surface->scanline];
976
977 for (size_t x = cmd->left; x < cmd->left + cmd->width; x++)
978 {
979 UINT32 color = 0;
980 BYTE r = 0;
981 BYTE g = 0;
982 BYTE b = 0;
983 BYTE a = 0;
984 BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
985 Stream_Read_UINT8(s, a);
986 color = FreeRDPReadColor(src, surface->format);
987 FreeRDPSplitColor(color, surface->format, &r, &g, &b, nullptr, nullptr);
988 color = FreeRDPGetColor(surface->format, r, g, b, a);
989 FreeRDPWriteColor(src, surface->format, color);
990 }
991 }
992 }
993 else
994 {
995 UINT32 startOffsetX = 0;
996 RECTANGLE_16 rect = WINPR_C_ARRAY_INIT;
997 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
998 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
999 rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);
1000 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);
1001
1002 while (rect.top < rect.bottom)
1003 {
1004 UINT32 count = 0;
1005 BYTE a = 0;
1006
1007 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1008 return ERROR_INVALID_DATA;
1009
1010 Stream_Read_UINT8(s, a);
1011 Stream_Read_UINT8(s, count);
1012
1013 if (count >= 0xFF)
1014 {
1015 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1016 return ERROR_INVALID_DATA;
1017
1018 Stream_Read_UINT16(s, count);
1019
1020 if (count >= 0xFFFF)
1021 {
1022 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1023 return ERROR_INVALID_DATA;
1024
1025 Stream_Read_UINT32(s, count);
1026 }
1027 }
1028
1029 if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1030 startOffsetX, count, a))
1031 return ERROR_INTERNAL_ERROR;
1032
1033 startOffsetX += count;
1034
1035 while (startOffsetX >= cmd->width)
1036 {
1037 startOffsetX -= cmd->width;
1038 rect.top++;
1039 }
1040 }
1041 }
1042
1043 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1044 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1045 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
1046 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
1047 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
1048 goto fail;
1049 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1050 &invalidRect);
1051
1052 if (status != CHANNEL_RC_OK)
1053 goto fail;
1054
1055 status = gdi_interFrameUpdate(gdi, context);
1056
1057fail:
1058 return status;
1059}
1060
1061#if defined(WITH_GFX_FRAME_DUMP)
1062static void dump_cmd(const RDPGFX_SURFACE_COMMAND* cmd, UINT32 frameId)
1063{
1064 static UINT64 xxx = 0;
1065 const char* path = "/tmp/dump/";
1066 WINPR_ASSERT(cmd);
1067 char fname[1024] = WINPR_C_ARRAY_INIT;
1068
1069 if (_snprintf(fname, sizeof(fname), "%s/%08" PRIx64 ".raw", path, xxx++) < 0)
1070 return;
1071
1072 FILE* fp = fopen(fname, "w");
1073 if (!fp)
1074 return;
1075 (void)fprintf(fp, "frameid: %" PRIu32 "\n", frameId);
1076 (void)fprintf(fp, "surfaceId: %" PRIu32 "\n", cmd->surfaceId);
1077 (void)fprintf(fp, "codecId: %" PRIu32 "\n", cmd->codecId);
1078 (void)fprintf(fp, "contextId: %" PRIu32 "\n", cmd->contextId);
1079 (void)fprintf(fp, "format: %" PRIu32 "\n", cmd->format);
1080 (void)fprintf(fp, "left: %" PRIu32 "\n", cmd->left);
1081 (void)fprintf(fp, "top: %" PRIu32 "\n", cmd->top);
1082 (void)fprintf(fp, "right: %" PRIu32 "\n", cmd->right);
1083 (void)fprintf(fp, "bottom: %" PRIu32 "\n", cmd->bottom);
1084 (void)fprintf(fp, "width: %" PRIu32 "\n", cmd->width);
1085 (void)fprintf(fp, "height: %" PRIu32 "\n", cmd->height);
1086 (void)fprintf(fp, "length: %" PRIu32 "\n", cmd->length);
1087
1088 char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1089 (void)fprintf(fp, "data: %s\n", bdata);
1090 free(bdata);
1091 fclose(fp);
1092}
1093#endif
1094
1100WINPR_ATTR_NODISCARD
1101static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1102 const RDPGFX_SURFACE_COMMAND* cmd)
1103{
1104 INT32 rc = 0;
1105 UINT status = ERROR_INTERNAL_ERROR;
1106 gdiGfxSurface* surface = nullptr;
1107 REGION16 invalidRegion;
1108 const RECTANGLE_16* rects = nullptr;
1109 UINT32 nrRects = 0;
1115 WINPR_ASSERT(gdi);
1116 WINPR_ASSERT(context);
1117 WINPR_ASSERT(cmd);
1118 const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1119
1120 WINPR_ASSERT(context->GetSurfaceData);
1121 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1122
1123 if (!surface)
1124 {
1125 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
1126 return ERROR_NOT_FOUND;
1127 }
1128
1129 if (!is_within_surface(surface, cmd))
1130 return ERROR_INVALID_DATA;
1131
1132 WINPR_ASSERT(surface->codecs);
1133 rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1134 surface->height);
1135
1136 if (rc < 0)
1137 {
1138 WLog_ERR(TAG, "progressive_create_surface_context failure: %" PRId32 "", rc);
1139 return ERROR_INTERNAL_ERROR;
1140 }
1141
1142 region16_init(&invalidRegion);
1143
1144 rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
1145 surface->format, surface->scanline, cmd->left, cmd->top,
1146 &invalidRegion, surfaceId, gdi->frameId);
1147
1148 if (rc < 0)
1149 {
1150 WLog_ERR(TAG, "progressive_decompress failure: %" PRId32 "", rc);
1151 goto fail;
1152 }
1153
1154 rects = region16_rects(&invalidRegion, &nrRects);
1155 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1156 nrRects, rects);
1157
1158 if (status != CHANNEL_RC_OK)
1159 goto fail;
1160
1161 status = ERROR_INTERNAL_ERROR;
1162 for (UINT32 x = 0; x < nrRects; x++)
1163 {
1164 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
1165 goto fail;
1166 }
1167
1168 status = gdi_interFrameUpdate(gdi, context);
1169
1170fail:
1171
1172 region16_uninit(&invalidRegion);
1173 return status;
1174}
1175
1181WINPR_ATTR_NODISCARD
1182static UINT gdi_SurfaceCommand(RdpgfxClientContext* context, const RDPGFX_SURFACE_COMMAND* cmd)
1183{
1184 UINT status = CHANNEL_RC_OK;
1185 rdpGdi* gdi = nullptr;
1186
1187 if (!context || !cmd)
1188 return ERROR_INVALID_PARAMETER;
1189
1190 gdi = (rdpGdi*)context->custom;
1191
1192 EnterCriticalSection(&context->mux);
1193 const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1194 WLog_Print(gdi->log, WLOG_TRACE,
1195 "surfaceId=%" PRIu32 ", codec=%s [%" PRIu32 "], contextId=%" PRIu32 ", format=%s, "
1196 "left=%" PRIu32 ", top=%" PRIu32 ", right=%" PRIu32 ", bottom=%" PRIu32
1197 ", width=%" PRIu32 ", height=%" PRIu32 " "
1198 "length=%" PRIu32 ", data=%p, extra=%p",
1199 cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1200 FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1201 cmd->width, cmd->height, cmd->length, (void*)cmd->data, (void*)cmd->extra);
1202#if defined(WITH_GFX_FRAME_DUMP)
1203 dump_cmd(cmd, gdi->frameId);
1204#endif
1205
1206 switch (codecId)
1207 {
1208 case RDPGFX_CODECID_UNCOMPRESSED:
1209 status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1210 break;
1211
1212#if defined(WITH_GFX_AV1)
1213 case RDPGFX_CODECID_AV1:
1214 status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1215 break;
1216#endif
1217
1218 case RDPGFX_CODECID_CAVIDEO:
1219 status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1220 break;
1221
1222 case RDPGFX_CODECID_CLEARCODEC:
1223 status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1224 break;
1225
1226 case RDPGFX_CODECID_PLANAR:
1227 status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1228 break;
1229
1230 case RDPGFX_CODECID_AVC420:
1231 status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1232 break;
1233
1234 case RDPGFX_CODECID_AVC444v2:
1235 case RDPGFX_CODECID_AVC444:
1236 status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1237 break;
1238
1239 case RDPGFX_CODECID_ALPHA:
1240 status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1241 break;
1242
1243 case RDPGFX_CODECID_CAPROGRESSIVE:
1244 status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1245 break;
1246
1247 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1248 WLog_WARN(TAG, "SurfaceCommand %s [0x%08" PRIX16 "] not implemented",
1249 rdpgfx_get_codec_id_string(codecId), codecId);
1250 break;
1251
1252 default:
1253 WLog_WARN(TAG, "Invalid SurfaceCommand %s [0x%08" PRIX16 "]",
1254 rdpgfx_get_codec_id_string(codecId), codecId);
1255 break;
1256 }
1257
1258 LeaveCriticalSection(&context->mux);
1259 return status;
1260}
1261
1267WINPR_ATTR_NODISCARD
1268static UINT
1269gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1270 const RDPGFX_DELETE_ENCODING_CONTEXT_PDU* deleteEncodingContext)
1271{
1272 WINPR_ASSERT(context);
1273 WINPR_ASSERT(deleteEncodingContext);
1274 WINPR_UNUSED(context);
1275 WINPR_UNUSED(deleteEncodingContext);
1276 return CHANNEL_RC_OK;
1277}
1278
1284WINPR_ATTR_NODISCARD
1285static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1286 const RDPGFX_CREATE_SURFACE_PDU* createSurface)
1287{
1288 UINT rc = ERROR_INTERNAL_ERROR;
1289 gdiGfxSurface* surface = nullptr;
1290 rdpGdi* gdi = nullptr;
1291 WINPR_ASSERT(context);
1292 WINPR_ASSERT(createSurface);
1293 gdi = (rdpGdi*)context->custom;
1294 WINPR_ASSERT(gdi);
1295 WINPR_ASSERT(gdi->context);
1296 EnterCriticalSection(&context->mux);
1297 surface = (gdiGfxSurface*)calloc(1, sizeof(gdiGfxSurface));
1298
1299 if (!surface)
1300 goto fail;
1301
1302 if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
1303 {
1304 WINPR_ASSERT(context->codecs);
1305 surface->codecs = context->codecs;
1306
1307 if (!surface->codecs)
1308 {
1309 free(surface);
1310 goto fail;
1311 }
1312 }
1313
1314 surface->surfaceId = createSurface->surfaceId;
1315 surface->width = gfx_align_scanline(createSurface->width, 16);
1316 surface->height = gfx_align_scanline(createSurface->height, 16);
1317 surface->mappedWidth = createSurface->width;
1318 surface->mappedHeight = createSurface->height;
1319 surface->outputTargetWidth = createSurface->width;
1320 surface->outputTargetHeight = createSurface->height;
1321
1322 const BOOL rails =
1323 freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode);
1324 switch (createSurface->pixelFormat)
1325 {
1326 case GFX_PIXEL_FORMAT_ARGB_8888:
1327 surface->format = PIXEL_FORMAT_BGRA32;
1328 break;
1329
1330 case GFX_PIXEL_FORMAT_XRGB_8888:
1331 surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1332 break;
1333
1334 default:
1335 free(surface);
1336 goto fail;
1337 }
1338
1339 if (surface->width > UINT32_MAX / 4 - 2 * 16)
1340 {
1341 free(surface);
1342 goto fail;
1343 }
1344
1345 /* Align to multiples of 16, but also add additional 16 bytes so SIMD implementations do not
1346 * accidentally read out of bound */
1347 surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 16ul;
1348 surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height, 16);
1349
1350 if (!surface->data)
1351 {
1352 free(surface);
1353 goto fail;
1354 }
1355
1356 for (UINT64 y = 0; y < surface->height; y++)
1357 memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1358 region16_init(&surface->invalidRegion);
1359
1360 WINPR_ASSERT(context->SetSurfaceData);
1361 rc = context->SetSurfaceData(context, surface->surfaceId, (void*)surface);
1362fail:
1363 LeaveCriticalSection(&context->mux);
1364 return rc;
1365}
1366
1372WINPR_ATTR_NODISCARD
1373static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1374 const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
1375{
1376 UINT rc = CHANNEL_RC_OK;
1377 UINT res = ERROR_INTERNAL_ERROR;
1378 rdpCodecs* codecs = nullptr;
1379 gdiGfxSurface* surface = nullptr;
1380 EnterCriticalSection(&context->mux);
1381
1382 WINPR_ASSERT(context->GetSurfaceData);
1383 surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1384
1385 if (surface)
1386 {
1387 if (surface->windowMapped)
1388 rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1389 surface->windowId);
1390
1391#ifdef WITH_GFX_H264
1392 h264_context_free(surface->h264);
1393#endif
1394#if defined(WITH_GFX_AV1)
1395 freerdp_av1_context_free(surface->av1);
1396#endif
1397 region16_uninit(&surface->invalidRegion);
1398 codecs = surface->codecs;
1399 winpr_aligned_free(surface->data);
1400 free(surface);
1401 }
1402
1403 WINPR_ASSERT(context->SetSurfaceData);
1404 res = context->SetSurfaceData(context, deleteSurface->surfaceId, nullptr);
1405 if (res)
1406 rc = res;
1407
1408 if (codecs && codecs->progressive)
1409 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1410
1411 LeaveCriticalSection(&context->mux);
1412 return rc;
1413}
1414
1415WINPR_ATTR_NODISCARD
1416static BOOL intersect_rect(const RECTANGLE_16* rect, const gdiGfxSurface* surface,
1417 RECTANGLE_16* prect)
1418{
1419 WINPR_ASSERT(rect);
1420 WINPR_ASSERT(surface);
1421 WINPR_ASSERT(prect);
1422
1423 if (rect->left > rect->right)
1424 return FALSE;
1425 if (rect->left > surface->width)
1426 return FALSE;
1427 if (rect->top > rect->bottom)
1428 return FALSE;
1429 if (rect->top > surface->height)
1430 return FALSE;
1431 prect->left = rect->left;
1432 prect->top = rect->top;
1433
1434 prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1435 prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1436 return TRUE;
1437}
1438
1444WINPR_ATTR_NODISCARD
1445static UINT gdi_SolidFill(RdpgfxClientContext* context, const RDPGFX_SOLID_FILL_PDU* solidFill)
1446{
1447 UINT status = ERROR_INTERNAL_ERROR;
1448 BYTE a = 0xff;
1449 RECTANGLE_16 invalidRect = WINPR_C_ARRAY_INIT;
1450 rdpGdi* gdi = (rdpGdi*)context->custom;
1451
1452 EnterCriticalSection(&context->mux);
1453
1454 WINPR_ASSERT(context->GetSurfaceData);
1455 gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1456
1457 if (!surface)
1458 goto fail;
1459
1460 {
1461 const BYTE b = solidFill->fillPixel.B;
1462 const BYTE g = solidFill->fillPixel.G;
1463 const BYTE r = solidFill->fillPixel.R;
1464
1465 /* [MS-RDPEGFX] 3.3.5.4 Processing an RDPGFX_SOLIDFILL_PDU message
1466 * https://learn.microsoft.com/en-us/windows/win32/gdi/binary-raster-operations
1467 *
1468 * this sounds like the alpha value is always ignored.
1469 */
1470 const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1471 for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1472 {
1473 const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1474
1475 if (!intersect_rect(rect, surface, &invalidRect))
1476 goto fail;
1477
1478 const UINT32 nWidth = invalidRect.right - invalidRect.left;
1479 const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1480
1481 if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1482 invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1483 goto fail;
1484
1485 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
1486 &invalidRect))
1487 goto fail;
1488 }
1489 }
1490
1491 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1492 solidFill->fillRectCount, solidFill->fillRects);
1493
1494 if (status != CHANNEL_RC_OK)
1495 goto fail;
1496
1497 LeaveCriticalSection(&context->mux);
1498
1499 return gdi_interFrameUpdate(gdi, context);
1500fail:
1501 LeaveCriticalSection(&context->mux);
1502 return status;
1503}
1504
1510WINPR_ATTR_NODISCARD
1511static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1512 const RDPGFX_SURFACE_TO_SURFACE_PDU* surfaceToSurface)
1513{
1514 UINT status = ERROR_INTERNAL_ERROR;
1515 BOOL sameSurface = 0;
1516 const RECTANGLE_16* rectSrc = nullptr;
1517 RECTANGLE_16 invalidRect;
1518 gdiGfxSurface* surfaceSrc = nullptr;
1519 gdiGfxSurface* surfaceDst = nullptr;
1520 rdpGdi* gdi = (rdpGdi*)context->custom;
1521 EnterCriticalSection(&context->mux);
1522 rectSrc = &(surfaceToSurface->rectSrc);
1523
1524 WINPR_ASSERT(context->GetSurfaceData);
1525 surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1526 sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1527
1528 if (!sameSurface)
1529 surfaceDst =
1530 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1531 else
1532 surfaceDst = surfaceSrc;
1533
1534 if (!surfaceSrc || !surfaceDst)
1535 goto fail;
1536
1537 if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1538 goto fail;
1539
1540 {
1541 const UINT32 nWidth = rectSrc->right - rectSrc->left;
1542 const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1543
1544 for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1545 {
1546 const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1547 const RECTANGLE_16 rect = { destPt->x, destPt->y,
1548 (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1549 (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1550 if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1551 goto fail;
1552
1553 const UINT32 rwidth = rect.right - rect.left;
1554 const UINT32 rheight = rect.bottom - rect.top;
1555 if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1556 destPt->x, destPt->y, rwidth, rheight, surfaceSrc->data,
1557 surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1558 rectSrc->top, nullptr, FREERDP_FLIP_NONE))
1559 goto fail;
1560
1561 invalidRect = rect;
1562 if (!region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion,
1563 &invalidRect))
1564 goto fail;
1565 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1566 surfaceDst->surfaceId, 1, &invalidRect);
1567
1568 if (status != CHANNEL_RC_OK)
1569 goto fail;
1570 }
1571 }
1572
1573 LeaveCriticalSection(&context->mux);
1574
1575 return gdi_interFrameUpdate(gdi, context);
1576fail:
1577 LeaveCriticalSection(&context->mux);
1578 return status;
1579}
1580
1581static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1582{
1583 if (!entry)
1584 return;
1585 free(entry->data);
1586 free(entry);
1587}
1588
1589WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1590static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1591 UINT32 format)
1592{
1593 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1, sizeof(gdiGfxCacheEntry));
1594 if (!cacheEntry)
1595 goto fail;
1596
1597 cacheEntry->cacheKey = cacheKey;
1598 cacheEntry->width = width;
1599 cacheEntry->height = height;
1600 cacheEntry->format = format;
1601
1602 const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1603 cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1604
1605 if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1606 {
1607 cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1608
1609 if (!cacheEntry->data)
1610 goto fail;
1611 }
1612 return cacheEntry;
1613fail:
1614 gdi_GfxCacheEntryFree(cacheEntry);
1615 return nullptr;
1616}
1617
1623WINPR_ATTR_NODISCARD
1624static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1625 const RDPGFX_SURFACE_TO_CACHE_PDU* surfaceToCache)
1626{
1627 gdiGfxCacheEntry* cacheEntry = nullptr;
1628 UINT rc = ERROR_INTERNAL_ERROR;
1629 EnterCriticalSection(&context->mux);
1630 const RECTANGLE_16* rect = &(surfaceToCache->rectSrc);
1631
1632 WINPR_ASSERT(context->GetSurfaceData);
1633 gdiGfxSurface* surface =
1634 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1635
1636 if (!surface)
1637 goto fail;
1638
1639 if (!is_rect_valid(rect, surface->width, surface->height))
1640 goto fail;
1641
1642 cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1643 (UINT32)(rect->bottom - rect->top), surface->format);
1644
1645 if (!cacheEntry)
1646 goto fail;
1647
1648 if (!cacheEntry->data)
1649 goto fail;
1650
1651 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1652 0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1653 surface->format, surface->scanline, rect->left, rect->top,
1654 nullptr, FREERDP_FLIP_NONE))
1655 goto fail;
1656
1657 {
1658 RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { surfaceToCache->cacheSlot };
1659 WINPR_ASSERT(context->EvictCacheEntry);
1660 rc = context->EvictCacheEntry(context, &evict);
1661 if (rc != CHANNEL_RC_OK)
1662 goto fail;
1663 }
1664
1665 WINPR_ASSERT(context->SetCacheSlotData);
1666 rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (void*)cacheEntry);
1667fail:
1668 if (rc != CHANNEL_RC_OK)
1669 gdi_GfxCacheEntryFree(cacheEntry);
1670 LeaveCriticalSection(&context->mux);
1671 return rc;
1672}
1673
1679WINPR_ATTR_NODISCARD
1680static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1681 const RDPGFX_CACHE_TO_SURFACE_PDU* cacheToSurface)
1682{
1683 UINT status = ERROR_INTERNAL_ERROR;
1684 gdiGfxSurface* surface = nullptr;
1685 gdiGfxCacheEntry* cacheEntry = nullptr;
1686 RECTANGLE_16 invalidRect;
1687 rdpGdi* gdi = (rdpGdi*)context->custom;
1688
1689 EnterCriticalSection(&context->mux);
1690
1691 WINPR_ASSERT(context->GetSurfaceData);
1692 surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1693
1694 WINPR_ASSERT(context->GetCacheSlotData);
1695 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1696
1697 if (!surface || !cacheEntry)
1698 goto fail;
1699
1700 for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1701 {
1702 const RDPGFX_POINT16* destPt = &cacheToSurface->destPts[index];
1703 const RECTANGLE_16 rect = { destPt->x, destPt->y,
1704 (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1705 (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1706
1707 if (rectangle_is_empty(&rect))
1708 continue;
1709
1710 if (!is_rect_valid(&rect, surface->width, surface->height))
1711 goto fail;
1712
1713 const UINT32 w = rect.right - rect.left;
1714 const UINT32 h = rect.bottom - rect.top;
1715 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1716 destPt->x, destPt->y, w, h, cacheEntry->data,
1717 cacheEntry->format, cacheEntry->scanline, 0, 0, nullptr,
1718 FREERDP_FLIP_NONE))
1719 goto fail;
1720
1721 invalidRect = rect;
1722 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect))
1723 goto fail;
1724 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1725 surface->surfaceId, 1, &invalidRect);
1726
1727 if (status != CHANNEL_RC_OK)
1728 goto fail;
1729 }
1730
1731 LeaveCriticalSection(&context->mux);
1732
1733 return gdi_interFrameUpdate(gdi, context);
1734
1735fail:
1736 LeaveCriticalSection(&context->mux);
1737 return status;
1738}
1739
1745WINPR_ATTR_NODISCARD
1746static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
1747 const RDPGFX_CACHE_IMPORT_REPLY_PDU* cacheImportReply)
1748{
1749 UINT16 count = 0;
1750 const UINT16* slots = nullptr;
1751 UINT error = CHANNEL_RC_OK;
1752
1753 slots = cacheImportReply->cacheSlots;
1754 count = cacheImportReply->importedEntriesCount;
1755
1756 for (UINT16 index = 0; index < count; index++)
1757 {
1758 UINT16 cacheSlot = slots[index];
1759
1760 if (cacheSlot == 0)
1761 continue;
1762
1763 WINPR_ASSERT(context->GetCacheSlotData);
1764 gdiGfxCacheEntry* cacheEntry =
1765 (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1766
1767 if (cacheEntry)
1768 continue;
1769
1770 cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
1771
1772 if (!cacheEntry)
1773 return ERROR_INTERNAL_ERROR;
1774
1775 WINPR_ASSERT(context->SetCacheSlotData);
1776 error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
1777
1778 if (error)
1779 {
1780 WLog_ERR(TAG, "CacheImportReply: SetCacheSlotData failed with error %" PRIu32 "",
1781 error);
1782 gdi_GfxCacheEntryFree(cacheEntry);
1783 break;
1784 }
1785 }
1786
1787 return error;
1788}
1789
1790WINPR_ATTR_NODISCARD
1791static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1792 const PERSISTENT_CACHE_ENTRY* importCacheEntry)
1793{
1794 UINT error = ERROR_INTERNAL_ERROR;
1795 gdiGfxCacheEntry* cacheEntry = nullptr;
1796
1797 if (cacheSlot == 0)
1798 return CHANNEL_RC_OK;
1799
1800 cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
1801 importCacheEntry->height, PIXEL_FORMAT_BGRX32);
1802
1803 if (!cacheEntry)
1804 goto fail;
1805
1806 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1807 0, 0, cacheEntry->width, cacheEntry->height,
1808 importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0,
1809 nullptr, FREERDP_FLIP_NONE))
1810 goto fail;
1811
1812 {
1813 RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { cacheSlot };
1814 WINPR_ASSERT(context->EvictCacheEntry);
1815 error = context->EvictCacheEntry(context, &evict);
1816 }
1817 if (error != CHANNEL_RC_OK)
1818 goto fail;
1819
1820 WINPR_ASSERT(context->SetCacheSlotData);
1821 error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
1822
1823fail:
1824 if (error)
1825 {
1826 gdi_GfxCacheEntryFree(cacheEntry);
1827 WLog_ERR(TAG, "ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32 "", error);
1828 }
1829
1830 return error;
1831}
1832
1833WINPR_ATTR_NODISCARD
1834static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1835 PERSISTENT_CACHE_ENTRY* exportCacheEntry)
1836{
1837 WINPR_ASSERT(context->GetCacheSlotData);
1838 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1839
1840 if (cacheEntry)
1841 {
1842 exportCacheEntry->key64 = cacheEntry->cacheKey;
1843 exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
1844 exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
1845 const UINT64 size = 4ull * cacheEntry->width * cacheEntry->height;
1846 if (size > UINT32_MAX)
1847 return ERROR_NOT_FOUND;
1848
1849 exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
1850 exportCacheEntry->flags = 0;
1851 exportCacheEntry->data = cacheEntry->data;
1852 return CHANNEL_RC_OK;
1853 }
1854
1855 return ERROR_NOT_FOUND;
1856}
1857
1863WINPR_ATTR_NODISCARD
1864static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
1865 const RDPGFX_EVICT_CACHE_ENTRY_PDU* evictCacheEntry)
1866{
1867 gdiGfxCacheEntry* cacheEntry = nullptr;
1868 UINT rc = ERROR_NOT_FOUND;
1869
1870 WINPR_ASSERT(context);
1871 WINPR_ASSERT(evictCacheEntry);
1872
1873 EnterCriticalSection(&context->mux);
1874
1875 WINPR_ASSERT(context->GetCacheSlotData);
1876 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
1877
1878 gdi_GfxCacheEntryFree(cacheEntry);
1879
1880 WINPR_ASSERT(context->SetCacheSlotData);
1881 rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot, nullptr);
1882 LeaveCriticalSection(&context->mux);
1883 return rc;
1884}
1885
1891WINPR_ATTR_NODISCARD
1892static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
1893 const RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU* surfaceToOutput)
1894{
1895 UINT rc = ERROR_INTERNAL_ERROR;
1896 gdiGfxSurface* surface = nullptr;
1897 EnterCriticalSection(&context->mux);
1898
1899 WINPR_ASSERT(context->GetSurfaceData);
1900 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1901
1902 if (!surface)
1903 goto fail;
1904
1905 if (surface->windowMapped)
1906 {
1907 WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
1908 goto fail;
1909 }
1910
1911 surface->outputMapped = TRUE;
1912 surface->outputOriginX = surfaceToOutput->outputOriginX;
1913 surface->outputOriginY = surfaceToOutput->outputOriginY;
1914 surface->outputTargetWidth = surface->mappedWidth;
1915 surface->outputTargetHeight = surface->mappedHeight;
1916 region16_clear(&surface->invalidRegion);
1917 rc = CHANNEL_RC_OK;
1918fail:
1919 LeaveCriticalSection(&context->mux);
1920 return rc;
1921}
1922
1923WINPR_ATTR_NODISCARD
1924static UINT
1925gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
1926 const RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU* surfaceToOutput)
1927{
1928 UINT rc = ERROR_INTERNAL_ERROR;
1929 gdiGfxSurface* surface = nullptr;
1930 EnterCriticalSection(&context->mux);
1931
1932 WINPR_ASSERT(context->GetSurfaceData);
1933 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1934
1935 if (!surface)
1936 goto fail;
1937
1938 if (surface->windowMapped)
1939 {
1940 WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
1941 goto fail;
1942 }
1943
1944 surface->outputMapped = TRUE;
1945 surface->outputOriginX = surfaceToOutput->outputOriginX;
1946 surface->outputOriginY = surfaceToOutput->outputOriginY;
1947 surface->outputTargetWidth = surfaceToOutput->targetWidth;
1948 surface->outputTargetHeight = surfaceToOutput->targetHeight;
1949 region16_clear(&surface->invalidRegion);
1950 rc = CHANNEL_RC_OK;
1951fail:
1952 LeaveCriticalSection(&context->mux);
1953 return rc;
1954}
1955
1961WINPR_ATTR_NODISCARD
1962static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
1963 const RDPGFX_MAP_SURFACE_TO_WINDOW_PDU* surfaceToWindow)
1964{
1965 UINT rc = ERROR_INTERNAL_ERROR;
1966 gdiGfxSurface* surface = nullptr;
1967 EnterCriticalSection(&context->mux);
1968
1969 WINPR_ASSERT(context->GetSurfaceData);
1970 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1971
1972 if (!surface)
1973 goto fail;
1974
1975 if (surface->outputMapped)
1976 {
1977 WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
1978 goto fail;
1979 }
1980
1981 if (surface->windowMapped)
1982 {
1983 if (surface->windowId != surfaceToWindow->windowId)
1984 {
1985 WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
1986 surface->windowId, surfaceToWindow->windowId);
1987 goto fail;
1988 }
1989 }
1990 surface->windowMapped = TRUE;
1991
1992 surface->windowId = surfaceToWindow->windowId;
1993 surface->mappedWidth = surfaceToWindow->mappedWidth;
1994 if (surface->mappedWidth > surface->width)
1995 {
1996 WLog_WARN(TAG, "surface mappedWidth[%" PRIu32 "] > width[%" PRIu32 "]",
1997 surface->mappedWidth, surface->width);
1998 goto fail;
1999 }
2000 surface->mappedHeight = surfaceToWindow->mappedHeight;
2001 if (surface->mappedHeight > surface->height)
2002 {
2003 WLog_WARN(TAG, "surface mappedHeight[%" PRIu32 "] > height[%" PRIu32 "]",
2004 surface->mappedHeight, surface->height);
2005 goto fail;
2006 }
2007 surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2008 surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2009 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2010 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2011fail:
2012 LeaveCriticalSection(&context->mux);
2013 return rc;
2014}
2015
2016WINPR_ATTR_NODISCARD
2017static UINT
2018gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2019 const RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU* surfaceToWindow)
2020{
2021 UINT rc = ERROR_INTERNAL_ERROR;
2022 gdiGfxSurface* surface = nullptr;
2023 EnterCriticalSection(&context->mux);
2024
2025 WINPR_ASSERT(context->GetSurfaceData);
2026 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2027
2028 if (!surface)
2029 goto fail;
2030
2031 if (surface->outputMapped)
2032 {
2033 WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
2034 goto fail;
2035 }
2036
2037 if (surface->windowMapped)
2038 {
2039 if (surface->windowId != surfaceToWindow->windowId)
2040 {
2041 WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
2042 surface->windowId, surfaceToWindow->windowId);
2043 goto fail;
2044 }
2045 }
2046 surface->windowMapped = TRUE;
2047
2048 surface->windowId = surfaceToWindow->windowId;
2049 surface->mappedWidth = surfaceToWindow->mappedWidth;
2050 if (surface->mappedWidth > surface->width)
2051 {
2052 WLog_WARN(TAG, "surface mappedWidth[%" PRIu32 "] > width[%" PRIu32 "]",
2053 surface->mappedWidth, surface->width);
2054 goto fail;
2055 }
2056
2057 surface->mappedHeight = surfaceToWindow->mappedHeight;
2058 if (surface->mappedHeight > surface->height)
2059 {
2060 WLog_WARN(TAG, "surface mappedHeight[%" PRIu32 "] > height[%" PRIu32 "]",
2061 surface->mappedHeight, surface->height);
2062 goto fail;
2063 }
2064 surface->outputTargetWidth = surfaceToWindow->targetWidth;
2065 surface->outputTargetHeight = surfaceToWindow->targetHeight;
2066 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2067 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2068fail:
2069 LeaveCriticalSection(&context->mux);
2070 return rc;
2071}
2072
2073BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2074{
2075 return gdi_graphics_pipeline_init_ex(gdi, gfx, nullptr, nullptr, nullptr);
2076}
2077
2078BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2079 pcRdpgfxMapWindowForSurface map,
2080 pcRdpgfxUnmapWindowForSurface unmap,
2081 pcRdpgfxUpdateSurfaceArea update)
2082{
2083 if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2084 return FALSE;
2085
2086 rdpContext* context = gdi->context;
2087 rdpSettings* settings = context->settings;
2088
2089 gdi->gfx = gfx;
2090 gfx->custom = (void*)gdi;
2091 gfx->ResetGraphics = gdi_ResetGraphics;
2092 gfx->StartFrame = gdi_StartFrame;
2093 gfx->EndFrame = gdi_EndFrame;
2094 gfx->SurfaceCommand = gdi_SurfaceCommand;
2095 gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
2096 gfx->CreateSurface = gdi_CreateSurface;
2097 gfx->DeleteSurface = gdi_DeleteSurface;
2098 gfx->SolidFill = gdi_SolidFill;
2099 gfx->SurfaceToSurface = gdi_SurfaceToSurface;
2100 gfx->SurfaceToCache = gdi_SurfaceToCache;
2101 gfx->CacheToSurface = gdi_CacheToSurface;
2102 gfx->CacheImportReply = gdi_CacheImportReply;
2103 gfx->ImportCacheEntry = gdi_ImportCacheEntry;
2104 gfx->ExportCacheEntry = gdi_ExportCacheEntry;
2105 gfx->EvictCacheEntry = gdi_EvictCacheEntry;
2106 gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
2107 gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
2108 gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
2109 gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
2110 gfx->UpdateSurfaces = gdi_UpdateSurfaces;
2111 gfx->MapWindowForSurface = map;
2112 gfx->UnmapWindowForSurface = unmap;
2113 gfx->UpdateSurfaceArea = update;
2114
2115 if (!freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
2116 {
2117 const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
2118 const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2119 const UINT32 flags = freerdp_settings_get_uint32(settings, FreeRDP_ThreadingFlags);
2120
2121 gfx->codecs = freerdp_client_codecs_new(flags);
2122 if (!gfx->codecs)
2123 return FALSE;
2124 if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2125 return FALSE;
2126 }
2127 InitializeCriticalSection(&gfx->mux);
2128 PROFILER_CREATE(gfx->SurfaceProfiler, "GFX-PROFILER")
2129
2130
2136 gdi->graphicsReset = TRUE;
2137 if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
2138 {
2139 gfx->UpdateSurfaceArea = nullptr;
2140 gfx->UpdateSurfaces = nullptr;
2141 gfx->SurfaceCommand = nullptr;
2142 }
2143
2144 return TRUE;
2145}
2146
2147void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
2148{
2149 if (gdi)
2150 gdi->gfx = nullptr;
2151
2152 if (!gfx)
2153 return;
2154
2155 gfx->custom = nullptr;
2156 freerdp_client_codecs_free(gfx->codecs);
2157 gfx->codecs = nullptr;
2158 DeleteCriticalSection(&gfx->mux);
2159 PROFILER_PRINT_HEADER
2160 PROFILER_PRINT(gfx->SurfaceProfiler)
2161 PROFILER_PRINT_FOOTER
2162 PROFILER_FREE(gfx->SurfaceProfiler)
2163}
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_codecs_flags(const rdpSettings *settings)
helper function to get a mask of supported codec flags.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
Definition persistent.h:70