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 rc = context->GetSurfaceIds(context, &pSurfaceIds, &count);
137 if (rc != CHANNEL_RC_OK)
140 for (UINT32 index = 0; index < count; index++)
142 WINPR_ASSERT(context->GetSurfaceData);
143 gdiGfxSurface* surface =
144 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
149 memset(surface->data, 0xFF, (
size_t)surface->scanline * surface->height);
150 region16_clear(&surface->invalidRegion);
157 const UINT32 width = (UINT32)MAX(0, gdi->width);
158 const UINT32 height = (UINT32)MAX(0, gdi->height);
160 if (!freerdp_client_codecs_reset(
165 if (!freerdp_client_codecs_reset(
174 LeaveCriticalSection(&context->mux);
179static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
181 UINT rc = ERROR_INTERNAL_ERROR;
187 rdpUpdate* update =
nullptr;
190 WINPR_ASSERT(gdi->context);
191 WINPR_ASSERT(surface);
193 update = gdi->context->update;
194 WINPR_ASSERT(update);
196 if (gdi->suppressOutput)
197 return CHANNEL_RC_OK;
199 surfaceX = surface->outputOriginX;
200 surfaceY = surface->outputOriginY;
201 surfaceRect.left = 0;
203 surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
204 surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
205 if (!region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion),
209 const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
210 const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
212 rects = region16_rects(&surface->invalidRegion, &nbRects);
213 if (!rects || (nbRects == 0))
219 if (!update_begin_paint(update))
222 for (UINT32 i = 0; i < nbRects; i++)
224 const UINT32 nXSrc = rects[i].left;
225 const UINT32 nYSrc = rects[i].top;
226 const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
227 const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
228 const UINT32 swidth = rects[i].right - rects[i].left;
229 const UINT32 sheight = rects[i].bottom - rects[i].top;
230 const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
231 const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
233 if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
234 dwidth, dheight, surface->data, surface->format, surface->scanline,
235 nXSrc, nYSrc, swidth, sheight))
237 rc = CHANNEL_RC_NULL_DATA;
241 if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
249 if (!update_end_paint(update))
250 rc = ERROR_INTERNAL_ERROR;
253 region16_clear(&(surface->invalidRegion));
258static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
260 WINPR_ASSERT(context);
261 WINPR_ASSERT(surface);
262 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
266static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
269 UINT16* pSurfaceIds =
nullptr;
271 WINPR_ASSERT(context);
273 rdpGdi* gdi = (rdpGdi*)context->custom;
276 EnterCriticalSection(&context->mux);
278 WINPR_ASSERT(context->GetSurfaceIds);
279 UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
280 if (status != CHANNEL_RC_OK)
283 for (UINT32 index = 0; index < count; index++)
285 WINPR_ASSERT(context->GetSurfaceData);
286 gdiGfxSurface* surface =
287 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
293 if (context->UpdateSurfaceArea)
295 if (surface->handleInUpdateSurfaceArea)
299 if (surface->outputMapped)
300 status = gdi_OutputUpdate(gdi, surface);
301 else if (surface->windowMapped)
302 status = gdi_WindowUpdate(context, surface);
304 if (status != CHANNEL_RC_OK)
310 LeaveCriticalSection(&context->mux);
322 rdpGdi* gdi =
nullptr;
324 WINPR_ASSERT(context);
325 WINPR_ASSERT(startFrame);
327 gdi = (rdpGdi*)context->custom;
329 gdi->inGfxFrame = TRUE;
330 gdi->frameId = startFrame->frameId;
331 return CHANNEL_RC_OK;
335static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
337 WINPR_ASSERT(context);
338 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
347static UINT gdi_EndFrame(RdpgfxClientContext* context,
350 WINPR_ASSERT(context);
351 WINPR_ASSERT(endFrame);
353 rdpGdi* gdi = (rdpGdi*)context->custom;
355 const UINT status = gdi_call_update_surfaces(context);
356 gdi->inGfxFrame = FALSE;
361static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
364 UINT status = CHANNEL_RC_OK;
365 if (!gdi->inGfxFrame)
366 status = gdi_call_update_surfaces(context);
376static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
379 UINT status = CHANNEL_RC_OK;
380 gdiGfxSurface* surface =
nullptr;
385 WINPR_ASSERT(context);
388 WINPR_ASSERT(context->GetSurfaceData);
390 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
394 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
395 return ERROR_NOT_FOUND;
398 if (!is_within_surface(surface, cmd))
399 return ERROR_INVALID_DATA;
401 bpp = FreeRDPGetBytesPerPixel(cmd->format);
402 size = 1ull * bpp * cmd->width * cmd->height;
403 if (cmd->length < size)
405 WLog_ERR(TAG,
"Not enough data, got %" PRIu32
", expected %" PRIuz, cmd->length, size);
406 return ERROR_INVALID_DATA;
409 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
410 cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
411 0, 0,
nullptr, FREERDP_FLIP_NONE))
412 return ERROR_INTERNAL_ERROR;
414 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
415 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
416 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
417 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
418 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
420 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
423 if (status != CHANNEL_RC_OK)
426 status = gdi_interFrameUpdate(gdi, context);
432#if defined(WITH_GFX_AV1)
434static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
438 UINT status = CHANNEL_RC_OK;
439 gdiGfxSurface* surface =
nullptr;
443 WINPR_ASSERT(context);
446 WINPR_ASSERT(context->GetSurfaceData);
448 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
452 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
453 return ERROR_NOT_FOUND;
458 surface->av1 = freerdp_av1_context_new(FALSE);
462 WLog_ERR(TAG,
"unable to create av1 context");
463 return ERROR_NOT_ENOUGH_MEMORY;
466 if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
467 return ERROR_INTERNAL_ERROR;
471 return ERROR_NOT_SUPPORTED;
473 if (!is_within_surface(surface, cmd))
474 return ERROR_INVALID_DATA;
479 return ERROR_INTERNAL_ERROR;
482 rc = freerdp_av1_decompress(surface->av1, bs->data, bs->length, surface->data, surface->format,
483 surface->scanline, surface->width, surface->height,
484 meta->regionRects, meta->numRegionRects);
488 WLog_WARN(TAG,
"freerdp_av1_decompress failure: %" PRId32
", ignoring update.", rc);
489 return CHANNEL_RC_OK;
492 for (UINT32 i = 0; i < meta->numRegionRects; i++)
494 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
495 &(meta->regionRects[i])))
499 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
500 meta->numRegionRects, meta->regionRects);
502 if (status != CHANNEL_RC_OK)
505 status = gdi_interFrameUpdate(gdi, context);
518static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
521 UINT status = ERROR_INTERNAL_ERROR;
522 gdiGfxSurface* surface =
nullptr;
527 WINPR_ASSERT(context);
530 WINPR_ASSERT(context->GetSurfaceData);
532 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
536 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
537 return ERROR_NOT_FOUND;
540 if (!is_within_surface(surface, cmd))
541 return ERROR_INVALID_DATA;
543 WINPR_ASSERT(surface->codecs);
544 rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
545 region16_init(&invalidRegion);
547 if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
548 surface->data, surface->format, surface->scanline, surface->height,
551 WLog_ERR(TAG,
"Failed to process RemoteFX message");
555 rects = region16_rects(&invalidRegion, &nrRects);
556 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
559 if (status != CHANNEL_RC_OK)
562 for (UINT32 x = 0; x < nrRects; x++)
564 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
568 status = gdi_interFrameUpdate(gdi, context);
571 region16_uninit(&invalidRegion);
581static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
585 UINT status = CHANNEL_RC_OK;
586 gdiGfxSurface* surface =
nullptr;
589 WINPR_ASSERT(context);
592 WINPR_ASSERT(context->GetSurfaceData);
594 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
598 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
599 return ERROR_NOT_FOUND;
602 if (!is_within_surface(surface, cmd))
603 return ERROR_INVALID_DATA;
605 WINPR_ASSERT(surface->codecs);
606 rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
607 surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
608 surface->width, surface->height, &gdi->palette);
612 WLog_ERR(TAG,
"clear_decompress failure: %" PRId32
"", rc);
613 return ERROR_INTERNAL_ERROR;
616 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
617 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
618 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
619 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
620 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
622 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
625 if (status != CHANNEL_RC_OK)
628 status = gdi_interFrameUpdate(gdi, context);
640static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
643 UINT status = CHANNEL_RC_OK;
644 BYTE* DstData =
nullptr;
645 gdiGfxSurface* surface =
nullptr;
648 WINPR_ASSERT(context);
651 WINPR_ASSERT(context->GetSurfaceData);
653 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
657 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
658 return ERROR_NOT_FOUND;
661 DstData = surface->data;
663 if (!is_within_surface(surface, cmd))
664 return ERROR_INVALID_DATA;
666 if (!freerdp_bitmap_decompress_planar(surface->codecs->planar, cmd->data, cmd->length,
667 cmd->width, cmd->height, DstData, surface->format,
668 surface->scanline, cmd->left, cmd->top, cmd->width,
670 return ERROR_INTERNAL_ERROR;
672 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
673 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
674 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
675 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
676 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
678 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
681 if (status != CHANNEL_RC_OK)
684 status = gdi_interFrameUpdate(gdi, context);
696static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
701 UINT status = CHANNEL_RC_OK;
702 gdiGfxSurface* surface =
nullptr;
706 WINPR_ASSERT(context);
709 WINPR_ASSERT(context->GetSurfaceData);
711 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
715 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
716 return ERROR_NOT_FOUND;
721 surface->h264 = h264_context_new(FALSE);
725 WLog_ERR(TAG,
"unable to create h264 context");
726 return ERROR_NOT_ENOUGH_MEMORY;
729 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
731 FreeRDP_SoftwareGdi) == 0))
732 return ERROR_INTERNAL_ERROR;
734 if (!h264_context_reset(surface->h264, surface->width, surface->height))
735 return ERROR_INTERNAL_ERROR;
739 return ERROR_NOT_SUPPORTED;
741 if (!is_within_surface(surface, cmd))
742 return ERROR_INVALID_DATA;
747 return ERROR_INTERNAL_ERROR;
750 rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
751 surface->scanline, surface->width, surface->height, meta->regionRects,
752 meta->numRegionRects);
756 WLog_WARN(TAG,
"avc420_decompress failure: %" PRId32
", ignoring update.", rc);
757 return CHANNEL_RC_OK;
760 for (UINT32 i = 0; i < meta->numRegionRects; i++)
762 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
763 &(meta->regionRects[i])))
767 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
768 meta->numRegionRects, meta->regionRects);
770 if (status != CHANNEL_RC_OK)
773 status = gdi_interFrameUpdate(gdi, context);
778 return ERROR_NOT_SUPPORTED;
788static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
793 UINT status = CHANNEL_RC_OK;
794 gdiGfxSurface* surface =
nullptr;
801 WINPR_ASSERT(context);
804 WINPR_ASSERT(context->GetSurfaceData);
806 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
810 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
811 return ERROR_NOT_FOUND;
816 surface->h264 = h264_context_new(FALSE);
820 WLog_ERR(TAG,
"unable to create h264 context");
821 return ERROR_NOT_ENOUGH_MEMORY;
824 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
826 FreeRDP_SoftwareGdi) == 0))
827 return ERROR_INTERNAL_ERROR;
828 if (!h264_context_reset(surface->h264, surface->width, surface->height))
829 return ERROR_INTERNAL_ERROR;
833 return ERROR_NOT_SUPPORTED;
835 if (!is_within_surface(surface, cmd))
836 return ERROR_INVALID_DATA;
841 return ERROR_INTERNAL_ERROR;
843 avc1 = &bs->bitstream[0];
844 avc2 = &bs->bitstream[1];
847 rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
848 avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
849 avc2->data, avc2->length, surface->data, surface->format,
850 surface->scanline, surface->width, surface->height, cmd->codecId);
854 WLog_WARN(TAG,
"avc444_decompress failure: %" PRId32
", ignoring update.", rc);
855 return CHANNEL_RC_OK;
858 for (UINT32 i = 0; i < meta1->numRegionRects; i++)
860 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
861 &(meta1->regionRects[i])))
865 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
866 meta1->numRegionRects, meta1->regionRects);
868 if (status != CHANNEL_RC_OK)
871 for (UINT32 i = 0; i < meta2->numRegionRects; i++)
873 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
874 &(meta2->regionRects[i])))
878 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
879 meta2->numRegionRects, meta2->regionRects);
881 if (status != CHANNEL_RC_OK)
884 status = gdi_interFrameUpdate(gdi, context);
889 return ERROR_NOT_SUPPORTED;
894static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride,
RECTANGLE_16* rect,
895 UINT32 startOffsetX, UINT32 count, BYTE a)
899 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
902 for (
size_t y = rect->top; y < rect->bottom; y++)
904 BYTE* line = &data[y * stride];
906 for (
size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
912 if (written == count)
915 BYTE* src = &line[x * bpp];
916 UINT32 color = FreeRDPReadColor(src, format);
917 FreeRDPSplitColor(color, format, &r, &g, &b,
nullptr,
nullptr);
918 color = FreeRDPGetColor(format, r, g, b, a);
919 FreeRDPWriteColor(src, format, color);
934static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
937 UINT status = CHANNEL_RC_OK;
939 UINT16 compressed = 0;
940 gdiGfxSurface* surface =
nullptr;
945 WINPR_ASSERT(context);
948 s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
950 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
951 return ERROR_INVALID_DATA;
953 WINPR_ASSERT(context->GetSurfaceData);
955 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
959 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
960 return ERROR_NOT_FOUND;
963 if (!is_within_surface(surface, cmd))
964 return ERROR_INVALID_DATA;
966 Stream_Read_UINT16(s, alphaSig);
967 Stream_Read_UINT16(s, compressed);
969 if (alphaSig != 0x414C)
970 return ERROR_INVALID_DATA;
974 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
975 return ERROR_INVALID_DATA;
977 for (
size_t y = cmd->top; y < cmd->top + cmd->height; y++)
979 BYTE* line = &surface->data[y * surface->scanline];
981 for (
size_t x = cmd->left; x < cmd->left + cmd->width; x++)
988 BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
989 Stream_Read_UINT8(s, a);
990 color = FreeRDPReadColor(src, surface->format);
991 FreeRDPSplitColor(color, surface->format, &r, &g, &b,
nullptr,
nullptr);
992 color = FreeRDPGetColor(surface->format, r, g, b, a);
993 FreeRDPWriteColor(src, surface->format, color);
999 UINT32 startOffsetX = 0;
1001 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1002 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1003 rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);
1004 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);
1006 while (rect.top < rect.bottom)
1011 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1012 return ERROR_INVALID_DATA;
1014 Stream_Read_UINT8(s, a);
1015 Stream_Read_UINT8(s, count);
1019 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1020 return ERROR_INVALID_DATA;
1022 Stream_Read_UINT16(s, count);
1024 if (count >= 0xFFFF)
1026 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1027 return ERROR_INVALID_DATA;
1029 Stream_Read_UINT32(s, count);
1033 if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1034 startOffsetX, count, a))
1035 return ERROR_INTERNAL_ERROR;
1037 startOffsetX += count;
1039 while (startOffsetX >= cmd->width)
1041 startOffsetX -= cmd->width;
1047 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1048 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1049 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
1050 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
1051 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
1053 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1056 if (status != CHANNEL_RC_OK)
1059 status = gdi_interFrameUpdate(gdi, context);
1065#if defined(WITH_GFX_FRAME_DUMP)
1068 static UINT64 xxx = 0;
1069 const char* path =
"/tmp/dump/";
1071 char fname[1024] = WINPR_C_ARRAY_INIT;
1073 if (_snprintf(fname,
sizeof(fname),
"%s/%08" PRIx64
".raw", path, xxx++) < 0)
1076 FILE* fp = fopen(fname,
"w");
1079 (void)fprintf(fp,
"frameid: %" PRIu32
"\n", frameId);
1080 (void)fprintf(fp,
"surfaceId: %" PRIu32
"\n", cmd->surfaceId);
1081 (void)fprintf(fp,
"codecId: %" PRIu32
"\n", cmd->codecId);
1082 (void)fprintf(fp,
"contextId: %" PRIu32
"\n", cmd->contextId);
1083 (void)fprintf(fp,
"format: %" PRIu32
"\n", cmd->format);
1084 (void)fprintf(fp,
"left: %" PRIu32
"\n", cmd->left);
1085 (void)fprintf(fp,
"top: %" PRIu32
"\n", cmd->top);
1086 (void)fprintf(fp,
"right: %" PRIu32
"\n", cmd->right);
1087 (void)fprintf(fp,
"bottom: %" PRIu32
"\n", cmd->bottom);
1088 (void)fprintf(fp,
"width: %" PRIu32
"\n", cmd->width);
1089 (void)fprintf(fp,
"height: %" PRIu32
"\n", cmd->height);
1090 (void)fprintf(fp,
"length: %" PRIu32
"\n", cmd->length);
1092 char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1093 (void)fprintf(fp,
"data: %s\n", bdata);
1105static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1109 UINT status = ERROR_INTERNAL_ERROR;
1110 gdiGfxSurface* surface =
nullptr;
1120 WINPR_ASSERT(context);
1122 const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1124 WINPR_ASSERT(context->GetSurfaceData);
1125 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1129 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
1130 return ERROR_NOT_FOUND;
1133 if (!is_within_surface(surface, cmd))
1134 return ERROR_INVALID_DATA;
1136 WINPR_ASSERT(surface->codecs);
1137 rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1142 WLog_ERR(TAG,
"progressive_create_surface_context failure: %" PRId32
"", rc);
1143 return ERROR_INTERNAL_ERROR;
1146 region16_init(&invalidRegion);
1148 rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
1149 surface->format, surface->scanline, cmd->left, cmd->top,
1150 &invalidRegion, surfaceId, gdi->frameId);
1154 WLog_ERR(TAG,
"progressive_decompress failure: %" PRId32
"", rc);
1158 rects = region16_rects(&invalidRegion, &nrRects);
1159 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1162 if (status != CHANNEL_RC_OK)
1165 status = ERROR_INTERNAL_ERROR;
1166 for (UINT32 x = 0; x < nrRects; x++)
1168 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
1172 status = gdi_interFrameUpdate(gdi, context);
1176 region16_uninit(&invalidRegion);
1188 UINT status = CHANNEL_RC_OK;
1189 rdpGdi* gdi =
nullptr;
1191 if (!context || !cmd)
1192 return ERROR_INVALID_PARAMETER;
1194 gdi = (rdpGdi*)context->custom;
1196 EnterCriticalSection(&context->mux);
1197 const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1198 WLog_Print(gdi->log, WLOG_TRACE,
1199 "surfaceId=%" PRIu32
", codec=%s [%" PRIu32
"], contextId=%" PRIu32
", format=%s, "
1200 "left=%" PRIu32
", top=%" PRIu32
", right=%" PRIu32
", bottom=%" PRIu32
1201 ", width=%" PRIu32
", height=%" PRIu32
" "
1202 "length=%" PRIu32
", data=%p, extra=%p",
1203 cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1204 FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1205 cmd->width, cmd->height, cmd->length, (
void*)cmd->data, (
void*)cmd->extra);
1206#if defined(WITH_GFX_FRAME_DUMP)
1207 dump_cmd(cmd, gdi->frameId);
1212 case RDPGFX_CODECID_UNCOMPRESSED:
1213 status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1216#if defined(WITH_GFX_AV1)
1217 case RDPGFX_CODECID_AV1:
1218 status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1222 case RDPGFX_CODECID_CAVIDEO:
1223 status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1226 case RDPGFX_CODECID_CLEARCODEC:
1227 status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1230 case RDPGFX_CODECID_PLANAR:
1231 status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1234 case RDPGFX_CODECID_AVC420:
1235 status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1238 case RDPGFX_CODECID_AVC444v2:
1239 case RDPGFX_CODECID_AVC444:
1240 status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1243 case RDPGFX_CODECID_ALPHA:
1244 status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1247 case RDPGFX_CODECID_CAPROGRESSIVE:
1248 status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1251 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1252 WLog_WARN(TAG,
"SurfaceCommand %s [0x%08" PRIX16
"] not implemented",
1253 rdpgfx_get_codec_id_string(codecId), codecId);
1257 WLog_WARN(TAG,
"Invalid SurfaceCommand %s [0x%08" PRIX16
"]",
1258 rdpgfx_get_codec_id_string(codecId), codecId);
1262 LeaveCriticalSection(&context->mux);
1273gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1276 WINPR_ASSERT(context);
1277 WINPR_ASSERT(deleteEncodingContext);
1278 WINPR_UNUSED(context);
1279 WINPR_UNUSED(deleteEncodingContext);
1280 return CHANNEL_RC_OK;
1289static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1292 UINT rc = ERROR_INTERNAL_ERROR;
1293 gdiGfxSurface* surface =
nullptr;
1294 rdpGdi* gdi =
nullptr;
1295 WINPR_ASSERT(context);
1296 WINPR_ASSERT(createSurface);
1297 gdi = (rdpGdi*)context->custom;
1299 WINPR_ASSERT(gdi->context);
1300 EnterCriticalSection(&context->mux);
1301 surface = (gdiGfxSurface*)calloc(1,
sizeof(gdiGfxSurface));
1308 WINPR_ASSERT(context->codecs);
1309 surface->codecs = context->codecs;
1311 if (!surface->codecs)
1318 surface->surfaceId = createSurface->surfaceId;
1319 surface->width = gfx_align_scanline(createSurface->width, 16);
1320 surface->height = gfx_align_scanline(createSurface->height, 16);
1321 surface->mappedWidth = createSurface->width;
1322 surface->mappedHeight = createSurface->height;
1323 surface->outputTargetWidth = createSurface->width;
1324 surface->outputTargetHeight = createSurface->height;
1328 switch (createSurface->pixelFormat)
1330 case GFX_PIXEL_FORMAT_ARGB_8888:
1331 surface->format = PIXEL_FORMAT_BGRA32;
1334 case GFX_PIXEL_FORMAT_XRGB_8888:
1335 surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1343 if (surface->width > UINT32_MAX / 4 - 2 * 16)
1351 surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 16ul;
1352 surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height, 16);
1360 for (UINT64 y = 0; y < surface->height; y++)
1361 memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1362 region16_init(&surface->invalidRegion);
1364 WINPR_ASSERT(context->SetSurfaceData);
1365 rc = context->SetSurfaceData(context, surface->surfaceId, (
void*)surface);
1367 LeaveCriticalSection(&context->mux);
1377static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1380 UINT rc = CHANNEL_RC_OK;
1381 UINT res = ERROR_INTERNAL_ERROR;
1382 rdpCodecs* codecs =
nullptr;
1383 gdiGfxSurface* surface =
nullptr;
1384 EnterCriticalSection(&context->mux);
1386 WINPR_ASSERT(context->GetSurfaceData);
1387 surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1391 if (surface->windowMapped)
1392 rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1396 h264_context_free(surface->h264);
1398#if defined(WITH_GFX_AV1)
1399 freerdp_av1_context_free(surface->av1);
1401 region16_uninit(&surface->invalidRegion);
1402 codecs = surface->codecs;
1403 winpr_aligned_free(surface->data);
1407 WINPR_ASSERT(context->SetSurfaceData);
1408 res = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
1412 if (codecs && codecs->progressive)
1413 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1415 LeaveCriticalSection(&context->mux);
1420static BOOL intersect_rect(
const RECTANGLE_16* rect,
const gdiGfxSurface* surface,
1424 WINPR_ASSERT(surface);
1425 WINPR_ASSERT(prect);
1427 if (rect->left > rect->right)
1429 if (rect->left > surface->width)
1431 if (rect->top > rect->bottom)
1433 if (rect->top > surface->height)
1435 prect->left = rect->left;
1436 prect->top = rect->top;
1438 prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1439 prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1451 UINT status = ERROR_INTERNAL_ERROR;
1454 rdpGdi* gdi = (rdpGdi*)context->custom;
1456 EnterCriticalSection(&context->mux);
1458 WINPR_ASSERT(context->GetSurfaceData);
1459 gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1465 const BYTE b = solidFill->fillPixel.B;
1466 const BYTE g = solidFill->fillPixel.G;
1467 const BYTE r = solidFill->fillPixel.R;
1474 const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1475 for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1477 const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1479 if (!intersect_rect(rect, surface, &invalidRect))
1482 const UINT32 nWidth = invalidRect.right - invalidRect.left;
1483 const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1485 if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1486 invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1489 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
1495 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1496 solidFill->fillRectCount, solidFill->fillRects);
1498 if (status != CHANNEL_RC_OK)
1501 LeaveCriticalSection(&context->mux);
1503 return gdi_interFrameUpdate(gdi, context);
1505 LeaveCriticalSection(&context->mux);
1515static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1518 UINT status = ERROR_INTERNAL_ERROR;
1519 BOOL sameSurface = 0;
1522 gdiGfxSurface* surfaceSrc =
nullptr;
1523 gdiGfxSurface* surfaceDst =
nullptr;
1524 rdpGdi* gdi = (rdpGdi*)context->custom;
1525 EnterCriticalSection(&context->mux);
1526 rectSrc = &(surfaceToSurface->rectSrc);
1528 WINPR_ASSERT(context->GetSurfaceData);
1529 surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1530 sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1534 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1536 surfaceDst = surfaceSrc;
1538 if (!surfaceSrc || !surfaceDst)
1541 if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1545 const UINT32 nWidth = rectSrc->right - rectSrc->left;
1546 const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1548 for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1550 const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1552 (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1553 (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1554 if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1557 const UINT32 rwidth = rect.right - rect.left;
1558 const UINT32 rheight = rect.bottom - rect.top;
1559 if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1560 destPt->x, destPt->y, rwidth, rheight, surfaceSrc->data,
1561 surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1562 rectSrc->top,
nullptr, FREERDP_FLIP_NONE))
1566 if (!region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion,
1569 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1570 surfaceDst->surfaceId, 1, &invalidRect);
1572 if (status != CHANNEL_RC_OK)
1577 LeaveCriticalSection(&context->mux);
1579 return gdi_interFrameUpdate(gdi, context);
1581 LeaveCriticalSection(&context->mux);
1585static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1593WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1594static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1597 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1,
sizeof(gdiGfxCacheEntry));
1601 cacheEntry->cacheKey = cacheKey;
1602 cacheEntry->width = width;
1603 cacheEntry->height = height;
1604 cacheEntry->format = format;
1606 const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1607 cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1609 if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1611 cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1613 if (!cacheEntry->data)
1618 gdi_GfxCacheEntryFree(cacheEntry);
1628static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1631 gdiGfxCacheEntry* cacheEntry =
nullptr;
1632 UINT rc = ERROR_INTERNAL_ERROR;
1633 EnterCriticalSection(&context->mux);
1636 WINPR_ASSERT(context->GetSurfaceData);
1637 gdiGfxSurface* surface =
1638 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1643 if (!is_rect_valid(rect, surface->width, surface->height))
1646 cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1647 (UINT32)(rect->bottom - rect->top), surface->format);
1652 if (!cacheEntry->data)
1655 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1656 0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1657 surface->format, surface->scanline, rect->left, rect->top,
1658 nullptr, FREERDP_FLIP_NONE))
1663 WINPR_ASSERT(context->EvictCacheEntry);
1664 rc = context->EvictCacheEntry(context, &evict);
1665 if (rc != CHANNEL_RC_OK)
1669 WINPR_ASSERT(context->SetCacheSlotData);
1670 rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (
void*)cacheEntry);
1672 if (rc != CHANNEL_RC_OK)
1673 gdi_GfxCacheEntryFree(cacheEntry);
1674 LeaveCriticalSection(&context->mux);
1684static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1687 UINT status = ERROR_INTERNAL_ERROR;
1688 gdiGfxSurface* surface =
nullptr;
1689 gdiGfxCacheEntry* cacheEntry =
nullptr;
1691 rdpGdi* gdi = (rdpGdi*)context->custom;
1693 EnterCriticalSection(&context->mux);
1695 WINPR_ASSERT(context->GetSurfaceData);
1696 surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1698 WINPR_ASSERT(context->GetCacheSlotData);
1699 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1701 if (!surface || !cacheEntry)
1704 for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1708 (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1709 (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1711 if (rectangle_is_empty(&rect))
1714 if (!is_rect_valid(&rect, surface->width, surface->height))
1717 const UINT32 w = rect.right - rect.left;
1718 const UINT32 h = rect.bottom - rect.top;
1719 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1720 destPt->x, destPt->y, w, h, cacheEntry->data,
1721 cacheEntry->format, cacheEntry->scanline, 0, 0,
nullptr,
1726 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect))
1728 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1729 surface->surfaceId, 1, &invalidRect);
1731 if (status != CHANNEL_RC_OK)
1735 LeaveCriticalSection(&context->mux);
1737 return gdi_interFrameUpdate(gdi, context);
1740 LeaveCriticalSection(&context->mux);
1750static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
1754 const UINT16* slots =
nullptr;
1755 UINT error = CHANNEL_RC_OK;
1757 slots = cacheImportReply->cacheSlots;
1758 count = cacheImportReply->importedEntriesCount;
1760 for (UINT16 index = 0; index < count; index++)
1762 UINT16 cacheSlot = slots[index];
1767 WINPR_ASSERT(context->GetCacheSlotData);
1768 gdiGfxCacheEntry* cacheEntry =
1769 (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1774 cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
1777 return ERROR_INTERNAL_ERROR;
1779 WINPR_ASSERT(context->SetCacheSlotData);
1780 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
1784 WLog_ERR(TAG,
"CacheImportReply: SetCacheSlotData failed with error %" PRIu32
"",
1786 gdi_GfxCacheEntryFree(cacheEntry);
1795static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1798 UINT error = ERROR_INTERNAL_ERROR;
1799 gdiGfxCacheEntry* cacheEntry =
nullptr;
1802 return CHANNEL_RC_OK;
1804 cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
1805 importCacheEntry->height, PIXEL_FORMAT_BGRX32);
1810 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1811 0, 0, cacheEntry->width, cacheEntry->height,
1812 importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0,
1813 nullptr, FREERDP_FLIP_NONE))
1818 WINPR_ASSERT(context->EvictCacheEntry);
1819 error = context->EvictCacheEntry(context, &evict);
1821 if (error != CHANNEL_RC_OK)
1824 WINPR_ASSERT(context->SetCacheSlotData);
1825 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
1830 gdi_GfxCacheEntryFree(cacheEntry);
1831 WLog_ERR(TAG,
"ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32
"", error);
1838static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1841 WINPR_ASSERT(context->GetCacheSlotData);
1842 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1846 exportCacheEntry->key64 = cacheEntry->cacheKey;
1847 exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
1848 exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
1849 const UINT64 size = 4ull * cacheEntry->width * cacheEntry->height;
1850 if (size > UINT32_MAX)
1851 return ERROR_NOT_FOUND;
1853 exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
1854 exportCacheEntry->flags = 0;
1855 exportCacheEntry->data = cacheEntry->data;
1856 return CHANNEL_RC_OK;
1859 return ERROR_NOT_FOUND;
1868static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
1871 gdiGfxCacheEntry* cacheEntry =
nullptr;
1872 UINT rc = ERROR_NOT_FOUND;
1874 WINPR_ASSERT(context);
1875 WINPR_ASSERT(evictCacheEntry);
1877 EnterCriticalSection(&context->mux);
1879 WINPR_ASSERT(context->GetCacheSlotData);
1880 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
1882 gdi_GfxCacheEntryFree(cacheEntry);
1884 WINPR_ASSERT(context->SetCacheSlotData);
1885 rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot,
nullptr);
1886 LeaveCriticalSection(&context->mux);
1896static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
1899 UINT rc = ERROR_INTERNAL_ERROR;
1900 gdiGfxSurface* surface =
nullptr;
1901 EnterCriticalSection(&context->mux);
1903 WINPR_ASSERT(context->GetSurfaceData);
1904 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1909 if (surface->windowMapped)
1911 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
1915 surface->outputMapped = TRUE;
1916 surface->outputOriginX = surfaceToOutput->outputOriginX;
1917 surface->outputOriginY = surfaceToOutput->outputOriginY;
1918 surface->outputTargetWidth = surface->mappedWidth;
1919 surface->outputTargetHeight = surface->mappedHeight;
1920 region16_clear(&surface->invalidRegion);
1923 LeaveCriticalSection(&context->mux);
1929gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
1932 UINT rc = ERROR_INTERNAL_ERROR;
1933 gdiGfxSurface* surface =
nullptr;
1934 EnterCriticalSection(&context->mux);
1936 WINPR_ASSERT(context->GetSurfaceData);
1937 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1942 if (surface->windowMapped)
1944 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
1948 surface->outputMapped = TRUE;
1949 surface->outputOriginX = surfaceToOutput->outputOriginX;
1950 surface->outputOriginY = surfaceToOutput->outputOriginY;
1951 surface->outputTargetWidth = surfaceToOutput->targetWidth;
1952 surface->outputTargetHeight = surfaceToOutput->targetHeight;
1953 region16_clear(&surface->invalidRegion);
1956 LeaveCriticalSection(&context->mux);
1966static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
1969 UINT rc = ERROR_INTERNAL_ERROR;
1970 gdiGfxSurface* surface =
nullptr;
1971 EnterCriticalSection(&context->mux);
1973 WINPR_ASSERT(context->GetSurfaceData);
1974 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1979 if (surface->outputMapped)
1981 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
1985 if (surface->windowMapped)
1987 if (surface->windowId != surfaceToWindow->windowId)
1989 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
1990 surface->windowId, surfaceToWindow->windowId);
1994 surface->windowMapped = TRUE;
1996 surface->windowId = surfaceToWindow->windowId;
1997 surface->mappedWidth = surfaceToWindow->mappedWidth;
1998 if (surface->mappedWidth > surface->width)
2000 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2001 surface->mappedWidth, surface->width);
2004 surface->mappedHeight = surfaceToWindow->mappedHeight;
2005 if (surface->mappedHeight > surface->height)
2007 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2008 surface->mappedHeight, surface->height);
2011 surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2012 surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2013 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2014 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2016 LeaveCriticalSection(&context->mux);
2022gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2025 UINT rc = ERROR_INTERNAL_ERROR;
2026 gdiGfxSurface* surface =
nullptr;
2027 EnterCriticalSection(&context->mux);
2029 WINPR_ASSERT(context->GetSurfaceData);
2030 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2035 if (surface->outputMapped)
2037 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
2041 if (surface->windowMapped)
2043 if (surface->windowId != surfaceToWindow->windowId)
2045 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
2046 surface->windowId, surfaceToWindow->windowId);
2050 surface->windowMapped = TRUE;
2052 surface->windowId = surfaceToWindow->windowId;
2053 surface->mappedWidth = surfaceToWindow->mappedWidth;
2054 if (surface->mappedWidth > surface->width)
2056 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2057 surface->mappedWidth, surface->width);
2061 surface->mappedHeight = surfaceToWindow->mappedHeight;
2062 if (surface->mappedHeight > surface->height)
2064 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2065 surface->mappedHeight, surface->height);
2068 surface->outputTargetWidth = surfaceToWindow->targetWidth;
2069 surface->outputTargetHeight = surfaceToWindow->targetHeight;
2070 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2071 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2073 LeaveCriticalSection(&context->mux);
2077BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2079 return gdi_graphics_pipeline_init_ex(gdi, gfx,
nullptr,
nullptr,
nullptr);
2082BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2083 pcRdpgfxMapWindowForSurface map,
2084 pcRdpgfxUnmapWindowForSurface unmap,
2085 pcRdpgfxUpdateSurfaceArea update)
2087 if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2090 rdpContext* context = gdi->context;
2091 rdpSettings* settings = context->settings;
2094 gfx->custom = (
void*)gdi;
2095 gfx->ResetGraphics = gdi_ResetGraphics;
2096 gfx->StartFrame = gdi_StartFrame;
2097 gfx->EndFrame = gdi_EndFrame;
2098 gfx->SurfaceCommand = gdi_SurfaceCommand;
2099 gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
2100 gfx->CreateSurface = gdi_CreateSurface;
2101 gfx->DeleteSurface = gdi_DeleteSurface;
2102 gfx->SolidFill = gdi_SolidFill;
2103 gfx->SurfaceToSurface = gdi_SurfaceToSurface;
2104 gfx->SurfaceToCache = gdi_SurfaceToCache;
2105 gfx->CacheToSurface = gdi_CacheToSurface;
2106 gfx->CacheImportReply = gdi_CacheImportReply;
2107 gfx->ImportCacheEntry = gdi_ImportCacheEntry;
2108 gfx->ExportCacheEntry = gdi_ExportCacheEntry;
2109 gfx->EvictCacheEntry = gdi_EvictCacheEntry;
2110 gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
2111 gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
2112 gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
2113 gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
2114 gfx->UpdateSurfaces = gdi_UpdateSurfaces;
2115 gfx->MapWindowForSurface = map;
2116 gfx->UnmapWindowForSurface = unmap;
2117 gfx->UpdateSurfaceArea = update;
2125 gfx->codecs = freerdp_client_codecs_new(flags);
2128 if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2131 InitializeCriticalSection(&gfx->mux);
2132 PROFILER_CREATE(gfx->SurfaceProfiler,
"GFX-PROFILER")
2140 gdi->graphicsReset = TRUE;
2143 gfx->UpdateSurfaceArea =
nullptr;
2144 gfx->UpdateSurfaces =
nullptr;
2145 gfx->SurfaceCommand =
nullptr;
2151void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
2159 gfx->custom =
nullptr;
2160 freerdp_client_codecs_free(gfx->codecs);
2161 gfx->codecs =
nullptr;
2162 DeleteCriticalSection(&gfx->mux);
2163 PROFILER_PRINT_HEADER
2164 PROFILER_PRINT(gfx->SurfaceProfiler)
2165 PROFILER_PRINT_FOOTER
2166 PROFILER_FREE(gfx->SurfaceProfiler)
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.