24#include <freerdp/config.h>
26#include <winpr/assert.h>
27#include <winpr/cast.h>
30#include <winpr/wlog.h>
31#include <winpr/print.h>
32#include <winpr/synch.h>
33#include <winpr/thread.h>
34#include <winpr/stream.h>
35#include <winpr/sysinfo.h>
36#include <winpr/cmdline.h>
37#include <winpr/collections.h>
39#include <freerdp/addin.h>
40#include <freerdp/channels/log.h>
41#include <freerdp/codec/h264.h>
43#include "rdpgfx_common.h"
44#include "rdpgfx_codec.h"
46#include "rdpgfx_main.h"
48#define GFXTAG CHANNELS_TAG("rdpgfx.client")
50#define RDPGFX_NUMBER_CAPSETS 0x100
54 uint64_t cntGfxCodecID[RDPGFX_CODECID_MAX];
55 uint64_t cntGfxCommandID[RDPGFX_CMDID_MAX + 1];
56 uint64_t bytesGfxCodecID[RDPGFX_CODECID_MAX];
57 uint64_t framesDecoded;
58 uint64_t framesUnacknowledged;
59} RdpgfxClientContextStats;
63 RdpgfxClientContext common;
64 RdpgfxClientContextStats stats;
65} RdpgfxClientContextInt;
68static char bytesGfxCodecNames[RDPGFX_CODECID_MAX][64];
69static INIT_ONCE bytesGfxCodecNamesOnce = INIT_ONCE_STATIC_INIT;
71static BOOL CALLBACK rdpgfx_stats_init_names(WINPR_ATTR_UNUSED
PINIT_ONCE once,
72 WINPR_ATTR_UNUSED PVOID param,
73 WINPR_ATTR_UNUSED PVOID* context)
75 for (UINT16 x = 0; x < RDPGFX_CODECID_MAX; x++)
76 (
void)_snprintf(bytesGfxCodecNames[x],
sizeof(bytesGfxCodecNames[x]),
"%s_BYTES",
77 rdpgfx_get_codec_id_string(x));
81static const UINT32 MAX_SURFACE_SIZE = 32766;
82static const UINT32 MAX_MONITOR_COUNT = 16;
84size_t rdpgfx_stats_max_index(
void)
86 return sizeof(RdpgfxClientContextStats) /
sizeof(uint64_t);
89const char* rdpgfx_stats_name_for_index(
size_t index)
91 if (index < RDPGFX_CODECID_MAX)
92 return rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
94 index -= RDPGFX_CODECID_MAX;
95 if (index < RDPGFX_CMDID_MAX)
96 return rdpgfx_get_cmd_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
97 if (index == RDPGFX_CMDID_MAX)
98 return "RDPGFX_CMDID_INVALID";
100 index -= RDPGFX_CMDID_MAX + 1;
101 if (index < RDPGFX_CODECID_MAX)
103 if (!InitOnceExecuteOnce(&bytesGfxCodecNamesOnce, rdpgfx_stats_init_names,
nullptr,
105 return "RDPGFX_STATS_UNUSED";
106 return bytesGfxCodecNames[index];
109 index -= RDPGFX_CODECID_MAX;
111 return "RDPGFX_FRAMES_DECODED";
113 return "RDPGFX_FRAMES_UNACKNOWLEDGED";
114 return "RDPGFX_STATS_UNUSED";
117uint64_t rdpgfx_stats_value_for_index(RdpgfxClientContext* context,
size_t index)
119 WINPR_ASSERT(context);
121 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
123 if (index < RDPGFX_CODECID_MAX)
124 return intCtx->stats.cntGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
126 index -= RDPGFX_CODECID_MAX;
127 if (index <= RDPGFX_CMDID_MAX)
128 return intCtx->stats.cntGfxCommandID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
130 index -= RDPGFX_CMDID_MAX + 1;
131 if (index < RDPGFX_CODECID_MAX)
132 return intCtx->stats.bytesGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
134 index -= RDPGFX_CODECID_MAX;
136 return intCtx->stats.framesDecoded;
138 return intCtx->stats.framesUnacknowledged;
142static void rdpgfx_codecid_event(RdpgfxClientContext* context, uint32_t index, uint32_t length)
146 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
147 WINPR_ASSERT(intCtx);
148 if (index < RDPGFX_CODECID_MAX)
150 intCtx->stats.cntGfxCodecID[index]++;
151 intCtx->stats.bytesGfxCodecID[index] += length;
157 WLog_Print(gfx->base.log, WLOG_WARN,
"Invalid event index %" PRIu32, index);
161static void rdpgfx_stats_cmdid_event(RdpgfxClientContext* context, uint32_t index)
165 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
166 WINPR_ASSERT(intCtx);
168 if (index >= RDPGFX_CMDID_MAX)
172 WLog_Print(gfx->base.log, WLOG_WARN,
"Invalid codecID %" PRIu32, index);
173 index = RDPGFX_CMDID_MAX;
175 intCtx->stats.cntGfxCommandID[index]++;
178static void rdpgfx_stats_frame_decoded(RdpgfxClientContext* context)
182 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
183 intCtx->stats.framesDecoded++;
187static void rdpgfx_stats_frames_unacknowledged(RdpgfxClientContext* context, UINT32 frames)
191 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
192 intCtx->stats.framesUnacknowledged = frames;
195static BOOL delete_surface(
const void* key,
void* value,
void* arg)
197 const ULONG_PTR
id = (ULONG_PTR)(key);
198 if ((
id < 1) || (
id - 1 > UINT16_MAX))
200 const UINT16 uid = WINPR_ASSERTING_INT_CAST(UINT16,
id - 1);
202 RdpgfxClientContext* context = arg;
209 UINT error = CHANNEL_RC_OK;
210 IFCALLRET(context->DeleteSurface, error, context, &pdu);
216 WLog_Print(gfx->base.log, WLOG_ERROR,
217 "context->DeleteSurface failed with error %" PRIu32
"", error);
223static void free_surfaces(RdpgfxClientContext* context, wHashTable* SurfaceTable)
225 WINPR_ASSERT(context);
226 if (!HashTable_Foreach(SurfaceTable, delete_surface, context))
230 WLog_Print(gfx->base.log, WLOG_WARN,
"delete_surface failed");
234static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots,
void** CacheSlots)
236 UINT error = CHANNEL_RC_OK;
238 WINPR_ASSERT(CacheSlots);
239 for (UINT16 index = 0; index < MaxCacheSlots; index++)
241 if (CacheSlots[index])
245 if (context && context->EvictCacheEntry)
247 const UINT rc = context->EvictCacheEntry(context, &pdu);
248 if (rc != CHANNEL_RC_OK)
252 CacheSlots[index] =
nullptr;
263static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
266 UINT error = CHANNEL_RC_OK;
269 WINPR_ASSERT(context);
273 if (!gfx || !gfx->base.listener_callback)
274 return ERROR_BAD_ARGUMENTS;
276 memset(gfx->allowedCodecs, FALSE,
sizeof(gfx->allowedCodecs));
281 .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
282 .pduLength = RDPGFX_HEADER_SIZE + 2 };
284 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
287 header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
290 WLog_Print(gfx->base.log, WLOG_DEBUG,
"SendCapsAdvertisePdu %" PRIu16
"", pdu->capsSetCount);
291 wStream* s = Stream_New(
nullptr, header.pduLength);
295 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
296 return CHANNEL_RC_NO_MEMORY;
299 if ((error = rdpgfx_write_header(s, &header)))
303 Stream_Write_UINT16(s, pdu->capsSetCount);
305 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
309 char buffer[128] = WINPR_C_ARRAY_INIT;
310 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Sending %s [0x%08" PRIx32
"] flags=%s",
311 rdpgfx_caps_version_str(capsSet->version), capsSet->version,
312 rdpgfx_caps_flags_str(capsSet->flags, buffer,
sizeof(buffer)));
314 Stream_Write_UINT32(s, capsSet->version);
315 Stream_Write_UINT32(s, capsSet->length);
316 Stream_Write_UINT32(s, capsSet->flags);
317 if (capsSet->length < 4)
319 Stream_Zero(s, capsSet->length - 4);
322 Stream_SealLength(s);
323 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
326 Stream_Free(s, TRUE);
330static BOOL rdpgfx_is_capability_filtered(
RDPGFX_PLUGIN* gfx, UINT32 caps)
333 const UINT32 filter =
335 const UINT32 capList[] = {
336#if defined(WITH_GFX_AV1)
337 RDPGFX_CAPVERSION_FRDP_1,
339#if defined(WITH_GFX_AV2)
340 RDPGFX_CAPVERSION_FRDP_2,
343 RDPGFX_CAPVERSION_81,
344 RDPGFX_CAPVERSION_10,
345 RDPGFX_CAPVERSION_101,
346 RDPGFX_CAPVERSION_102,
347 RDPGFX_CAPVERSION_103,
348 RDPGFX_CAPVERSION_104,
349 RDPGFX_CAPVERSION_105,
350 RDPGFX_CAPVERSION_106,
351 RDPGFX_CAPVERSION_106_ERR,
352 RDPGFX_CAPVERSION_107
353#if defined(WITH_GFX_AZURE)
355 RDPGFX_CAPVERSION_111,
356 RDPGFX_CAPVERSION_112,
357 RDPGFX_CAPVERSION_113,
358 RDPGFX_CAPVERSION_115
362 for (
size_t x = 0; x < ARRAYSIZE(capList); x++)
364 if (caps == capList[x])
365 return (filter & (1u << x)) != 0;
375 WINPR_ASSERT(pdu->capsSets);
376 WINPR_ASSERT(count > 0);
377 WINPR_ASSERT(pdu->capsSetCount < count);
378 if (pdu->capsSetCount >= count)
380 return &pdu->capsSets[pdu->capsSetCount++];
383#if defined(WITH_GFX_AZURE)
392 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I420, FALSE))
395 WLog_Print(gfx->base.log, WLOG_WARN,
396 "HEVC mode enabled, but decoder does not support it. Disabling.");
408 RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
411 return ERROR_BAD_ARGUMENTS;
416 return ERROR_BAD_CONFIGURATION;
418 RdpgfxClientContext* context = gfx->context;
421 return ERROR_BAD_CONFIGURATION;
425 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
427 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
429 return ERROR_BAD_CONFIGURATION;
430 capsSet->version = RDPGFX_CAPVERSION_8;
435 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
442 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
445 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
447 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
449 return ERROR_BAD_CONFIGURATION;
450 capsSet->version = RDPGFX_CAPVERSION_81;
455 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
458 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
461 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC420, FALSE))
462 capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
465 UINT32 caps10Flags = 0;
470 caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
473 !freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC444, FALSE))
474 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
477 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
479 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
481 return ERROR_BAD_CONFIGURATION;
482 capsSet->version = RDPGFX_CAPVERSION_10;
484 capsSet->flags = caps10Flags;
487 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
489 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
491 return ERROR_BAD_CONFIGURATION;
492 capsSet->version = RDPGFX_CAPVERSION_101;
493 capsSet->length = 0x10;
497 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
499 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
501 return ERROR_BAD_CONFIGURATION;
502 capsSet->version = RDPGFX_CAPVERSION_102;
503 capsSet->length = 0x4;
504 capsSet->flags = caps10Flags;
509 if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
510 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
513 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
515 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
517 return ERROR_BAD_CONFIGURATION;
518 capsSet->version = RDPGFX_CAPVERSION_103;
519 capsSet->length = 0x4;
520 capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
523 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
525 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
527 return ERROR_BAD_CONFIGURATION;
528 capsSet->version = RDPGFX_CAPVERSION_104;
529 capsSet->length = 0x4;
530 capsSet->flags = caps10Flags;
536 if (freerdp_image_scaling_supported())
538 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
540 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
542 return ERROR_BAD_CONFIGURATION;
543 capsSet->version = RDPGFX_CAPVERSION_105;
544 capsSet->length = 0x4;
545 capsSet->flags = caps10Flags;
548 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
550 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
552 return ERROR_BAD_CONFIGURATION;
553 capsSet->version = RDPGFX_CAPVERSION_106;
554 capsSet->length = 0x4;
555 capsSet->flags = caps10Flags;
558 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
560 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
562 return ERROR_BAD_CONFIGURATION;
563 capsSet->version = RDPGFX_CAPVERSION_106_ERR;
564 capsSet->length = 0x4;
565 capsSet->flags = caps10Flags;
569 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
571 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
573 return ERROR_BAD_CONFIGURATION;
574 capsSet->version = RDPGFX_CAPVERSION_107;
575 capsSet->length = 0x4;
576 capsSet->flags = caps10Flags;
577 if (!freerdp_image_scaling_supported())
578 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
581#if defined(WITH_GFX_AZURE)
582 if (freerdp_image_scaling_supported())
584 const UINT32 caps[] = { RDPGFX_CAPVERSION_111, RDPGFX_CAPVERSION_112, RDPGFX_CAPVERSION_113,
585 RDPGFX_CAPVERSION_115 };
586 const BOOL hevc = rdpgfx_hevc_enabled(gfx);
589 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I444, FALSE);
591 for (
size_t x = 0; x < ARRAYSIZE(caps); x++)
593 const UINT32 cap = caps[x];
595 if (rdpgfx_is_capability_filtered(gfx, cap))
599 UINT32 flags = caps10Flags;
603 if (hevc && (cap >= RDPGFX_CAPVERSION_112))
604 flags |= RDPGFX_CAPS_FLAG_HEVC_ENABLE;
607 if (hevc444 && (cap >= RDPGFX_CAPVERSION_115))
608 flags |= RDPGFX_CAPS_FLAG_HEVC_I444;
615 if ((cap >= RDPGFX_CAPVERSION_113) &&
616 ((flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0))
617 flags |= RDPGFX_CAPS_FLAG_AVCMM_ENABLE;
620 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
622 return ERROR_BAD_CONFIGURATION;
623 capsSet->version = cap;
624 capsSet->length = 0x4;
625 capsSet->flags = flags;
626 if (!freerdp_image_scaling_supported())
627 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
632#if defined(WITH_GFX_AV1)
635 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I420, FALSE) &&
636 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
638 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
640 return ERROR_BAD_CONFIGURATION;
641 capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
642 capsSet->length = 0x4;
644 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I444, FALSE))
645 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
647 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
649 const UINT32 profile =
652 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
654 if (!freerdp_image_scaling_supported())
655 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
660#if defined(WITH_GFX_AV2)
665 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I420, FALSE) &&
666 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_2))
668 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
670 return ERROR_BAD_CONFIGURATION;
671 capsSet->version = RDPGFX_CAPVERSION_FRDP_2;
672 capsSet->length = 0x4;
674 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I444, FALSE))
676 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
679 FreeRDP_GfxCodecAV2Profile);
681 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
686 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
689 if (!freerdp_image_scaling_supported())
690 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
695 return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
705 WINPR_ASSERT(callback);
709 RdpgfxClientContext* context = gfx->context;
714 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
715 return ERROR_INVALID_DATA;
717 Stream_Read_UINT32(s, capsSet.version);
718 Stream_Read_UINT32(s, capsSet.length);
719 Stream_Read_UINT32(s, capsSet.flags);
720 gfx->TotalDecodedFrames = 0;
721 gfx->ConnectionCaps = capsSet;
723 context->CapsVersion = capsSet.version;
725 char buffer[128] = WINPR_C_ARRAY_INIT;
727 WLog_Print(gfx->base.log, WLOG_DEBUG,
728 "RecvCapsConfirmPdu: version: %s [0x%08" PRIX32
"] flags: %s",
729 rdpgfx_caps_version_str(capsSet.version), capsSet.version,
730 rdpgfx_caps_flags_str(capsSet.flags, buffer,
sizeof(buffer)));
732 memset(gfx->allowedCodecs, TRUE,
sizeof(gfx->allowedCodecs));
734 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = FALSE;
735 gfx->allowedCodecs[RDPGFX_CODECID_AVC444] = FALSE;
736 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = FALSE;
737#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
738 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = FALSE;
741 if (capsSet.version >= RDPGFX_CAPVERSION_10)
743 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
745 WLog_Print(gfx->base.log, WLOG_INFO,
"AVC not disabled by server");
746 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
747 gfx->allowedCodecs[RDPGFX_CODECID_AVC444] = TRUE;
751 if (capsSet.version >= RDPGFX_CAPVERSION_103)
753 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
754 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = TRUE;
757 if (capsSet.version == RDPGFX_CAPVERSION_81)
759 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC420_ENABLED) != 0)
761 WLog_Print(gfx->base.log, WLOG_INFO,
"AVC420 enabled by server");
762 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
766#if defined(WITH_GFX_AV1)
767 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_1)
768 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
770#if defined(WITH_GFX_AV2)
771 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_2)
772 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
775#if defined(WITH_GFX_AZURE)
776 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = FALSE;
777 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = FALSE;
779 if (capsSet.version >= RDPGFX_CAPVERSION_112)
781 if ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_ENABLE) != 0)
783 WLog_Print(gfx->base.log, WLOG_INFO,
"HEVC confirmed by server");
784 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = TRUE;
787 if ((capsSet.version >= RDPGFX_CAPVERSION_115) &&
788 ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_I444) != 0))
790 WLog_Print(gfx->base.log, WLOG_INFO,
"HEVC I444 confirmed by server");
791 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = TRUE;
798 return ERROR_BAD_CONFIGURATION;
800 return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
808static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
813 if (!context || !pdu)
814 return ERROR_BAD_ARGUMENTS;
818 if (!gfx || !gfx->base.listener_callback)
819 return ERROR_BAD_CONFIGURATION;
824 return ERROR_BAD_CONFIGURATION;
827 .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
828 .pduLength = RDPGFX_HEADER_SIZE + 12 };
830 WLog_Print(gfx->base.log, WLOG_TRACE,
"SendFrameAcknowledgePdu: %" PRIu32
"", pdu->frameId);
832 wStream* s = Stream_New(
nullptr, header.pduLength);
836 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
837 return CHANNEL_RC_NO_MEMORY;
840 if ((error = rdpgfx_write_header(s, &header)))
844 Stream_Write_UINT32(s, pdu->queueDepth);
845 Stream_Write_UINT32(s, pdu->frameId);
846 Stream_Write_UINT32(s, pdu->totalFramesDecoded);
847 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
850 if (error == CHANNEL_RC_OK)
852 gfx->UnacknowledgedFrames--;
853 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
857 Stream_Free(s, TRUE);
861static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
864 UINT error = CHANNEL_RC_OK;
866 .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
867 .pduLength = RDPGFX_HEADER_SIZE + 12 };
869 if (!context || !pdu)
870 return ERROR_BAD_ARGUMENTS;
874 if (!gfx || !gfx->base.listener_callback)
875 return ERROR_BAD_CONFIGURATION;
880 return ERROR_BAD_CONFIGURATION;
882 WLog_Print(gfx->base.log, WLOG_TRACE,
"SendQoeFrameAcknowledgePdu: %" PRIu32
"", pdu->frameId);
883 wStream* s = Stream_New(
nullptr, header.pduLength);
887 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
888 return CHANNEL_RC_NO_MEMORY;
891 if ((error = rdpgfx_write_header(s, &header)))
895 Stream_Write_UINT32(s, pdu->frameId);
896 Stream_Write_UINT32(s, pdu->timestamp);
897 Stream_Write_UINT16(s, pdu->timeDiffSE);
898 Stream_Write_UINT16(s, pdu->timeDiffEDR);
899 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
902 Stream_Free(s, TRUE);
914 WINPR_ASSERT(callback);
920 RdpgfxClientContext* context = gfx->context;
921 UINT error = CHANNEL_RC_OK;
922 GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
924 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
925 return ERROR_INVALID_DATA;
927 Stream_Read_UINT32(s, pdu.width);
928 Stream_Read_UINT32(s, pdu.height);
929 Stream_Read_UINT32(s, pdu.monitorCount);
931 if ((pdu.width == 0) || (pdu.height == 0) || (pdu.monitorCount == 0))
933 WLog_Print(gfx->base.log, WLOG_ERROR,
934 "context->ResetGraphics Width[%" PRIu32
"], Height[%" PRIu32
935 "] or monitorCount[%" PRIu32
" is 0",
936 pdu.width, pdu.height, pdu.monitorCount);
937 return ERROR_INVALID_DATA;
939 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE) ||
940 (pdu.monitorCount > MAX_MONITOR_COUNT))
942 WLog_Print(gfx->base.log, WLOG_ERROR,
943 "context->ResetGraphics Width[%" PRIu32
"], Height[%" PRIu32
"] > %" PRIu32
944 " or monitorCount[%" PRIu32
" > %" PRIu32,
945 pdu.width, pdu.height, MAX_SURFACE_SIZE, pdu.monitorCount, MAX_MONITOR_COUNT);
946 return ERROR_INVALID_DATA;
949 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
950 return ERROR_INVALID_DATA;
954 if (!pdu.monitorDefArray)
956 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
957 return CHANNEL_RC_NO_MEMORY;
960 for (UINT32 index = 0; index < pdu.monitorCount; index++)
962 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
963 Stream_Read_INT32(s, monitor->left);
964 Stream_Read_INT32(s, monitor->top);
965 Stream_Read_INT32(s, monitor->right);
966 Stream_Read_INT32(s, monitor->bottom);
967 Stream_Read_UINT32(s, monitor->flags);
970 const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
973 free(pdu.monitorDefArray);
974 return CHANNEL_RC_NULL_DATA;
976 const size_t pad = 340ULL - size;
978 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (
size_t)pad))
980 free(pdu.monitorDefArray);
981 return CHANNEL_RC_NO_MEMORY;
985 WLog_Print(gfx->base.log, WLOG_DEBUG,
986 "RecvResetGraphicsPdu: width: %" PRIu32
" height: %" PRIu32
" count: %" PRIu32
"",
987 pdu.width, pdu.height, pdu.monitorCount);
989 for (UINT32 index = 0; index < pdu.monitorCount; index++)
991 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
992 WLog_Print(gfx->base.log, WLOG_TRACE,
993 "RecvResetGraphicsPdu: monitor left:%" PRIi32
" top:%" PRIi32
" right:%" PRIi32
994 " bottom:%" PRIi32
" flags:0x%" PRIx32
"",
995 monitor->left, monitor->top, monitor->right, monitor->bottom, monitor->flags);
1000 IFCALLRET(context->ResetGraphics, error, context, &pdu);
1003 WLog_Print(gfx->base.log, WLOG_ERROR,
1004 "context->ResetGraphics failed with error %" PRIu32
"", error);
1007 free(pdu.monitorDefArray);
1010 if (error == CHANNEL_RC_OK)
1012 EventArgsInit(&graphicsReset,
"libfreerdp");
1013 graphicsReset.width = pdu.width;
1014 graphicsReset.height = pdu.height;
1015 if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
1016 return ERROR_INTERNAL_ERROR;
1029 WINPR_ASSERT(callback);
1032 RdpgfxClientContext* context = gfx->context;
1033 UINT error = CHANNEL_RC_OK;
1035 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1036 return ERROR_INVALID_DATA;
1038 Stream_Read_UINT16(s, pdu.cacheSlot);
1039 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16
"",
1044 IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
1047 WLog_Print(gfx->base.log, WLOG_ERROR,
1048 "context->EvictCacheEntry failed with error %" PRIu32
"", error);
1061 UINT error = CHANNEL_RC_OK;
1064 WINPR_ASSERT(gfx->rdpcontext);
1065 rdpSettings* settings = gfx->rdpcontext->settings;
1066 RdpgfxClientContext* context = gfx->context;
1068 WINPR_ASSERT(context);
1069 WINPR_ASSERT(settings);
1072 return CHANNEL_RC_OK;
1074 const char* BitmapCachePersistFile =
1076 if (!BitmapCachePersistFile)
1077 return CHANNEL_RC_OK;
1079 if (!context->ExportCacheEntry)
1080 return CHANNEL_RC_INITIALIZATION_ERROR;
1082 rdpPersistentCache* persistent = persistent_cache_new();
1085 return CHANNEL_RC_NO_MEMORY;
1087 if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
1089 error = CHANNEL_RC_INITIALIZATION_ERROR;
1093 for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
1095 if (gfx->CacheSlots[idx])
1097 const UINT16 cacheSlot = idx;
1100 if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
1103 if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
1108 persistent_cache_free(persistent);
1112 persistent_cache_free(persistent);
1121static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
1124 UINT error = CHANNEL_RC_OK;
1126 if (!context || !pdu)
1127 return ERROR_BAD_ARGUMENTS;
1131 if (!gfx || !gfx->base.listener_callback)
1132 return ERROR_BAD_CONFIGURATION;
1137 return ERROR_BAD_CONFIGURATION;
1140 .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
1142 RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
1144 WLog_Print(gfx->base.log, WLOG_DEBUG,
"SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16
"",
1145 pdu->cacheEntriesCount);
1146 wStream* s = Stream_New(
nullptr, header.pduLength);
1150 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
1151 return CHANNEL_RC_NO_MEMORY;
1154 if ((error = rdpgfx_write_header(s, &header)))
1157 if (pdu->cacheEntriesCount <= 0)
1159 WLog_Print(gfx->base.log, WLOG_ERROR,
"Invalid cacheEntriesCount: %" PRIu16
"",
1160 pdu->cacheEntriesCount);
1161 error = ERROR_INVALID_DATA;
1166 Stream_Write_UINT16(s, pdu->cacheEntriesCount);
1168 for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
1171 Stream_Write_UINT64(s, cacheEntry->cacheKey);
1172 Stream_Write_UINT32(s, cacheEntry->bitmapLength);
1175 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
1179 Stream_Free(s, TRUE);
1191 UINT error = CHANNEL_RC_OK;
1196 WINPR_ASSERT(gfx->rdpcontext);
1198 RdpgfxClientContext* context = gfx->context;
1199 rdpSettings* settings = gfx->rdpcontext->settings;
1202 return CHANNEL_RC_OK;
1204 const char* BitmapCachePersistFile =
1206 if (!BitmapCachePersistFile)
1207 return CHANNEL_RC_OK;
1209 rdpPersistentCache* persistent = persistent_cache_new();
1212 return CHANNEL_RC_NO_MEMORY;
1214 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1216 error = CHANNEL_RC_INITIALIZATION_ERROR;
1220 if (persistent_cache_get_version(persistent) != 3)
1222 error = ERROR_INVALID_DATA;
1226 count = persistent_cache_get_count(persistent);
1229 error = ERROR_INVALID_DATA;
1233 if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
1234 count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
1236 if (count > gfx->MaxCacheSlots)
1237 count = gfx->MaxCacheSlots;
1242 error = CHANNEL_RC_NO_MEMORY;
1246 WINPR_ASSERT(count <= UINT16_MAX);
1247 offer->cacheEntriesCount = (UINT16)count;
1249 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Sending Cache Import Offer: %d", count);
1251 for (
int idx = 0; idx < count; idx++)
1253 if (persistent_cache_read_entry(persistent, &entry) < 1)
1255 error = ERROR_INVALID_DATA;
1259 offer->cacheEntries[idx].cacheKey = entry.key64;
1260 offer->cacheEntries[idx].bitmapLength = entry.size;
1263 if (offer->cacheEntriesCount > 0)
1265 error = rdpgfx_send_cache_import_offer_pdu(context, offer);
1266 if (error != CHANNEL_RC_OK)
1268 WLog_Print(gfx->base.log, WLOG_ERROR,
"Failed to send cache import offer PDU");
1274 persistent_cache_free(persistent);
1284static UINT rdpgfx_load_cache_import_reply(
RDPGFX_PLUGIN* gfx,
1287 UINT error = CHANNEL_RC_OK;
1288 rdpPersistentCache* persistent =
nullptr;
1290 WINPR_ASSERT(gfx->rdpcontext);
1291 rdpSettings* settings = gfx->rdpcontext->settings;
1292 RdpgfxClientContext* context = gfx->context;
1294 WINPR_ASSERT(settings);
1295 WINPR_ASSERT(reply);
1297 return CHANNEL_RC_OK;
1299 const char* BitmapCachePersistFile =
1301 if (!BitmapCachePersistFile)
1302 return CHANNEL_RC_OK;
1304 persistent = persistent_cache_new();
1307 return CHANNEL_RC_NO_MEMORY;
1309 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1311 error = CHANNEL_RC_INITIALIZATION_ERROR;
1315 if (persistent_cache_get_version(persistent) != 3)
1317 error = ERROR_INVALID_DATA;
1321 int count = persistent_cache_get_count(persistent);
1323 count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
1325 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Receiving Cache Import Reply: %d", count);
1327 for (
int idx = 0; idx < count; idx++)
1330 if (persistent_cache_read_entry(persistent, &entry) < 1)
1332 error = ERROR_INVALID_DATA;
1336 const UINT16 cacheSlot = reply->cacheSlots[idx];
1337 if (context && context->ImportCacheEntry)
1339 error = context->ImportCacheEntry(context, cacheSlot, &entry);
1340 if (error != CHANNEL_RC_OK)
1345 persistent_cache_free(persistent);
1349 persistent_cache_free(persistent);
1361 WINPR_ASSERT(callback);
1364 RdpgfxClientContext* context = gfx->context;
1365 UINT error = CHANNEL_RC_OK;
1367 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1368 return ERROR_INVALID_DATA;
1370 Stream_Read_UINT16(s, pdu.importedEntriesCount);
1372 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1374 return ERROR_INVALID_DATA;
1376 if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1377 return ERROR_INVALID_DATA;
1379 for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1381 Stream_Read_UINT16(s, pdu.cacheSlots[idx]);
1384 WLog_Print(gfx->base.log, WLOG_TRACE,
1385 "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16
"",
1386 pdu.importedEntriesCount);
1388 error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1392 WLog_Print(gfx->base.log, WLOG_ERROR,
1393 "rdpgfx_load_cache_import_reply failed with error %" PRIu32
"", error);
1399 IFCALLRET(context->CacheImportReply, error, context, &pdu);
1402 WLog_Print(gfx->base.log, WLOG_ERROR,
1403 "context->CacheImportReply failed with error %" PRIu32
"", error);
1417 WINPR_ASSERT(callback);
1420 RdpgfxClientContext* context = gfx->context;
1421 UINT error = CHANNEL_RC_OK;
1423 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1424 return ERROR_INVALID_DATA;
1426 Stream_Read_UINT16(s, pdu.surfaceId);
1427 Stream_Read_UINT16(s, pdu.width);
1428 Stream_Read_UINT16(s, pdu.height);
1429 Stream_Read_UINT8(s, pdu.pixelFormat);
1431 if ((pdu.width == 0) || (pdu.height == 0))
1433 WLog_Print(gfx->base.log, WLOG_ERROR,
1434 "context->RecvCreateSurfacePdu Width[%" PRIu32
"], Height[%" PRIu32
"] is 0",
1435 pdu.width, pdu.height);
1436 return ERROR_INVALID_DATA;
1438 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE))
1440 WLog_Print(gfx->base.log, WLOG_ERROR,
1441 "context->RecvCreateSurfacePdu Width[%" PRIu32
"], Height[%" PRIu32
1443 pdu.width, pdu.height, MAX_SURFACE_SIZE);
1444 return ERROR_INVALID_DATA;
1447 WLog_Print(gfx->base.log, WLOG_DEBUG,
1448 "RecvCreateSurfacePdu: surfaceId: %" PRIu16
" width: %" PRIu16
" height: %" PRIu16
1449 " pixelFormat: 0x%02" PRIX8
"",
1450 pdu.surfaceId, pdu.width, pdu.height, pdu.pixelFormat);
1459 const UINT drc = IFCALLRESULT(CHANNEL_RC_OK, context->DeleteSurface, context, &deletePdu);
1460 if (drc != CHANNEL_RC_OK)
1461 WLog_Print(gfx->base.log, WLOG_WARN,
1462 "context->DeleteSurface failed with error %" PRIu32
", ignoring", drc);
1464 IFCALLRET(context->CreateSurface, error, context, &pdu);
1467 WLog_Print(gfx->base.log, WLOG_ERROR,
1468 "context->CreateSurface failed with error %" PRIu32
"", error);
1482 WINPR_ASSERT(callback);
1485 RdpgfxClientContext* context = gfx->context;
1486 UINT error = CHANNEL_RC_OK;
1488 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1489 return ERROR_INVALID_DATA;
1491 Stream_Read_UINT16(s, pdu.surfaceId);
1492 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvDeleteSurfacePdu: surfaceId: %" PRIu16
"",
1497 IFCALLRET(context->DeleteSurface, error, context, &pdu);
1500 WLog_Print(gfx->base.log, WLOG_ERROR,
1501 "context->DeleteSurface failed with error %" PRIu32
"", error);
1515 WINPR_ASSERT(callback);
1518 RdpgfxClientContext* context = gfx->context;
1519 UINT error = CHANNEL_RC_OK;
1521 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1522 return ERROR_INVALID_DATA;
1524 Stream_Read_UINT32(s, pdu.timestamp);
1525 Stream_Read_UINT32(s, pdu.frameId);
1526 WLog_Print(gfx->base.log, WLOG_TRACE,
1527 "RecvStartFramePdu: frameId: %" PRIu32
" timestamp: 0x%08" PRIX32
"", pdu.frameId,
1529 gfx->StartDecodingTime = GetTickCount64();
1533 IFCALLRET(context->StartFrame, error, context, &pdu);
1536 WLog_Print(gfx->base.log, WLOG_ERROR,
1537 "context->StartFrame failed with error %" PRIu32
"", error);
1540 gfx->UnacknowledgedFrames++;
1541 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
1554 WINPR_ASSERT(callback);
1557 RdpgfxClientContext* context = gfx->context;
1558 UINT error = CHANNEL_RC_OK;
1560 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1561 return ERROR_INVALID_DATA;
1563 Stream_Read_UINT32(s, pdu.frameId);
1564 WLog_Print(gfx->base.log, WLOG_TRACE,
"RecvEndFramePdu: frameId: %" PRIu32
"", pdu.frameId);
1566 const UINT64 start = GetTickCount64();
1569 IFCALLRET(context->EndFrame, error, context, &pdu);
1573 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->EndFrame failed with error %" PRIu32
"",
1578 const UINT64 end = GetTickCount64();
1579 const UINT64 EndFrameTime = end - start;
1580 gfx->TotalDecodedFrames++;
1581 rdpgfx_stats_frame_decoded(context);
1583 if (!gfx->sendFrameAcks)
1586 ack.frameId = pdu.frameId;
1587 ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1589 if (gfx->suspendFrameAcks)
1591 ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1593 if (gfx->TotalDecodedFrames == 1)
1594 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1595 WLog_Print(gfx->base.log, WLOG_ERROR,
1596 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32
"",
1601 ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1603 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1604 WLog_Print(gfx->base.log, WLOG_ERROR,
1605 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32
"", error);
1608 switch (gfx->ConnectionCaps.version)
1610#if defined(WITH_GFX_AV1)
1611 case RDPGFX_CAPVERSION_FRDP_1:
1613#if defined(WITH_GFX_AV2)
1614 case RDPGFX_CAPVERSION_FRDP_2:
1616 case RDPGFX_CAPVERSION_10:
1617 case RDPGFX_CAPVERSION_102:
1618 case RDPGFX_CAPVERSION_103:
1619 case RDPGFX_CAPVERSION_104:
1620 case RDPGFX_CAPVERSION_105:
1621 case RDPGFX_CAPVERSION_106:
1622 case RDPGFX_CAPVERSION_106_ERR:
1623 case RDPGFX_CAPVERSION_107:
1624#if defined(WITH_GFX_AZURE)
1625 case RDPGFX_CAPVERSION_111:
1626 case RDPGFX_CAPVERSION_112:
1627 case RDPGFX_CAPVERSION_113:
1628 case RDPGFX_CAPVERSION_115:
1633 UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1638 qoe.frameId = pdu.frameId;
1639 qoe.timestamp = gfx->StartDecodingTime % UINT32_MAX;
1640 qoe.timeDiffSE = WINPR_ASSERTING_INT_CAST(UINT16, diff);
1641 qoe.timeDiffEDR = WINPR_ASSERTING_INT_CAST(UINT16, EndFrameTime);
1643 if ((error = rdpgfx_send_qoe_frame_acknowledge_pdu(context, &qoe)))
1644 WLog_Print(gfx->base.log, WLOG_ERROR,
1645 "rdpgfx_send_qoe_frame_acknowledge_pdu failed with error %" PRIu32
1668 WINPR_ASSERT(callback);
1673 if (!Stream_SafeSeek(s, Stream_GetRemainingLength(s)))
1674 return ERROR_INVALID_DATA;
1676 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvCapsResetPdu: sending the capabilities again");
1678 free_surfaces(gfx->context, gfx->SurfaceTable);
1679 const UINT error = evict_cache_slots(gfx->context, gfx->MaxCacheSlots, gfx->CacheSlots);
1683 WLog_Print(gfx->base.log, WLOG_ERROR,
"evict_cache_slots failed with error %" PRIu32
"",
1686 gfx->UnacknowledgedFrames = 0;
1687 gfx->TotalDecodedFrames = 0;
1689 return rdpgfx_send_supported_caps(callback);
1693static BOOL rdpgfx_codec_has_region_data_embedded(UINT16 codecId)
1697 case RDPGFX_CODECID_AVC420:
1698 case RDPGFX_CODECID_AVC444:
1699 case RDPGFX_CODECID_AVC444v2:
1700#if defined(WITH_GFX_AZURE)
1701 case RDPGFX_CODECID_HEVC420:
1702 case RDPGFX_CODECID_HEVC444:
1704#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1705 case RDPGFX_CODECID_AV1:
1722 WINPR_ASSERT(callback);
1727 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1728 return ERROR_INVALID_DATA;
1730 Stream_Read_UINT16(s, pdu.surfaceId);
1731 Stream_Read_UINT16(s, pdu.codecId);
1732 Stream_Read_UINT8(s, pdu.pixelFormat);
1734 if ((error = rdpgfx_read_rect16(
1735 gfx->base.log, s, &(pdu.destRect),
1736 rdpgfx_codec_has_region_data_embedded(pdu.codecId))))
1738 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"",
1743 Stream_Read_UINT32(s, pdu.bitmapDataLength);
1745 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1746 return ERROR_INVALID_DATA;
1748 pdu.bitmapData = Stream_Pointer(s);
1749 Stream_Seek(s, pdu.bitmapDataLength);
1751 WLog_Print(gfx->base.log, WLOG_TRACE,
1752 "RecvWireToSurface1Pdu: surfaceId: %" PRIu16
" codecId: %s (0x%04" PRIX16
1753 ") pixelFormat: 0x%02" PRIX8
" "
1754 "destRect: left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
1755 " bitmapDataLength: %" PRIu32
"",
1756 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId, pdu.pixelFormat,
1757 pdu.destRect.left, pdu.destRect.top, pdu.destRect.right, pdu.destRect.bottom,
1758 pdu.bitmapDataLength);
1759 cmd.surfaceId = pdu.surfaceId;
1760 cmd.codecId = pdu.codecId;
1763 switch (pdu.pixelFormat)
1765 case GFX_PIXEL_FORMAT_XRGB_8888:
1766 cmd.format = PIXEL_FORMAT_BGRX32;
1769 case GFX_PIXEL_FORMAT_ARGB_8888:
1770 cmd.format = PIXEL_FORMAT_BGRA32;
1774 return ERROR_INVALID_DATA;
1777 cmd.left = pdu.destRect.left;
1778 cmd.top = pdu.destRect.top;
1779 cmd.right = pdu.destRect.right;
1780 cmd.bottom = pdu.destRect.bottom;
1781 cmd.width = cmd.right - cmd.left;
1782 cmd.height = cmd.bottom - cmd.top;
1783 cmd.length = pdu.bitmapDataLength;
1784 cmd.data = pdu.bitmapData;
1785 cmd.extra =
nullptr;
1787 if (cmd.right < cmd.left)
1789 WLog_Print(gfx->base.log, WLOG_ERROR,
1790 "RecvWireToSurface1Pdu right=%" PRIu32
" < left=%" PRIu32, cmd.right, cmd.left);
1791 return ERROR_INVALID_DATA;
1793 if (cmd.bottom < cmd.top)
1795 WLog_Print(gfx->base.log, WLOG_ERROR,
1796 "RecvWireToSurface1Pdu bottom=%" PRIu32
" < top=%" PRIu32, cmd.bottom, cmd.top);
1797 return ERROR_INVALID_DATA;
1800 if ((error = rdpgfx_decode(gfx, &cmd)))
1801 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_decode failed with error %" PRIu32
"!",
1810 switch (cmd->codecId)
1812 case RDPGFX_CODECID_UNCOMPRESSED:
1813 case RDPGFX_CODECID_CAVIDEO:
1814 case RDPGFX_CODECID_CLEARCODEC:
1815 case RDPGFX_CODECID_PLANAR:
1816 case RDPGFX_CODECID_AVC420:
1817 case RDPGFX_CODECID_ALPHA:
1818 case RDPGFX_CODECID_AVC444:
1819 case RDPGFX_CODECID_AVC444v2:
1820 case RDPGFX_CODECID_CAPROGRESSIVE:
1821 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1822#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1823 case RDPGFX_CODECID_AV1:
1825#if defined(WITH_GFX_AZURE)
1826 case RDPGFX_CODECID_HEVC420:
1827 case RDPGFX_CODECID_HEVC444:
1829 if (!gfx->allowedCodecs[cmd->codecId])
1831 char buffer[128] = WINPR_C_ARRAY_INIT;
1833 gfx->base.log, WLOG_ERROR,
1834 "RDPGFX_SURFACE_COMMAND::codecId %s not allowed with %s [%s]",
1835 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)),
1836 rdpgfx_caps_version_str(gfx->ConnectionCaps.version),
1837 rdpgfx_caps_flags_str(gfx->ConnectionCaps.flags, buffer,
sizeof(buffer)));
1842 WLog_Print(gfx->base.log, WLOG_ERROR,
1843 "Unknown RDPGFX_SURFACE_COMMAND::codecId %" PRIu32, cmd->codecId);
1852 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Got GFX %s",
1853 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)));
1855 RdpgfxClientContext* context = gfx->context;
1857 return CHANNEL_RC_OK;
1859 if (!checkSurfaceCommand(gfx, cmd))
1860 return CHANNEL_RC_NULL_DATA;
1862 const UINT error = IFCALLRESULT(CHANNEL_RC_OK, context->SurfaceCommand, context, cmd);
1865 WLog_Print(gfx->base.log, WLOG_ERROR,
1866 "context->SurfaceCommand failed with error %" PRIu32
"", error);
1868 rdpgfx_codecid_event(context, cmd->codecId, cmd->length);
1882 WINPR_ASSERT(callback);
1886 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1887 return ERROR_INVALID_DATA;
1889 Stream_Read_UINT16(s, pdu.surfaceId);
1890 Stream_Read_UINT16(s, pdu.codecId);
1891 Stream_Read_UINT32(s, pdu.codecContextId);
1892 Stream_Read_UINT8(s, pdu.pixelFormat);
1893 Stream_Read_UINT32(s, pdu.bitmapDataLength);
1894 pdu.bitmapData = Stream_Pointer(s);
1895 if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1896 return ERROR_INVALID_DATA;
1898 WLog_Print(gfx->base.log, WLOG_TRACE,
1899 "RecvWireToSurface2Pdu: surfaceId: %" PRIu16
" codecId: %s (0x%04" PRIX16
") "
1900 "codecContextId: %" PRIu32
" pixelFormat: 0x%02" PRIX8
" bitmapDataLength: %" PRIu32
1902 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1903 pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1905 cmd.surfaceId = pdu.surfaceId;
1906 cmd.codecId = pdu.codecId;
1907 cmd.contextId = pdu.codecContextId;
1909 switch (pdu.pixelFormat)
1911 case GFX_PIXEL_FORMAT_XRGB_8888:
1912 cmd.format = PIXEL_FORMAT_BGRX32;
1915 case GFX_PIXEL_FORMAT_ARGB_8888:
1916 cmd.format = PIXEL_FORMAT_BGRA32;
1920 return ERROR_INVALID_DATA;
1923 cmd.length = pdu.bitmapDataLength;
1924 cmd.data = pdu.bitmapData;
1925 cmd.extra =
nullptr;
1927 if (cmd.codecId != RDPGFX_CODECID_CAPROGRESSIVE)
1928 return ERROR_INVALID_DATA;
1930 return logSurfaceCommand(gfx, &cmd);
1941 WINPR_ASSERT(callback);
1944 RdpgfxClientContext* context = gfx->context;
1945 UINT error = CHANNEL_RC_OK;
1947 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1948 return ERROR_INVALID_DATA;
1950 Stream_Read_UINT16(s, pdu.surfaceId);
1951 Stream_Read_UINT32(s, pdu.codecContextId);
1953 WLog_Print(gfx->base.log, WLOG_DEBUG,
1954 "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16
" codecContextId: %" PRIu32
"",
1955 pdu.surfaceId, pdu.codecContextId);
1959 IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1962 WLog_Print(gfx->base.log, WLOG_ERROR,
1963 "context->DeleteEncodingContext failed with error %" PRIu32
"", error);
1977 WINPR_ASSERT(callback);
1980 RdpgfxClientContext* context = gfx->context;
1982 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1983 return ERROR_INVALID_DATA;
1985 Stream_Read_UINT16(s, pdu.surfaceId);
1987 UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
1988 if (error != CHANNEL_RC_OK)
1990 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_color32 failed with error %" PRIu32
"!",
1995 Stream_Read_UINT16(s, pdu.fillRectCount);
1997 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
1998 return ERROR_INVALID_DATA;
2004 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2005 return CHANNEL_RC_NO_MEMORY;
2008 for (UINT16 index = 0; index < pdu.fillRectCount; index++)
2012 if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect, FALSE)))
2014 WLog_Print(gfx->base.log, WLOG_ERROR,
2015 "rdpgfx_read_rect16 failed with error %" PRIu32
"!", error);
2016 free(pdu.fillRects);
2020 WLog_Print(gfx->base.log, WLOG_TRACE,
2021 "RecvSolidFillPdu: surfaceId: %" PRIu16
" fillRectCount: %" PRIu16
"", pdu.surfaceId,
2026 IFCALLRET(context->SolidFill, error, context, &pdu);
2029 WLog_Print(gfx->base.log, WLOG_ERROR,
2030 "context->SolidFill failed with error %" PRIu32
"", error);
2033 free(pdu.fillRects);
2045 WINPR_ASSERT(callback);
2048 RdpgfxClientContext* context = gfx->context;
2051 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
2052 return ERROR_INVALID_DATA;
2054 Stream_Read_UINT16(s, pdu.surfaceIdSrc);
2055 Stream_Read_UINT16(s, pdu.surfaceIdDest);
2058 rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE)))
2060 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"!",
2065 Stream_Read_UINT16(s, pdu.destPtsCount);
2067 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2068 return ERROR_INVALID_DATA;
2074 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2075 return CHANNEL_RC_NO_MEMORY;
2078 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2082 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2084 WLog_Print(gfx->base.log, WLOG_ERROR,
2085 "rdpgfx_read_point16 failed with error %" PRIu32
"!", error);
2091 WLog_Print(gfx->base.log, WLOG_TRACE,
2092 "RecvSurfaceToSurfacePdu: surfaceIdSrc: %" PRIu16
" surfaceIdDest: %" PRIu16
" "
2093 "left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
2094 " destPtsCount: %" PRIu16
"",
2095 pdu.surfaceIdSrc, pdu.surfaceIdDest, pdu.rectSrc.left, pdu.rectSrc.top,
2096 pdu.rectSrc.right, pdu.rectSrc.bottom, pdu.destPtsCount);
2100 IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
2103 WLog_Print(gfx->base.log, WLOG_ERROR,
2104 "context->SurfaceToSurface failed with error %" PRIu32
"", error);
2119 WINPR_ASSERT(callback);
2123 RdpgfxClientContext* context = gfx->context;
2125 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2126 return ERROR_INVALID_DATA;
2128 Stream_Read_UINT16(s, pdu.surfaceId);
2129 Stream_Read_UINT64(s, pdu.cacheKey);
2130 Stream_Read_UINT16(s, pdu.cacheSlot);
2132 UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE);
2133 if (error != CHANNEL_RC_OK)
2135 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"!",
2140 WLog_Print(gfx->base.log, WLOG_TRACE,
2141 "RecvSurfaceToCachePdu: surfaceId: %" PRIu16
" cacheKey: 0x%016" PRIX64
2142 " cacheSlot: %" PRIu16
" "
2143 "left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
"",
2144 pdu.surfaceId, pdu.cacheKey, pdu.cacheSlot, pdu.rectSrc.left, pdu.rectSrc.top,
2145 pdu.rectSrc.right, pdu.rectSrc.bottom);
2149 IFCALLRET(context->SurfaceToCache, error, context, &pdu);
2152 WLog_Print(gfx->base.log, WLOG_ERROR,
2153 "context->SurfaceToCache failed with error %" PRIu32
"", error);
2167 WINPR_ASSERT(callback);
2171 RdpgfxClientContext* context = gfx->context;
2172 UINT error = CHANNEL_RC_OK;
2174 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
2175 return ERROR_INVALID_DATA;
2177 Stream_Read_UINT16(s, pdu.cacheSlot);
2178 Stream_Read_UINT16(s, pdu.surfaceId);
2179 Stream_Read_UINT16(s, pdu.destPtsCount);
2181 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2182 return ERROR_INVALID_DATA;
2188 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2189 return CHANNEL_RC_NO_MEMORY;
2192 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2196 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2198 WLog_Print(gfx->base.log, WLOG_ERROR,
2199 "rdpgfx_read_point16 failed with error %" PRIu32
"", error);
2205 WLog_Print(gfx->base.log, WLOG_TRACE,
2206 "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16
" surfaceId: %" PRIu16
2207 " destPtsCount: %" PRIu16
"",
2208 pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
2212 IFCALLRET(context->CacheToSurface, error, context, &pdu);
2215 WLog_Print(gfx->base.log, WLOG_ERROR,
2216 "context->CacheToSurface failed with error %" PRIu32
"", error);
2231 WINPR_ASSERT(callback);
2235 RdpgfxClientContext* context = gfx->context;
2236 UINT error = CHANNEL_RC_OK;
2238 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
2239 return ERROR_INVALID_DATA;
2241 Stream_Read_UINT16(s, pdu.surfaceId);
2242 Stream_Read_UINT16(s, pdu.reserved);
2243 Stream_Read_UINT32(s, pdu.outputOriginX);
2244 Stream_Read_UINT32(s, pdu.outputOriginY);
2246 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2248 WLog_Print(gfx->base.log, WLOG_ERROR,
2249 "context->MapSurfaceToOutput outputOriginX[%" PRIu32
"], outputOriginY[%" PRIu32
2251 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2252 return ERROR_INVALID_DATA;
2255 WLog_Print(gfx->base.log, WLOG_DEBUG,
2256 "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16
" outputOriginX: %" PRIu32
2257 " outputOriginY: %" PRIu32
"",
2258 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
2262 IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
2265 WLog_Print(gfx->base.log, WLOG_ERROR,
2266 "context->MapSurfaceToOutput failed with error %" PRIu32
"", error);
2276 WINPR_ASSERT(callback);
2280 RdpgfxClientContext* context = gfx->context;
2281 UINT error = CHANNEL_RC_OK;
2283 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2284 return ERROR_INVALID_DATA;
2286 Stream_Read_UINT16(s, pdu.surfaceId);
2287 Stream_Read_UINT16(s, pdu.reserved);
2288 Stream_Read_UINT32(s, pdu.outputOriginX);
2289 Stream_Read_UINT32(s, pdu.outputOriginY);
2290 Stream_Read_UINT32(s, pdu.targetWidth);
2291 Stream_Read_UINT32(s, pdu.targetHeight);
2293 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2295 WLog_Print(gfx->base.log, WLOG_ERROR,
2296 "context->MapSurfaceToScaledOutput outputOriginX[%" PRIu32
2297 "], outputOriginY[%" PRIu32
"] > %" PRIu32,
2298 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2299 return ERROR_INVALID_DATA;
2301 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2303 WLog_Print(gfx->base.log, WLOG_ERROR,
2304 "context->MapSurfaceToScaledOutput targetWidth[%" PRIu32
2305 "], targetHeight[%" PRIu32
"] > %" PRIu32,
2306 pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2307 return ERROR_INVALID_DATA;
2310 WLog_Print(gfx->base.log, WLOG_DEBUG,
2311 "RecvMapSurfaceToScaledOutputPdu: surfaceId: %" PRIu16
" outputOriginX: %" PRIu32
2312 " outputOriginY: %" PRIu32
" targetWidth: %" PRIu32
" targetHeight: %" PRIu32,
2313 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY, pdu.targetWidth,
2318 IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
2321 WLog_Print(gfx->base.log, WLOG_ERROR,
2322 "context->MapSurfaceToScaledOutput failed with error %" PRIu32
"", error);
2336 WINPR_ASSERT(callback);
2340 RdpgfxClientContext* context = gfx->context;
2341 UINT error = CHANNEL_RC_OK;
2343 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
2344 return ERROR_INVALID_DATA;
2346 Stream_Read_UINT16(s, pdu.surfaceId);
2347 Stream_Read_UINT64(s, pdu.windowId);
2348 Stream_Read_UINT32(s, pdu.mappedWidth);
2349 Stream_Read_UINT32(s, pdu.mappedHeight);
2351 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2353 WLog_Print(gfx->base.log, WLOG_ERROR,
2354 "context->MapSurfaceToWindow mappedWidth[%" PRIu32
"], mappedHeight[%" PRIu32
2356 pdu.mappedWidth, pdu.mappedHeight);
2357 return ERROR_INVALID_DATA;
2359 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2361 WLog_Print(gfx->base.log, WLOG_ERROR,
2362 "context->MapSurfaceToWindow mappedWidth[%" PRIu32
"], mappedHeight[%" PRIu32
2364 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2365 return ERROR_INVALID_DATA;
2368 WLog_Print(gfx->base.log, WLOG_DEBUG,
2369 "RecvMapSurfaceToWindowPdu: surfaceId: %" PRIu16
" windowId: 0x%016" PRIX64
2370 " mappedWidth: %" PRIu32
" mappedHeight: %" PRIu32
"",
2371 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight);
2373 if (context && context->MapSurfaceToWindow)
2375 IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
2378 WLog_Print(gfx->base.log, WLOG_ERROR,
2379 "context->MapSurfaceToWindow failed with error %" PRIu32
"", error);
2389 WINPR_ASSERT(callback);
2392 RdpgfxClientContext* context = gfx->context;
2393 UINT error = CHANNEL_RC_OK;
2395 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
2396 return ERROR_INVALID_DATA;
2398 Stream_Read_UINT16(s, pdu.surfaceId);
2399 Stream_Read_UINT64(s, pdu.windowId);
2400 Stream_Read_UINT32(s, pdu.mappedWidth);
2401 Stream_Read_UINT32(s, pdu.mappedHeight);
2402 Stream_Read_UINT32(s, pdu.targetWidth);
2403 Stream_Read_UINT32(s, pdu.targetHeight);
2405 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2407 WLog_Print(gfx->base.log, WLOG_ERROR,
2408 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2409 "], mappedHeight[%" PRIu32
"] is 0",
2410 pdu.mappedWidth, pdu.mappedHeight);
2411 return ERROR_INVALID_DATA;
2413 if ((pdu.targetWidth == 0) || (pdu.targetHeight == 0))
2415 WLog_Print(gfx->base.log, WLOG_ERROR,
2416 "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2417 "], targetHeight[%" PRIu32
"] is 0",
2418 pdu.targetWidth, pdu.targetHeight);
2419 return ERROR_INVALID_DATA;
2422 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2424 WLog_Print(gfx->base.log, WLOG_ERROR,
2425 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2426 "], mappedHeight[%" PRIu32
"] > %" PRIu32,
2427 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2428 return ERROR_INVALID_DATA;
2430 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2432 WLog_Print(gfx->base.log, WLOG_ERROR,
2433 "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2434 "], targetHeight[%" PRIu32
"] > %" PRIu32,
2435 pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2436 return ERROR_INVALID_DATA;
2439 WLog_Print(gfx->base.log, WLOG_DEBUG,
2440 "RecvMapSurfaceToScaledWindowPdu: surfaceId: %" PRIu16
" windowId: 0x%016" PRIX64
2441 " mappedWidth: %" PRIu32
" mappedHeight: %" PRIu32
" targetWidth: %" PRIu32
2442 " targetHeight: %" PRIu32
"",
2443 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight, pdu.targetWidth,
2446 if (context && context->MapSurfaceToScaledWindow)
2448 IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
2451 WLog_Print(gfx->base.log, WLOG_ERROR,
2452 "context->MapSurfaceToScaledWindow failed with error %" PRIu32
"", error);
2466 wStream sbuffer = WINPR_C_ARRAY_INIT;
2468 WINPR_ASSERT(callback);
2473 size_t packetlen = 0;
2474 UINT error = rdpgfx_read_header(gfx->base.log, data, &header, &packetlen);
2475 if (error != CHANNEL_RC_OK)
2477 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_header failed with error %" PRIu32
"!",
2483 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
2484 Stream_Seek(data, packetlen);
2486 WLog_Print(gfx->base.log, WLOG_TRACE,
2487 "cmdId: %s (0x%04" PRIX16
") flags: 0x%04" PRIX16
" pduLength: %" PRIu32
"",
2488 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
2491 rdpgfx_stats_cmdid_event(gfx->context, header.cmdId);
2492 switch (header.cmdId)
2494 case RDPGFX_CMDID_WIRETOSURFACE_1:
2495 if ((error = rdpgfx_recv_wire_to_surface_1_pdu(callback, s)))
2496 WLog_Print(gfx->base.log, WLOG_ERROR,
2497 "rdpgfx_recv_wire_to_surface_1_pdu failed with error %" PRIu32
"!",
2502 case RDPGFX_CMDID_WIRETOSURFACE_2:
2503 if ((error = rdpgfx_recv_wire_to_surface_2_pdu(callback, s)))
2504 WLog_Print(gfx->base.log, WLOG_ERROR,
2505 "rdpgfx_recv_wire_to_surface_2_pdu failed with error %" PRIu32
"!",
2510 case RDPGFX_CMDID_DELETEENCODINGCONTEXT:
2511 if ((error = rdpgfx_recv_delete_encoding_context_pdu(callback, s)))
2512 WLog_Print(gfx->base.log, WLOG_ERROR,
2513 "rdpgfx_recv_delete_encoding_context_pdu failed with error %" PRIu32
"!",
2518 case RDPGFX_CMDID_SOLIDFILL:
2519 if ((error = rdpgfx_recv_solid_fill_pdu(callback, s)))
2520 WLog_Print(gfx->base.log, WLOG_ERROR,
2521 "rdpgfx_recv_solid_fill_pdu failed with error %" PRIu32
"!", error);
2525 case RDPGFX_CMDID_SURFACETOSURFACE:
2526 if ((error = rdpgfx_recv_surface_to_surface_pdu(callback, s)))
2527 WLog_Print(gfx->base.log, WLOG_ERROR,
2528 "rdpgfx_recv_surface_to_surface_pdu failed with error %" PRIu32
"!",
2533 case RDPGFX_CMDID_SURFACETOCACHE:
2534 if ((error = rdpgfx_recv_surface_to_cache_pdu(callback, s)))
2535 WLog_Print(gfx->base.log, WLOG_ERROR,
2536 "rdpgfx_recv_surface_to_cache_pdu failed with error %" PRIu32
"!",
2541 case RDPGFX_CMDID_CACHETOSURFACE:
2542 if ((error = rdpgfx_recv_cache_to_surface_pdu(callback, s)))
2543 WLog_Print(gfx->base.log, WLOG_ERROR,
2544 "rdpgfx_recv_cache_to_surface_pdu failed with error %" PRIu32
"!",
2549 case RDPGFX_CMDID_EVICTCACHEENTRY:
2550 if ((error = rdpgfx_recv_evict_cache_entry_pdu(callback, s)))
2551 WLog_Print(gfx->base.log, WLOG_ERROR,
2552 "rdpgfx_recv_evict_cache_entry_pdu failed with error %" PRIu32
"!",
2557 case RDPGFX_CMDID_CREATESURFACE:
2558 if ((error = rdpgfx_recv_create_surface_pdu(callback, s)))
2559 WLog_Print(gfx->base.log, WLOG_ERROR,
2560 "rdpgfx_recv_create_surface_pdu failed with error %" PRIu32
"!", error);
2564 case RDPGFX_CMDID_DELETESURFACE:
2565 if ((error = rdpgfx_recv_delete_surface_pdu(callback, s)))
2566 WLog_Print(gfx->base.log, WLOG_ERROR,
2567 "rdpgfx_recv_delete_surface_pdu failed with error %" PRIu32
"!", error);
2571 case RDPGFX_CMDID_STARTFRAME:
2572 if ((error = rdpgfx_recv_start_frame_pdu(callback, s)))
2573 WLog_Print(gfx->base.log, WLOG_ERROR,
2574 "rdpgfx_recv_start_frame_pdu failed with error %" PRIu32
"!", error);
2578 case RDPGFX_CMDID_ENDFRAME:
2579 if ((error = rdpgfx_recv_end_frame_pdu(callback, s)))
2580 WLog_Print(gfx->base.log, WLOG_ERROR,
2581 "rdpgfx_recv_end_frame_pdu failed with error %" PRIu32
"!", error);
2585 case RDPGFX_CMDID_RESETGRAPHICS:
2586 if ((error = rdpgfx_recv_reset_graphics_pdu(callback, s)))
2587 WLog_Print(gfx->base.log, WLOG_ERROR,
2588 "rdpgfx_recv_reset_graphics_pdu failed with error %" PRIu32
"!", error);
2592 case RDPGFX_CMDID_MAPSURFACETOOUTPUT:
2593 if ((error = rdpgfx_recv_map_surface_to_output_pdu(callback, s)))
2594 WLog_Print(gfx->base.log, WLOG_ERROR,
2595 "rdpgfx_recv_map_surface_to_output_pdu failed with error %" PRIu32
"!",
2600 case RDPGFX_CMDID_CACHEIMPORTREPLY:
2601 if ((error = rdpgfx_recv_cache_import_reply_pdu(callback, s)))
2602 WLog_Print(gfx->base.log, WLOG_ERROR,
2603 "rdpgfx_recv_cache_import_reply_pdu failed with error %" PRIu32
"!",
2608 case RDPGFX_CMDID_CAPSCONFIRM:
2609 if ((error = rdpgfx_recv_caps_confirm_pdu(callback, s)))
2610 WLog_Print(gfx->base.log, WLOG_ERROR,
2611 "rdpgfx_recv_caps_confirm_pdu failed with error %" PRIu32
"!", error);
2613 if ((error = rdpgfx_send_cache_offer(gfx)))
2614 WLog_Print(gfx->base.log, WLOG_ERROR,
2615 "rdpgfx_send_cache_offer failed with error %" PRIu32
"!", error);
2619 case RDPGFX_CMDID_MAPSURFACETOWINDOW:
2620 if ((error = rdpgfx_recv_map_surface_to_window_pdu(callback, s)))
2621 WLog_Print(gfx->base.log, WLOG_ERROR,
2622 "rdpgfx_recv_map_surface_to_window_pdu failed with error %" PRIu32
"!",
2627 case RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW:
2628 if ((error = rdpgfx_recv_map_surface_to_scaled_window_pdu(callback, s)))
2629 WLog_Print(gfx->base.log, WLOG_ERROR,
2630 "rdpgfx_recv_map_surface_to_scaled_window_pdu failed with error %" PRIu32
2636 case RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT:
2637 if ((error = rdpgfx_recv_map_surface_to_scaled_output_pdu(callback, s)))
2638 WLog_Print(gfx->base.log, WLOG_ERROR,
2639 "rdpgfx_recv_map_surface_to_scaled_output_pdu failed with error %" PRIu32
2645 case RDPGFX_CMDID_PROTECT_SURFACE:
2646 case RDPGFX_CMDID_WATERMARK:
2647 WLog_Print(gfx->base.log, WLOG_WARN,
2648 "Command %s not implemented, skipping %" PRIu32
" bytes",
2649 rdpgfx_get_cmd_id_string(header.cmdId), header.pduLength);
2650 if (!Stream_SafeSeek(s, packetlen))
2651 error = ERROR_INVALID_DATA;
2654 case RDPGFX_CMDID_UNUSED_0014:
2656 WLog_Print(gfx->base.log, WLOG_DEBUG,
2657 "Diagnostic information from the server, skipping %" PRIu32
" bytes",
2659 if (!Stream_SafeSeek(s, packetlen))
2660 error = ERROR_INVALID_DATA;
2663 case RDPGFX_CMDID_CAPSRESET:
2664 if ((error = rdpgfx_recv_caps_reset_pdu(callback, s)))
2665 WLog_Print(gfx->base.log, WLOG_ERROR,
2666 "rdpgfx_recv_caps_reset_pdu failed with error %" PRIu32
"!", error);
2670 error = CHANNEL_RC_BAD_PROC;
2676 WLog_Print(gfx->base.log, WLOG_ERROR,
2677 "Error while processing GFX cmdId: %s (0x%04" PRIX16
")",
2678 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
2682 const size_t end = Stream_GetPosition(s);
2684 if (end != packetlen)
2686 WLog_Print(gfx->base.log, WLOG_ERROR,
2687 "Unexpected gfx pdu end: Actual: %" PRIuz
", Expected: %" PRIuz, end, packetlen);
2698static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
2701 BYTE* pDstData =
nullptr;
2703 WINPR_ASSERT(callback);
2705 UINT error = CHANNEL_RC_OK;
2708 int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2709 (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2711 if ((status < 0) || (DstSize == 0))
2713 WLog_Print(gfx->base.log, WLOG_ERROR,
2714 "zgfx_decompress failure! status: %d, DstSize=%" PRIu32, status, DstSize);
2716 return ERROR_INTERNAL_ERROR;
2719 wStream sbuffer = WINPR_C_ARRAY_INIT;
2720 wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2724 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2726 return CHANNEL_RC_NO_MEMORY;
2729 while (Stream_GetPosition(s) < Stream_Length(s))
2731 if ((error = rdpgfx_recv_pdu(callback, s)))
2733 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_recv_pdu failed with error %" PRIu32
"!",
2748static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2751 WINPR_ASSERT(callback);
2754 RdpgfxClientContext* context = gfx->context;
2755 UINT error = CHANNEL_RC_OK;
2756 BOOL do_caps_advertise = TRUE;
2758 gfx->sendFrameAcks = TRUE;
2762 IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2765 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->OnOpen failed with error %" PRIu32
"",
2768 RdpgfxClientContextInt* ctx = (RdpgfxClientContextInt*)context;
2769 const RdpgfxClientContextStats empty = WINPR_C_ARRAY_INIT;
2773 if (do_caps_advertise)
2774 error = rdpgfx_send_supported_caps(callback);
2779static void rdpgfx_dump_stats(RdpgfxClientContext* context)
2781 WINPR_ASSERT(context);
2786 wLog* log = gfx->base.log;
2787 const DWORD level = WLOG_TRACE;
2788 if (!WLog_IsLevelActive(log, level))
2791 WLog_Print(log, level,
"RdpgfxClientContextStats");
2792 for (
size_t x = 0; x < rdpgfx_stats_max_index(); x++)
2794 const char* name = rdpgfx_stats_name_for_index(x);
2795 const uint64_t val = rdpgfx_stats_value_for_index(context, x);
2796 WLog_Print(log, level,
"%s: %" PRIu64, name, val);
2805static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2807 UINT error = CHANNEL_RC_OK;
2809 WINPR_ASSERT(callback);
2815 RdpgfxClientContext* context = gfx->context;
2817 WLog_Print(gfx->base.log, WLOG_DEBUG,
"OnClose");
2818 error = rdpgfx_save_persistent_cache(gfx);
2823 WLog_Print(gfx->base.log, WLOG_ERROR,
2824 "rdpgfx_save_persistent_cache failed with error %" PRIu32
"", error);
2827 free_surfaces(context, gfx->SurfaceTable);
2828 error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2832 WLog_Print(gfx->base.log, WLOG_ERROR,
"evict_cache_slots failed with error %" PRIu32
"",
2837 gfx->UnacknowledgedFrames = 0;
2838 gfx->TotalDecodedFrames = 0;
2842 error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2846 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->OnClose failed with error %" PRIu32
"",
2849 rdpgfx_dump_stats(context);
2853 return CHANNEL_RC_OK;
2860 RdpgfxClientContext* context = gfx->context;
2862 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Terminated");
2863 rdpgfx_client_context_free(context);
2871static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId,
void* pData)
2873 WINPR_ASSERT(context);
2876 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2880 if (!HashTable_Insert(gfx->SurfaceTable, (
void*)key, pData))
2881 return ERROR_BAD_ARGUMENTS;
2884 HashTable_Remove(gfx->SurfaceTable, (
void*)key);
2886 return CHANNEL_RC_OK;
2894static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2897 ULONG_PTR* pKeys =
nullptr;
2898 WINPR_ASSERT(context);
2901 size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2903 WINPR_ASSERT(ppSurfaceIds);
2904 WINPR_ASSERT(count_out);
2908 return CHANNEL_RC_OK;
2911 UINT16* pSurfaceIds = (UINT16*)calloc(count,
sizeof(UINT16));
2915 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2917 return CHANNEL_RC_NO_MEMORY;
2920 for (
size_t index = 0; index < count; index++)
2922 pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2926 *ppSurfaceIds = pSurfaceIds;
2927 *count_out = (UINT16)count;
2928 return CHANNEL_RC_OK;
2931static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2933 WINPR_ASSERT(context);
2936 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2937 return HashTable_GetItemValue(gfx->SurfaceTable, (
void*)key);
2945static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot,
void* pData)
2947 WINPR_ASSERT(context);
2952 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2954 WLog_Print(gfx->base.log, WLOG_ERROR,
2955 "invalid cache slot %" PRIu16
", must be between 1 and %" PRIu16
"", cacheSlot,
2956 gfx->MaxCacheSlots);
2957 return ERROR_INVALID_INDEX;
2960 gfx->CacheSlots[cacheSlot - 1] = pData;
2961 return CHANNEL_RC_OK;
2964static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2966 WINPR_ASSERT(context);
2970 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2972 WLog_Print(gfx->base.log, WLOG_ERROR,
2973 "invalid cache slot %" PRIu16
", must be between 1 and %" PRIu16
"", cacheSlot,
2974 gfx->MaxCacheSlots);
2978 return gfx->CacheSlots[cacheSlot - 1];
2982 WINPR_ATTR_UNUSED rdpSettings* settings)
2987 gfx->rdpcontext = rcontext;
2989 gfx->SurfaceTable = HashTable_New(TRUE);
2990 if (!gfx->SurfaceTable)
2992 WLog_Print(gfx->base.log, WLOG_ERROR,
"HashTable_New for surfaces failed !");
2993 return CHANNEL_RC_NO_MEMORY;
2996 gfx->suspendFrameAcks =
2998 gfx->MaxCacheSlots =
3001 RdpgfxClientContextInt* context =
3002 (RdpgfxClientContextInt*)calloc(1,
sizeof(RdpgfxClientContextInt));
3005 WLog_Print(gfx->base.log, WLOG_ERROR,
"context calloc failed!");
3006 HashTable_Free(gfx->SurfaceTable);
3007 gfx->SurfaceTable =
nullptr;
3008 return CHANNEL_RC_NO_MEMORY;
3011 gfx->zgfx = zgfx_context_new(FALSE);
3014 WLog_Print(gfx->base.log, WLOG_ERROR,
"zgfx_context_new failed!");
3015 HashTable_Free(gfx->SurfaceTable);
3016 gfx->SurfaceTable =
nullptr;
3018 return CHANNEL_RC_NO_MEMORY;
3021 context->common.handle = (
void*)gfx;
3022 context->common.GetSurfaceIds = rdpgfx_get_surface_ids;
3023 context->common.SetSurfaceData = rdpgfx_set_surface_data;
3024 context->common.GetSurfaceData = rdpgfx_get_surface_data;
3025 context->common.SetCacheSlotData = rdpgfx_set_cache_slot_data;
3026 context->common.GetCacheSlotData = rdpgfx_get_cache_slot_data;
3027 context->common.CapsAdvertise = rdpgfx_send_caps_advertise_pdu;
3028 context->common.FrameAcknowledge = rdpgfx_send_frame_acknowledge_pdu;
3029 context->common.CacheImportOffer = rdpgfx_send_cache_import_offer_pdu;
3030 context->common.QoeFrameAcknowledge = rdpgfx_send_qoe_frame_acknowledge_pdu;
3032 gfx->base.iface.pInterface = (
void*)context;
3033 gfx->context = &context->common;
3034 return CHANNEL_RC_OK;
3037void rdpgfx_client_context_free(RdpgfxClientContext* context)
3044 free_surfaces(context, gfx->SurfaceTable);
3045 evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
3049 zgfx_context_free(gfx->zgfx);
3050 gfx->zgfx =
nullptr;
3053 HashTable_Free(gfx->SurfaceTable);
3057static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
3058 rdpgfx_on_open, rdpgfx_on_close,
3066FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
3068 return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
3070 &rdpgfx_callbacks, init_plugin_cb, terminate_plugin_cb);
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string 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.