FreeRDP
Loading...
Searching...
No Matches
gdi/gfx.c
1
21
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
38/* Maximum number of rectangles kept in gdiGfxSurface::invalidRegion before it is replaced by its
39 * bounding box. */
40#define GDI_GFX_MAX_INVALID_RECTS 256
41
42WINPR_ATTR_NODISCARD
43static BOOL is_rect_valid(const RECTANGLE_16* rect, size_t width, size_t height)
44{
45 if (!rect)
46 return FALSE;
47 if ((rect->left > rect->right) || (rect->right > width))
48 return FALSE;
49 if ((rect->top > rect->bottom) || (rect->bottom > height))
50 return FALSE;
51 return TRUE;
52}
53
54WINPR_ATTR_NODISCARD
55static BOOL is_within_surface(const gdiGfxSurface* surface, const RDPGFX_SURFACE_COMMAND* cmd)
56{
57 RECTANGLE_16 rect;
58 if (!surface || !cmd)
59 return FALSE;
60 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
61 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
62 rect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
63 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
64 if (!is_rect_valid(&rect, surface->width, surface->height))
65 {
66 WLog_ERR(TAG,
67 "Command rect %" PRIu32 "x%" PRIu32 "-%" PRIu32 "x%" PRIu32
68 " not within bounds of %" PRIu32 "x%" PRIu32,
69 rect.left, rect.top, cmd->width, cmd->height, surface->width, surface->height);
70 return FALSE;
71 }
72 if (rect.left > surface->width)
73 return FALSE;
74 if (rect.right > surface->width)
75 return FALSE;
76 if (rect.top > surface->height)
77 return FALSE;
78 if (rect.bottom > surface->height)
79 return FALSE;
80
81 return TRUE;
82}
83
95WINPR_ATTR_NODISCARD
96static BOOL gdi_surface_invalidate_rect(gdiGfxSurface* surface, const RECTANGLE_16* rect)
97{
98 WINPR_ASSERT(surface);
99
100 REGION16* region = &surface->invalidRegion;
101 if (!region16_union_rect(region, region, rect))
102 return FALSE;
103
104 if (region16_n_rects(region) <= GDI_GFX_MAX_INVALID_RECTS)
105 return TRUE;
106
107 const RECTANGLE_16 extents = *region16_extents(region);
108 region16_clear(region);
109 return region16_union_rect(region, region, &extents);
110}
111
112WINPR_ATTR_NODISCARD
113static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
114{
115 const UINT32 align = alignment;
116 const UINT32 pad = align - (widthInBytes % alignment);
117 UINT32 scanline = widthInBytes;
118
119 if (align != pad)
120 scanline += pad;
121
122 return scanline;
123}
124
130WINPR_ATTR_NODISCARD
131static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
132 const RDPGFX_RESET_GRAPHICS_PDU* resetGraphics)
133{
134 UINT rc = ERROR_INTERNAL_ERROR;
135 UINT16 count = 0;
136 UINT16* pSurfaceIds = nullptr;
137
138 WINPR_ASSERT(context);
139 WINPR_ASSERT(resetGraphics);
140
141 rdpGdi* gdi = (rdpGdi*)context->custom;
142 WINPR_ASSERT(gdi);
143
144 rdpUpdate* update = gdi->context->update;
145 WINPR_ASSERT(update);
146
147 rdpSettings* settings = gdi->context->settings;
148 WINPR_ASSERT(settings);
149 EnterCriticalSection(&context->mux);
150 const UINT32 DesktopWidth = resetGraphics->width;
151 const UINT32 DesktopHeight = resetGraphics->height;
152
153 WLog_DBG(TAG, "new size: %" PRIu32 "x%" PRIu32, DesktopWidth, DesktopHeight);
154
155 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, DesktopWidth))
156 goto fail;
157 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, DesktopHeight))
158 goto fail;
159
160 if (update)
161 {
162 WINPR_ASSERT(update->DesktopResize);
163
164 /* Keep the lock order of BeginPaint/EndPaint: update lock first, client locks second */
165 rdp_update_lock(update);
166 const BOOL resized = update->DesktopResize(gdi->context);
167 rdp_update_unlock(update);
168 if (!resized)
169 goto fail;
170 }
171
172 WINPR_ASSERT(context->GetSurfaceIds);
173 const UINT res = context->GetSurfaceIds(context, &pSurfaceIds, &count);
174 if (res != CHANNEL_RC_OK)
175 {
176 rc = res;
177 goto fail;
178 }
179
180 for (UINT32 index = 0; index < count; index++)
181 {
182 WINPR_ASSERT(context->GetSurfaceData);
183 gdiGfxSurface* surface =
184 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
185
186 if (!surface)
187 continue;
188
189 memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);
190 region16_clear(&surface->invalidRegion);
191 }
192
193 free(pSurfaceIds);
194
195 if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
196 {
197 const UINT32 width = (UINT32)MAX(0, gdi->width);
198 const UINT32 height = (UINT32)MAX(0, gdi->height);
199
200 if (!freerdp_client_codecs_reset(
201 context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))
202 {
203 goto fail;
204 }
205 }
206
207 rc = CHANNEL_RC_OK;
208fail:
209 LeaveCriticalSection(&context->mux);
210 return rc;
211}
212
213WINPR_ATTR_NODISCARD
214static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
215{
216 UINT rc = ERROR_INTERNAL_ERROR;
217 UINT32 surfaceX = 0;
218 UINT32 surfaceY = 0;
219 RECTANGLE_16 surfaceRect;
220 const RECTANGLE_16* rects = nullptr;
221 UINT32 nbRects = 0;
222 rdpUpdate* update = nullptr;
223
224 WINPR_ASSERT(gdi);
225 WINPR_ASSERT(gdi->context);
226 WINPR_ASSERT(surface);
227
228 update = gdi->context->update;
229 WINPR_ASSERT(update);
230
231 if (gdi->suppressOutput)
232 return CHANNEL_RC_OK;
233
234 surfaceX = surface->outputOriginX;
235 surfaceY = surface->outputOriginY;
236 surfaceRect.left = 0;
237 surfaceRect.top = 0;
238 surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
239 surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
240 if (!region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion),
241 &surfaceRect))
242 goto fail;
243
244 const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
245 const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
246
247 rects = region16_rects(&surface->invalidRegion, &nbRects);
248 if (!rects || (nbRects == 0))
249 {
250 rc = CHANNEL_RC_OK;
251 goto fail;
252 }
253
254 if (!update_begin_paint(update))
255 goto fail;
256
257 for (UINT32 i = 0; i < nbRects; i++)
258 {
259 const UINT32 nXSrc = rects[i].left;
260 const UINT32 nYSrc = rects[i].top;
261 const double xDst = surfaceX + nXSrc * sx;
262 const double yDst = surfaceY + nYSrc * sy;
263
264 /* A surface can be larger than the output for a while, e.g. right after the desktop
265 * shrank and before the server sent surfaces of the new size: a part outside the
266 * output has nothing to show. */
267 const UINT32 outWidth = (UINT32)MAX(0, gdi->width);
268 const UINT32 outHeight = (UINT32)MAX(0, gdi->height);
269 if ((xDst >= outWidth) || (yDst >= outHeight))
270 continue;
271
272 const UINT32 nXDst = (UINT32)xDst;
273 const UINT32 nYDst = (UINT32)yDst;
274 UINT32 swidth = rects[i].right - rects[i].left;
275 UINT32 sheight = rects[i].bottom - rects[i].top;
276 UINT32 dwidth = (UINT32)(swidth * sx);
277 UINT32 dheight = (UINT32)(sheight * sy);
278
279 /* Clip at the output's edge by cropping the source as well: shrinking only the
280 * destination would turn the clip into a scale, which squeezes the picture and fails
281 * outright in a build without a scaler (no swscale, no cairo), and that failure ends
282 * the graphics channel. */
283 if (dwidth > outWidth - nXDst)
284 {
285 dwidth = outWidth - nXDst;
286 swidth = MAX(1, MIN(swidth, (UINT32)(dwidth / sx)));
287 }
288 if (dheight > outHeight - nYDst)
289 {
290 dheight = outHeight - nYDst;
291 sheight = MAX(1, MIN(sheight, (UINT32)(dheight / sy)));
292 }
293
294 if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
295 dwidth, dheight, surface->data, surface->format, surface->scanline,
296 nXSrc, nYSrc, swidth, sheight))
297 {
298 rc = CHANNEL_RC_NULL_DATA;
299 goto clear;
300 }
301
302 if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
303 (INT32)dheight))
304 goto clear;
305 }
306
307 rc = CHANNEL_RC_OK;
308clear:
309
310 if (!update_end_paint(update))
311 rc = ERROR_INTERNAL_ERROR;
312
313fail:
314 region16_clear(&(surface->invalidRegion));
315 return rc;
316}
317
318WINPR_ATTR_NODISCARD
319static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
320{
321 WINPR_ASSERT(context);
322 WINPR_ASSERT(surface);
323 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
324}
325
326WINPR_ATTR_NODISCARD
327static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
328{
329 UINT16 count = 0;
330 UINT16* pSurfaceIds = nullptr;
331
332 WINPR_ASSERT(context);
333
334 rdpGdi* gdi = (rdpGdi*)context->custom;
335 WINPR_ASSERT(gdi);
336
337 EnterCriticalSection(&context->mux);
338
339 WINPR_ASSERT(context->GetSurfaceIds);
340 UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
341 if (status != CHANNEL_RC_OK)
342 goto fail;
343
344 for (UINT32 index = 0; index < count; index++)
345 {
346 WINPR_ASSERT(context->GetSurfaceData);
347 gdiGfxSurface* surface =
348 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
349
350 if (!surface)
351 continue;
352
353 /* Already handled in UpdateSurfaceArea callbacks */
354 if (context->UpdateSurfaceArea)
355 {
356 if (surface->handleInUpdateSurfaceArea)
357 continue;
358 }
359
360 if (surface->outputMapped)
361 status = gdi_OutputUpdate(gdi, surface);
362 else if (surface->windowMapped)
363 status = gdi_WindowUpdate(context, surface);
364
365 if (status != CHANNEL_RC_OK)
366 break;
367 }
368
369fail:
370 free(pSurfaceIds);
371 LeaveCriticalSection(&context->mux);
372 return status;
373}
374
380WINPR_ATTR_NODISCARD
381static UINT gdi_StartFrame(RdpgfxClientContext* context, const RDPGFX_START_FRAME_PDU* startFrame)
382{
383 rdpGdi* gdi = nullptr;
384
385 WINPR_ASSERT(context);
386 WINPR_ASSERT(startFrame);
387
388 gdi = (rdpGdi*)context->custom;
389 WINPR_ASSERT(gdi);
390 gdi->inGfxFrame = TRUE;
391 gdi->frameId = startFrame->frameId;
392 return CHANNEL_RC_OK;
393}
394
395WINPR_ATTR_NODISCARD
396static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
397{
398 WINPR_ASSERT(context);
399 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
400}
401
407WINPR_ATTR_NODISCARD
408static UINT gdi_EndFrame(RdpgfxClientContext* context,
409 WINPR_ATTR_UNUSED const RDPGFX_END_FRAME_PDU* endFrame)
410{
411 WINPR_ASSERT(context);
412 WINPR_ASSERT(endFrame);
413
414 rdpGdi* gdi = (rdpGdi*)context->custom;
415 WINPR_ASSERT(gdi);
416 const UINT status = gdi_call_update_surfaces(context);
417 gdi->inGfxFrame = FALSE;
418 return status;
419}
420
421WINPR_ATTR_NODISCARD
422static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
423{
424 WINPR_ASSERT(gdi);
425 UINT status = CHANNEL_RC_OK;
426 if (!gdi->inGfxFrame)
427 status = gdi_call_update_surfaces(context);
428 return status;
429}
430
436WINPR_ATTR_NODISCARD
437static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
438 const RDPGFX_SURFACE_COMMAND* cmd)
439{
440 UINT status = ERROR_INTERNAL_ERROR;
441 gdiGfxSurface* surface = nullptr;
442 RECTANGLE_16 invalidRect;
443 DWORD bpp = 0;
444 size_t size = 0;
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 (!is_within_surface(surface, cmd))
460 return ERROR_INVALID_DATA;
461
462 bpp = FreeRDPGetBytesPerPixel(cmd->format);
463 size = 1ull * bpp * cmd->width * cmd->height;
464 if (cmd->length < size)
465 {
466 WLog_ERR(TAG, "Not enough data, got %" PRIu32 ", expected %" PRIuz, cmd->length, size);
467 return ERROR_INVALID_DATA;
468 }
469
470 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
471 cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
472 0, 0, nullptr, FREERDP_FLIP_NONE))
473 return ERROR_INTERNAL_ERROR;
474
475 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
476 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
477 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
478 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
479 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
480 goto fail;
481 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
482 &invalidRect);
483
484 if (status != CHANNEL_RC_OK)
485 goto fail;
486
487 status = gdi_interFrameUpdate(gdi, context);
488
489fail:
490 return status;
491}
492
493#if defined(WITH_GFX_AV1)
494WINPR_ATTR_NODISCARD
495static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
496 const RDPGFX_SURFACE_COMMAND* cmd)
497{
498 INT32 rc = 0;
499 UINT status = ERROR_INTERNAL_ERROR;
500 gdiGfxSurface* surface = nullptr;
501 RDPGFX_H264_METABLOCK* meta = nullptr;
502 RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
503 WINPR_ASSERT(gdi);
504 WINPR_ASSERT(context);
505 WINPR_ASSERT(cmd);
506
507 WINPR_ASSERT(context->GetSurfaceData);
508 surface =
509 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
510
511 if (!surface)
512 {
513 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
514 return ERROR_NOT_FOUND;
515 }
516
517 if (!surface->av1)
518 {
519 surface->av1 = freerdp_av1_context_new(FALSE);
520
521 if (!surface->av1)
522 {
523 WLog_ERR(TAG, "unable to create av1 context");
524 return ERROR_NOT_ENOUGH_MEMORY;
525 }
526
527 if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
528 return ERROR_INTERNAL_ERROR;
529 }
530
531 if (!surface->av1)
532 return ERROR_NOT_SUPPORTED;
533
534 if (!is_within_surface(surface, cmd))
535 return ERROR_INVALID_DATA;
536
537 bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
538
539 if (!bs)
540 return ERROR_INTERNAL_ERROR;
541
542 meta = &(bs->meta);
543 rc = freerdp_av1_decompress(surface->av1, bs->data, bs->length, surface->data, surface->format,
544 surface->scanline, surface->width, surface->height,
545 meta->regionRects, meta->numRegionRects);
546
547 if (rc < 0)
548 {
549 WLog_WARN(TAG, "freerdp_av1_decompress failure: %" PRId32 ", ignoring update.", rc);
550 return CHANNEL_RC_OK;
551 }
552
553 for (UINT32 i = 0; i < meta->numRegionRects; i++)
554 {
555 if (!gdi_surface_invalidate_rect(surface, &(meta->regionRects[i])))
556 goto fail;
557 }
558
559 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
560 meta->numRegionRects, meta->regionRects);
561
562 if (status != CHANNEL_RC_OK)
563 goto fail;
564
565 status = gdi_interFrameUpdate(gdi, context);
566
567fail:
568 return status;
569}
570#endif
571
572#if defined(WITH_GFX_AV2)
573WINPR_ATTR_NODISCARD
574static UINT gdi_SurfaceCommand_AV2(rdpGdi* gdi, RdpgfxClientContext* context,
575 const RDPGFX_SURFACE_COMMAND* cmd)
576{
577 INT32 rc = 0;
578 UINT status = ERROR_INTERNAL_ERROR;
579 gdiGfxSurface* surface = nullptr;
580 RDPGFX_H264_METABLOCK* meta = nullptr;
581 RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
582 WINPR_ASSERT(gdi);
583 WINPR_ASSERT(context);
584 WINPR_ASSERT(cmd);
585
586 WINPR_ASSERT(context->GetSurfaceData);
587 surface =
588 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
589
590 if (!surface)
591 {
592 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
593 return ERROR_NOT_FOUND;
594 }
595
596 if (!surface->av2)
597 {
598 surface->av2 = freerdp_av2_context_new(FALSE);
599
600 if (!surface->av2)
601 {
602 WLog_ERR(TAG, "unable to create av2 context");
603 return ERROR_NOT_ENOUGH_MEMORY;
604 }
605
606 if (!freerdp_av2_context_reset(surface->av2, surface->width, surface->height))
607 return ERROR_INTERNAL_ERROR;
608 }
609
610 if (!surface->av2)
611 return ERROR_NOT_SUPPORTED;
612
613 if (!is_within_surface(surface, cmd))
614 return ERROR_INVALID_DATA;
615
616 bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
617
618 if (!bs)
619 return ERROR_INTERNAL_ERROR;
620
621 meta = &(bs->meta);
622 rc = freerdp_av2_decompress(surface->av2, bs->data, bs->length, surface->data, surface->format,
623 surface->scanline, surface->width, surface->height,
624 meta->regionRects, meta->numRegionRects);
625
626 if (rc < 0)
627 {
628 WLog_WARN(TAG, "freerdp_av2_decompress failure: %" PRId32 ", ignoring update.", rc);
629 return CHANNEL_RC_OK;
630 }
631
632 for (UINT32 i = 0; i < meta->numRegionRects; i++)
633 {
634 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
635 &(meta->regionRects[i])))
636 goto fail;
637 }
638
639 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
640 meta->numRegionRects, meta->regionRects);
641
642 if (status != CHANNEL_RC_OK)
643 goto fail;
644
645 status = gdi_interFrameUpdate(gdi, context);
646
647fail:
648 return status;
649}
650#endif
651
657WINPR_ATTR_NODISCARD
658static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
659 const RDPGFX_SURFACE_COMMAND* cmd)
660{
661 UINT status = ERROR_INTERNAL_ERROR;
662 gdiGfxSurface* surface = nullptr;
663 REGION16 invalidRegion;
664 const RECTANGLE_16* rects = nullptr;
665 UINT32 nrRects = 0;
666 WINPR_ASSERT(gdi);
667 WINPR_ASSERT(context);
668 WINPR_ASSERT(cmd);
669
670 WINPR_ASSERT(context->GetSurfaceData);
671 surface =
672 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
673
674 if (!surface)
675 {
676 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
677 return ERROR_NOT_FOUND;
678 }
679
680 if (!is_within_surface(surface, cmd))
681 return ERROR_INVALID_DATA;
682
683 WINPR_ASSERT(surface->codecs);
684 rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
685 region16_init(&invalidRegion);
686
687 if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
688 surface->data, surface->format, surface->scanline, surface->height,
689 &invalidRegion))
690 {
691 WLog_ERR(TAG, "Failed to process RemoteFX message");
692 goto fail;
693 }
694
695 rects = region16_rects(&invalidRegion, &nrRects);
696 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
697 nrRects, rects);
698
699 if (status != CHANNEL_RC_OK)
700 goto fail;
701 status = ERROR_INTERNAL_ERROR;
702
703 for (UINT32 x = 0; x < nrRects; x++)
704 {
705 if (!gdi_surface_invalidate_rect(surface, &rects[x]))
706 goto fail;
707 }
708
709 status = gdi_interFrameUpdate(gdi, context);
710
711fail:
712 region16_uninit(&invalidRegion);
713 return status;
714}
715
721WINPR_ATTR_NODISCARD
722static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
723 const RDPGFX_SURFACE_COMMAND* cmd)
724{
725 INT32 rc = 0;
726 UINT status = ERROR_INTERNAL_ERROR;
727 gdiGfxSurface* surface = nullptr;
728 RECTANGLE_16 invalidRect;
729 WINPR_ASSERT(gdi);
730 WINPR_ASSERT(context);
731 WINPR_ASSERT(cmd);
732
733 WINPR_ASSERT(context->GetSurfaceData);
734 surface =
735 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
736
737 if (!surface)
738 {
739 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
740 return ERROR_NOT_FOUND;
741 }
742
743 if (!is_within_surface(surface, cmd))
744 return ERROR_INVALID_DATA;
745
746 WINPR_ASSERT(surface->codecs);
747 rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
748 surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
749 surface->width, surface->height, &gdi->palette);
750
751 if (rc < 0)
752 {
753 WLog_ERR(TAG, "clear_decompress failure: %" PRId32 "", rc);
754 return ERROR_INTERNAL_ERROR;
755 }
756
757 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
758 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
759 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
760 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
761 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
762 goto fail;
763 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
764 &invalidRect);
765
766 if (status != CHANNEL_RC_OK)
767 goto fail;
768
769 status = gdi_interFrameUpdate(gdi, context);
770
771fail:
772 return status;
773}
774
780WINPR_ATTR_NODISCARD
781static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
782 const RDPGFX_SURFACE_COMMAND* cmd)
783{
784 UINT status = ERROR_INTERNAL_ERROR;
785 BYTE* DstData = nullptr;
786 gdiGfxSurface* surface = nullptr;
787 RECTANGLE_16 invalidRect;
788 WINPR_ASSERT(gdi);
789 WINPR_ASSERT(context);
790 WINPR_ASSERT(cmd);
791
792 WINPR_ASSERT(context->GetSurfaceData);
793 surface =
794 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
795
796 if (!surface)
797 {
798 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
799 return ERROR_NOT_FOUND;
800 }
801
802 DstData = surface->data;
803
804 if (!is_within_surface(surface, cmd))
805 return ERROR_INVALID_DATA;
806
807 if (!freerdp_bitmap_decompress_planar(surface->codecs->planar, cmd->data, cmd->length,
808 cmd->width, cmd->height, DstData, surface->format,
809 surface->scanline, cmd->left, cmd->top, cmd->width,
810 cmd->height, FALSE))
811 return ERROR_INTERNAL_ERROR;
812
813 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
814 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
815 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
816 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
817 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
818 goto fail;
819 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
820 &invalidRect);
821
822 if (status != CHANNEL_RC_OK)
823 goto fail;
824
825 status = gdi_interFrameUpdate(gdi, context);
826
827fail:
828 return status;
829}
830
836WINPR_ATTR_NODISCARD
837static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
838 const RDPGFX_SURFACE_COMMAND* cmd)
839{
840#ifdef WITH_GFX_H264
841 INT32 rc = 0;
842 UINT status = ERROR_INTERNAL_ERROR;
843 gdiGfxSurface* surface = nullptr;
844 RDPGFX_H264_METABLOCK* meta = nullptr;
845 RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
846 WINPR_ASSERT(gdi);
847 WINPR_ASSERT(context);
848 WINPR_ASSERT(cmd);
849
850 WINPR_ASSERT(context->GetSurfaceData);
851 surface =
852 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
853
854 if (!surface)
855 {
856 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
857 return ERROR_NOT_FOUND;
858 }
859
860 if (!surface->h264)
861 {
862 surface->h264 = h264_context_new(FALSE);
863
864 if (!surface->h264)
865 {
866 WLog_ERR(TAG, "unable to create h264 context");
867 return ERROR_NOT_ENOUGH_MEMORY;
868 }
869
870 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
871 (UINT32)freerdp_settings_get_bool(gdi->context->settings,
872 FreeRDP_SoftwareGdi) == 0))
873 return ERROR_INTERNAL_ERROR;
874
875 if (!h264_context_reset(surface->h264, surface->width, surface->height))
876 return ERROR_INTERNAL_ERROR;
877 }
878
879 if (!surface->h264)
880 return ERROR_NOT_SUPPORTED;
881
882 if (!is_within_surface(surface, cmd))
883 return ERROR_INVALID_DATA;
884
885 bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
886
887 if (!bs)
888 return ERROR_INTERNAL_ERROR;
889
890 meta = &(bs->meta);
891 rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
892 surface->scanline, surface->width, surface->height, meta->regionRects,
893 meta->numRegionRects);
894
895 if (rc < 0)
896 {
897 WLog_WARN(TAG, "avc420_decompress failure: %" PRId32 ", ignoring update.", rc);
898 return CHANNEL_RC_OK;
899 }
900
901 for (UINT32 i = 0; i < meta->numRegionRects; i++)
902 {
903 if (!gdi_surface_invalidate_rect(surface, &(meta->regionRects[i])))
904 goto fail;
905 }
906
907 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
908 meta->numRegionRects, meta->regionRects);
909
910 if (status != CHANNEL_RC_OK)
911 goto fail;
912
913 status = gdi_interFrameUpdate(gdi, context);
914
915fail:
916 return status;
917#else
918 return ERROR_NOT_SUPPORTED;
919#endif
920}
921
922#if defined(WITH_GFX_AZURE)
923/* AVC420 and HEVC420 share the bitstream layout (metablock + Annex B), only the codec differs */
924WINPR_ATTR_NODISCARD
925static UINT gdi_SurfaceCommand_HEVC(rdpGdi* gdi, RdpgfxClientContext* context,
926 const RDPGFX_SURFACE_COMMAND* cmd)
927{
928 UINT status = ERROR_INTERNAL_ERROR;
929
930 WINPR_ASSERT(gdi);
931 WINPR_ASSERT(context);
932 WINPR_ASSERT(cmd);
933
934 WINPR_ASSERT(context->GetSurfaceData);
935 gdiGfxSurface* surface =
936 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
937
938 if (!surface)
939 {
940 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
941 return ERROR_NOT_FOUND;
942 }
943
944 if (!surface->hevc)
945 {
946 surface->hevc = freerdp_hevc_context_new(FALSE);
947
948 if (!surface->hevc)
949 {
950 WLog_ERR(TAG, "unable to create hevc context");
951 return ERROR_NOT_ENOUGH_MEMORY;
952 }
953
954 if (!freerdp_hevc_context_set_option(surface->hevc, FREERDP_HEVC_CONTEXT_OPTION_HW_ACCEL,
956 gdi->context->settings, FreeRDP_SoftwareGdi) == 0))
957 return ERROR_INTERNAL_ERROR;
958
959 if (!freerdp_hevc_context_reset(surface->hevc, surface->width, surface->height))
960 return ERROR_INTERNAL_ERROR;
961 }
962
963 if (!surface->hevc)
964 return ERROR_NOT_SUPPORTED;
965
966 /* HEVC 4:2:0 and 4:4:4 may need different decoders (the hardware ones and Media Foundation
967 * often lack 4:4:4), follow the codec of each command. A switch starts with a key frame. */
968 const UINT32 profile = (cmd->codecId == RDPGFX_CODECID_HEVC444) ? 1 : 0;
969 if (freerdp_hevc_context_get_option(surface->hevc, FREERDP_HEVC_CONTEXT_OPTION_PROFILE) !=
970 profile)
971 {
972 if (!freerdp_hevc_context_set_option(surface->hevc, FREERDP_HEVC_CONTEXT_OPTION_PROFILE,
973 profile))
974 {
975 WLog_ERR(TAG, "no HEVC %s decoder available", profile ? "4:4:4" : "4:2:0");
976 return ERROR_NOT_SUPPORTED;
977 }
978 if (!freerdp_hevc_context_reset(surface->hevc, surface->width, surface->height))
979 return ERROR_INTERNAL_ERROR;
980 }
981
982 if (!is_within_surface(surface, cmd))
983 return ERROR_INVALID_DATA;
984
985 const RDPGFX_HEVC_BITMAP_STREAM* bs = (const RDPGFX_HEVC_BITMAP_STREAM*)cmd->extra;
986
987 if (!bs)
988 return ERROR_INTERNAL_ERROR;
989
990 const RDPGFX_H264_METABLOCK* meta = &(bs->meta);
991 const INT32 rc = freerdp_hevc_decompress(
992 surface->hevc, bs->data, bs->length, surface->data, surface->format, surface->scanline,
993 surface->width, surface->height, meta->regionRects, meta->numRegionRects);
994
995 if (rc < 0)
996 {
997 WLog_WARN(TAG, "freerdp_hevc_decompress failure: %" PRId32 ", ignoring update.", rc);
998 return CHANNEL_RC_OK;
999 }
1000
1001 for (UINT32 i = 0; i < meta->numRegionRects; i++)
1002 {
1003 if (!gdi_surface_invalidate_rect(surface, &(meta->regionRects[i])))
1004 goto fail;
1005 }
1006
1007 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1008 meta->numRegionRects, meta->regionRects);
1009
1010 if (status != CHANNEL_RC_OK)
1011 goto fail;
1012
1013 status = gdi_interFrameUpdate(gdi, context);
1014
1015fail:
1016 return status;
1017}
1018#endif
1019
1025WINPR_ATTR_NODISCARD
1026static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
1027 const RDPGFX_SURFACE_COMMAND* cmd)
1028{
1029#ifdef WITH_GFX_H264
1030 INT32 rc = 0;
1031 UINT status = ERROR_INTERNAL_ERROR;
1032 gdiGfxSurface* surface = nullptr;
1033 RDPGFX_AVC444_BITMAP_STREAM* bs = nullptr;
1034 RDPGFX_AVC420_BITMAP_STREAM* avc1 = nullptr;
1035 RDPGFX_H264_METABLOCK* meta1 = nullptr;
1036 RDPGFX_AVC420_BITMAP_STREAM* avc2 = nullptr;
1037 RDPGFX_H264_METABLOCK* meta2 = nullptr;
1038 WINPR_ASSERT(gdi);
1039 WINPR_ASSERT(context);
1040 WINPR_ASSERT(cmd);
1041
1042 WINPR_ASSERT(context->GetSurfaceData);
1043 surface =
1044 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
1045
1046 if (!surface)
1047 {
1048 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
1049 return ERROR_NOT_FOUND;
1050 }
1051
1052 if (!surface->h264)
1053 {
1054 surface->h264 = h264_context_new(FALSE);
1055
1056 if (!surface->h264)
1057 {
1058 WLog_ERR(TAG, "unable to create h264 context");
1059 return ERROR_NOT_ENOUGH_MEMORY;
1060 }
1061
1062 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
1063 (UINT32)freerdp_settings_get_bool(gdi->context->settings,
1064 FreeRDP_SoftwareGdi) == 0))
1065 return ERROR_INTERNAL_ERROR;
1066 if (!h264_context_reset(surface->h264, surface->width, surface->height))
1067 return ERROR_INTERNAL_ERROR;
1068 }
1069
1070 if (!surface->h264)
1071 return ERROR_NOT_SUPPORTED;
1072
1073 if (!is_within_surface(surface, cmd))
1074 return ERROR_INVALID_DATA;
1075
1076 bs = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
1077
1078 if (!bs)
1079 return ERROR_INTERNAL_ERROR;
1080
1081 avc1 = &bs->bitstream[0];
1082 avc2 = &bs->bitstream[1];
1083 meta1 = &avc1->meta;
1084 meta2 = &avc2->meta;
1085 rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
1086 avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
1087 avc2->data, avc2->length, surface->data, surface->format,
1088 surface->scanline, surface->width, surface->height, cmd->codecId);
1089
1090 if (rc < 0)
1091 {
1092 WLog_WARN(TAG, "avc444_decompress failure: %" PRId32 ", ignoring update.", rc);
1093 return CHANNEL_RC_OK;
1094 }
1095
1096 for (UINT32 i = 0; i < meta1->numRegionRects; i++)
1097 {
1098 if (!gdi_surface_invalidate_rect(surface, &(meta1->regionRects[i])))
1099 goto fail;
1100 }
1101
1102 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1103 meta1->numRegionRects, meta1->regionRects);
1104
1105 if (status != CHANNEL_RC_OK)
1106 goto fail;
1107 status = ERROR_INTERNAL_ERROR;
1108
1109 for (UINT32 i = 0; i < meta2->numRegionRects; i++)
1110 {
1111 if (!gdi_surface_invalidate_rect(surface, &(meta2->regionRects[i])))
1112 goto fail;
1113 }
1114
1115 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1116 meta2->numRegionRects, meta2->regionRects);
1117
1118 if (status != CHANNEL_RC_OK)
1119 goto fail;
1120
1121 status = gdi_interFrameUpdate(gdi, context);
1122
1123fail:
1124 return status;
1125#else
1126 return ERROR_NOT_SUPPORTED;
1127#endif
1128}
1129
1130WINPR_ATTR_NODISCARD
1131static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride, RECTANGLE_16* rect,
1132 UINT32 startOffsetX, UINT32 count, BYTE a)
1133{
1134 UINT32 written = 0;
1135 BOOL first = TRUE;
1136 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
1137 WINPR_ASSERT(rect);
1138
1139 for (size_t y = rect->top; y < rect->bottom; y++)
1140 {
1141 BYTE* line = &data[y * stride];
1142
1143 for (size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
1144 {
1145 BYTE r = 0;
1146 BYTE g = 0;
1147 BYTE b = 0;
1148
1149 if (written == count)
1150 return TRUE;
1151
1152 BYTE* src = &line[x * bpp];
1153 UINT32 color = FreeRDPReadColor(src, format);
1154 FreeRDPSplitColor(color, format, &r, &g, &b, nullptr, nullptr);
1155 color = FreeRDPGetColor(format, r, g, b, a);
1156 FreeRDPWriteColor(src, format, color);
1157 written++;
1158 }
1159
1160 first = FALSE;
1161 }
1162
1163 return TRUE;
1164}
1170WINPR_ATTR_NODISCARD
1171static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
1172 const RDPGFX_SURFACE_COMMAND* cmd)
1173{
1174 UINT status = ERROR_INTERNAL_ERROR;
1175 UINT16 alphaSig = 0;
1176 UINT16 compressed = 0;
1177 RECTANGLE_16 invalidRect;
1178 wStream buffer = WINPR_C_ARRAY_INIT;
1179 WINPR_ASSERT(gdi);
1180 WINPR_ASSERT(context);
1181 WINPR_ASSERT(cmd);
1182
1183 wStream* s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
1184
1185 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1186 return ERROR_INVALID_DATA;
1187
1188 WINPR_ASSERT(context->GetSurfaceData);
1189 gdiGfxSurface* surface =
1190 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
1191
1192 if (!surface)
1193 {
1194 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
1195 return ERROR_NOT_FOUND;
1196 }
1197
1198 if (!is_within_surface(surface, cmd))
1199 return ERROR_INVALID_DATA;
1200
1201 Stream_Read_UINT16(s, alphaSig);
1202 Stream_Read_UINT16(s, compressed);
1203
1204 if (alphaSig != 0x414C)
1205 return ERROR_INVALID_DATA;
1206
1207 if (compressed == 0)
1208 {
1209 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
1210 return ERROR_INVALID_DATA;
1211
1212 for (size_t y = cmd->top; y < cmd->top + cmd->height; y++)
1213 {
1214 BYTE* line = &surface->data[y * surface->scanline];
1215
1216 for (size_t x = cmd->left; x < cmd->left + cmd->width; x++)
1217 {
1218 UINT32 color = 0;
1219 BYTE r = 0;
1220 BYTE g = 0;
1221 BYTE b = 0;
1222 BYTE a = 0;
1223 BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
1224 Stream_Read_UINT8(s, a);
1225 color = FreeRDPReadColor(src, surface->format);
1226 FreeRDPSplitColor(color, surface->format, &r, &g, &b, nullptr, nullptr);
1227 color = FreeRDPGetColor(surface->format, r, g, b, a);
1228 FreeRDPWriteColor(src, surface->format, color);
1229 }
1230 }
1231 }
1232 else
1233 {
1234 UINT32 startOffsetX = 0;
1235 RECTANGLE_16 rect = { .left = (UINT16)MIN(UINT16_MAX, cmd->left),
1236 .top = (UINT16)MIN(UINT16_MAX, cmd->top),
1237 .right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width),
1238 .bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height) };
1239 UINT64 remaining = 1ull * cmd->width * cmd->height;
1240
1241 while (rect.top < rect.bottom)
1242 {
1243 UINT32 count = 0;
1244 BYTE a = 0;
1245
1246 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1247 return ERROR_INVALID_DATA;
1248
1249 Stream_Read_UINT8(s, a);
1250 Stream_Read_UINT8(s, count);
1251
1252 if (count >= 0xFF)
1253 {
1254 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1255 return ERROR_INVALID_DATA;
1256
1257 Stream_Read_UINT16(s, count);
1258
1259 if (count >= 0xFFFF)
1260 {
1261 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1262 return ERROR_INVALID_DATA;
1263
1264 Stream_Read_UINT32(s, count);
1265 }
1266 }
1267
1268 if (count > remaining)
1269 {
1270 WLog_ERR(TAG,
1271 "alpha codec run length %" PRIu32 " exceeds the remaining %" PRIu64
1272 " pixels of the %" PRIu32 "x%" PRIu32 " destination rectangle",
1273 count, remaining, cmd->width, cmd->height);
1274 return ERROR_INVALID_DATA;
1275 }
1276 remaining -= count;
1277 if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1278 startOffsetX, count, a))
1279 return ERROR_INTERNAL_ERROR;
1280
1281 startOffsetX += count;
1282
1283 while (startOffsetX >= cmd->width)
1284 {
1285 startOffsetX -= cmd->width;
1286 rect.top++;
1287 }
1288 }
1289 }
1290
1291 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1292 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1293 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
1294 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
1295 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
1296 goto fail;
1297 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1298 &invalidRect);
1299
1300 if (status != CHANNEL_RC_OK)
1301 goto fail;
1302
1303 status = gdi_interFrameUpdate(gdi, context);
1304
1305fail:
1306 return status;
1307}
1308
1309#if defined(WITH_GFX_FRAME_DUMP)
1310static void dump_cmd(const RDPGFX_SURFACE_COMMAND* cmd, UINT32 frameId)
1311{
1312 static UINT64 xxx = 0;
1313 const char* path = "/tmp/dump/";
1314 WINPR_ASSERT(cmd);
1315 char fname[1024] = WINPR_C_ARRAY_INIT;
1316
1317 if (_snprintf(fname, sizeof(fname), "%s/%08" PRIx64 ".raw", path, xxx++) < 0)
1318 return;
1319
1320 FILE* fp = fopen(fname, "w");
1321 if (!fp)
1322 return;
1323 (void)fprintf(fp, "frameid: %" PRIu32 "\n", frameId);
1324 (void)fprintf(fp, "surfaceId: %" PRIu32 "\n", cmd->surfaceId);
1325 (void)fprintf(fp, "codecId: %" PRIu32 "\n", cmd->codecId);
1326 (void)fprintf(fp, "contextId: %" PRIu32 "\n", cmd->contextId);
1327 (void)fprintf(fp, "format: %" PRIu32 "\n", cmd->format);
1328 (void)fprintf(fp, "left: %" PRIu32 "\n", cmd->left);
1329 (void)fprintf(fp, "top: %" PRIu32 "\n", cmd->top);
1330 (void)fprintf(fp, "right: %" PRIu32 "\n", cmd->right);
1331 (void)fprintf(fp, "bottom: %" PRIu32 "\n", cmd->bottom);
1332 (void)fprintf(fp, "width: %" PRIu32 "\n", cmd->width);
1333 (void)fprintf(fp, "height: %" PRIu32 "\n", cmd->height);
1334 (void)fprintf(fp, "length: %" PRIu32 "\n", cmd->length);
1335
1336 char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1337 (void)fprintf(fp, "data: %s\n", bdata);
1338 free(bdata);
1339 fclose(fp);
1340}
1341#endif
1342
1348WINPR_ATTR_NODISCARD
1349static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1350 const RDPGFX_SURFACE_COMMAND* cmd)
1351{
1352 INT32 rc = 0;
1353 UINT status = ERROR_INTERNAL_ERROR;
1354 gdiGfxSurface* surface = nullptr;
1355 REGION16 invalidRegion;
1356 const RECTANGLE_16* rects = nullptr;
1357 UINT32 nrRects = 0;
1363 WINPR_ASSERT(gdi);
1364 WINPR_ASSERT(context);
1365 WINPR_ASSERT(cmd);
1366 const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1367
1368 WINPR_ASSERT(context->GetSurfaceData);
1369 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1370
1371 if (!surface)
1372 {
1373 WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
1374 return ERROR_NOT_FOUND;
1375 }
1376
1377 if (!is_within_surface(surface, cmd))
1378 return ERROR_INVALID_DATA;
1379
1380 WINPR_ASSERT(surface->codecs);
1381 rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1382 surface->height);
1383
1384 if (rc < 0)
1385 {
1386 WLog_ERR(TAG, "progressive_create_surface_context failure: %" PRId32 "", rc);
1387 return ERROR_INTERNAL_ERROR;
1388 }
1389
1390 region16_init(&invalidRegion);
1391
1392 rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
1393 surface->format, surface->scanline, cmd->left, cmd->top,
1394 &invalidRegion, surfaceId, gdi->frameId);
1395
1396 if (rc < 0)
1397 {
1398 WLog_ERR(TAG, "progressive_decompress failure: %" PRId32 "", rc);
1399 goto fail;
1400 }
1401
1402 rects = region16_rects(&invalidRegion, &nrRects);
1403 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1404 nrRects, rects);
1405
1406 if (status != CHANNEL_RC_OK)
1407 goto fail;
1408
1409 status = ERROR_INTERNAL_ERROR;
1410 for (UINT32 x = 0; x < nrRects; x++)
1411 {
1412 if (!gdi_surface_invalidate_rect(surface, &rects[x]))
1413 goto fail;
1414 }
1415
1416 status = gdi_interFrameUpdate(gdi, context);
1417
1418fail:
1419
1420 region16_uninit(&invalidRegion);
1421 return status;
1422}
1423
1429WINPR_ATTR_NODISCARD
1430static UINT gdi_SurfaceCommand(RdpgfxClientContext* context, const RDPGFX_SURFACE_COMMAND* cmd)
1431{
1432 UINT status = CHANNEL_RC_OK;
1433 rdpGdi* gdi = nullptr;
1434
1435 if (!context || !cmd)
1436 return ERROR_INVALID_PARAMETER;
1437
1438 gdi = (rdpGdi*)context->custom;
1439
1440 EnterCriticalSection(&context->mux);
1441 const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1442 WLog_Print(gdi->log, WLOG_TRACE,
1443 "surfaceId=%" PRIu32 ", codec=%s [%" PRIu32 "], contextId=%" PRIu32 ", format=%s, "
1444 "left=%" PRIu32 ", top=%" PRIu32 ", right=%" PRIu32 ", bottom=%" PRIu32
1445 ", width=%" PRIu32 ", height=%" PRIu32 " "
1446 "length=%" PRIu32 ", data=%p, extra=%p",
1447 cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1448 FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1449 cmd->width, cmd->height, cmd->length, (void*)cmd->data, (void*)cmd->extra);
1450#if defined(WITH_GFX_FRAME_DUMP)
1451 dump_cmd(cmd, gdi->frameId);
1452#endif
1453
1454 switch (codecId)
1455 {
1456 case RDPGFX_CODECID_UNCOMPRESSED:
1457 status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1458 break;
1459
1460#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1461 case RDPGFX_CODECID_AV1:
1462 /* the codec ID is shared, the confirmed caps version tells AV1 from AV2 */
1463 switch (context->ConfirmedCaps.version)
1464 {
1465#if defined(WITH_GFX_AV1)
1466 case RDPGFX_CAPVERSION_FRDP_1:
1467 status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1468 break;
1469#endif
1470#if defined(WITH_GFX_AV2)
1471 case RDPGFX_CAPVERSION_FRDP_2:
1472 status = gdi_SurfaceCommand_AV2(gdi, context, cmd);
1473 break;
1474#endif
1475 default:
1476 WLog_ERR(TAG,
1477 "SurfaceCommand %s [0x%08" PRIX16 "] not valid with caps 0x%08" PRIX32,
1478 rdpgfx_get_codec_id_string(codecId), codecId,
1479 context->ConfirmedCaps.version);
1480 status = ERROR_INVALID_DATA;
1481 break;
1482 }
1483 break;
1484#endif
1485
1486 case RDPGFX_CODECID_CAVIDEO:
1487 status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1488 break;
1489
1490 case RDPGFX_CODECID_CLEARCODEC:
1491 status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1492 break;
1493
1494 case RDPGFX_CODECID_PLANAR:
1495 status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1496 break;
1497
1498 case RDPGFX_CODECID_AVC420:
1499 status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1500 break;
1501
1502 case RDPGFX_CODECID_AVC444v2:
1503 case RDPGFX_CODECID_AVC444:
1504 status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1505 break;
1506
1507#if defined(WITH_GFX_AZURE)
1508 case RDPGFX_CODECID_HEVC444:
1509 case RDPGFX_CODECID_HEVC420:
1510 status = gdi_SurfaceCommand_HEVC(gdi, context, cmd);
1511 break;
1512#endif
1513
1514 case RDPGFX_CODECID_ALPHA:
1515 status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1516 break;
1517
1518 case RDPGFX_CODECID_CAPROGRESSIVE:
1519 status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1520 break;
1521
1522 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1523 WLog_WARN(TAG, "SurfaceCommand %s [0x%08" PRIX16 "] not implemented",
1524 rdpgfx_get_codec_id_string(codecId), codecId);
1525 break;
1526
1527 default:
1528 WLog_WARN(TAG, "Invalid SurfaceCommand %s [0x%08" PRIX16 "]",
1529 rdpgfx_get_codec_id_string(codecId), codecId);
1530 break;
1531 }
1532
1533 LeaveCriticalSection(&context->mux);
1534 return status;
1535}
1536
1542WINPR_ATTR_NODISCARD
1543static UINT
1544gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1545 const RDPGFX_DELETE_ENCODING_CONTEXT_PDU* deleteEncodingContext)
1546{
1547 WINPR_ASSERT(context);
1548 WINPR_ASSERT(deleteEncodingContext);
1549 WINPR_UNUSED(context);
1550 WINPR_UNUSED(deleteEncodingContext);
1551 return CHANNEL_RC_OK;
1552}
1553
1559WINPR_ATTR_NODISCARD
1560static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1561 const RDPGFX_CREATE_SURFACE_PDU* createSurface)
1562{
1563 UINT rc = ERROR_INTERNAL_ERROR;
1564 gdiGfxSurface* surface = nullptr;
1565 rdpGdi* gdi = nullptr;
1566 WINPR_ASSERT(context);
1567 WINPR_ASSERT(createSurface);
1568 gdi = (rdpGdi*)context->custom;
1569 WINPR_ASSERT(gdi);
1570 WINPR_ASSERT(gdi->context);
1571 EnterCriticalSection(&context->mux);
1572 surface = (gdiGfxSurface*)calloc(1, sizeof(gdiGfxSurface));
1573
1574 if (!surface)
1575 goto fail;
1576
1577 if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
1578 {
1579 WINPR_ASSERT(context->codecs);
1580 surface->codecs = context->codecs;
1581
1582 if (!surface->codecs)
1583 {
1584 free(surface);
1585 goto fail;
1586 }
1587 }
1588
1589 surface->surfaceId = createSurface->surfaceId;
1590 surface->width = gfx_align_scanline(createSurface->width, 16);
1591 surface->height = gfx_align_scanline(createSurface->height, 16);
1592 surface->mappedWidth = createSurface->width;
1593 surface->mappedHeight = createSurface->height;
1594 surface->outputTargetWidth = createSurface->width;
1595 surface->outputTargetHeight = createSurface->height;
1596
1597 const BOOL rails =
1598 freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode);
1599 switch (createSurface->pixelFormat)
1600 {
1601 case GFX_PIXEL_FORMAT_ARGB_8888:
1602 surface->format = PIXEL_FORMAT_BGRA32;
1603 break;
1604
1605 case GFX_PIXEL_FORMAT_XRGB_8888:
1606 surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1607 break;
1608
1609 default:
1610 free(surface);
1611 goto fail;
1612 }
1613
1614 if ((surface->width > UINT32_MAX / 4 - 64) || (surface->height > UINT32_MAX - 64))
1615 {
1616 free(surface);
1617 goto fail;
1618 }
1619
1620 /* Align to multiples of 16, but also add additional 64 bytes so SIMD implementations do not
1621 * accidentally read out of bound.
1622 * Also add 4 extra lines for the same reason */
1623 surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 64ul;
1624 surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height + 4ull, 16);
1625
1626 if (!surface->data)
1627 {
1628 free(surface);
1629 goto fail;
1630 }
1631
1632 for (UINT64 y = 0; y < surface->height; y++)
1633 memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1634 region16_init(&surface->invalidRegion);
1635
1636 WINPR_ASSERT(context->SetSurfaceData);
1637 rc = context->SetSurfaceData(context, surface->surfaceId, (void*)surface);
1638fail:
1639 LeaveCriticalSection(&context->mux);
1640 return rc;
1641}
1642
1648WINPR_ATTR_NODISCARD
1649static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1650 const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
1651{
1652 UINT rc = CHANNEL_RC_OK;
1653 UINT res = ERROR_INTERNAL_ERROR;
1654 rdpCodecs* codecs = nullptr;
1655 gdiGfxSurface* surface = nullptr;
1656 EnterCriticalSection(&context->mux);
1657
1658 WINPR_ASSERT(context->GetSurfaceData);
1659 surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1660
1661 if (surface)
1662 {
1663 if (surface->windowMapped)
1664 rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1665 surface->windowId);
1666
1667#ifdef WITH_GFX_H264
1668 h264_context_free(surface->h264);
1669#if defined(WITH_GFX_AZURE)
1670 freerdp_hevc_context_free(surface->hevc);
1671#endif
1672#endif
1673#if defined(WITH_GFX_AV1)
1674 freerdp_av1_context_free(surface->av1);
1675#endif
1676#if defined(WITH_GFX_AV2)
1677 freerdp_av2_context_free(surface->av2);
1678#endif
1679 region16_uninit(&surface->invalidRegion);
1680 codecs = surface->codecs;
1681 winpr_aligned_free(surface->data);
1682 free(surface);
1683 }
1684
1685 WINPR_ASSERT(context->SetSurfaceData);
1686 res = context->SetSurfaceData(context, deleteSurface->surfaceId, nullptr);
1687 if (res)
1688 rc = res;
1689
1690 if (codecs && codecs->progressive)
1691 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1692
1693 LeaveCriticalSection(&context->mux);
1694 return rc;
1695}
1696
1697WINPR_ATTR_NODISCARD
1698static BOOL intersect_rect(const RECTANGLE_16* rect, const gdiGfxSurface* surface,
1699 RECTANGLE_16* prect)
1700{
1701 WINPR_ASSERT(rect);
1702 WINPR_ASSERT(surface);
1703 WINPR_ASSERT(prect);
1704
1705 if (rect->left > rect->right)
1706 return FALSE;
1707 if (rect->left > surface->width)
1708 return FALSE;
1709 if (rect->top > rect->bottom)
1710 return FALSE;
1711 if (rect->top > surface->height)
1712 return FALSE;
1713 prect->left = rect->left;
1714 prect->top = rect->top;
1715
1716 prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1717 prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1718 return TRUE;
1719}
1720
1726WINPR_ATTR_NODISCARD
1727static UINT gdi_SolidFill(RdpgfxClientContext* context, const RDPGFX_SOLID_FILL_PDU* solidFill)
1728{
1729 UINT status = ERROR_INTERNAL_ERROR;
1730 BYTE a = 0xff;
1731 RECTANGLE_16 invalidRect = WINPR_C_ARRAY_INIT;
1732 rdpGdi* gdi = (rdpGdi*)context->custom;
1733
1734 EnterCriticalSection(&context->mux);
1735
1736 WINPR_ASSERT(context->GetSurfaceData);
1737 gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1738
1739 if (!surface)
1740 goto fail;
1741
1742 {
1743 const BYTE b = solidFill->fillPixel.B;
1744 const BYTE g = solidFill->fillPixel.G;
1745 const BYTE r = solidFill->fillPixel.R;
1746
1747 /* [MS-RDPEGFX] 3.3.5.4 Processing an RDPGFX_SOLIDFILL_PDU message
1748 * https://learn.microsoft.com/en-us/windows/win32/gdi/binary-raster-operations
1749 *
1750 * this sounds like the alpha value is always ignored.
1751 */
1752 const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1753 for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1754 {
1755 const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1756
1757 if (!intersect_rect(rect, surface, &invalidRect))
1758 goto fail;
1759
1760 const UINT32 nWidth = invalidRect.right - invalidRect.left;
1761 const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1762
1763 if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1764 invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1765 goto fail;
1766
1767 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
1768 goto fail;
1769 }
1770 }
1771
1772 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1773 solidFill->fillRectCount, solidFill->fillRects);
1774
1775 if (status != CHANNEL_RC_OK)
1776 goto fail;
1777
1778 LeaveCriticalSection(&context->mux);
1779
1780 return gdi_interFrameUpdate(gdi, context);
1781fail:
1782 LeaveCriticalSection(&context->mux);
1783 return status;
1784}
1785
1791WINPR_ATTR_NODISCARD
1792static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1793 const RDPGFX_SURFACE_TO_SURFACE_PDU* surfaceToSurface)
1794{
1795 UINT status = ERROR_INTERNAL_ERROR;
1796 BOOL sameSurface = 0;
1797 const RECTANGLE_16* rectSrc = nullptr;
1798 RECTANGLE_16 invalidRect;
1799 gdiGfxSurface* surfaceSrc = nullptr;
1800 gdiGfxSurface* surfaceDst = nullptr;
1801 rdpGdi* gdi = (rdpGdi*)context->custom;
1802 EnterCriticalSection(&context->mux);
1803 rectSrc = &(surfaceToSurface->rectSrc);
1804
1805 WINPR_ASSERT(context->GetSurfaceData);
1806 surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1807 sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1808
1809 if (!sameSurface)
1810 surfaceDst =
1811 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1812 else
1813 surfaceDst = surfaceSrc;
1814
1815 if (!surfaceSrc || !surfaceDst)
1816 goto fail;
1817
1818 if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1819 goto fail;
1820
1821 {
1822 const UINT32 nWidth = rectSrc->right - rectSrc->left;
1823 const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1824
1825 for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1826 {
1827 const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1828 const RECTANGLE_16 rect = { destPt->x, destPt->y,
1829 (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1830 (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1831 if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1832 goto fail;
1833
1834 const UINT32 rwidth = rect.right - rect.left;
1835 const UINT32 rheight = rect.bottom - rect.top;
1836 if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1837 destPt->x, destPt->y, rwidth, rheight, surfaceSrc->data,
1838 surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1839 rectSrc->top, nullptr, FREERDP_FLIP_NONE))
1840 goto fail;
1841
1842 invalidRect = rect;
1843 if (!gdi_surface_invalidate_rect(surfaceDst, &invalidRect))
1844 goto fail;
1845 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1846 surfaceDst->surfaceId, 1, &invalidRect);
1847
1848 if (status != CHANNEL_RC_OK)
1849 goto fail;
1850 status = ERROR_INTERNAL_ERROR;
1851 }
1852 }
1853
1854 LeaveCriticalSection(&context->mux);
1855
1856 return gdi_interFrameUpdate(gdi, context);
1857fail:
1858 LeaveCriticalSection(&context->mux);
1859 return status;
1860}
1861
1862static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1863{
1864 if (!entry)
1865 return;
1866 free(entry->data);
1867 free(entry);
1868}
1869
1870WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1871static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1872 UINT32 format)
1873{
1874 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1, sizeof(gdiGfxCacheEntry));
1875 if (!cacheEntry)
1876 goto fail;
1877
1878 cacheEntry->cacheKey = cacheKey;
1879 cacheEntry->width = width;
1880 cacheEntry->height = height;
1881 cacheEntry->format = format;
1882
1883 const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1884 cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1885
1886 if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1887 {
1888 cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1889
1890 if (!cacheEntry->data)
1891 goto fail;
1892 }
1893 return cacheEntry;
1894fail:
1895 gdi_GfxCacheEntryFree(cacheEntry);
1896 return nullptr;
1897}
1898
1904WINPR_ATTR_NODISCARD
1905static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1906 const RDPGFX_SURFACE_TO_CACHE_PDU* surfaceToCache)
1907{
1908 gdiGfxCacheEntry* cacheEntry = nullptr;
1909 UINT rc = ERROR_INTERNAL_ERROR;
1910 EnterCriticalSection(&context->mux);
1911 const RECTANGLE_16* rect = &(surfaceToCache->rectSrc);
1912
1913 WINPR_ASSERT(context->GetSurfaceData);
1914 gdiGfxSurface* surface =
1915 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1916
1917 if (!surface)
1918 goto fail;
1919
1920 if (!is_rect_valid(rect, surface->width, surface->height))
1921 goto fail;
1922
1923 cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1924 (UINT32)(rect->bottom - rect->top), surface->format);
1925
1926 if (!cacheEntry)
1927 goto fail;
1928
1929 if (!cacheEntry->data)
1930 goto fail;
1931
1932 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1933 0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1934 surface->format, surface->scanline, rect->left, rect->top,
1935 nullptr, FREERDP_FLIP_NONE))
1936 goto fail;
1937
1938 {
1939 RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { surfaceToCache->cacheSlot };
1940 WINPR_ASSERT(context->EvictCacheEntry);
1941 rc = context->EvictCacheEntry(context, &evict);
1942 if (rc != CHANNEL_RC_OK)
1943 goto fail;
1944 }
1945
1946 WINPR_ASSERT(context->SetCacheSlotData);
1947 rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (void*)cacheEntry);
1948fail:
1949 if (rc != CHANNEL_RC_OK)
1950 gdi_GfxCacheEntryFree(cacheEntry);
1951 LeaveCriticalSection(&context->mux);
1952 return rc;
1953}
1954
1960WINPR_ATTR_NODISCARD
1961static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1962 const RDPGFX_CACHE_TO_SURFACE_PDU* cacheToSurface)
1963{
1964 UINT status = ERROR_INTERNAL_ERROR;
1965 gdiGfxSurface* surface = nullptr;
1966 gdiGfxCacheEntry* cacheEntry = nullptr;
1967 RECTANGLE_16 invalidRect;
1968 rdpGdi* gdi = (rdpGdi*)context->custom;
1969
1970 EnterCriticalSection(&context->mux);
1971
1972 WINPR_ASSERT(context->GetSurfaceData);
1973 surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1974
1975 WINPR_ASSERT(context->GetCacheSlotData);
1976 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1977
1978 if (!surface || !cacheEntry)
1979 goto fail;
1980
1981 for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1982 {
1983 const RDPGFX_POINT16* destPt = &cacheToSurface->destPts[index];
1984 const RECTANGLE_16 rect = { destPt->x, destPt->y,
1985 (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1986 (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1987
1988 if (rectangle_is_empty(&rect))
1989 continue;
1990
1991 if (!is_rect_valid(&rect, surface->width, surface->height))
1992 goto fail;
1993
1994 const UINT32 w = rect.right - rect.left;
1995 const UINT32 h = rect.bottom - rect.top;
1996 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1997 destPt->x, destPt->y, w, h, cacheEntry->data,
1998 cacheEntry->format, cacheEntry->scanline, 0, 0, nullptr,
1999 FREERDP_FLIP_NONE))
2000 goto fail;
2001
2002 invalidRect = rect;
2003 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
2004 goto fail;
2005 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
2006 surface->surfaceId, 1, &invalidRect);
2007
2008 if (status != CHANNEL_RC_OK)
2009 goto fail;
2010 status = ERROR_INTERNAL_ERROR;
2011 }
2012
2013 LeaveCriticalSection(&context->mux);
2014
2015 return gdi_interFrameUpdate(gdi, context);
2016
2017fail:
2018 LeaveCriticalSection(&context->mux);
2019 return status;
2020}
2021
2027WINPR_ATTR_NODISCARD
2028static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
2029 const RDPGFX_CACHE_IMPORT_REPLY_PDU* cacheImportReply)
2030{
2031 UINT16 count = 0;
2032 const UINT16* slots = nullptr;
2033 UINT error = CHANNEL_RC_OK;
2034
2035 slots = cacheImportReply->cacheSlots;
2036 count = cacheImportReply->importedEntriesCount;
2037
2038 for (UINT16 index = 0; index < count; index++)
2039 {
2040 UINT16 cacheSlot = slots[index];
2041
2042 if (cacheSlot == 0)
2043 continue;
2044
2045 WINPR_ASSERT(context->GetCacheSlotData);
2046 gdiGfxCacheEntry* cacheEntry =
2047 (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
2048
2049 if (cacheEntry)
2050 continue;
2051
2052 cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
2053
2054 if (!cacheEntry)
2055 return ERROR_INTERNAL_ERROR;
2056
2057 WINPR_ASSERT(context->SetCacheSlotData);
2058 error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
2059
2060 if (error)
2061 {
2062 WLog_ERR(TAG, "CacheImportReply: SetCacheSlotData failed with error %" PRIu32 "",
2063 error);
2064 gdi_GfxCacheEntryFree(cacheEntry);
2065 break;
2066 }
2067 }
2068
2069 return error;
2070}
2071
2072WINPR_ATTR_NODISCARD
2073static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
2074 const PERSISTENT_CACHE_ENTRY* importCacheEntry)
2075{
2076 UINT error = ERROR_INTERNAL_ERROR;
2077 gdiGfxCacheEntry* cacheEntry = nullptr;
2078
2079 if (cacheSlot == 0)
2080 return CHANNEL_RC_OK;
2081
2082 cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
2083 importCacheEntry->height, PIXEL_FORMAT_BGRX32);
2084
2085 if (!cacheEntry)
2086 goto fail;
2087
2088 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
2089 0, 0, cacheEntry->width, cacheEntry->height,
2090 importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0,
2091 nullptr, FREERDP_FLIP_NONE))
2092 goto fail;
2093
2094 {
2095 RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { cacheSlot };
2096 WINPR_ASSERT(context->EvictCacheEntry);
2097 error = context->EvictCacheEntry(context, &evict);
2098 }
2099 if (error != CHANNEL_RC_OK)
2100 goto fail;
2101
2102 WINPR_ASSERT(context->SetCacheSlotData);
2103 error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
2104
2105fail:
2106 if (error)
2107 {
2108 gdi_GfxCacheEntryFree(cacheEntry);
2109 WLog_ERR(TAG, "ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32 "", error);
2110 }
2111
2112 return error;
2113}
2114
2115WINPR_ATTR_NODISCARD
2116static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
2117 PERSISTENT_CACHE_ENTRY* exportCacheEntry)
2118{
2119 WINPR_ASSERT(context->GetCacheSlotData);
2120 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
2121
2122 if (cacheEntry)
2123 {
2124 exportCacheEntry->key64 = cacheEntry->cacheKey;
2125 exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
2126 exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
2127 const UINT64 size = 4ull * cacheEntry->width * cacheEntry->height;
2128 if (size > UINT32_MAX)
2129 return ERROR_NOT_FOUND;
2130
2131 exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
2132 exportCacheEntry->flags = 0;
2133 exportCacheEntry->data = cacheEntry->data;
2134 return CHANNEL_RC_OK;
2135 }
2136
2137 return ERROR_NOT_FOUND;
2138}
2139
2145WINPR_ATTR_NODISCARD
2146static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
2147 const RDPGFX_EVICT_CACHE_ENTRY_PDU* evictCacheEntry)
2148{
2149 gdiGfxCacheEntry* cacheEntry = nullptr;
2150 UINT rc = ERROR_NOT_FOUND;
2151
2152 WINPR_ASSERT(context);
2153 WINPR_ASSERT(evictCacheEntry);
2154
2155 EnterCriticalSection(&context->mux);
2156
2157 WINPR_ASSERT(context->GetCacheSlotData);
2158 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
2159
2160 gdi_GfxCacheEntryFree(cacheEntry);
2161
2162 WINPR_ASSERT(context->SetCacheSlotData);
2163 rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot, nullptr);
2164 LeaveCriticalSection(&context->mux);
2165 return rc;
2166}
2167
2173WINPR_ATTR_NODISCARD
2174static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
2175 const RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU* surfaceToOutput)
2176{
2177 UINT rc = ERROR_INTERNAL_ERROR;
2178 gdiGfxSurface* surface = nullptr;
2179 EnterCriticalSection(&context->mux);
2180
2181 WINPR_ASSERT(context->GetSurfaceData);
2182 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
2183
2184 if (!surface)
2185 goto fail;
2186
2187 if (surface->windowMapped)
2188 {
2189 WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
2190 goto fail;
2191 }
2192
2193 surface->outputMapped = TRUE;
2194 surface->outputOriginX = surfaceToOutput->outputOriginX;
2195 surface->outputOriginY = surfaceToOutput->outputOriginY;
2196 surface->outputTargetWidth = surface->mappedWidth;
2197 surface->outputTargetHeight = surface->mappedHeight;
2198 region16_clear(&surface->invalidRegion);
2199 rc = CHANNEL_RC_OK;
2200fail:
2201 LeaveCriticalSection(&context->mux);
2202 return rc;
2203}
2204
2205WINPR_ATTR_NODISCARD
2206static UINT
2207gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
2208 const RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU* surfaceToOutput)
2209{
2210 UINT rc = ERROR_INTERNAL_ERROR;
2211 gdiGfxSurface* surface = nullptr;
2212 EnterCriticalSection(&context->mux);
2213
2214 WINPR_ASSERT(context->GetSurfaceData);
2215 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
2216
2217 if (!surface)
2218 goto fail;
2219
2220 if (surface->windowMapped)
2221 {
2222 WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
2223 goto fail;
2224 }
2225
2226 surface->outputMapped = TRUE;
2227 surface->outputOriginX = surfaceToOutput->outputOriginX;
2228 surface->outputOriginY = surfaceToOutput->outputOriginY;
2229 surface->outputTargetWidth = surfaceToOutput->targetWidth;
2230 surface->outputTargetHeight = surfaceToOutput->targetHeight;
2231 region16_clear(&surface->invalidRegion);
2232 rc = CHANNEL_RC_OK;
2233fail:
2234 LeaveCriticalSection(&context->mux);
2235 return rc;
2236}
2237
2243WINPR_ATTR_NODISCARD
2244static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
2245 const RDPGFX_MAP_SURFACE_TO_WINDOW_PDU* surfaceToWindow)
2246{
2247 UINT rc = ERROR_INTERNAL_ERROR;
2248 gdiGfxSurface* surface = nullptr;
2249 EnterCriticalSection(&context->mux);
2250
2251 WINPR_ASSERT(context->GetSurfaceData);
2252 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2253
2254 if (!surface)
2255 goto fail;
2256
2257 if (surface->outputMapped)
2258 {
2259 WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
2260 goto fail;
2261 }
2262
2263 if (surface->windowMapped)
2264 {
2265 if (surface->windowId != surfaceToWindow->windowId)
2266 {
2267 WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
2268 surface->windowId, surfaceToWindow->windowId);
2269 goto fail;
2270 }
2271 }
2272 surface->windowMapped = TRUE;
2273
2274 surface->windowId = surfaceToWindow->windowId;
2275 surface->mappedWidth = surfaceToWindow->mappedWidth;
2276 if (surface->mappedWidth > surface->width)
2277 {
2278 WLog_WARN(TAG, "surface mappedWidth[%" PRIu32 "] > width[%" PRIu32 "]",
2279 surface->mappedWidth, surface->width);
2280 goto fail;
2281 }
2282 surface->mappedHeight = surfaceToWindow->mappedHeight;
2283 if (surface->mappedHeight > surface->height)
2284 {
2285 WLog_WARN(TAG, "surface mappedHeight[%" PRIu32 "] > height[%" PRIu32 "]",
2286 surface->mappedHeight, surface->height);
2287 goto fail;
2288 }
2289 surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2290 surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2291 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2292 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2293fail:
2294 LeaveCriticalSection(&context->mux);
2295 return rc;
2296}
2297
2298WINPR_ATTR_NODISCARD
2299static UINT
2300gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2301 const RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU* surfaceToWindow)
2302{
2303 UINT rc = ERROR_INTERNAL_ERROR;
2304 gdiGfxSurface* surface = nullptr;
2305 EnterCriticalSection(&context->mux);
2306
2307 WINPR_ASSERT(context->GetSurfaceData);
2308 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2309
2310 if (!surface)
2311 goto fail;
2312
2313 if (surface->outputMapped)
2314 {
2315 WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
2316 goto fail;
2317 }
2318
2319 if (surface->windowMapped)
2320 {
2321 if (surface->windowId != surfaceToWindow->windowId)
2322 {
2323 WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
2324 surface->windowId, surfaceToWindow->windowId);
2325 goto fail;
2326 }
2327 }
2328 surface->windowMapped = TRUE;
2329
2330 surface->windowId = surfaceToWindow->windowId;
2331 surface->mappedWidth = surfaceToWindow->mappedWidth;
2332 if (surface->mappedWidth > surface->width)
2333 {
2334 WLog_WARN(TAG, "surface mappedWidth[%" PRIu32 "] > width[%" PRIu32 "]",
2335 surface->mappedWidth, surface->width);
2336 goto fail;
2337 }
2338
2339 surface->mappedHeight = surfaceToWindow->mappedHeight;
2340 if (surface->mappedHeight > surface->height)
2341 {
2342 WLog_WARN(TAG, "surface mappedHeight[%" PRIu32 "] > height[%" PRIu32 "]",
2343 surface->mappedHeight, surface->height);
2344 goto fail;
2345 }
2346 surface->outputTargetWidth = surfaceToWindow->targetWidth;
2347 surface->outputTargetHeight = surfaceToWindow->targetHeight;
2348 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2349 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2350fail:
2351 LeaveCriticalSection(&context->mux);
2352 return rc;
2353}
2354
2355BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2356{
2357 return gdi_graphics_pipeline_init_ex(gdi, gfx, nullptr, nullptr, nullptr);
2358}
2359
2360BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2361 pcRdpgfxMapWindowForSurface map,
2362 pcRdpgfxUnmapWindowForSurface unmap,
2363 pcRdpgfxUpdateSurfaceArea update)
2364{
2365 if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2366 return FALSE;
2367
2368 rdpContext* context = gdi->context;
2369 rdpSettings* settings = context->settings;
2370
2371 gdi->gfx = gfx;
2372 gfx->custom = (void*)gdi;
2373 gfx->ResetGraphics = gdi_ResetGraphics;
2374 gfx->StartFrame = gdi_StartFrame;
2375 gfx->EndFrame = gdi_EndFrame;
2376 gfx->SurfaceCommand = gdi_SurfaceCommand;
2377 gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
2378 gfx->CreateSurface = gdi_CreateSurface;
2379 gfx->DeleteSurface = gdi_DeleteSurface;
2380 gfx->SolidFill = gdi_SolidFill;
2381 gfx->SurfaceToSurface = gdi_SurfaceToSurface;
2382 gfx->SurfaceToCache = gdi_SurfaceToCache;
2383 gfx->CacheToSurface = gdi_CacheToSurface;
2384 gfx->CacheImportReply = gdi_CacheImportReply;
2385 gfx->ImportCacheEntry = gdi_ImportCacheEntry;
2386 gfx->ExportCacheEntry = gdi_ExportCacheEntry;
2387 gfx->EvictCacheEntry = gdi_EvictCacheEntry;
2388 gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
2389 gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
2390 gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
2391 gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
2392 gfx->UpdateSurfaces = gdi_UpdateSurfaces;
2393 gfx->MapWindowForSurface = map;
2394 gfx->UnmapWindowForSurface = unmap;
2395 gfx->UpdateSurfaceArea = update;
2396
2397 if (!freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
2398 {
2399 const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
2400 const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2401 const UINT32 flags = freerdp_settings_get_uint32(settings, FreeRDP_ThreadingFlags);
2402
2403 gfx->codecs = freerdp_client_codecs_new(flags);
2404 if (!gfx->codecs)
2405 return FALSE;
2406 if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2407 return FALSE;
2408 }
2409 InitializeCriticalSection(&gfx->mux);
2410 PROFILER_CREATE(gfx->SurfaceProfiler, "GFX-PROFILER")
2411
2412
2418 gdi->graphicsReset = TRUE;
2419 if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
2420 {
2421 gfx->UpdateSurfaceArea = nullptr;
2422 gfx->UpdateSurfaces = nullptr;
2423 gfx->SurfaceCommand = nullptr;
2424 }
2425
2426 return TRUE;
2427}
2428
2429void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
2430{
2431 if (gdi)
2432 gdi->gfx = nullptr;
2433
2434 if (!gfx)
2435 return;
2436
2437 gfx->custom = nullptr;
2438 freerdp_client_codecs_free(gfx->codecs);
2439 gfx->codecs = nullptr;
2440 DeleteCriticalSection(&gfx->mux);
2441 PROFILER_PRINT_HEADER
2442 PROFILER_PRINT(gfx->SurfaceProfiler)
2443 PROFILER_PRINT_FOOTER
2444 PROFILER_FREE(gfx->SurfaceProfiler)
2445}
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