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