22#include <freerdp/config.h>
24#include "../core/update.h"
26#include <winpr/assert.h>
27#include <winpr/cast.h>
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>
36#define TAG FREERDP_TAG("gdi")
40#define GDI_GFX_MAX_INVALID_RECTS 256
43static BOOL is_rect_valid(
const RECTANGLE_16* rect,
size_t width,
size_t height)
47 if ((rect->left > rect->right) || (rect->right > width))
49 if ((rect->top > rect->bottom) || (rect->bottom > height))
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))
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);
72 if (rect.left > surface->width)
74 if (rect.right > surface->width)
76 if (rect.top > surface->height)
78 if (rect.bottom > surface->height)
96static BOOL gdi_surface_invalidate_rect(gdiGfxSurface* surface,
const RECTANGLE_16* rect)
98 WINPR_ASSERT(surface);
100 REGION16* region = &surface->invalidRegion;
101 if (!region16_union_rect(region, region, rect))
104 if (region16_n_rects(region) <= GDI_GFX_MAX_INVALID_RECTS)
108 region16_clear(region);
109 return region16_union_rect(region, region, &extents);
113static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
115 const UINT32 align = alignment;
116 const UINT32 pad = align - (widthInBytes % alignment);
117 UINT32 scanline = widthInBytes;
131static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
134 UINT rc = ERROR_INTERNAL_ERROR;
136 UINT16* pSurfaceIds =
nullptr;
138 WINPR_ASSERT(context);
139 WINPR_ASSERT(resetGraphics);
141 rdpGdi* gdi = (rdpGdi*)context->custom;
144 rdpUpdate* update = gdi->context->update;
145 WINPR_ASSERT(update);
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;
153 WLog_DBG(TAG,
"new size: %" PRIu32
"x%" PRIu32, DesktopWidth, DesktopHeight);
162 WINPR_ASSERT(update->DesktopResize);
165 rdp_update_lock(update);
166 const BOOL resized = update->DesktopResize(gdi->context);
167 rdp_update_unlock(update);
172 WINPR_ASSERT(context->GetSurfaceIds);
173 const UINT res = context->GetSurfaceIds(context, &pSurfaceIds, &count);
174 if (res != CHANNEL_RC_OK)
180 for (UINT32 index = 0; index < count; index++)
182 WINPR_ASSERT(context->GetSurfaceData);
183 gdiGfxSurface* surface =
184 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
189 memset(surface->data, 0xFF, (
size_t)surface->scanline * surface->height);
190 region16_clear(&surface->invalidRegion);
197 const UINT32 width = (UINT32)MAX(0, gdi->width);
198 const UINT32 height = (UINT32)MAX(0, gdi->height);
200 if (!freerdp_client_codecs_reset(
209 LeaveCriticalSection(&context->mux);
214static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
216 UINT rc = ERROR_INTERNAL_ERROR;
222 rdpUpdate* update =
nullptr;
225 WINPR_ASSERT(gdi->context);
226 WINPR_ASSERT(surface);
228 update = gdi->context->update;
229 WINPR_ASSERT(update);
231 if (gdi->suppressOutput)
232 return CHANNEL_RC_OK;
234 surfaceX = surface->outputOriginX;
235 surfaceY = surface->outputOriginY;
236 surfaceRect.left = 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),
244 const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
245 const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
247 rects = region16_rects(&surface->invalidRegion, &nbRects);
248 if (!rects || (nbRects == 0))
254 if (!update_begin_paint(update))
257 for (UINT32 i = 0; i < nbRects; i++)
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;
267 const UINT32 outWidth = (UINT32)MAX(0, gdi->width);
268 const UINT32 outHeight = (UINT32)MAX(0, gdi->height);
269 if ((xDst >= outWidth) || (yDst >= outHeight))
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);
283 if (dwidth > outWidth - nXDst)
285 dwidth = outWidth - nXDst;
286 swidth = MAX(1, MIN(swidth, (UINT32)(dwidth / sx)));
288 if (dheight > outHeight - nYDst)
290 dheight = outHeight - nYDst;
291 sheight = MAX(1, MIN(sheight, (UINT32)(dheight / sy)));
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))
298 rc = CHANNEL_RC_NULL_DATA;
302 if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
310 if (!update_end_paint(update))
311 rc = ERROR_INTERNAL_ERROR;
314 region16_clear(&(surface->invalidRegion));
319static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
321 WINPR_ASSERT(context);
322 WINPR_ASSERT(surface);
323 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
327static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
330 UINT16* pSurfaceIds =
nullptr;
332 WINPR_ASSERT(context);
334 rdpGdi* gdi = (rdpGdi*)context->custom;
337 EnterCriticalSection(&context->mux);
339 WINPR_ASSERT(context->GetSurfaceIds);
340 UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
341 if (status != CHANNEL_RC_OK)
344 for (UINT32 index = 0; index < count; index++)
346 WINPR_ASSERT(context->GetSurfaceData);
347 gdiGfxSurface* surface =
348 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
354 if (context->UpdateSurfaceArea)
356 if (surface->handleInUpdateSurfaceArea)
360 if (surface->outputMapped)
361 status = gdi_OutputUpdate(gdi, surface);
362 else if (surface->windowMapped)
363 status = gdi_WindowUpdate(context, surface);
365 if (status != CHANNEL_RC_OK)
371 LeaveCriticalSection(&context->mux);
383 rdpGdi* gdi =
nullptr;
385 WINPR_ASSERT(context);
386 WINPR_ASSERT(startFrame);
388 gdi = (rdpGdi*)context->custom;
390 gdi->inGfxFrame = TRUE;
391 gdi->frameId = startFrame->frameId;
392 return CHANNEL_RC_OK;
396static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
398 WINPR_ASSERT(context);
399 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
408static UINT gdi_EndFrame(RdpgfxClientContext* context,
411 WINPR_ASSERT(context);
412 WINPR_ASSERT(endFrame);
414 rdpGdi* gdi = (rdpGdi*)context->custom;
416 const UINT status = gdi_call_update_surfaces(context);
417 gdi->inGfxFrame = FALSE;
422static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
425 UINT status = CHANNEL_RC_OK;
426 if (!gdi->inGfxFrame)
427 status = gdi_call_update_surfaces(context);
437static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
440 UINT status = ERROR_INTERNAL_ERROR;
441 gdiGfxSurface* surface =
nullptr;
446 WINPR_ASSERT(context);
449 WINPR_ASSERT(context->GetSurfaceData);
451 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
455 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
456 return ERROR_NOT_FOUND;
459 if (!is_within_surface(surface, cmd))
460 return ERROR_INVALID_DATA;
462 bpp = FreeRDPGetBytesPerPixel(cmd->format);
463 size = 1ull * bpp * cmd->width * cmd->height;
464 if (cmd->length < size)
466 WLog_ERR(TAG,
"Not enough data, got %" PRIu32
", expected %" PRIuz, cmd->length, size);
467 return ERROR_INVALID_DATA;
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;
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))
481 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
484 if (status != CHANNEL_RC_OK)
487 status = gdi_interFrameUpdate(gdi, context);
493#if defined(WITH_GFX_AV1)
495static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
499 UINT status = ERROR_INTERNAL_ERROR;
500 gdiGfxSurface* surface =
nullptr;
504 WINPR_ASSERT(context);
507 WINPR_ASSERT(context->GetSurfaceData);
509 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
513 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
514 return ERROR_NOT_FOUND;
519 surface->av1 = freerdp_av1_context_new(FALSE);
523 WLog_ERR(TAG,
"unable to create av1 context");
524 return ERROR_NOT_ENOUGH_MEMORY;
527 if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
528 return ERROR_INTERNAL_ERROR;
532 return ERROR_NOT_SUPPORTED;
534 if (!is_within_surface(surface, cmd))
535 return ERROR_INVALID_DATA;
540 return ERROR_INTERNAL_ERROR;
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);
549 WLog_WARN(TAG,
"freerdp_av1_decompress failure: %" PRId32
", ignoring update.", rc);
550 return CHANNEL_RC_OK;
553 for (UINT32 i = 0; i < meta->numRegionRects; i++)
555 if (!gdi_surface_invalidate_rect(surface, &(meta->regionRects[i])))
559 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
560 meta->numRegionRects, meta->regionRects);
562 if (status != CHANNEL_RC_OK)
565 status = gdi_interFrameUpdate(gdi, context);
572#if defined(WITH_GFX_AV2)
574static UINT gdi_SurfaceCommand_AV2(rdpGdi* gdi, RdpgfxClientContext* context,
578 UINT status = ERROR_INTERNAL_ERROR;
579 gdiGfxSurface* surface =
nullptr;
583 WINPR_ASSERT(context);
586 WINPR_ASSERT(context->GetSurfaceData);
588 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
592 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
593 return ERROR_NOT_FOUND;
598 surface->av2 = freerdp_av2_context_new(FALSE);
602 WLog_ERR(TAG,
"unable to create av2 context");
603 return ERROR_NOT_ENOUGH_MEMORY;
606 if (!freerdp_av2_context_reset(surface->av2, surface->width, surface->height))
607 return ERROR_INTERNAL_ERROR;
611 return ERROR_NOT_SUPPORTED;
613 if (!is_within_surface(surface, cmd))
614 return ERROR_INVALID_DATA;
619 return ERROR_INTERNAL_ERROR;
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);
628 WLog_WARN(TAG,
"freerdp_av2_decompress failure: %" PRId32
", ignoring update.", rc);
629 return CHANNEL_RC_OK;
632 for (UINT32 i = 0; i < meta->numRegionRects; i++)
634 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
635 &(meta->regionRects[i])))
639 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
640 meta->numRegionRects, meta->regionRects);
642 if (status != CHANNEL_RC_OK)
645 status = gdi_interFrameUpdate(gdi, context);
658static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
661 UINT status = ERROR_INTERNAL_ERROR;
662 gdiGfxSurface* surface =
nullptr;
667 WINPR_ASSERT(context);
670 WINPR_ASSERT(context->GetSurfaceData);
672 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
676 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
677 return ERROR_NOT_FOUND;
680 if (!is_within_surface(surface, cmd))
681 return ERROR_INVALID_DATA;
683 WINPR_ASSERT(surface->codecs);
684 rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
685 region16_init(&invalidRegion);
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,
691 WLog_ERR(TAG,
"Failed to process RemoteFX message");
695 rects = region16_rects(&invalidRegion, &nrRects);
696 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
699 if (status != CHANNEL_RC_OK)
701 status = ERROR_INTERNAL_ERROR;
703 for (UINT32 x = 0; x < nrRects; x++)
705 if (!gdi_surface_invalidate_rect(surface, &rects[x]))
709 status = gdi_interFrameUpdate(gdi, context);
712 region16_uninit(&invalidRegion);
722static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
726 UINT status = ERROR_INTERNAL_ERROR;
727 gdiGfxSurface* surface =
nullptr;
730 WINPR_ASSERT(context);
733 WINPR_ASSERT(context->GetSurfaceData);
735 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
739 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
740 return ERROR_NOT_FOUND;
743 if (!is_within_surface(surface, cmd))
744 return ERROR_INVALID_DATA;
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);
753 WLog_ERR(TAG,
"clear_decompress failure: %" PRId32
"", rc);
754 return ERROR_INTERNAL_ERROR;
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))
763 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
766 if (status != CHANNEL_RC_OK)
769 status = gdi_interFrameUpdate(gdi, context);
781static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
784 UINT status = ERROR_INTERNAL_ERROR;
785 BYTE* DstData =
nullptr;
786 gdiGfxSurface* surface =
nullptr;
789 WINPR_ASSERT(context);
792 WINPR_ASSERT(context->GetSurfaceData);
794 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
798 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
799 return ERROR_NOT_FOUND;
802 DstData = surface->data;
804 if (!is_within_surface(surface, cmd))
805 return ERROR_INVALID_DATA;
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,
811 return ERROR_INTERNAL_ERROR;
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))
819 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
822 if (status != CHANNEL_RC_OK)
825 status = gdi_interFrameUpdate(gdi, context);
837static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
842 UINT status = ERROR_INTERNAL_ERROR;
843 gdiGfxSurface* surface =
nullptr;
847 WINPR_ASSERT(context);
850 WINPR_ASSERT(context->GetSurfaceData);
852 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
856 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
857 return ERROR_NOT_FOUND;
862 surface->h264 = h264_context_new(FALSE);
866 WLog_ERR(TAG,
"unable to create h264 context");
867 return ERROR_NOT_ENOUGH_MEMORY;
870 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
872 FreeRDP_SoftwareGdi) == 0))
873 return ERROR_INTERNAL_ERROR;
875 if (!h264_context_reset(surface->h264, surface->width, surface->height))
876 return ERROR_INTERNAL_ERROR;
880 return ERROR_NOT_SUPPORTED;
882 if (!is_within_surface(surface, cmd))
883 return ERROR_INVALID_DATA;
888 return ERROR_INTERNAL_ERROR;
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);
897 WLog_WARN(TAG,
"avc420_decompress failure: %" PRId32
", ignoring update.", rc);
898 return CHANNEL_RC_OK;
901 for (UINT32 i = 0; i < meta->numRegionRects; i++)
903 if (!gdi_surface_invalidate_rect(surface, &(meta->regionRects[i])))
907 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
908 meta->numRegionRects, meta->regionRects);
910 if (status != CHANNEL_RC_OK)
913 status = gdi_interFrameUpdate(gdi, context);
918 return ERROR_NOT_SUPPORTED;
922#if defined(WITH_GFX_AZURE)
925static UINT gdi_SurfaceCommand_HEVC(rdpGdi* gdi, RdpgfxClientContext* context,
928 UINT status = ERROR_INTERNAL_ERROR;
931 WINPR_ASSERT(context);
934 WINPR_ASSERT(context->GetSurfaceData);
935 gdiGfxSurface* surface =
936 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
940 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
941 return ERROR_NOT_FOUND;
946 surface->hevc = freerdp_hevc_context_new(FALSE);
950 WLog_ERR(TAG,
"unable to create hevc context");
951 return ERROR_NOT_ENOUGH_MEMORY;
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;
959 if (!freerdp_hevc_context_reset(surface->hevc, surface->width, surface->height))
960 return ERROR_INTERNAL_ERROR;
964 return ERROR_NOT_SUPPORTED;
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) !=
972 if (!freerdp_hevc_context_set_option(surface->hevc, FREERDP_HEVC_CONTEXT_OPTION_PROFILE,
975 WLog_ERR(TAG,
"no HEVC %s decoder available", profile ?
"4:4:4" :
"4:2:0");
976 return ERROR_NOT_SUPPORTED;
978 if (!freerdp_hevc_context_reset(surface->hevc, surface->width, surface->height))
979 return ERROR_INTERNAL_ERROR;
982 if (!is_within_surface(surface, cmd))
983 return ERROR_INVALID_DATA;
988 return ERROR_INTERNAL_ERROR;
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);
997 WLog_WARN(TAG,
"freerdp_hevc_decompress failure: %" PRId32
", ignoring update.", rc);
998 return CHANNEL_RC_OK;
1001 for (UINT32 i = 0; i < meta->numRegionRects; i++)
1003 if (!gdi_surface_invalidate_rect(surface, &(meta->regionRects[i])))
1007 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1008 meta->numRegionRects, meta->regionRects);
1010 if (status != CHANNEL_RC_OK)
1013 status = gdi_interFrameUpdate(gdi, context);
1026static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
1031 UINT status = ERROR_INTERNAL_ERROR;
1032 gdiGfxSurface* surface =
nullptr;
1039 WINPR_ASSERT(context);
1042 WINPR_ASSERT(context->GetSurfaceData);
1044 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
1048 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
1049 return ERROR_NOT_FOUND;
1054 surface->h264 = h264_context_new(FALSE);
1058 WLog_ERR(TAG,
"unable to create h264 context");
1059 return ERROR_NOT_ENOUGH_MEMORY;
1062 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
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;
1071 return ERROR_NOT_SUPPORTED;
1073 if (!is_within_surface(surface, cmd))
1074 return ERROR_INVALID_DATA;
1079 return ERROR_INTERNAL_ERROR;
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);
1092 WLog_WARN(TAG,
"avc444_decompress failure: %" PRId32
", ignoring update.", rc);
1093 return CHANNEL_RC_OK;
1096 for (UINT32 i = 0; i < meta1->numRegionRects; i++)
1098 if (!gdi_surface_invalidate_rect(surface, &(meta1->regionRects[i])))
1102 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1103 meta1->numRegionRects, meta1->regionRects);
1105 if (status != CHANNEL_RC_OK)
1107 status = ERROR_INTERNAL_ERROR;
1109 for (UINT32 i = 0; i < meta2->numRegionRects; i++)
1111 if (!gdi_surface_invalidate_rect(surface, &(meta2->regionRects[i])))
1115 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1116 meta2->numRegionRects, meta2->regionRects);
1118 if (status != CHANNEL_RC_OK)
1121 status = gdi_interFrameUpdate(gdi, context);
1126 return ERROR_NOT_SUPPORTED;
1131static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride,
RECTANGLE_16* rect,
1132 UINT32 startOffsetX, UINT32 count, BYTE a)
1136 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
1139 for (
size_t y = rect->top; y < rect->bottom; y++)
1141 BYTE* line = &data[y * stride];
1143 for (
size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
1149 if (written == count)
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);
1171static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
1174 UINT status = ERROR_INTERNAL_ERROR;
1175 UINT16 alphaSig = 0;
1176 UINT16 compressed = 0;
1178 wStream buffer = WINPR_C_ARRAY_INIT;
1180 WINPR_ASSERT(context);
1183 wStream* s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
1185 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1186 return ERROR_INVALID_DATA;
1188 WINPR_ASSERT(context->GetSurfaceData);
1189 gdiGfxSurface* surface =
1190 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
1194 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
1195 return ERROR_NOT_FOUND;
1198 if (!is_within_surface(surface, cmd))
1199 return ERROR_INVALID_DATA;
1201 Stream_Read_UINT16(s, alphaSig);
1202 Stream_Read_UINT16(s, compressed);
1204 if (alphaSig != 0x414C)
1205 return ERROR_INVALID_DATA;
1207 if (compressed == 0)
1209 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
1210 return ERROR_INVALID_DATA;
1212 for (
size_t y = cmd->top; y < cmd->top + cmd->height; y++)
1214 BYTE* line = &surface->data[y * surface->scanline];
1216 for (
size_t x = cmd->left; x < cmd->left + cmd->width; x++)
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);
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;
1241 while (rect.top < rect.bottom)
1246 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1247 return ERROR_INVALID_DATA;
1249 Stream_Read_UINT8(s, a);
1250 Stream_Read_UINT8(s, count);
1254 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1255 return ERROR_INVALID_DATA;
1257 Stream_Read_UINT16(s, count);
1259 if (count >= 0xFFFF)
1261 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1262 return ERROR_INVALID_DATA;
1264 Stream_Read_UINT32(s, count);
1268 if (count > remaining)
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;
1277 if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1278 startOffsetX, count, a))
1279 return ERROR_INTERNAL_ERROR;
1281 startOffsetX += count;
1283 while (startOffsetX >= cmd->width)
1285 startOffsetX -= cmd->width;
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))
1297 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1300 if (status != CHANNEL_RC_OK)
1303 status = gdi_interFrameUpdate(gdi, context);
1309#if defined(WITH_GFX_FRAME_DUMP)
1312 static UINT64 xxx = 0;
1313 const char* path =
"/tmp/dump/";
1315 char fname[1024] = WINPR_C_ARRAY_INIT;
1317 if (_snprintf(fname,
sizeof(fname),
"%s/%08" PRIx64
".raw", path, xxx++) < 0)
1320 FILE* fp = fopen(fname,
"w");
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);
1336 char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1337 (void)fprintf(fp,
"data: %s\n", bdata);
1349static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1353 UINT status = ERROR_INTERNAL_ERROR;
1354 gdiGfxSurface* surface =
nullptr;
1364 WINPR_ASSERT(context);
1366 const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1368 WINPR_ASSERT(context->GetSurfaceData);
1369 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1373 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
1374 return ERROR_NOT_FOUND;
1377 if (!is_within_surface(surface, cmd))
1378 return ERROR_INVALID_DATA;
1380 WINPR_ASSERT(surface->codecs);
1381 rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1386 WLog_ERR(TAG,
"progressive_create_surface_context failure: %" PRId32
"", rc);
1387 return ERROR_INTERNAL_ERROR;
1390 region16_init(&invalidRegion);
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);
1398 WLog_ERR(TAG,
"progressive_decompress failure: %" PRId32
"", rc);
1402 rects = region16_rects(&invalidRegion, &nrRects);
1403 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1406 if (status != CHANNEL_RC_OK)
1409 status = ERROR_INTERNAL_ERROR;
1410 for (UINT32 x = 0; x < nrRects; x++)
1412 if (!gdi_surface_invalidate_rect(surface, &rects[x]))
1416 status = gdi_interFrameUpdate(gdi, context);
1420 region16_uninit(&invalidRegion);
1432 UINT status = CHANNEL_RC_OK;
1433 rdpGdi* gdi =
nullptr;
1435 if (!context || !cmd)
1436 return ERROR_INVALID_PARAMETER;
1438 gdi = (rdpGdi*)context->custom;
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);
1456 case RDPGFX_CODECID_UNCOMPRESSED:
1457 status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1460#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1461 case RDPGFX_CODECID_AV1:
1463 switch (context->ConfirmedCaps.version)
1465#if defined(WITH_GFX_AV1)
1466 case RDPGFX_CAPVERSION_FRDP_1:
1467 status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1470#if defined(WITH_GFX_AV2)
1471 case RDPGFX_CAPVERSION_FRDP_2:
1472 status = gdi_SurfaceCommand_AV2(gdi, context, cmd);
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;
1486 case RDPGFX_CODECID_CAVIDEO:
1487 status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1490 case RDPGFX_CODECID_CLEARCODEC:
1491 status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1494 case RDPGFX_CODECID_PLANAR:
1495 status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1498 case RDPGFX_CODECID_AVC420:
1499 status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1502 case RDPGFX_CODECID_AVC444v2:
1503 case RDPGFX_CODECID_AVC444:
1504 status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1507#if defined(WITH_GFX_AZURE)
1508 case RDPGFX_CODECID_HEVC444:
1509 case RDPGFX_CODECID_HEVC420:
1510 status = gdi_SurfaceCommand_HEVC(gdi, context, cmd);
1514 case RDPGFX_CODECID_ALPHA:
1515 status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1518 case RDPGFX_CODECID_CAPROGRESSIVE:
1519 status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
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);
1528 WLog_WARN(TAG,
"Invalid SurfaceCommand %s [0x%08" PRIX16
"]",
1529 rdpgfx_get_codec_id_string(codecId), codecId);
1533 LeaveCriticalSection(&context->mux);
1544gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1547 WINPR_ASSERT(context);
1548 WINPR_ASSERT(deleteEncodingContext);
1549 WINPR_UNUSED(context);
1550 WINPR_UNUSED(deleteEncodingContext);
1551 return CHANNEL_RC_OK;
1560static UINT gdi_CreateSurface(RdpgfxClientContext* context,
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;
1570 WINPR_ASSERT(gdi->context);
1571 EnterCriticalSection(&context->mux);
1572 surface = (gdiGfxSurface*)calloc(1,
sizeof(gdiGfxSurface));
1579 WINPR_ASSERT(context->codecs);
1580 surface->codecs = context->codecs;
1582 if (!surface->codecs)
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;
1599 switch (createSurface->pixelFormat)
1601 case GFX_PIXEL_FORMAT_ARGB_8888:
1602 surface->format = PIXEL_FORMAT_BGRA32;
1605 case GFX_PIXEL_FORMAT_XRGB_8888:
1606 surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1614 if ((surface->width > UINT32_MAX / 4 - 64) || (surface->height > UINT32_MAX - 64))
1623 surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 64ul;
1624 surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height + 4ull, 16);
1632 for (UINT64 y = 0; y < surface->height; y++)
1633 memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1634 region16_init(&surface->invalidRegion);
1636 WINPR_ASSERT(context->SetSurfaceData);
1637 rc = context->SetSurfaceData(context, surface->surfaceId, (
void*)surface);
1639 LeaveCriticalSection(&context->mux);
1649static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1652 UINT rc = CHANNEL_RC_OK;
1653 UINT res = ERROR_INTERNAL_ERROR;
1654 rdpCodecs* codecs =
nullptr;
1655 gdiGfxSurface* surface =
nullptr;
1656 EnterCriticalSection(&context->mux);
1658 WINPR_ASSERT(context->GetSurfaceData);
1659 surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1663 if (surface->windowMapped)
1664 rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1668 h264_context_free(surface->h264);
1669#if defined(WITH_GFX_AZURE)
1670 freerdp_hevc_context_free(surface->hevc);
1673#if defined(WITH_GFX_AV1)
1674 freerdp_av1_context_free(surface->av1);
1676#if defined(WITH_GFX_AV2)
1677 freerdp_av2_context_free(surface->av2);
1679 region16_uninit(&surface->invalidRegion);
1680 codecs = surface->codecs;
1681 winpr_aligned_free(surface->data);
1685 WINPR_ASSERT(context->SetSurfaceData);
1686 res = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
1690 if (codecs && codecs->progressive)
1691 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1693 LeaveCriticalSection(&context->mux);
1698static BOOL intersect_rect(
const RECTANGLE_16* rect,
const gdiGfxSurface* surface,
1702 WINPR_ASSERT(surface);
1703 WINPR_ASSERT(prect);
1705 if (rect->left > rect->right)
1707 if (rect->left > surface->width)
1709 if (rect->top > rect->bottom)
1711 if (rect->top > surface->height)
1713 prect->left = rect->left;
1714 prect->top = rect->top;
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));
1729 UINT status = ERROR_INTERNAL_ERROR;
1732 rdpGdi* gdi = (rdpGdi*)context->custom;
1734 EnterCriticalSection(&context->mux);
1736 WINPR_ASSERT(context->GetSurfaceData);
1737 gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1743 const BYTE b = solidFill->fillPixel.B;
1744 const BYTE g = solidFill->fillPixel.G;
1745 const BYTE r = solidFill->fillPixel.R;
1752 const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1753 for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1755 const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1757 if (!intersect_rect(rect, surface, &invalidRect))
1760 const UINT32 nWidth = invalidRect.right - invalidRect.left;
1761 const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1763 if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1764 invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1767 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
1772 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1773 solidFill->fillRectCount, solidFill->fillRects);
1775 if (status != CHANNEL_RC_OK)
1778 LeaveCriticalSection(&context->mux);
1780 return gdi_interFrameUpdate(gdi, context);
1782 LeaveCriticalSection(&context->mux);
1792static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1795 UINT status = ERROR_INTERNAL_ERROR;
1796 BOOL sameSurface = 0;
1799 gdiGfxSurface* surfaceSrc =
nullptr;
1800 gdiGfxSurface* surfaceDst =
nullptr;
1801 rdpGdi* gdi = (rdpGdi*)context->custom;
1802 EnterCriticalSection(&context->mux);
1803 rectSrc = &(surfaceToSurface->rectSrc);
1805 WINPR_ASSERT(context->GetSurfaceData);
1806 surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1807 sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1811 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1813 surfaceDst = surfaceSrc;
1815 if (!surfaceSrc || !surfaceDst)
1818 if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1822 const UINT32 nWidth = rectSrc->right - rectSrc->left;
1823 const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1825 for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1827 const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
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))
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))
1843 if (!gdi_surface_invalidate_rect(surfaceDst, &invalidRect))
1845 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1846 surfaceDst->surfaceId, 1, &invalidRect);
1848 if (status != CHANNEL_RC_OK)
1850 status = ERROR_INTERNAL_ERROR;
1854 LeaveCriticalSection(&context->mux);
1856 return gdi_interFrameUpdate(gdi, context);
1858 LeaveCriticalSection(&context->mux);
1862static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1870WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1871static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1874 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1,
sizeof(gdiGfxCacheEntry));
1878 cacheEntry->cacheKey = cacheKey;
1879 cacheEntry->width = width;
1880 cacheEntry->height = height;
1881 cacheEntry->format = format;
1883 const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1884 cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1886 if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1888 cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1890 if (!cacheEntry->data)
1895 gdi_GfxCacheEntryFree(cacheEntry);
1905static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1908 gdiGfxCacheEntry* cacheEntry =
nullptr;
1909 UINT rc = ERROR_INTERNAL_ERROR;
1910 EnterCriticalSection(&context->mux);
1913 WINPR_ASSERT(context->GetSurfaceData);
1914 gdiGfxSurface* surface =
1915 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1920 if (!is_rect_valid(rect, surface->width, surface->height))
1923 cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1924 (UINT32)(rect->bottom - rect->top), surface->format);
1929 if (!cacheEntry->data)
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))
1940 WINPR_ASSERT(context->EvictCacheEntry);
1941 rc = context->EvictCacheEntry(context, &evict);
1942 if (rc != CHANNEL_RC_OK)
1946 WINPR_ASSERT(context->SetCacheSlotData);
1947 rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (
void*)cacheEntry);
1949 if (rc != CHANNEL_RC_OK)
1950 gdi_GfxCacheEntryFree(cacheEntry);
1951 LeaveCriticalSection(&context->mux);
1961static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1964 UINT status = ERROR_INTERNAL_ERROR;
1965 gdiGfxSurface* surface =
nullptr;
1966 gdiGfxCacheEntry* cacheEntry =
nullptr;
1968 rdpGdi* gdi = (rdpGdi*)context->custom;
1970 EnterCriticalSection(&context->mux);
1972 WINPR_ASSERT(context->GetSurfaceData);
1973 surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1975 WINPR_ASSERT(context->GetCacheSlotData);
1976 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1978 if (!surface || !cacheEntry)
1981 for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1985 (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1986 (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1988 if (rectangle_is_empty(&rect))
1991 if (!is_rect_valid(&rect, surface->width, surface->height))
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,
2003 if (!gdi_surface_invalidate_rect(surface, &invalidRect))
2005 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
2006 surface->surfaceId, 1, &invalidRect);
2008 if (status != CHANNEL_RC_OK)
2010 status = ERROR_INTERNAL_ERROR;
2013 LeaveCriticalSection(&context->mux);
2015 return gdi_interFrameUpdate(gdi, context);
2018 LeaveCriticalSection(&context->mux);
2028static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
2032 const UINT16* slots =
nullptr;
2033 UINT error = CHANNEL_RC_OK;
2035 slots = cacheImportReply->cacheSlots;
2036 count = cacheImportReply->importedEntriesCount;
2038 for (UINT16 index = 0; index < count; index++)
2040 UINT16 cacheSlot = slots[index];
2045 WINPR_ASSERT(context->GetCacheSlotData);
2046 gdiGfxCacheEntry* cacheEntry =
2047 (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
2052 cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
2055 return ERROR_INTERNAL_ERROR;
2057 WINPR_ASSERT(context->SetCacheSlotData);
2058 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
2062 WLog_ERR(TAG,
"CacheImportReply: SetCacheSlotData failed with error %" PRIu32
"",
2064 gdi_GfxCacheEntryFree(cacheEntry);
2073static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
2076 UINT error = ERROR_INTERNAL_ERROR;
2077 gdiGfxCacheEntry* cacheEntry =
nullptr;
2080 return CHANNEL_RC_OK;
2082 cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
2083 importCacheEntry->height, PIXEL_FORMAT_BGRX32);
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))
2096 WINPR_ASSERT(context->EvictCacheEntry);
2097 error = context->EvictCacheEntry(context, &evict);
2099 if (error != CHANNEL_RC_OK)
2102 WINPR_ASSERT(context->SetCacheSlotData);
2103 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
2108 gdi_GfxCacheEntryFree(cacheEntry);
2109 WLog_ERR(TAG,
"ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32
"", error);
2116static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
2119 WINPR_ASSERT(context->GetCacheSlotData);
2120 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
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;
2131 exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
2132 exportCacheEntry->flags = 0;
2133 exportCacheEntry->data = cacheEntry->data;
2134 return CHANNEL_RC_OK;
2137 return ERROR_NOT_FOUND;
2146static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
2149 gdiGfxCacheEntry* cacheEntry =
nullptr;
2150 UINT rc = ERROR_NOT_FOUND;
2152 WINPR_ASSERT(context);
2153 WINPR_ASSERT(evictCacheEntry);
2155 EnterCriticalSection(&context->mux);
2157 WINPR_ASSERT(context->GetCacheSlotData);
2158 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
2160 gdi_GfxCacheEntryFree(cacheEntry);
2162 WINPR_ASSERT(context->SetCacheSlotData);
2163 rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot,
nullptr);
2164 LeaveCriticalSection(&context->mux);
2174static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
2177 UINT rc = ERROR_INTERNAL_ERROR;
2178 gdiGfxSurface* surface =
nullptr;
2179 EnterCriticalSection(&context->mux);
2181 WINPR_ASSERT(context->GetSurfaceData);
2182 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
2187 if (surface->windowMapped)
2189 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
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);
2201 LeaveCriticalSection(&context->mux);
2207gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
2210 UINT rc = ERROR_INTERNAL_ERROR;
2211 gdiGfxSurface* surface =
nullptr;
2212 EnterCriticalSection(&context->mux);
2214 WINPR_ASSERT(context->GetSurfaceData);
2215 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
2220 if (surface->windowMapped)
2222 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
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);
2234 LeaveCriticalSection(&context->mux);
2244static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
2247 UINT rc = ERROR_INTERNAL_ERROR;
2248 gdiGfxSurface* surface =
nullptr;
2249 EnterCriticalSection(&context->mux);
2251 WINPR_ASSERT(context->GetSurfaceData);
2252 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2257 if (surface->outputMapped)
2259 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
2263 if (surface->windowMapped)
2265 if (surface->windowId != surfaceToWindow->windowId)
2267 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
2268 surface->windowId, surfaceToWindow->windowId);
2272 surface->windowMapped = TRUE;
2274 surface->windowId = surfaceToWindow->windowId;
2275 surface->mappedWidth = surfaceToWindow->mappedWidth;
2276 if (surface->mappedWidth > surface->width)
2278 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2279 surface->mappedWidth, surface->width);
2282 surface->mappedHeight = surfaceToWindow->mappedHeight;
2283 if (surface->mappedHeight > surface->height)
2285 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2286 surface->mappedHeight, surface->height);
2289 surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2290 surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2291 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2292 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2294 LeaveCriticalSection(&context->mux);
2300gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2303 UINT rc = ERROR_INTERNAL_ERROR;
2304 gdiGfxSurface* surface =
nullptr;
2305 EnterCriticalSection(&context->mux);
2307 WINPR_ASSERT(context->GetSurfaceData);
2308 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2313 if (surface->outputMapped)
2315 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
2319 if (surface->windowMapped)
2321 if (surface->windowId != surfaceToWindow->windowId)
2323 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
2324 surface->windowId, surfaceToWindow->windowId);
2328 surface->windowMapped = TRUE;
2330 surface->windowId = surfaceToWindow->windowId;
2331 surface->mappedWidth = surfaceToWindow->mappedWidth;
2332 if (surface->mappedWidth > surface->width)
2334 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2335 surface->mappedWidth, surface->width);
2339 surface->mappedHeight = surfaceToWindow->mappedHeight;
2340 if (surface->mappedHeight > surface->height)
2342 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2343 surface->mappedHeight, surface->height);
2346 surface->outputTargetWidth = surfaceToWindow->targetWidth;
2347 surface->outputTargetHeight = surfaceToWindow->targetHeight;
2348 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2349 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2351 LeaveCriticalSection(&context->mux);
2355BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2357 return gdi_graphics_pipeline_init_ex(gdi, gfx,
nullptr,
nullptr,
nullptr);
2360BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2361 pcRdpgfxMapWindowForSurface map,
2362 pcRdpgfxUnmapWindowForSurface unmap,
2363 pcRdpgfxUpdateSurfaceArea update)
2365 if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2368 rdpContext* context = gdi->context;
2369 rdpSettings* settings = context->settings;
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;
2403 gfx->codecs = freerdp_client_codecs_new(flags);
2406 if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2409 InitializeCriticalSection(&gfx->mux);
2410 PROFILER_CREATE(gfx->SurfaceProfiler,
"GFX-PROFILER")
2418 gdi->graphicsReset = TRUE;
2421 gfx->UpdateSurfaceArea =
nullptr;
2422 gfx->UpdateSurfaces =
nullptr;
2423 gfx->SurfaceCommand =
nullptr;
2429void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
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)
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.