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);
133 rdp_update_lock(update);
134 const BOOL resized = update->DesktopResize(gdi->context);
135 rdp_update_unlock(update);
140 WINPR_ASSERT(context->GetSurfaceIds);
141 const UINT res = context->GetSurfaceIds(context, &pSurfaceIds, &count);
142 if (res != CHANNEL_RC_OK)
148 for (UINT32 index = 0; index < count; index++)
150 WINPR_ASSERT(context->GetSurfaceData);
151 gdiGfxSurface* surface =
152 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
157 memset(surface->data, 0xFF, (
size_t)surface->scanline * surface->height);
158 region16_clear(&surface->invalidRegion);
165 const UINT32 width = (UINT32)MAX(0, gdi->width);
166 const UINT32 height = (UINT32)MAX(0, gdi->height);
168 if (!freerdp_client_codecs_reset(
177 LeaveCriticalSection(&context->mux);
182static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
184 UINT rc = ERROR_INTERNAL_ERROR;
190 rdpUpdate* update =
nullptr;
193 WINPR_ASSERT(gdi->context);
194 WINPR_ASSERT(surface);
196 update = gdi->context->update;
197 WINPR_ASSERT(update);
199 if (gdi->suppressOutput)
200 return CHANNEL_RC_OK;
202 surfaceX = surface->outputOriginX;
203 surfaceY = surface->outputOriginY;
204 surfaceRect.left = 0;
206 surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
207 surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
208 if (!region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion),
212 const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
213 const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
215 rects = region16_rects(&surface->invalidRegion, &nbRects);
216 if (!rects || (nbRects == 0))
222 if (!update_begin_paint(update))
225 for (UINT32 i = 0; i < nbRects; i++)
227 const UINT32 nXSrc = rects[i].left;
228 const UINT32 nYSrc = rects[i].top;
229 const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
230 const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
231 const UINT32 swidth = rects[i].right - rects[i].left;
232 const UINT32 sheight = rects[i].bottom - rects[i].top;
233 const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
234 const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
236 if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
237 dwidth, dheight, surface->data, surface->format, surface->scanline,
238 nXSrc, nYSrc, swidth, sheight))
240 rc = CHANNEL_RC_NULL_DATA;
244 if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
252 if (!update_end_paint(update))
253 rc = ERROR_INTERNAL_ERROR;
256 region16_clear(&(surface->invalidRegion));
261static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
263 WINPR_ASSERT(context);
264 WINPR_ASSERT(surface);
265 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
269static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
272 UINT16* pSurfaceIds =
nullptr;
274 WINPR_ASSERT(context);
276 rdpGdi* gdi = (rdpGdi*)context->custom;
279 EnterCriticalSection(&context->mux);
281 WINPR_ASSERT(context->GetSurfaceIds);
282 UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
283 if (status != CHANNEL_RC_OK)
286 for (UINT32 index = 0; index < count; index++)
288 WINPR_ASSERT(context->GetSurfaceData);
289 gdiGfxSurface* surface =
290 (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
296 if (context->UpdateSurfaceArea)
298 if (surface->handleInUpdateSurfaceArea)
302 if (surface->outputMapped)
303 status = gdi_OutputUpdate(gdi, surface);
304 else if (surface->windowMapped)
305 status = gdi_WindowUpdate(context, surface);
307 if (status != CHANNEL_RC_OK)
313 LeaveCriticalSection(&context->mux);
325 rdpGdi* gdi =
nullptr;
327 WINPR_ASSERT(context);
328 WINPR_ASSERT(startFrame);
330 gdi = (rdpGdi*)context->custom;
332 gdi->inGfxFrame = TRUE;
333 gdi->frameId = startFrame->frameId;
334 return CHANNEL_RC_OK;
338static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
340 WINPR_ASSERT(context);
341 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
350static UINT gdi_EndFrame(RdpgfxClientContext* context,
353 WINPR_ASSERT(context);
354 WINPR_ASSERT(endFrame);
356 rdpGdi* gdi = (rdpGdi*)context->custom;
358 const UINT status = gdi_call_update_surfaces(context);
359 gdi->inGfxFrame = FALSE;
364static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
367 UINT status = CHANNEL_RC_OK;
368 if (!gdi->inGfxFrame)
369 status = gdi_call_update_surfaces(context);
379static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
382 UINT status = CHANNEL_RC_OK;
383 gdiGfxSurface* surface =
nullptr;
388 WINPR_ASSERT(context);
391 WINPR_ASSERT(context->GetSurfaceData);
393 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
397 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
398 return ERROR_NOT_FOUND;
401 if (!is_within_surface(surface, cmd))
402 return ERROR_INVALID_DATA;
404 bpp = FreeRDPGetBytesPerPixel(cmd->format);
405 size = 1ull * bpp * cmd->width * cmd->height;
406 if (cmd->length < size)
408 WLog_ERR(TAG,
"Not enough data, got %" PRIu32
", expected %" PRIuz, cmd->length, size);
409 return ERROR_INVALID_DATA;
412 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
413 cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
414 0, 0,
nullptr, FREERDP_FLIP_NONE))
415 return ERROR_INTERNAL_ERROR;
417 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
418 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
419 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
420 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
421 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
423 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
426 if (status != CHANNEL_RC_OK)
429 status = gdi_interFrameUpdate(gdi, context);
435#if defined(WITH_GFX_AV1)
437static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
441 UINT status = CHANNEL_RC_OK;
442 gdiGfxSurface* surface =
nullptr;
446 WINPR_ASSERT(context);
449 WINPR_ASSERT(context->GetSurfaceData);
451 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
455 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
456 return ERROR_NOT_FOUND;
461 surface->av1 = freerdp_av1_context_new(FALSE);
465 WLog_ERR(TAG,
"unable to create av1 context");
466 return ERROR_NOT_ENOUGH_MEMORY;
469 if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
470 return ERROR_INTERNAL_ERROR;
474 return ERROR_NOT_SUPPORTED;
476 if (!is_within_surface(surface, cmd))
477 return ERROR_INVALID_DATA;
482 return ERROR_INTERNAL_ERROR;
485 rc = freerdp_av1_decompress(surface->av1, bs->data, bs->length, surface->data, surface->format,
486 surface->scanline, surface->width, surface->height,
487 meta->regionRects, meta->numRegionRects);
491 WLog_WARN(TAG,
"freerdp_av1_decompress failure: %" PRId32
", ignoring update.", rc);
492 return CHANNEL_RC_OK;
495 for (UINT32 i = 0; i < meta->numRegionRects; i++)
497 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
498 &(meta->regionRects[i])))
502 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
503 meta->numRegionRects, meta->regionRects);
505 if (status != CHANNEL_RC_OK)
508 status = gdi_interFrameUpdate(gdi, context);
521static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
524 UINT status = ERROR_INTERNAL_ERROR;
525 gdiGfxSurface* surface =
nullptr;
530 WINPR_ASSERT(context);
533 WINPR_ASSERT(context->GetSurfaceData);
535 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
539 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
540 return ERROR_NOT_FOUND;
543 if (!is_within_surface(surface, cmd))
544 return ERROR_INVALID_DATA;
546 WINPR_ASSERT(surface->codecs);
547 rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
548 region16_init(&invalidRegion);
550 if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
551 surface->data, surface->format, surface->scanline, surface->height,
554 WLog_ERR(TAG,
"Failed to process RemoteFX message");
558 rects = region16_rects(&invalidRegion, &nrRects);
559 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
562 if (status != CHANNEL_RC_OK)
565 for (UINT32 x = 0; x < nrRects; x++)
567 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
571 status = gdi_interFrameUpdate(gdi, context);
574 region16_uninit(&invalidRegion);
584static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
588 UINT status = CHANNEL_RC_OK;
589 gdiGfxSurface* surface =
nullptr;
592 WINPR_ASSERT(context);
595 WINPR_ASSERT(context->GetSurfaceData);
597 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
601 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
602 return ERROR_NOT_FOUND;
605 if (!is_within_surface(surface, cmd))
606 return ERROR_INVALID_DATA;
608 WINPR_ASSERT(surface->codecs);
609 rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
610 surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
611 surface->width, surface->height, &gdi->palette);
615 WLog_ERR(TAG,
"clear_decompress failure: %" PRId32
"", rc);
616 return ERROR_INTERNAL_ERROR;
619 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
620 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
621 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
622 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
623 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
625 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
628 if (status != CHANNEL_RC_OK)
631 status = gdi_interFrameUpdate(gdi, context);
643static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
646 UINT status = CHANNEL_RC_OK;
647 BYTE* DstData =
nullptr;
648 gdiGfxSurface* surface =
nullptr;
651 WINPR_ASSERT(context);
654 WINPR_ASSERT(context->GetSurfaceData);
656 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
660 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
661 return ERROR_NOT_FOUND;
664 DstData = surface->data;
666 if (!is_within_surface(surface, cmd))
667 return ERROR_INVALID_DATA;
669 if (!freerdp_bitmap_decompress_planar(surface->codecs->planar, cmd->data, cmd->length,
670 cmd->width, cmd->height, DstData, surface->format,
671 surface->scanline, cmd->left, cmd->top, cmd->width,
673 return ERROR_INTERNAL_ERROR;
675 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
676 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
677 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
678 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
679 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
681 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
684 if (status != CHANNEL_RC_OK)
687 status = gdi_interFrameUpdate(gdi, context);
699static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
704 UINT status = CHANNEL_RC_OK;
705 gdiGfxSurface* surface =
nullptr;
709 WINPR_ASSERT(context);
712 WINPR_ASSERT(context->GetSurfaceData);
714 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
718 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
719 return ERROR_NOT_FOUND;
724 surface->h264 = h264_context_new(FALSE);
728 WLog_ERR(TAG,
"unable to create h264 context");
729 return ERROR_NOT_ENOUGH_MEMORY;
732 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
734 FreeRDP_SoftwareGdi) == 0))
735 return ERROR_INTERNAL_ERROR;
737 if (!h264_context_reset(surface->h264, surface->width, surface->height))
738 return ERROR_INTERNAL_ERROR;
742 return ERROR_NOT_SUPPORTED;
744 if (!is_within_surface(surface, cmd))
745 return ERROR_INVALID_DATA;
750 return ERROR_INTERNAL_ERROR;
753 rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
754 surface->scanline, surface->width, surface->height, meta->regionRects,
755 meta->numRegionRects);
759 WLog_WARN(TAG,
"avc420_decompress failure: %" PRId32
", ignoring update.", rc);
760 return CHANNEL_RC_OK;
763 for (UINT32 i = 0; i < meta->numRegionRects; i++)
765 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
766 &(meta->regionRects[i])))
770 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
771 meta->numRegionRects, meta->regionRects);
773 if (status != CHANNEL_RC_OK)
776 status = gdi_interFrameUpdate(gdi, context);
781 return ERROR_NOT_SUPPORTED;
791static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
796 UINT status = CHANNEL_RC_OK;
797 gdiGfxSurface* surface =
nullptr;
804 WINPR_ASSERT(context);
807 WINPR_ASSERT(context->GetSurfaceData);
809 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
813 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
814 return ERROR_NOT_FOUND;
819 surface->h264 = h264_context_new(FALSE);
823 WLog_ERR(TAG,
"unable to create h264 context");
824 return ERROR_NOT_ENOUGH_MEMORY;
827 if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
829 FreeRDP_SoftwareGdi) == 0))
830 return ERROR_INTERNAL_ERROR;
831 if (!h264_context_reset(surface->h264, surface->width, surface->height))
832 return ERROR_INTERNAL_ERROR;
836 return ERROR_NOT_SUPPORTED;
838 if (!is_within_surface(surface, cmd))
839 return ERROR_INVALID_DATA;
844 return ERROR_INTERNAL_ERROR;
846 avc1 = &bs->bitstream[0];
847 avc2 = &bs->bitstream[1];
850 rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
851 avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
852 avc2->data, avc2->length, surface->data, surface->format,
853 surface->scanline, surface->width, surface->height, cmd->codecId);
857 WLog_WARN(TAG,
"avc444_decompress failure: %" PRId32
", ignoring update.", rc);
858 return CHANNEL_RC_OK;
861 for (UINT32 i = 0; i < meta1->numRegionRects; i++)
863 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
864 &(meta1->regionRects[i])))
868 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
869 meta1->numRegionRects, meta1->regionRects);
871 if (status != CHANNEL_RC_OK)
874 for (UINT32 i = 0; i < meta2->numRegionRects; i++)
876 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
877 &(meta2->regionRects[i])))
881 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
882 meta2->numRegionRects, meta2->regionRects);
884 if (status != CHANNEL_RC_OK)
887 status = gdi_interFrameUpdate(gdi, context);
892 return ERROR_NOT_SUPPORTED;
897static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride,
RECTANGLE_16* rect,
898 UINT32 startOffsetX, UINT32 count, BYTE a)
902 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
905 for (
size_t y = rect->top; y < rect->bottom; y++)
907 BYTE* line = &data[y * stride];
909 for (
size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
915 if (written == count)
918 BYTE* src = &line[x * bpp];
919 UINT32 color = FreeRDPReadColor(src, format);
920 FreeRDPSplitColor(color, format, &r, &g, &b,
nullptr,
nullptr);
921 color = FreeRDPGetColor(format, r, g, b, a);
922 FreeRDPWriteColor(src, format, color);
937static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
940 UINT status = CHANNEL_RC_OK;
942 UINT16 compressed = 0;
944 wStream buffer = WINPR_C_ARRAY_INIT;
946 WINPR_ASSERT(context);
949 wStream* s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
951 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
952 return ERROR_INVALID_DATA;
954 WINPR_ASSERT(context->GetSurfaceData);
955 gdiGfxSurface* surface =
956 (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
960 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
961 return ERROR_NOT_FOUND;
964 if (!is_within_surface(surface, cmd))
965 return ERROR_INVALID_DATA;
967 Stream_Read_UINT16(s, alphaSig);
968 Stream_Read_UINT16(s, compressed);
970 if (alphaSig != 0x414C)
971 return ERROR_INVALID_DATA;
975 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
976 return ERROR_INVALID_DATA;
978 for (
size_t y = cmd->top; y < cmd->top + cmd->height; y++)
980 BYTE* line = &surface->data[y * surface->scanline];
982 for (
size_t x = cmd->left; x < cmd->left + cmd->width; x++)
989 BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
990 Stream_Read_UINT8(s, a);
991 color = FreeRDPReadColor(src, surface->format);
992 FreeRDPSplitColor(color, surface->format, &r, &g, &b,
nullptr,
nullptr);
993 color = FreeRDPGetColor(surface->format, r, g, b, a);
994 FreeRDPWriteColor(src, surface->format, color);
1000 UINT32 startOffsetX = 0;
1002 rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1003 rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1004 rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);
1005 rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);
1007 while (rect.top < rect.bottom)
1012 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1013 return ERROR_INVALID_DATA;
1015 Stream_Read_UINT8(s, a);
1016 Stream_Read_UINT8(s, count);
1020 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1021 return ERROR_INVALID_DATA;
1023 Stream_Read_UINT16(s, count);
1025 if (count >= 0xFFFF)
1027 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1028 return ERROR_INVALID_DATA;
1030 Stream_Read_UINT32(s, count);
1034 if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1035 startOffsetX, count, a))
1036 return ERROR_INTERNAL_ERROR;
1038 startOffsetX += count;
1040 while (startOffsetX >= cmd->width)
1042 startOffsetX -= cmd->width;
1048 invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1049 invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1050 invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
1051 invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
1052 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
1054 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1057 if (status != CHANNEL_RC_OK)
1060 status = gdi_interFrameUpdate(gdi, context);
1066#if defined(WITH_GFX_FRAME_DUMP)
1069 static UINT64 xxx = 0;
1070 const char* path =
"/tmp/dump/";
1072 char fname[1024] = WINPR_C_ARRAY_INIT;
1074 if (_snprintf(fname,
sizeof(fname),
"%s/%08" PRIx64
".raw", path, xxx++) < 0)
1077 FILE* fp = fopen(fname,
"w");
1080 (void)fprintf(fp,
"frameid: %" PRIu32
"\n", frameId);
1081 (void)fprintf(fp,
"surfaceId: %" PRIu32
"\n", cmd->surfaceId);
1082 (void)fprintf(fp,
"codecId: %" PRIu32
"\n", cmd->codecId);
1083 (void)fprintf(fp,
"contextId: %" PRIu32
"\n", cmd->contextId);
1084 (void)fprintf(fp,
"format: %" PRIu32
"\n", cmd->format);
1085 (void)fprintf(fp,
"left: %" PRIu32
"\n", cmd->left);
1086 (void)fprintf(fp,
"top: %" PRIu32
"\n", cmd->top);
1087 (void)fprintf(fp,
"right: %" PRIu32
"\n", cmd->right);
1088 (void)fprintf(fp,
"bottom: %" PRIu32
"\n", cmd->bottom);
1089 (void)fprintf(fp,
"width: %" PRIu32
"\n", cmd->width);
1090 (void)fprintf(fp,
"height: %" PRIu32
"\n", cmd->height);
1091 (void)fprintf(fp,
"length: %" PRIu32
"\n", cmd->length);
1093 char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1094 (void)fprintf(fp,
"data: %s\n", bdata);
1106static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1110 UINT status = ERROR_INTERNAL_ERROR;
1111 gdiGfxSurface* surface =
nullptr;
1121 WINPR_ASSERT(context);
1123 const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1125 WINPR_ASSERT(context->GetSurfaceData);
1126 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1130 WLog_ERR(TAG,
"unable to retrieve surfaceData for surfaceId=%" PRIu32
"", cmd->surfaceId);
1131 return ERROR_NOT_FOUND;
1134 if (!is_within_surface(surface, cmd))
1135 return ERROR_INVALID_DATA;
1137 WINPR_ASSERT(surface->codecs);
1138 rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1143 WLog_ERR(TAG,
"progressive_create_surface_context failure: %" PRId32
"", rc);
1144 return ERROR_INTERNAL_ERROR;
1147 region16_init(&invalidRegion);
1149 rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
1150 surface->format, surface->scanline, cmd->left, cmd->top,
1151 &invalidRegion, surfaceId, gdi->frameId);
1155 WLog_ERR(TAG,
"progressive_decompress failure: %" PRId32
"", rc);
1159 rects = region16_rects(&invalidRegion, &nrRects);
1160 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1163 if (status != CHANNEL_RC_OK)
1166 status = ERROR_INTERNAL_ERROR;
1167 for (UINT32 x = 0; x < nrRects; x++)
1169 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
1173 status = gdi_interFrameUpdate(gdi, context);
1177 region16_uninit(&invalidRegion);
1189 UINT status = CHANNEL_RC_OK;
1190 rdpGdi* gdi =
nullptr;
1192 if (!context || !cmd)
1193 return ERROR_INVALID_PARAMETER;
1195 gdi = (rdpGdi*)context->custom;
1197 EnterCriticalSection(&context->mux);
1198 const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1199 WLog_Print(gdi->log, WLOG_TRACE,
1200 "surfaceId=%" PRIu32
", codec=%s [%" PRIu32
"], contextId=%" PRIu32
", format=%s, "
1201 "left=%" PRIu32
", top=%" PRIu32
", right=%" PRIu32
", bottom=%" PRIu32
1202 ", width=%" PRIu32
", height=%" PRIu32
" "
1203 "length=%" PRIu32
", data=%p, extra=%p",
1204 cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1205 FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1206 cmd->width, cmd->height, cmd->length, (
void*)cmd->data, (
void*)cmd->extra);
1207#if defined(WITH_GFX_FRAME_DUMP)
1208 dump_cmd(cmd, gdi->frameId);
1213 case RDPGFX_CODECID_UNCOMPRESSED:
1214 status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1217#if defined(WITH_GFX_AV1)
1218 case RDPGFX_CODECID_AV1:
1219 status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1223 case RDPGFX_CODECID_CAVIDEO:
1224 status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1227 case RDPGFX_CODECID_CLEARCODEC:
1228 status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1231 case RDPGFX_CODECID_PLANAR:
1232 status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1235 case RDPGFX_CODECID_AVC420:
1236 status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1239 case RDPGFX_CODECID_AVC444v2:
1240 case RDPGFX_CODECID_AVC444:
1241 status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1244 case RDPGFX_CODECID_ALPHA:
1245 status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1248 case RDPGFX_CODECID_CAPROGRESSIVE:
1249 status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1252 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1253 WLog_WARN(TAG,
"SurfaceCommand %s [0x%08" PRIX16
"] not implemented",
1254 rdpgfx_get_codec_id_string(codecId), codecId);
1258 WLog_WARN(TAG,
"Invalid SurfaceCommand %s [0x%08" PRIX16
"]",
1259 rdpgfx_get_codec_id_string(codecId), codecId);
1263 LeaveCriticalSection(&context->mux);
1274gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1277 WINPR_ASSERT(context);
1278 WINPR_ASSERT(deleteEncodingContext);
1279 WINPR_UNUSED(context);
1280 WINPR_UNUSED(deleteEncodingContext);
1281 return CHANNEL_RC_OK;
1290static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1293 UINT rc = ERROR_INTERNAL_ERROR;
1294 gdiGfxSurface* surface =
nullptr;
1295 rdpGdi* gdi =
nullptr;
1296 WINPR_ASSERT(context);
1297 WINPR_ASSERT(createSurface);
1298 gdi = (rdpGdi*)context->custom;
1300 WINPR_ASSERT(gdi->context);
1301 EnterCriticalSection(&context->mux);
1302 surface = (gdiGfxSurface*)calloc(1,
sizeof(gdiGfxSurface));
1309 WINPR_ASSERT(context->codecs);
1310 surface->codecs = context->codecs;
1312 if (!surface->codecs)
1319 surface->surfaceId = createSurface->surfaceId;
1320 surface->width = gfx_align_scanline(createSurface->width, 16);
1321 surface->height = gfx_align_scanline(createSurface->height, 16);
1322 surface->mappedWidth = createSurface->width;
1323 surface->mappedHeight = createSurface->height;
1324 surface->outputTargetWidth = createSurface->width;
1325 surface->outputTargetHeight = createSurface->height;
1329 switch (createSurface->pixelFormat)
1331 case GFX_PIXEL_FORMAT_ARGB_8888:
1332 surface->format = PIXEL_FORMAT_BGRA32;
1335 case GFX_PIXEL_FORMAT_XRGB_8888:
1336 surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1344 if (surface->width > UINT32_MAX / 4 - 2 * 16)
1352 surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 16ul;
1353 surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height, 16);
1361 for (UINT64 y = 0; y < surface->height; y++)
1362 memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1363 region16_init(&surface->invalidRegion);
1365 WINPR_ASSERT(context->SetSurfaceData);
1366 rc = context->SetSurfaceData(context, surface->surfaceId, (
void*)surface);
1368 LeaveCriticalSection(&context->mux);
1378static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1381 UINT rc = CHANNEL_RC_OK;
1382 UINT res = ERROR_INTERNAL_ERROR;
1383 rdpCodecs* codecs =
nullptr;
1384 gdiGfxSurface* surface =
nullptr;
1385 EnterCriticalSection(&context->mux);
1387 WINPR_ASSERT(context->GetSurfaceData);
1388 surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1392 if (surface->windowMapped)
1393 rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1397 h264_context_free(surface->h264);
1399#if defined(WITH_GFX_AV1)
1400 freerdp_av1_context_free(surface->av1);
1402 region16_uninit(&surface->invalidRegion);
1403 codecs = surface->codecs;
1404 winpr_aligned_free(surface->data);
1408 WINPR_ASSERT(context->SetSurfaceData);
1409 res = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
1413 if (codecs && codecs->progressive)
1414 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1416 LeaveCriticalSection(&context->mux);
1421static BOOL intersect_rect(
const RECTANGLE_16* rect,
const gdiGfxSurface* surface,
1425 WINPR_ASSERT(surface);
1426 WINPR_ASSERT(prect);
1428 if (rect->left > rect->right)
1430 if (rect->left > surface->width)
1432 if (rect->top > rect->bottom)
1434 if (rect->top > surface->height)
1436 prect->left = rect->left;
1437 prect->top = rect->top;
1439 prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1440 prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1452 UINT status = ERROR_INTERNAL_ERROR;
1455 rdpGdi* gdi = (rdpGdi*)context->custom;
1457 EnterCriticalSection(&context->mux);
1459 WINPR_ASSERT(context->GetSurfaceData);
1460 gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1466 const BYTE b = solidFill->fillPixel.B;
1467 const BYTE g = solidFill->fillPixel.G;
1468 const BYTE r = solidFill->fillPixel.R;
1475 const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1476 for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1478 const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1480 if (!intersect_rect(rect, surface, &invalidRect))
1483 const UINT32 nWidth = invalidRect.right - invalidRect.left;
1484 const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1486 if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1487 invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1490 if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
1496 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1497 solidFill->fillRectCount, solidFill->fillRects);
1499 if (status != CHANNEL_RC_OK)
1502 LeaveCriticalSection(&context->mux);
1504 return gdi_interFrameUpdate(gdi, context);
1506 LeaveCriticalSection(&context->mux);
1516static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1519 UINT status = ERROR_INTERNAL_ERROR;
1520 BOOL sameSurface = 0;
1523 gdiGfxSurface* surfaceSrc =
nullptr;
1524 gdiGfxSurface* surfaceDst =
nullptr;
1525 rdpGdi* gdi = (rdpGdi*)context->custom;
1526 EnterCriticalSection(&context->mux);
1527 rectSrc = &(surfaceToSurface->rectSrc);
1529 WINPR_ASSERT(context->GetSurfaceData);
1530 surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1531 sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1535 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1537 surfaceDst = surfaceSrc;
1539 if (!surfaceSrc || !surfaceDst)
1542 if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1546 const UINT32 nWidth = rectSrc->right - rectSrc->left;
1547 const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1549 for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1551 const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1553 (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1554 (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1555 if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1558 const UINT32 rwidth = rect.right - rect.left;
1559 const UINT32 rheight = rect.bottom - rect.top;
1560 if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1561 destPt->x, destPt->y, rwidth, rheight, surfaceSrc->data,
1562 surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1563 rectSrc->top,
nullptr, FREERDP_FLIP_NONE))
1567 if (!region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion,
1570 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1571 surfaceDst->surfaceId, 1, &invalidRect);
1573 if (status != CHANNEL_RC_OK)
1578 LeaveCriticalSection(&context->mux);
1580 return gdi_interFrameUpdate(gdi, context);
1582 LeaveCriticalSection(&context->mux);
1586static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1594WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1595static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1598 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1,
sizeof(gdiGfxCacheEntry));
1602 cacheEntry->cacheKey = cacheKey;
1603 cacheEntry->width = width;
1604 cacheEntry->height = height;
1605 cacheEntry->format = format;
1607 const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1608 cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1610 if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1612 cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1614 if (!cacheEntry->data)
1619 gdi_GfxCacheEntryFree(cacheEntry);
1629static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1632 gdiGfxCacheEntry* cacheEntry =
nullptr;
1633 UINT rc = ERROR_INTERNAL_ERROR;
1634 EnterCriticalSection(&context->mux);
1637 WINPR_ASSERT(context->GetSurfaceData);
1638 gdiGfxSurface* surface =
1639 (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1644 if (!is_rect_valid(rect, surface->width, surface->height))
1647 cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1648 (UINT32)(rect->bottom - rect->top), surface->format);
1653 if (!cacheEntry->data)
1656 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1657 0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1658 surface->format, surface->scanline, rect->left, rect->top,
1659 nullptr, FREERDP_FLIP_NONE))
1664 WINPR_ASSERT(context->EvictCacheEntry);
1665 rc = context->EvictCacheEntry(context, &evict);
1666 if (rc != CHANNEL_RC_OK)
1670 WINPR_ASSERT(context->SetCacheSlotData);
1671 rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (
void*)cacheEntry);
1673 if (rc != CHANNEL_RC_OK)
1674 gdi_GfxCacheEntryFree(cacheEntry);
1675 LeaveCriticalSection(&context->mux);
1685static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1688 UINT status = ERROR_INTERNAL_ERROR;
1689 gdiGfxSurface* surface =
nullptr;
1690 gdiGfxCacheEntry* cacheEntry =
nullptr;
1692 rdpGdi* gdi = (rdpGdi*)context->custom;
1694 EnterCriticalSection(&context->mux);
1696 WINPR_ASSERT(context->GetSurfaceData);
1697 surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1699 WINPR_ASSERT(context->GetCacheSlotData);
1700 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1702 if (!surface || !cacheEntry)
1705 for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1709 (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1710 (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1712 if (rectangle_is_empty(&rect))
1715 if (!is_rect_valid(&rect, surface->width, surface->height))
1718 const UINT32 w = rect.right - rect.left;
1719 const UINT32 h = rect.bottom - rect.top;
1720 if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1721 destPt->x, destPt->y, w, h, cacheEntry->data,
1722 cacheEntry->format, cacheEntry->scanline, 0, 0,
nullptr,
1727 if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect))
1729 status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1730 surface->surfaceId, 1, &invalidRect);
1732 if (status != CHANNEL_RC_OK)
1736 LeaveCriticalSection(&context->mux);
1738 return gdi_interFrameUpdate(gdi, context);
1741 LeaveCriticalSection(&context->mux);
1751static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
1755 const UINT16* slots =
nullptr;
1756 UINT error = CHANNEL_RC_OK;
1758 slots = cacheImportReply->cacheSlots;
1759 count = cacheImportReply->importedEntriesCount;
1761 for (UINT16 index = 0; index < count; index++)
1763 UINT16 cacheSlot = slots[index];
1768 WINPR_ASSERT(context->GetCacheSlotData);
1769 gdiGfxCacheEntry* cacheEntry =
1770 (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1775 cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
1778 return ERROR_INTERNAL_ERROR;
1780 WINPR_ASSERT(context->SetCacheSlotData);
1781 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
1785 WLog_ERR(TAG,
"CacheImportReply: SetCacheSlotData failed with error %" PRIu32
"",
1787 gdi_GfxCacheEntryFree(cacheEntry);
1796static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1799 UINT error = ERROR_INTERNAL_ERROR;
1800 gdiGfxCacheEntry* cacheEntry =
nullptr;
1803 return CHANNEL_RC_OK;
1805 cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
1806 importCacheEntry->height, PIXEL_FORMAT_BGRX32);
1811 if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1812 0, 0, cacheEntry->width, cacheEntry->height,
1813 importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0,
1814 nullptr, FREERDP_FLIP_NONE))
1819 WINPR_ASSERT(context->EvictCacheEntry);
1820 error = context->EvictCacheEntry(context, &evict);
1822 if (error != CHANNEL_RC_OK)
1825 WINPR_ASSERT(context->SetCacheSlotData);
1826 error = context->SetCacheSlotData(context, cacheSlot, (
void*)cacheEntry);
1831 gdi_GfxCacheEntryFree(cacheEntry);
1832 WLog_ERR(TAG,
"ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32
"", error);
1839static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1842 WINPR_ASSERT(context->GetCacheSlotData);
1843 gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1847 exportCacheEntry->key64 = cacheEntry->cacheKey;
1848 exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
1849 exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
1850 const UINT64 size = 4ull * cacheEntry->width * cacheEntry->height;
1851 if (size > UINT32_MAX)
1852 return ERROR_NOT_FOUND;
1854 exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
1855 exportCacheEntry->flags = 0;
1856 exportCacheEntry->data = cacheEntry->data;
1857 return CHANNEL_RC_OK;
1860 return ERROR_NOT_FOUND;
1869static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
1872 gdiGfxCacheEntry* cacheEntry =
nullptr;
1873 UINT rc = ERROR_NOT_FOUND;
1875 WINPR_ASSERT(context);
1876 WINPR_ASSERT(evictCacheEntry);
1878 EnterCriticalSection(&context->mux);
1880 WINPR_ASSERT(context->GetCacheSlotData);
1881 cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
1883 gdi_GfxCacheEntryFree(cacheEntry);
1885 WINPR_ASSERT(context->SetCacheSlotData);
1886 rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot,
nullptr);
1887 LeaveCriticalSection(&context->mux);
1897static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
1900 UINT rc = ERROR_INTERNAL_ERROR;
1901 gdiGfxSurface* surface =
nullptr;
1902 EnterCriticalSection(&context->mux);
1904 WINPR_ASSERT(context->GetSurfaceData);
1905 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1910 if (surface->windowMapped)
1912 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
1916 surface->outputMapped = TRUE;
1917 surface->outputOriginX = surfaceToOutput->outputOriginX;
1918 surface->outputOriginY = surfaceToOutput->outputOriginY;
1919 surface->outputTargetWidth = surface->mappedWidth;
1920 surface->outputTargetHeight = surface->mappedHeight;
1921 region16_clear(&surface->invalidRegion);
1924 LeaveCriticalSection(&context->mux);
1930gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
1933 UINT rc = ERROR_INTERNAL_ERROR;
1934 gdiGfxSurface* surface =
nullptr;
1935 EnterCriticalSection(&context->mux);
1937 WINPR_ASSERT(context->GetSurfaceData);
1938 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1943 if (surface->windowMapped)
1945 WLog_WARN(TAG,
"surface already windowMapped when trying to set outputMapped");
1949 surface->outputMapped = TRUE;
1950 surface->outputOriginX = surfaceToOutput->outputOriginX;
1951 surface->outputOriginY = surfaceToOutput->outputOriginY;
1952 surface->outputTargetWidth = surfaceToOutput->targetWidth;
1953 surface->outputTargetHeight = surfaceToOutput->targetHeight;
1954 region16_clear(&surface->invalidRegion);
1957 LeaveCriticalSection(&context->mux);
1967static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
1970 UINT rc = ERROR_INTERNAL_ERROR;
1971 gdiGfxSurface* surface =
nullptr;
1972 EnterCriticalSection(&context->mux);
1974 WINPR_ASSERT(context->GetSurfaceData);
1975 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1980 if (surface->outputMapped)
1982 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
1986 if (surface->windowMapped)
1988 if (surface->windowId != surfaceToWindow->windowId)
1990 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
1991 surface->windowId, surfaceToWindow->windowId);
1995 surface->windowMapped = TRUE;
1997 surface->windowId = surfaceToWindow->windowId;
1998 surface->mappedWidth = surfaceToWindow->mappedWidth;
1999 if (surface->mappedWidth > surface->width)
2001 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2002 surface->mappedWidth, surface->width);
2005 surface->mappedHeight = surfaceToWindow->mappedHeight;
2006 if (surface->mappedHeight > surface->height)
2008 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2009 surface->mappedHeight, surface->height);
2012 surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2013 surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2014 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2015 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2017 LeaveCriticalSection(&context->mux);
2023gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2026 UINT rc = ERROR_INTERNAL_ERROR;
2027 gdiGfxSurface* surface =
nullptr;
2028 EnterCriticalSection(&context->mux);
2030 WINPR_ASSERT(context->GetSurfaceData);
2031 surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2036 if (surface->outputMapped)
2038 WLog_WARN(TAG,
"surface already outputMapped when trying to set windowMapped");
2042 if (surface->windowMapped)
2044 if (surface->windowId != surfaceToWindow->windowId)
2046 WLog_WARN(TAG,
"surface windowId mismatch, has %" PRIu64
", expected %" PRIu64,
2047 surface->windowId, surfaceToWindow->windowId);
2051 surface->windowMapped = TRUE;
2053 surface->windowId = surfaceToWindow->windowId;
2054 surface->mappedWidth = surfaceToWindow->mappedWidth;
2055 if (surface->mappedWidth > surface->width)
2057 WLog_WARN(TAG,
"surface mappedWidth[%" PRIu32
"] > width[%" PRIu32
"]",
2058 surface->mappedWidth, surface->width);
2062 surface->mappedHeight = surfaceToWindow->mappedHeight;
2063 if (surface->mappedHeight > surface->height)
2065 WLog_WARN(TAG,
"surface mappedHeight[%" PRIu32
"] > height[%" PRIu32
"]",
2066 surface->mappedHeight, surface->height);
2069 surface->outputTargetWidth = surfaceToWindow->targetWidth;
2070 surface->outputTargetHeight = surfaceToWindow->targetHeight;
2071 rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2072 surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2074 LeaveCriticalSection(&context->mux);
2078BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2080 return gdi_graphics_pipeline_init_ex(gdi, gfx,
nullptr,
nullptr,
nullptr);
2083BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2084 pcRdpgfxMapWindowForSurface map,
2085 pcRdpgfxUnmapWindowForSurface unmap,
2086 pcRdpgfxUpdateSurfaceArea update)
2088 if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2091 rdpContext* context = gdi->context;
2092 rdpSettings* settings = context->settings;
2095 gfx->custom = (
void*)gdi;
2096 gfx->ResetGraphics = gdi_ResetGraphics;
2097 gfx->StartFrame = gdi_StartFrame;
2098 gfx->EndFrame = gdi_EndFrame;
2099 gfx->SurfaceCommand = gdi_SurfaceCommand;
2100 gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
2101 gfx->CreateSurface = gdi_CreateSurface;
2102 gfx->DeleteSurface = gdi_DeleteSurface;
2103 gfx->SolidFill = gdi_SolidFill;
2104 gfx->SurfaceToSurface = gdi_SurfaceToSurface;
2105 gfx->SurfaceToCache = gdi_SurfaceToCache;
2106 gfx->CacheToSurface = gdi_CacheToSurface;
2107 gfx->CacheImportReply = gdi_CacheImportReply;
2108 gfx->ImportCacheEntry = gdi_ImportCacheEntry;
2109 gfx->ExportCacheEntry = gdi_ExportCacheEntry;
2110 gfx->EvictCacheEntry = gdi_EvictCacheEntry;
2111 gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
2112 gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
2113 gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
2114 gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
2115 gfx->UpdateSurfaces = gdi_UpdateSurfaces;
2116 gfx->MapWindowForSurface = map;
2117 gfx->UnmapWindowForSurface = unmap;
2118 gfx->UpdateSurfaceArea = update;
2126 gfx->codecs = freerdp_client_codecs_new(flags);
2129 if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2132 InitializeCriticalSection(&gfx->mux);
2133 PROFILER_CREATE(gfx->SurfaceProfiler,
"GFX-PROFILER")
2141 gdi->graphicsReset = TRUE;
2144 gfx->UpdateSurfaceArea =
nullptr;
2145 gfx->UpdateSurfaces =
nullptr;
2146 gfx->SurfaceCommand =
nullptr;
2152void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
2160 gfx->custom =
nullptr;
2161 freerdp_client_codecs_free(gfx->codecs);
2162 gfx->codecs =
nullptr;
2163 DeleteCriticalSection(&gfx->mux);
2164 PROFILER_PRINT_HEADER
2165 PROFILER_PRINT(gfx->SurfaceProfiler)
2166 PROFILER_PRINT_FOOTER
2167 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.