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