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")
39static BOOL is_rect_valid(
const RECTANGLE_16* rect,
size_t width,
size_t height)
43 if ((rect->left > rect->right) || (rect->right > width))
45 if ((rect->top > rect->bottom) || (rect->bottom > height))
56 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
57 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
58 rect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
59 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
60 if (!is_rect_valid(&rect, surface->width, surface->height))
63 "Command rect %" PRIu32
"x%" PRIu32
"-%" PRIu32
"x%" PRIu32
64 " not within bounds of %" PRIu32
"x%" PRIu32,
65 rect.left, rect.top, cmd->width, cmd->height, surface->width, surface->height);
68 if (rect.left > surface->width)
70 if (rect.right > surface->width)
72 if (rect.top > surface->height)
74 if (rect.bottom > surface->height)
81static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
83 const UINT32 align = alignment;
84 const UINT32 pad = align - (widthInBytes % alignment);
85 UINT32 scanline = widthInBytes;
99static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
102 UINT rc = ERROR_INTERNAL_ERROR;
104 UINT16* pSurfaceIds =
nullptr;
106 WINPR_ASSERT(context);
107 WINPR_ASSERT(resetGraphics);
109 rdpGdi* gdi = (rdpGdi*)context->custom;
112 rdpUpdate* update = gdi->context->update;
113 WINPR_ASSERT(update);
115 rdpSettings* settings = gdi->context->settings;
116 WINPR_ASSERT(settings);
117 EnterCriticalSection(&context->mux);
118 const UINT32 DesktopWidth = resetGraphics->width;
119 const UINT32 DesktopHeight = resetGraphics->height;
121 WLog_DBG(TAG,
"new size: %" PRIu32
"x%" PRIu32, DesktopWidth, DesktopHeight);
130 WINPR_ASSERT(update->DesktopResize);
131 if (!update->DesktopResize(gdi->context))
135 WINPR_ASSERT(context->GetSurfaceIds);
136 const UINT res = context->GetSurfaceIds(context, &pSurfaceIds, &count);
137 if (res != CHANNEL_RC_OK)
143 for (UINT32 index = 0; index < count; index++)
145 WINPR_ASSERT(context->GetSurfaceData);
146 gdiGfxSurface* surface =
147 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
152 memset(surface->data, 0xFF, (
size_t)surface->scanline * surface->height);
153 region16_clear(&surface->invalidRegion);
160 const UINT32 width = (UINT32)MAX(0, gdi->width);
161 const UINT32 height = (UINT32)MAX(0, gdi->height);
163 if (!freerdp_client_codecs_reset(
172 LeaveCriticalSection(&context->mux);
177static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
179 UINT rc = ERROR_INTERNAL_ERROR;
185 rdpUpdate* update =
nullptr;
188 WINPR_ASSERT(gdi->context);
189 WINPR_ASSERT(surface);
191 update = gdi->context->update;
192 WINPR_ASSERT(update);
194 if (gdi->suppressOutput)
195 return CHANNEL_RC_OK;
197 surfaceX = surface->outputOriginX;
198 surfaceY = surface->outputOriginY;
199 surfaceRect.left = 0;
201 surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
202 surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
203 if (!region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion),
207 const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
208 const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
210 rects = region16_rects(&surface->invalidRegion, &nbRects);
211 if (!rects || (nbRects == 0))
217 if (!update_begin_paint(update))
220 for (UINT32 i = 0; i < nbRects; i++)
222 const UINT32 nXSrc = rects[i].left;
223 const UINT32 nYSrc = rects[i].top;
224 const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
225 const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
226 const UINT32 swidth = rects[i].right - rects[i].left;
227 const UINT32 sheight = rects[i].bottom - rects[i].top;
228 const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
229 const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
231 if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
232 dwidth, dheight, surface->data, surface->format, surface->scanline,
233 nXSrc, nYSrc, swidth, sheight))
235 rc = CHANNEL_RC_NULL_DATA;
239 if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
247 if (!update_end_paint(update))
248 rc = ERROR_INTERNAL_ERROR;
251 region16_clear(&(surface->invalidRegion));
256static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
258 WINPR_ASSERT(context);
259 WINPR_ASSERT(surface);
260 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
264static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
267 UINT16* pSurfaceIds =
nullptr;
269 WINPR_ASSERT(context);
271 rdpGdi* gdi = (rdpGdi*)context->custom;
274 EnterCriticalSection(&context->mux);
276 WINPR_ASSERT(context->GetSurfaceIds);
277 UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
278 if (status != CHANNEL_RC_OK)
281 for (UINT32 index = 0; index < count; index++)
283 WINPR_ASSERT(context->GetSurfaceData);
284 gdiGfxSurface* surface =
285 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
291 if (context->UpdateSurfaceArea)
293 if (surface->handleInUpdateSurfaceArea)
297 if (surface->outputMapped)
298 status = gdi_OutputUpdate(gdi, surface);
299 else if (surface->windowMapped)
300 status = gdi_WindowUpdate(context, surface);
302 if (status != CHANNEL_RC_OK)
308 LeaveCriticalSection(&context->mux);
320 rdpGdi* gdi =
nullptr;
322 WINPR_ASSERT(context);
323 WINPR_ASSERT(startFrame);
325 gdi = (rdpGdi*)context->custom;
327 gdi->inGfxFrame = TRUE;
328 gdi->frameId = startFrame->frameId;
329 return CHANNEL_RC_OK;
333static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
335 WINPR_ASSERT(context);
336 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
345static UINT gdi_EndFrame(RdpgfxClientContext* context,
348 WINPR_ASSERT(context);
349 WINPR_ASSERT(endFrame);
351 rdpGdi* gdi = (rdpGdi*)context->custom;
353 const UINT status = gdi_call_update_surfaces(context);
354 gdi->inGfxFrame = FALSE;
359static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
362 UINT status = CHANNEL_RC_OK;
363 if (!gdi->inGfxFrame)
364 status = gdi_call_update_surfaces(context);
374static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
377 UINT status = CHANNEL_RC_OK;
378 gdiGfxSurface* surface =
nullptr;
383 WINPR_ASSERT(context);
386 WINPR_ASSERT(context->GetSurfaceData);
388 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
392 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
393 return ERROR_NOT_FOUND;
396 if (!is_within_surface(surface, cmd))
397 return ERROR_INVALID_DATA;
399 bpp = FreeRDPGetBytesPerPixel(cmd->format);
400 size = 1ull * bpp * cmd->width * cmd->height;
401 if (cmd->length < size)
403 WLog_ERR(TAG,
"Not enough data, got %" PRIu32
", expected %" PRIuz, cmd->length, size);
404 return ERROR_INVALID_DATA;
407 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
408 cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
409 0, 0,
nullptr, FREERDP_FLIP_NONE))
410 return ERROR_INTERNAL_ERROR;
412 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
413 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
414 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
415 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
416 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
418 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
421 if (status != CHANNEL_RC_OK)
424 status = gdi_interFrameUpdate(gdi, context);
430#if defined(WITH_GFX_AV1)
432static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
436 UINT status = CHANNEL_RC_OK;
437 gdiGfxSurface* surface =
nullptr;
441 WINPR_ASSERT(context);
444 WINPR_ASSERT(context->GetSurfaceData);
446 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
450 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
451 return ERROR_NOT_FOUND;
456 surface->av1 = freerdp_av1_context_new(FALSE);
460 WLog_ERR(TAG,
"unable to create av1 context");
461 return ERROR_NOT_ENOUGH_MEMORY;
464 if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
465 return ERROR_INTERNAL_ERROR;
469 return ERROR_NOT_SUPPORTED;
471 if (!is_within_surface(surface, cmd))
472 return ERROR_INVALID_DATA;
477 return ERROR_INTERNAL_ERROR;
480 rc = freerdp_av1_decompress(surface->av1, bs->data, bs->length, surface->data, surface->format,
481 surface->scanline, surface->width, surface->height,
482 meta->regionRects, meta->numRegionRects);
486 WLog_WARN(TAG,
"freerdp_av1_decompress failure: %" PRId32
", ignoring update.", rc);
487 return CHANNEL_RC_OK;
490 for (UINT32 i = 0; i < meta->numRegionRects; i++)
492 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
493 &(meta->regionRects[i])))
497 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
498 meta->numRegionRects, meta->regionRects);
500 if (status != CHANNEL_RC_OK)
503 status = gdi_interFrameUpdate(gdi, context);
516static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
519 UINT status = ERROR_INTERNAL_ERROR;
520 gdiGfxSurface* surface =
nullptr;
525 WINPR_ASSERT(context);
528 WINPR_ASSERT(context->GetSurfaceData);
530 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
534 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
535 return ERROR_NOT_FOUND;
538 if (!is_within_surface(surface, cmd))
539 return ERROR_INVALID_DATA;
541 WINPR_ASSERT(surface->codecs);
542 rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
543 region16_init(&invalidRegion);
545 if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
546 surface->data, surface->format, surface->scanline, surface->height,
549 WLog_ERR(TAG,
"Failed to process RemoteFX message");
553 rects = region16_rects(&invalidRegion, &nrRects);
554 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
557 if (status != CHANNEL_RC_OK)
560 for (UINT32 x = 0; x < nrRects; x++)
562 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
566 status = gdi_interFrameUpdate(gdi, context);
569 region16_uninit(&invalidRegion);
579static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
583 UINT status = CHANNEL_RC_OK;
584 gdiGfxSurface* surface =
nullptr;
587 WINPR_ASSERT(context);
590 WINPR_ASSERT(context->GetSurfaceData);
592 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
596 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
597 return ERROR_NOT_FOUND;
600 if (!is_within_surface(surface, cmd))
601 return ERROR_INVALID_DATA;
603 WINPR_ASSERT(surface->codecs);
604 rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
605 surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
606 surface->width, surface->height, &gdi->palette);
610 WLog_ERR(TAG,
"clear_decompress failure: %" PRId32
"", rc);
611 return ERROR_INTERNAL_ERROR;
614 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
615 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
616 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
617 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
618 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
620 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
623 if (status != CHANNEL_RC_OK)
626 status = gdi_interFrameUpdate(gdi, context);
638static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
641 UINT status = CHANNEL_RC_OK;
642 BYTE* DstData =
nullptr;
643 gdiGfxSurface* surface =
nullptr;
646 WINPR_ASSERT(context);
649 WINPR_ASSERT(context->GetSurfaceData);
651 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
655 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
656 return ERROR_NOT_FOUND;
659 DstData = surface->data;
661 if (!is_within_surface(surface, cmd))
662 return ERROR_INVALID_DATA;
664 if (!freerdp_bitmap_decompress_planar(surface->codecs->planar, cmd->data, cmd->length,
665 cmd->width, cmd->height, DstData, surface->format,
666 surface->scanline, cmd->left, cmd->top, cmd->width,
668 return ERROR_INTERNAL_ERROR;
670 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
671 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
672 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
673 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
674 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
676 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
679 if (status != CHANNEL_RC_OK)
682 status = gdi_interFrameUpdate(gdi, context);
694static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
699 UINT status = CHANNEL_RC_OK;
700 gdiGfxSurface* surface =
nullptr;
704 WINPR_ASSERT(context);
707 WINPR_ASSERT(context->GetSurfaceData);
709 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
713 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
714 return ERROR_NOT_FOUND;
719 surface->h264 = h264_context_new(FALSE);
723 WLog_ERR(TAG,
"unable to create h264 context");
724 return ERROR_NOT_ENOUGH_MEMORY;
727 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
729 FreeRDP_SoftwareGdi) == 0))
730 return ERROR_INTERNAL_ERROR;
732 if (!h264_context_reset(surface->h264, surface->width, surface->height))
733 return ERROR_INTERNAL_ERROR;
737 return ERROR_NOT_SUPPORTED;
739 if (!is_within_surface(surface, cmd))
740 return ERROR_INVALID_DATA;
745 return ERROR_INTERNAL_ERROR;
748 rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
749 surface->scanline, surface->width, surface->height, meta->regionRects,
750 meta->numRegionRects);
754 WLog_WARN(TAG,
"avc420_decompress failure: %" PRId32
", ignoring update.", rc);
755 return CHANNEL_RC_OK;
758 for (UINT32 i = 0; i < meta->numRegionRects; i++)
760 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
761 &(meta->regionRects[i])))
765 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
766 meta->numRegionRects, meta->regionRects);
768 if (status != CHANNEL_RC_OK)
771 status = gdi_interFrameUpdate(gdi, context);
776 return ERROR_NOT_SUPPORTED;
786static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
791 UINT status = CHANNEL_RC_OK;
792 gdiGfxSurface* surface =
nullptr;
799 WINPR_ASSERT(context);
802 WINPR_ASSERT(context->GetSurfaceData);
804 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
808 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
809 return ERROR_NOT_FOUND;
814 surface->h264 = h264_context_new(FALSE);
818 WLog_ERR(TAG,
"unable to create h264 context");
819 return ERROR_NOT_ENOUGH_MEMORY;
822 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
824 FreeRDP_SoftwareGdi) == 0))
825 return ERROR_INTERNAL_ERROR;
826 if (!h264_context_reset(surface->h264, surface->width, surface->height))
827 return ERROR_INTERNAL_ERROR;
831 return ERROR_NOT_SUPPORTED;
833 if (!is_within_surface(surface, cmd))
834 return ERROR_INVALID_DATA;
839 return ERROR_INTERNAL_ERROR;
841 avc1 = &bs->bitstream[0];
842 avc2 = &bs->bitstream[1];
845 rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
846 avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
847 avc2->data, avc2->length, surface->data, surface->format,
848 surface->scanline, surface->width, surface->height, cmd->codecId);
852 WLog_WARN(TAG,
"avc444_decompress failure: %" PRId32
", ignoring update.", rc);
853 return CHANNEL_RC_OK;
856 for (UINT32 i = 0; i < meta1->numRegionRects; i++)
858 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
859 &(meta1->regionRects[i])))
863 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
864 meta1->numRegionRects, meta1->regionRects);
866 if (status != CHANNEL_RC_OK)
869 for (UINT32 i = 0; i < meta2->numRegionRects; i++)
871 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
872 &(meta2->regionRects[i])))
876 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
877 meta2->numRegionRects, meta2->regionRects);
879 if (status != CHANNEL_RC_OK)
882 status = gdi_interFrameUpdate(gdi, context);
887 return ERROR_NOT_SUPPORTED;
892static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride,
RECTANGLE_16* rect,
893 UINT32 startOffsetX, UINT32 count, BYTE a)
897 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
900 for (
size_t y = rect->top; y < rect->bottom; y++)
902 BYTE* line = &data[y * stride];
904 for (
size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
910 if (written == count)
913 BYTE* src = &line[x * bpp];
914 UINT32 color = FreeRDPReadColor(src, format);
915 FreeRDPSplitColor(color, format, &r, &g, &b,
nullptr,
nullptr);
916 color = FreeRDPGetColor(format, r, g, b, a);
917 FreeRDPWriteColor(src, format, color);
932static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
935 UINT status = CHANNEL_RC_OK;
937 UINT16 compressed = 0;
939 wStream buffer = WINPR_C_ARRAY_INIT;
941 WINPR_ASSERT(context);
944 wStream* s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
946 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
947 return ERROR_INVALID_DATA;
949 WINPR_ASSERT(context->GetSurfaceData);
950 gdiGfxSurface* surface =
951 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
955 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
956 return ERROR_NOT_FOUND;
959 if (!is_within_surface(surface, cmd))
960 return ERROR_INVALID_DATA;
962 Stream_Read_UINT16(s, alphaSig);
963 Stream_Read_UINT16(s, compressed);
965 if (alphaSig != 0x414C)
966 return ERROR_INVALID_DATA;
970 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
971 return ERROR_INVALID_DATA;
973 for (
size_t y = cmd->top; y < cmd->top + cmd->height; y++)
975 BYTE* line = &surface->data[y * surface->scanline];
977 for (
size_t x = cmd->left; x < cmd->left + cmd->width; x++)
984 BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
985 Stream_Read_UINT8(s, a);
986 color = FreeRDPReadColor(src, surface->format);
987 FreeRDPSplitColor(color, surface->format, &r, &g, &b,
nullptr,
nullptr);
988 color = FreeRDPGetColor(surface->format, r, g, b, a);
989 FreeRDPWriteColor(src, surface->format, color);
995 UINT32 startOffsetX = 0;
997 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
998 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
999 rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);
1000 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);
1002 while (rect.top < rect.bottom)
1007 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1008 return ERROR_INVALID_DATA;
1010 Stream_Read_UINT8(s, a);
1011 Stream_Read_UINT8(s, count);
1015 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1016 return ERROR_INVALID_DATA;
1018 Stream_Read_UINT16(s, count);
1020 if (count >= 0xFFFF)
1022 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1023 return ERROR_INVALID_DATA;
1025 Stream_Read_UINT32(s, count);
1029 if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1030 startOffsetX, count, a))
1031 return ERROR_INTERNAL_ERROR;
1033 startOffsetX += count;
1035 while (startOffsetX >= cmd->width)
1037 startOffsetX -= cmd->width;
1043 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1044 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1045 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
1046 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
1047 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
1049 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1052 if (status != CHANNEL_RC_OK)
1055 status = gdi_interFrameUpdate(gdi, context);
1061#if defined(WITH_GFX_FRAME_DUMP)
1064 static UINT64 xxx = 0;
1065 const char* path =
"/tmp/dump/";
1067 char fname[1024] = WINPR_C_ARRAY_INIT;
1069 if (_snprintf(fname,
sizeof(fname),
"%s/%08" PRIx64
".raw", path, xxx++) < 0)
1072 FILE* fp = fopen(fname,
"w");
1075 (void)fprintf(fp,
"frameid: %" PRIu32
"\n", frameId);
1076 (void)fprintf(fp,
"surfaceId: %" PRIu32
"\n", cmd->surfaceId);
1077 (void)fprintf(fp,
"codecId: %" PRIu32
"\n", cmd->codecId);
1078 (void)fprintf(fp,
"contextId: %" PRIu32
"\n", cmd->contextId);
1079 (void)fprintf(fp,
"format: %" PRIu32
"\n", cmd->format);
1080 (void)fprintf(fp,
"left: %" PRIu32
"\n", cmd->left);
1081 (void)fprintf(fp,
"top: %" PRIu32
"\n", cmd->top);
1082 (void)fprintf(fp,
"right: %" PRIu32
"\n", cmd->right);
1083 (void)fprintf(fp,
"bottom: %" PRIu32
"\n", cmd->bottom);
1084 (void)fprintf(fp,
"width: %" PRIu32
"\n", cmd->width);
1085 (void)fprintf(fp,
"height: %" PRIu32
"\n", cmd->height);
1086 (void)fprintf(fp,
"length: %" PRIu32
"\n", cmd->length);
1088 char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1089 (void)fprintf(fp,
"data: %s\n", bdata);
1101static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1105 UINT status = ERROR_INTERNAL_ERROR;
1106 gdiGfxSurface* surface =
nullptr;
1116 WINPR_ASSERT(context);
1118 const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1120 WINPR_ASSERT(context->GetSurfaceData);
1121 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1125 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
1126 return ERROR_NOT_FOUND;
1129 if (!is_within_surface(surface, cmd))
1130 return ERROR_INVALID_DATA;
1132 WINPR_ASSERT(surface->codecs);
1133 rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1138 WLog_ERR(TAG,
"progressive_create_surface_context failure: %" PRId32
"", rc);
1139 return ERROR_INTERNAL_ERROR;
1142 region16_init(&invalidRegion);
1144 rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
1145 surface->format, surface->scanline, cmd->left, cmd->top,
1146 &invalidRegion, surfaceId, gdi->frameId);
1150 WLog_ERR(TAG,
"progressive_decompress failure: %" PRId32
"", rc);
1154 rects = region16_rects(&invalidRegion, &nrRects);
1155 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1158 if (status != CHANNEL_RC_OK)
1161 status = ERROR_INTERNAL_ERROR;
1162 for (UINT32 x = 0; x < nrRects; x++)
1164 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
1168 status = gdi_interFrameUpdate(gdi, context);
1172 region16_uninit(&invalidRegion);
1184 UINT status = CHANNEL_RC_OK;
1185 rdpGdi* gdi =
nullptr;
1187 if (!context || !cmd)
1188 return ERROR_INVALID_PARAMETER;
1190 gdi = (rdpGdi*)context->custom;
1192 EnterCriticalSection(&context->mux);
1193 const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1194 WLog_Print(gdi->log, WLOG_TRACE,
1195 "surfaceId=%" PRIu32
", codec=%s [%" PRIu32
"], contextId=%" PRIu32
", format=%s, "
1196 "left=%" PRIu32
", top=%" PRIu32
", right=%" PRIu32
", bottom=%" PRIu32
1197 ", width=%" PRIu32
", height=%" PRIu32
" "
1198 "length=%" PRIu32
", data=%p, extra=%p",
1199 cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1200 FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1201 cmd->width, cmd->height, cmd->length, (
void*)cmd->data, (
void*)cmd->extra);
1202#if defined(WITH_GFX_FRAME_DUMP)
1203 dump_cmd(cmd, gdi->frameId);
1208 case RDPGFX_CODECID_UNCOMPRESSED:
1209 status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1212#if defined(WITH_GFX_AV1)
1213 case RDPGFX_CODECID_AV1:
1214 status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1218 case RDPGFX_CODECID_CAVIDEO:
1219 status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1222 case RDPGFX_CODECID_CLEARCODEC:
1223 status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1226 case RDPGFX_CODECID_PLANAR:
1227 status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1230 case RDPGFX_CODECID_AVC420:
1231 status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1234 case RDPGFX_CODECID_AVC444v2:
1235 case RDPGFX_CODECID_AVC444:
1236 status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1239 case RDPGFX_CODECID_ALPHA:
1240 status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1243 case RDPGFX_CODECID_CAPROGRESSIVE:
1244 status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1247 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1248 WLog_WARN(TAG,
"SurfaceCommand %s [0x%08" PRIX16
"] not implemented",
1249 rdpgfx_get_codec_id_string(codecId), codecId);
1253 WLog_WARN(TAG,
"Invalid SurfaceCommand %s [0x%08" PRIX16
"]",
1254 rdpgfx_get_codec_id_string(codecId), codecId);
1258 LeaveCriticalSection(&context->mux);
1269gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1272 WINPR_ASSERT(context);
1273 WINPR_ASSERT(deleteEncodingContext);
1274 WINPR_UNUSED(context);
1275 WINPR_UNUSED(deleteEncodingContext);
1276 return CHANNEL_RC_OK;
1285static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1288 UINT rc = ERROR_INTERNAL_ERROR;
1289 gdiGfxSurface* surface =
nullptr;
1290 rdpGdi* gdi =
nullptr;
1291 WINPR_ASSERT(context);
1292 WINPR_ASSERT(createSurface);
1293 gdi = (rdpGdi*)context->custom;
1295 WINPR_ASSERT(gdi->context);
1296 EnterCriticalSection(&context->mux);
1297 surface = (gdiGfxSurface*)calloc(1,
sizeof(gdiGfxSurface));
1304 WINPR_ASSERT(context->codecs);
1305 surface->codecs = context->codecs;
1307 if (!surface->codecs)
1314 surface->surfaceId = createSurface->surfaceId;
1315 surface->width = gfx_align_scanline(createSurface->width, 16);
1316 surface->height = gfx_align_scanline(createSurface->height, 16);
1317 surface->mappedWidth = createSurface->width;
1318 surface->mappedHeight = createSurface->height;
1319 surface->outputTargetWidth = createSurface->width;
1320 surface->outputTargetHeight = createSurface->height;
1324 switch (createSurface->pixelFormat)
1326 case GFX_PIXEL_FORMAT_ARGB_8888:
1327 surface->format = PIXEL_FORMAT_BGRA32;
1330 case GFX_PIXEL_FORMAT_XRGB_8888:
1331 surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1339 if (surface->width > UINT32_MAX / 4 - 2 * 16)
1347 surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 16ul;
1348 surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height, 16);
1356 for (UINT64 y = 0; y < surface->height; y++)
1357 memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1358 region16_init(&surface->invalidRegion);
1360 WINPR_ASSERT(context->SetSurfaceData);
1361 rc = context->SetSurfaceData(context, surface->surfaceId, (
void*)surface);
1363 LeaveCriticalSection(&context->mux);
1373static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1376 UINT rc = CHANNEL_RC_OK;
1377 UINT res = ERROR_INTERNAL_ERROR;
1378 rdpCodecs* codecs =
nullptr;
1379 gdiGfxSurface* surface =
nullptr;
1380 EnterCriticalSection(&context->mux);
1382 WINPR_ASSERT(context->GetSurfaceData);
1383 surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1387 if (surface->windowMapped)
1388 rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1392 h264_context_free(surface->h264);
1394#if defined(WITH_GFX_AV1)
1395 freerdp_av1_context_free(surface->av1);
1397 region16_uninit(&surface->invalidRegion);
1398 codecs = surface->codecs;
1399 winpr_aligned_free(surface->data);
1403 WINPR_ASSERT(context->SetSurfaceData);
1404 res = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
1408 if (codecs && codecs->progressive)
1409 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1411 LeaveCriticalSection(&context->mux);
1416static BOOL intersect_rect(
const RECTANGLE_16* rect,
const gdiGfxSurface* surface,
1420 WINPR_ASSERT(surface);
1421 WINPR_ASSERT(prect);
1423 if (rect->left > rect->right)
1425 if (rect->left > surface->width)
1427 if (rect->top > rect->bottom)
1429 if (rect->top > surface->height)
1431 prect->left = rect->left;
1432 prect->top = rect->top;
1434 prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1435 prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1447 UINT status = ERROR_INTERNAL_ERROR;
1450 rdpGdi* gdi = (rdpGdi*)context->custom;
1452 EnterCriticalSection(&context->mux);
1454 WINPR_ASSERT(context->GetSurfaceData);
1455 gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1461 const BYTE b = solidFill->fillPixel.B;
1462 const BYTE g = solidFill->fillPixel.G;
1463 const BYTE r = solidFill->fillPixel.R;
1470 const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1471 for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1473 const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1475 if (!intersect_rect(rect, surface, &invalidRect))
1478 const UINT32 nWidth = invalidRect.right - invalidRect.left;
1479 const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1481 if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1482 invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1485 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
1491 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1492 solidFill->fillRectCount, solidFill->fillRects);
1494 if (status != CHANNEL_RC_OK)
1497 LeaveCriticalSection(&context->mux);
1499 return gdi_interFrameUpdate(gdi, context);
1501 LeaveCriticalSection(&context->mux);
1511static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1514 UINT status = ERROR_INTERNAL_ERROR;
1515 BOOL sameSurface = 0;
1518 gdiGfxSurface* surfaceSrc =
nullptr;
1519 gdiGfxSurface* surfaceDst =
nullptr;
1520 rdpGdi* gdi = (rdpGdi*)context->custom;
1521 EnterCriticalSection(&context->mux);
1522 rectSrc = &(surfaceToSurface->rectSrc);
1524 WINPR_ASSERT(context->GetSurfaceData);
1525 surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1526 sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1530 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1532 surfaceDst = surfaceSrc;
1534 if (!surfaceSrc || !surfaceDst)
1537 if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1541 const UINT32 nWidth = rectSrc->right - rectSrc->left;
1542 const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1544 for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1546 const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1548 (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1549 (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1550 if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1553 const UINT32 rwidth = rect.right - rect.left;
1554 const UINT32 rheight = rect.bottom - rect.top;
1555 if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1556 destPt->x, destPt->y, rwidth, rheight, surfaceSrc->data,
1557 surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1558 rectSrc->top,
nullptr, FREERDP_FLIP_NONE))
1562 if (!region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion,
1565 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1566 surfaceDst->surfaceId, 1, &invalidRect);
1568 if (status != CHANNEL_RC_OK)
1573 LeaveCriticalSection(&context->mux);
1575 return gdi_interFrameUpdate(gdi, context);
1577 LeaveCriticalSection(&context->mux);
1581static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1589WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1590static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1593 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1,
sizeof(gdiGfxCacheEntry));
1597 cacheEntry->cacheKey = cacheKey;
1598 cacheEntry->width = width;
1599 cacheEntry->height = height;
1600 cacheEntry->format = format;
1602 const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1603 cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1605 if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1607 cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1609 if (!cacheEntry->data)
1614 gdi_GfxCacheEntryFree(cacheEntry);
1624static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1627 gdiGfxCacheEntry* cacheEntry =
nullptr;
1628 UINT rc = ERROR_INTERNAL_ERROR;
1629 EnterCriticalSection(&context->mux);
1632 WINPR_ASSERT(context->GetSurfaceData);
1633 gdiGfxSurface* surface =
1634 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1639 if (!is_rect_valid(rect, surface->width, surface->height))
1642 cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1643 (UINT32)(rect->bottom - rect->top), surface->format);
1648 if (!cacheEntry->data)
1651 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1652 0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1653 surface->format, surface->scanline, rect->left, rect->top,
1654 nullptr, FREERDP_FLIP_NONE))
1659 WINPR_ASSERT(context->EvictCacheEntry);
1660 rc = context->EvictCacheEntry(context, &evict);
1661 if (rc != CHANNEL_RC_OK)
1665 WINPR_ASSERT(context->SetCacheSlotData);
1666 rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (
void*)cacheEntry);
1668 if (rc != CHANNEL_RC_OK)
1669 gdi_GfxCacheEntryFree(cacheEntry);
1670 LeaveCriticalSection(&context->mux);
1680static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1683 UINT status = ERROR_INTERNAL_ERROR;
1684 gdiGfxSurface* surface =
nullptr;
1685 gdiGfxCacheEntry* cacheEntry =
nullptr;
1687 rdpGdi* gdi = (rdpGdi*)context->custom;
1689 EnterCriticalSection(&context->mux);
1691 WINPR_ASSERT(context->GetSurfaceData);
1692 surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1694 WINPR_ASSERT(context->GetCacheSlotData);
1695 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1697 if (!surface || !cacheEntry)
1700 for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1704 (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1705 (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1707 if (rectangle_is_empty(&rect))
1710 if (!is_rect_valid(&rect, surface->width, surface->height))
1713 const UINT32 w = rect.right - rect.left;
1714 const UINT32 h = rect.bottom - rect.top;
1715 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1716 destPt->x, destPt->y, w, h, cacheEntry->data,
1717 cacheEntry->format, cacheEntry->scanline, 0, 0,
nullptr,
1722 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect))
1724 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1725 surface->surfaceId, 1, &invalidRect);
1727 if (status != CHANNEL_RC_OK)
1731 LeaveCriticalSection(&context->mux);
1733 return gdi_interFrameUpdate(gdi, context);
1736 LeaveCriticalSection(&context->mux);
1746static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
1750 const UINT16* slots =
nullptr;
1751 UINT error = CHANNEL_RC_OK;
1753 slots = cacheImportReply->cacheSlots;
1754 count = cacheImportReply->importedEntriesCount;
1756 for (UINT16 index = 0; index < count; index++)
1758 UINT16 cacheSlot = slots[index];
1763 WINPR_ASSERT(context->GetCacheSlotData);
1764 gdiGfxCacheEntry* cacheEntry =
1765 (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1770 cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
1773 return ERROR_INTERNAL_ERROR;
1775 WINPR_ASSERT(context->SetCacheSlotData);
1776 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
1780 WLog_ERR(TAG,
"CacheImportReply: SetCacheSlotData failed with error %" PRIu32
"",
1782 gdi_GfxCacheEntryFree(cacheEntry);
1791static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1794 UINT error = ERROR_INTERNAL_ERROR;
1795 gdiGfxCacheEntry* cacheEntry =
nullptr;
1798 return CHANNEL_RC_OK;
1800 cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
1801 importCacheEntry->height, PIXEL_FORMAT_BGRX32);
1806 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1807 0, 0, cacheEntry->width, cacheEntry->height,
1808 importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0,
1809 nullptr, FREERDP_FLIP_NONE))
1814 WINPR_ASSERT(context->EvictCacheEntry);
1815 error = context->EvictCacheEntry(context, &evict);
1817 if (error != CHANNEL_RC_OK)
1820 WINPR_ASSERT(context->SetCacheSlotData);
1821 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
1826 gdi_GfxCacheEntryFree(cacheEntry);
1827 WLog_ERR(TAG,
"ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32
"", error);
1834static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1837 WINPR_ASSERT(context->GetCacheSlotData);
1838 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1842 exportCacheEntry->key64 = cacheEntry->cacheKey;
1843 exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
1844 exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
1845 const UINT64 size = 4ull * cacheEntry->width * cacheEntry->height;
1846 if (size > UINT32_MAX)
1847 return ERROR_NOT_FOUND;
1849 exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
1850 exportCacheEntry->flags = 0;
1851 exportCacheEntry->data = cacheEntry->data;
1852 return CHANNEL_RC_OK;
1855 return ERROR_NOT_FOUND;
1864static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
1867 gdiGfxCacheEntry* cacheEntry =
nullptr;
1868 UINT rc = ERROR_NOT_FOUND;
1870 WINPR_ASSERT(context);
1871 WINPR_ASSERT(evictCacheEntry);
1873 EnterCriticalSection(&context->mux);
1875 WINPR_ASSERT(context->GetCacheSlotData);
1876 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
1878 gdi_GfxCacheEntryFree(cacheEntry);
1880 WINPR_ASSERT(context->SetCacheSlotData);
1881 rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot,
nullptr);
1882 LeaveCriticalSection(&context->mux);
1892static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
1895 UINT rc = ERROR_INTERNAL_ERROR;
1896 gdiGfxSurface* surface =
nullptr;
1897 EnterCriticalSection(&context->mux);
1899 WINPR_ASSERT(context->GetSurfaceData);
1900 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1905 if (surface->windowMapped)
1907 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
1911 surface->outputMapped = TRUE;
1912 surface->outputOriginX = surfaceToOutput->outputOriginX;
1913 surface->outputOriginY = surfaceToOutput->outputOriginY;
1914 surface->outputTargetWidth = surface->mappedWidth;
1915 surface->outputTargetHeight = surface->mappedHeight;
1916 region16_clear(&surface->invalidRegion);
1919 LeaveCriticalSection(&context->mux);
1925gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
1928 UINT rc = ERROR_INTERNAL_ERROR;
1929 gdiGfxSurface* surface =
nullptr;
1930 EnterCriticalSection(&context->mux);
1932 WINPR_ASSERT(context->GetSurfaceData);
1933 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1938 if (surface->windowMapped)
1940 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
1944 surface->outputMapped = TRUE;
1945 surface->outputOriginX = surfaceToOutput->outputOriginX;
1946 surface->outputOriginY = surfaceToOutput->outputOriginY;
1947 surface->outputTargetWidth = surfaceToOutput->targetWidth;
1948 surface->outputTargetHeight = surfaceToOutput->targetHeight;
1949 region16_clear(&surface->invalidRegion);
1952 LeaveCriticalSection(&context->mux);
1962static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
1965 UINT rc = ERROR_INTERNAL_ERROR;
1966 gdiGfxSurface* surface =
nullptr;
1967 EnterCriticalSection(&context->mux);
1969 WINPR_ASSERT(context->GetSurfaceData);
1970 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1975 if (surface->outputMapped)
1977 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
1981 if (surface->windowMapped)
1983 if (surface->windowId != surfaceToWindow->windowId)
1985 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
1986 surface->windowId, surfaceToWindow->windowId);
1990 surface->windowMapped = TRUE;
1992 surface->windowId = surfaceToWindow->windowId;
1993 surface->mappedWidth = surfaceToWindow->mappedWidth;
1994 if (surface->mappedWidth > surface->width)
1996 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
1997 surface->mappedWidth, surface->width);
2000 surface->mappedHeight = surfaceToWindow->mappedHeight;
2001 if (surface->mappedHeight > surface->height)
2003 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2004 surface->mappedHeight, surface->height);
2007 surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2008 surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2009 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2010 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2012 LeaveCriticalSection(&context->mux);
2018gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2021 UINT rc = ERROR_INTERNAL_ERROR;
2022 gdiGfxSurface* surface =
nullptr;
2023 EnterCriticalSection(&context->mux);
2025 WINPR_ASSERT(context->GetSurfaceData);
2026 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2031 if (surface->outputMapped)
2033 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
2037 if (surface->windowMapped)
2039 if (surface->windowId != surfaceToWindow->windowId)
2041 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
2042 surface->windowId, surfaceToWindow->windowId);
2046 surface->windowMapped = TRUE;
2048 surface->windowId = surfaceToWindow->windowId;
2049 surface->mappedWidth = surfaceToWindow->mappedWidth;
2050 if (surface->mappedWidth > surface->width)
2052 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2053 surface->mappedWidth, surface->width);
2057 surface->mappedHeight = surfaceToWindow->mappedHeight;
2058 if (surface->mappedHeight > surface->height)
2060 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2061 surface->mappedHeight, surface->height);
2064 surface->outputTargetWidth = surfaceToWindow->targetWidth;
2065 surface->outputTargetHeight = surfaceToWindow->targetHeight;
2066 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2067 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2069 LeaveCriticalSection(&context->mux);
2073BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2075 return gdi_graphics_pipeline_init_ex(gdi, gfx,
nullptr,
nullptr,
nullptr);
2078BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2079 pcRdpgfxMapWindowForSurface map,
2080 pcRdpgfxUnmapWindowForSurface unmap,
2081 pcRdpgfxUpdateSurfaceArea update)
2083 if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2086 rdpContext* context = gdi->context;
2087 rdpSettings* settings = context->settings;
2090 gfx->custom = (
void*)gdi;
2091 gfx->ResetGraphics = gdi_ResetGraphics;
2092 gfx->StartFrame = gdi_StartFrame;
2093 gfx->EndFrame = gdi_EndFrame;
2094 gfx->SurfaceCommand = gdi_SurfaceCommand;
2095 gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
2096 gfx->CreateSurface = gdi_CreateSurface;
2097 gfx->DeleteSurface = gdi_DeleteSurface;
2098 gfx->SolidFill = gdi_SolidFill;
2099 gfx->SurfaceToSurface = gdi_SurfaceToSurface;
2100 gfx->SurfaceToCache = gdi_SurfaceToCache;
2101 gfx->CacheToSurface = gdi_CacheToSurface;
2102 gfx->CacheImportReply = gdi_CacheImportReply;
2103 gfx->ImportCacheEntry = gdi_ImportCacheEntry;
2104 gfx->ExportCacheEntry = gdi_ExportCacheEntry;
2105 gfx->EvictCacheEntry = gdi_EvictCacheEntry;
2106 gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
2107 gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
2108 gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
2109 gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
2110 gfx->UpdateSurfaces = gdi_UpdateSurfaces;
2111 gfx->MapWindowForSurface = map;
2112 gfx->UnmapWindowForSurface = unmap;
2113 gfx->UpdateSurfaceArea = update;
2121 gfx->codecs = freerdp_client_codecs_new(flags);
2124 if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2127 InitializeCriticalSection(&gfx->mux);
2128 PROFILER_CREATE(gfx->SurfaceProfiler,
"GFX-PROFILER")
2136 gdi->graphicsReset = TRUE;
2139 gfx->UpdateSurfaceArea =
nullptr;
2140 gfx->UpdateSurfaces =
nullptr;
2141 gfx->SurfaceCommand =
nullptr;
2147void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
2155 gfx->custom =
nullptr;
2156 freerdp_client_codecs_free(gfx->codecs);
2157 gfx->codecs =
nullptr;
2158 DeleteCriticalSection(&gfx->mux);
2159 PROFILER_PRINT_HEADER
2160 PROFILER_PRINT(gfx->SurfaceProfiler)
2161 PROFILER_PRINT_FOOTER
2162 PROFILER_FREE(gfx->SurfaceProfiler)
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.