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 ULONG_PTR* pKeys =
nullptr;
227 WINPR_ASSERT(context);
233 const size_t count = HashTable_GetKeys(SurfaceTable, &pKeys);
235 for (
size_t index = 0; index < count; index++)
237 if (!delete_surface((
const void*)pKeys[index],
nullptr, context))
241 WLog_Print(gfx->base.log, WLOG_WARN,
"delete_surface failed");
249static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots,
void** CacheSlots)
251 UINT error = CHANNEL_RC_OK;
253 WINPR_ASSERT(CacheSlots);
254 for (UINT16 index = 0; index < MaxCacheSlots; index++)
256 if (CacheSlots[index])
260 if (context && context->EvictCacheEntry)
262 const UINT rc = context->EvictCacheEntry(context, &pdu);
263 if (rc != CHANNEL_RC_OK)
267 CacheSlots[index] =
nullptr;
278static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
281 UINT error = CHANNEL_RC_OK;
284 WINPR_ASSERT(context);
288 if (!gfx || !gfx->base.listener_callback)
289 return ERROR_BAD_ARGUMENTS;
291 memset(gfx->allowedCodecs, FALSE,
sizeof(gfx->allowedCodecs));
296 .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
297 .pduLength = RDPGFX_HEADER_SIZE + 2 };
299 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
302 header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
305 WLog_Print(gfx->base.log, WLOG_DEBUG,
"SendCapsAdvertisePdu %" PRIu16
"", pdu->capsSetCount);
306 wStream* s = Stream_New(
nullptr, header.pduLength);
310 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
311 return CHANNEL_RC_NO_MEMORY;
314 if ((error = rdpgfx_write_header(s, &header)))
318 Stream_Write_UINT16(s, pdu->capsSetCount);
320 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
324 char buffer[128] = WINPR_C_ARRAY_INIT;
325 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Sending %s [0x%08" PRIx32
"] flags=%s",
326 rdpgfx_caps_version_str(capsSet->version), capsSet->version,
327 rdpgfx_caps_flags_str(capsSet->flags, buffer,
sizeof(buffer)));
329 Stream_Write_UINT32(s, capsSet->version);
330 Stream_Write_UINT32(s, capsSet->length);
331 Stream_Write_UINT32(s, capsSet->flags);
332 if (capsSet->length < 4)
334 Stream_Zero(s, capsSet->length - 4);
337 Stream_SealLength(s);
338 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
341 Stream_Free(s, TRUE);
345static BOOL rdpgfx_is_capability_filtered(
RDPGFX_PLUGIN* gfx, UINT32 caps)
348 const UINT32 filter =
350 const UINT32 capList[] = {
351#if defined(WITH_GFX_AV1)
352 RDPGFX_CAPVERSION_FRDP_1,
354#if defined(WITH_GFX_AV2)
355 RDPGFX_CAPVERSION_FRDP_2,
358 RDPGFX_CAPVERSION_81,
359 RDPGFX_CAPVERSION_10,
360 RDPGFX_CAPVERSION_101,
361 RDPGFX_CAPVERSION_102,
362 RDPGFX_CAPVERSION_103,
363 RDPGFX_CAPVERSION_104,
364 RDPGFX_CAPVERSION_105,
365 RDPGFX_CAPVERSION_106,
366 RDPGFX_CAPVERSION_106_ERR,
367 RDPGFX_CAPVERSION_107
368#if defined(WITH_GFX_AZURE)
370 RDPGFX_CAPVERSION_111,
371 RDPGFX_CAPVERSION_112,
372 RDPGFX_CAPVERSION_113,
373 RDPGFX_CAPVERSION_115
377 for (
size_t x = 0; x < ARRAYSIZE(capList); x++)
379 if (caps == capList[x])
380 return (filter & (1u << x)) != 0;
390 WINPR_ASSERT(pdu->capsSets);
391 WINPR_ASSERT(count > 0);
392 WINPR_ASSERT(pdu->capsSetCount < count);
393 if (pdu->capsSetCount >= count)
395 return &pdu->capsSets[pdu->capsSetCount++];
398#if defined(WITH_GFX_AZURE)
407 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I420, FALSE))
410 WLog_Print(gfx->base.log, WLOG_WARN,
411 "HEVC mode enabled, but decoder does not support it. Disabling.");
423 RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
426 return ERROR_BAD_ARGUMENTS;
431 return ERROR_BAD_CONFIGURATION;
433 RdpgfxClientContext* context = gfx->context;
436 return ERROR_BAD_CONFIGURATION;
440 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
442 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
444 return ERROR_BAD_CONFIGURATION;
445 capsSet->version = RDPGFX_CAPVERSION_8;
450 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
457 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
460 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
462 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
464 return ERROR_BAD_CONFIGURATION;
465 capsSet->version = RDPGFX_CAPVERSION_81;
470 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
473 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
476 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC420, FALSE))
477 capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
480 UINT32 caps10Flags = 0;
485 caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
488 !freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC444, FALSE))
489 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
492 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
494 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
496 return ERROR_BAD_CONFIGURATION;
497 capsSet->version = RDPGFX_CAPVERSION_10;
499 capsSet->flags = caps10Flags;
502 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
504 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
506 return ERROR_BAD_CONFIGURATION;
507 capsSet->version = RDPGFX_CAPVERSION_101;
508 capsSet->length = 0x10;
512 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
514 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
516 return ERROR_BAD_CONFIGURATION;
517 capsSet->version = RDPGFX_CAPVERSION_102;
518 capsSet->length = 0x4;
519 capsSet->flags = caps10Flags;
524 if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
525 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
528 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
530 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
532 return ERROR_BAD_CONFIGURATION;
533 capsSet->version = RDPGFX_CAPVERSION_103;
534 capsSet->length = 0x4;
535 capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
538 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
540 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
542 return ERROR_BAD_CONFIGURATION;
543 capsSet->version = RDPGFX_CAPVERSION_104;
544 capsSet->length = 0x4;
545 capsSet->flags = caps10Flags;
551 if (freerdp_image_scaling_supported())
553 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
555 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
557 return ERROR_BAD_CONFIGURATION;
558 capsSet->version = RDPGFX_CAPVERSION_105;
559 capsSet->length = 0x4;
560 capsSet->flags = caps10Flags;
563 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
565 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
567 return ERROR_BAD_CONFIGURATION;
568 capsSet->version = RDPGFX_CAPVERSION_106;
569 capsSet->length = 0x4;
570 capsSet->flags = caps10Flags;
573 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
575 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
577 return ERROR_BAD_CONFIGURATION;
578 capsSet->version = RDPGFX_CAPVERSION_106_ERR;
579 capsSet->length = 0x4;
580 capsSet->flags = caps10Flags;
584 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
586 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
588 return ERROR_BAD_CONFIGURATION;
589 capsSet->version = RDPGFX_CAPVERSION_107;
590 capsSet->length = 0x4;
591 capsSet->flags = caps10Flags;
592 if (!freerdp_image_scaling_supported())
593 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
596#if defined(WITH_GFX_AZURE)
597 if (freerdp_image_scaling_supported())
599 const UINT32 caps[] = { RDPGFX_CAPVERSION_111, RDPGFX_CAPVERSION_112, RDPGFX_CAPVERSION_113,
600 RDPGFX_CAPVERSION_115 };
601 const BOOL hevc = rdpgfx_hevc_enabled(gfx);
604 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I444, FALSE);
606 for (
size_t x = 0; x < ARRAYSIZE(caps); x++)
608 const UINT32 cap = caps[x];
610 if (rdpgfx_is_capability_filtered(gfx, cap))
614 UINT32 flags = caps10Flags;
618 if (hevc && (cap >= RDPGFX_CAPVERSION_112))
619 flags |= RDPGFX_CAPS_FLAG_HEVC_ENABLE;
622 if (hevc444 && (cap >= RDPGFX_CAPVERSION_115))
623 flags |= RDPGFX_CAPS_FLAG_HEVC_I444;
630 if ((cap >= RDPGFX_CAPVERSION_113) &&
631 ((flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0))
632 flags |= RDPGFX_CAPS_FLAG_AVCMM_ENABLE;
635 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
637 return ERROR_BAD_CONFIGURATION;
638 capsSet->version = cap;
639 capsSet->length = 0x4;
640 capsSet->flags = flags;
641 if (!freerdp_image_scaling_supported())
642 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
647#if defined(WITH_GFX_AV1)
650 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I420, FALSE) &&
651 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
653 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
655 return ERROR_BAD_CONFIGURATION;
656 capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
657 capsSet->length = 0x4;
659 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I444, FALSE))
660 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
662 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
664 const UINT32 profile =
667 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
669 if (!freerdp_image_scaling_supported())
670 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
675#if defined(WITH_GFX_AV2)
680 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I420, FALSE) &&
681 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_2))
683 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
685 return ERROR_BAD_CONFIGURATION;
686 capsSet->version = RDPGFX_CAPVERSION_FRDP_2;
687 capsSet->length = 0x4;
689 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I444, FALSE))
691 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
694 FreeRDP_GfxCodecAV2Profile);
696 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
701 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
704 if (!freerdp_image_scaling_supported())
705 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
710 return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
720 WINPR_ASSERT(callback);
724 RdpgfxClientContext* context = gfx->context;
729 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
730 return ERROR_INVALID_DATA;
732 Stream_Read_UINT32(s, capsSet.version);
733 Stream_Read_UINT32(s, capsSet.length);
734 Stream_Read_UINT32(s, capsSet.flags);
735 gfx->TotalDecodedFrames = 0;
736 gfx->ConnectionCaps = capsSet;
738 context->ConfirmedCaps = capsSet;
740 char buffer[128] = WINPR_C_ARRAY_INIT;
742 WLog_Print(gfx->base.log, WLOG_DEBUG,
743 "RecvCapsConfirmPdu: version: %s [0x%08" PRIX32
"] flags: %s",
744 rdpgfx_caps_version_str(capsSet.version), capsSet.version,
745 rdpgfx_caps_flags_str(capsSet.flags, buffer,
sizeof(buffer)));
747 memset(gfx->allowedCodecs, TRUE,
sizeof(gfx->allowedCodecs));
749 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = FALSE;
750 gfx->allowedCodecs[RDPGFX_CODECID_AVC444] = FALSE;
751 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = FALSE;
752#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
753 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = FALSE;
756 if (capsSet.version >= RDPGFX_CAPVERSION_10)
758 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
760 WLog_Print(gfx->base.log, WLOG_INFO,
"AVC not disabled by server");
761 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
762 gfx->allowedCodecs[RDPGFX_CODECID_AVC444] = TRUE;
766 if (capsSet.version >= RDPGFX_CAPVERSION_103)
768 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
769 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = TRUE;
772 if (capsSet.version == RDPGFX_CAPVERSION_81)
774 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC420_ENABLED) != 0)
776 WLog_Print(gfx->base.log, WLOG_INFO,
"AVC420 enabled by server");
777 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
781#if defined(WITH_GFX_AV1)
782 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_1)
783 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
785#if defined(WITH_GFX_AV2)
786 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_2)
787 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
790#if defined(WITH_GFX_AZURE)
791 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = FALSE;
792 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = FALSE;
794 if (capsSet.version >= RDPGFX_CAPVERSION_112)
796 if ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_ENABLE) != 0)
798 WLog_Print(gfx->base.log, WLOG_INFO,
"HEVC confirmed by server");
799 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = TRUE;
802 if ((capsSet.version >= RDPGFX_CAPVERSION_115) &&
803 ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_I444) != 0))
805 WLog_Print(gfx->base.log, WLOG_INFO,
"HEVC I444 confirmed by server");
806 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = TRUE;
813 return ERROR_BAD_CONFIGURATION;
815 return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
823static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
828 if (!context || !pdu)
829 return ERROR_BAD_ARGUMENTS;
833 if (!gfx || !gfx->base.listener_callback)
834 return ERROR_BAD_CONFIGURATION;
839 return ERROR_BAD_CONFIGURATION;
842 .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
843 .pduLength = RDPGFX_HEADER_SIZE + 12 };
845 WLog_Print(gfx->base.log, WLOG_TRACE,
"SendFrameAcknowledgePdu: %" PRIu32
"", pdu->frameId);
847 wStream* s = Stream_New(
nullptr, header.pduLength);
851 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
852 return CHANNEL_RC_NO_MEMORY;
855 if ((error = rdpgfx_write_header(s, &header)))
859 Stream_Write_UINT32(s, pdu->queueDepth);
860 Stream_Write_UINT32(s, pdu->frameId);
861 Stream_Write_UINT32(s, pdu->totalFramesDecoded);
862 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
865 if (error == CHANNEL_RC_OK)
867 gfx->UnacknowledgedFrames--;
868 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
872 Stream_Free(s, TRUE);
876static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
879 UINT error = CHANNEL_RC_OK;
881 .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
882 .pduLength = RDPGFX_HEADER_SIZE + 12 };
884 if (!context || !pdu)
885 return ERROR_BAD_ARGUMENTS;
889 if (!gfx || !gfx->base.listener_callback)
890 return ERROR_BAD_CONFIGURATION;
895 return ERROR_BAD_CONFIGURATION;
897 WLog_Print(gfx->base.log, WLOG_TRACE,
"SendQoeFrameAcknowledgePdu: %" PRIu32
"", pdu->frameId);
898 wStream* s = Stream_New(
nullptr, header.pduLength);
902 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
903 return CHANNEL_RC_NO_MEMORY;
906 if ((error = rdpgfx_write_header(s, &header)))
910 Stream_Write_UINT32(s, pdu->frameId);
911 Stream_Write_UINT32(s, pdu->timestamp);
912 Stream_Write_UINT16(s, pdu->timeDiffSE);
913 Stream_Write_UINT16(s, pdu->timeDiffEDR);
914 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
917 Stream_Free(s, TRUE);
929 WINPR_ASSERT(callback);
935 RdpgfxClientContext* context = gfx->context;
936 UINT error = CHANNEL_RC_OK;
937 GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
939 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
940 return ERROR_INVALID_DATA;
942 Stream_Read_UINT32(s, pdu.width);
943 Stream_Read_UINT32(s, pdu.height);
944 Stream_Read_UINT32(s, pdu.monitorCount);
946 if ((pdu.width == 0) || (pdu.height == 0) || (pdu.monitorCount == 0))
948 WLog_Print(gfx->base.log, WLOG_ERROR,
949 "context->ResetGraphics Width[%" PRIu32
"], Height[%" PRIu32
950 "] or monitorCount[%" PRIu32
" is 0",
951 pdu.width, pdu.height, pdu.monitorCount);
952 return ERROR_INVALID_DATA;
954 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE) ||
955 (pdu.monitorCount > MAX_MONITOR_COUNT))
957 WLog_Print(gfx->base.log, WLOG_ERROR,
958 "context->ResetGraphics Width[%" PRIu32
"], Height[%" PRIu32
"] > %" PRIu32
959 " or monitorCount[%" PRIu32
" > %" PRIu32,
960 pdu.width, pdu.height, MAX_SURFACE_SIZE, pdu.monitorCount, MAX_MONITOR_COUNT);
961 return ERROR_INVALID_DATA;
964 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
965 return ERROR_INVALID_DATA;
969 if (!pdu.monitorDefArray)
971 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
972 return CHANNEL_RC_NO_MEMORY;
975 for (UINT32 index = 0; index < pdu.monitorCount; index++)
977 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
978 Stream_Read_INT32(s, monitor->left);
979 Stream_Read_INT32(s, monitor->top);
980 Stream_Read_INT32(s, monitor->right);
981 Stream_Read_INT32(s, monitor->bottom);
982 Stream_Read_UINT32(s, monitor->flags);
985 const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
988 free(pdu.monitorDefArray);
989 return CHANNEL_RC_NULL_DATA;
991 const size_t pad = 340ULL - size;
993 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (
size_t)pad))
995 free(pdu.monitorDefArray);
996 return CHANNEL_RC_NO_MEMORY;
1000 WLog_Print(gfx->base.log, WLOG_DEBUG,
1001 "RecvResetGraphicsPdu: width: %" PRIu32
" height: %" PRIu32
" count: %" PRIu32
"",
1002 pdu.width, pdu.height, pdu.monitorCount);
1004 for (UINT32 index = 0; index < pdu.monitorCount; index++)
1006 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
1007 WLog_Print(gfx->base.log, WLOG_TRACE,
1008 "RecvResetGraphicsPdu: monitor left:%" PRIi32
" top:%" PRIi32
" right:%" PRIi32
1009 " bottom:%" PRIi32
" flags:0x%" PRIx32
"",
1010 monitor->left, monitor->top, monitor->right, monitor->bottom, monitor->flags);
1015 IFCALLRET(context->ResetGraphics, error, context, &pdu);
1018 WLog_Print(gfx->base.log, WLOG_ERROR,
1019 "context->ResetGraphics failed with error %" PRIu32
"", error);
1022 free(pdu.monitorDefArray);
1025 if (error == CHANNEL_RC_OK)
1027 EventArgsInit(&graphicsReset,
"libfreerdp");
1028 graphicsReset.width = pdu.width;
1029 graphicsReset.height = pdu.height;
1030 if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
1031 return ERROR_INTERNAL_ERROR;
1044 WINPR_ASSERT(callback);
1047 RdpgfxClientContext* context = gfx->context;
1048 UINT error = CHANNEL_RC_OK;
1050 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1051 return ERROR_INVALID_DATA;
1053 Stream_Read_UINT16(s, pdu.cacheSlot);
1054 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16
"",
1059 IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
1062 WLog_Print(gfx->base.log, WLOG_ERROR,
1063 "context->EvictCacheEntry failed with error %" PRIu32
"", error);
1076 UINT error = CHANNEL_RC_OK;
1079 WINPR_ASSERT(gfx->rdpcontext);
1080 rdpSettings* settings = gfx->rdpcontext->settings;
1081 RdpgfxClientContext* context = gfx->context;
1083 WINPR_ASSERT(context);
1084 WINPR_ASSERT(settings);
1087 return CHANNEL_RC_OK;
1089 const char* BitmapCachePersistFile =
1091 if (!BitmapCachePersistFile)
1092 return CHANNEL_RC_OK;
1094 if (!context->ExportCacheEntry)
1095 return CHANNEL_RC_INITIALIZATION_ERROR;
1097 rdpPersistentCache* persistent = persistent_cache_new();
1100 return CHANNEL_RC_NO_MEMORY;
1102 if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
1104 error = CHANNEL_RC_INITIALIZATION_ERROR;
1108 for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
1110 if (gfx->CacheSlots[idx])
1112 const UINT16 cacheSlot = idx;
1115 if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
1118 if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
1123 persistent_cache_free(persistent);
1127 persistent_cache_free(persistent);
1136static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
1139 UINT error = CHANNEL_RC_OK;
1141 if (!context || !pdu)
1142 return ERROR_BAD_ARGUMENTS;
1146 if (!gfx || !gfx->base.listener_callback)
1147 return ERROR_BAD_CONFIGURATION;
1152 return ERROR_BAD_CONFIGURATION;
1155 .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
1157 RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
1159 WLog_Print(gfx->base.log, WLOG_DEBUG,
"SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16
"",
1160 pdu->cacheEntriesCount);
1161 wStream* s = Stream_New(
nullptr, header.pduLength);
1165 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
1166 return CHANNEL_RC_NO_MEMORY;
1169 if ((error = rdpgfx_write_header(s, &header)))
1172 if (pdu->cacheEntriesCount <= 0)
1174 WLog_Print(gfx->base.log, WLOG_ERROR,
"Invalid cacheEntriesCount: %" PRIu16
"",
1175 pdu->cacheEntriesCount);
1176 error = ERROR_INVALID_DATA;
1181 Stream_Write_UINT16(s, pdu->cacheEntriesCount);
1183 for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
1186 Stream_Write_UINT64(s, cacheEntry->cacheKey);
1187 Stream_Write_UINT32(s, cacheEntry->bitmapLength);
1190 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
1194 Stream_Free(s, TRUE);
1206 UINT error = CHANNEL_RC_OK;
1211 WINPR_ASSERT(gfx->rdpcontext);
1213 RdpgfxClientContext* context = gfx->context;
1214 rdpSettings* settings = gfx->rdpcontext->settings;
1217 return CHANNEL_RC_OK;
1219 const char* BitmapCachePersistFile =
1221 if (!BitmapCachePersistFile)
1222 return CHANNEL_RC_OK;
1224 rdpPersistentCache* persistent = persistent_cache_new();
1227 return CHANNEL_RC_NO_MEMORY;
1229 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1231 error = CHANNEL_RC_INITIALIZATION_ERROR;
1235 if (persistent_cache_get_version(persistent) != 3)
1237 error = ERROR_INVALID_DATA;
1241 count = persistent_cache_get_count(persistent);
1244 error = ERROR_INVALID_DATA;
1248 if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
1249 count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
1251 if (count > gfx->MaxCacheSlots)
1252 count = gfx->MaxCacheSlots;
1257 error = CHANNEL_RC_NO_MEMORY;
1261 WINPR_ASSERT(count <= UINT16_MAX);
1262 offer->cacheEntriesCount = (UINT16)count;
1264 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Sending Cache Import Offer: %d", count);
1266 for (
int idx = 0; idx < count; idx++)
1268 if (persistent_cache_read_entry(persistent, &entry) < 1)
1270 error = ERROR_INVALID_DATA;
1274 offer->cacheEntries[idx].cacheKey = entry.key64;
1275 offer->cacheEntries[idx].bitmapLength = entry.size;
1278 if (offer->cacheEntriesCount > 0)
1280 error = rdpgfx_send_cache_import_offer_pdu(context, offer);
1281 if (error != CHANNEL_RC_OK)
1283 WLog_Print(gfx->base.log, WLOG_ERROR,
"Failed to send cache import offer PDU");
1289 persistent_cache_free(persistent);
1299static UINT rdpgfx_load_cache_import_reply(
RDPGFX_PLUGIN* gfx,
1302 UINT error = CHANNEL_RC_OK;
1303 rdpPersistentCache* persistent =
nullptr;
1305 WINPR_ASSERT(gfx->rdpcontext);
1306 rdpSettings* settings = gfx->rdpcontext->settings;
1307 RdpgfxClientContext* context = gfx->context;
1309 WINPR_ASSERT(settings);
1310 WINPR_ASSERT(reply);
1312 return CHANNEL_RC_OK;
1314 const char* BitmapCachePersistFile =
1316 if (!BitmapCachePersistFile)
1317 return CHANNEL_RC_OK;
1319 persistent = persistent_cache_new();
1322 return CHANNEL_RC_NO_MEMORY;
1324 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1326 error = CHANNEL_RC_INITIALIZATION_ERROR;
1330 if (persistent_cache_get_version(persistent) != 3)
1332 error = ERROR_INVALID_DATA;
1336 int count = persistent_cache_get_count(persistent);
1338 count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
1340 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Receiving Cache Import Reply: %d", count);
1342 for (
int idx = 0; idx < count; idx++)
1345 if (persistent_cache_read_entry(persistent, &entry) < 1)
1347 error = ERROR_INVALID_DATA;
1351 const UINT16 cacheSlot = reply->cacheSlots[idx];
1352 if (context && context->ImportCacheEntry)
1354 error = context->ImportCacheEntry(context, cacheSlot, &entry);
1355 if (error != CHANNEL_RC_OK)
1360 persistent_cache_free(persistent);
1364 persistent_cache_free(persistent);
1376 WINPR_ASSERT(callback);
1379 RdpgfxClientContext* context = gfx->context;
1380 UINT error = CHANNEL_RC_OK;
1382 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1383 return ERROR_INVALID_DATA;
1385 Stream_Read_UINT16(s, pdu.importedEntriesCount);
1387 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1389 return ERROR_INVALID_DATA;
1391 if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1392 return ERROR_INVALID_DATA;
1394 for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1396 Stream_Read_UINT16(s, pdu.cacheSlots[idx]);
1399 WLog_Print(gfx->base.log, WLOG_TRACE,
1400 "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16
"",
1401 pdu.importedEntriesCount);
1403 error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1407 WLog_Print(gfx->base.log, WLOG_ERROR,
1408 "rdpgfx_load_cache_import_reply failed with error %" PRIu32
"", error);
1414 IFCALLRET(context->CacheImportReply, error, context, &pdu);
1417 WLog_Print(gfx->base.log, WLOG_ERROR,
1418 "context->CacheImportReply failed with error %" PRIu32
"", error);
1432 WINPR_ASSERT(callback);
1435 RdpgfxClientContext* context = gfx->context;
1436 UINT error = CHANNEL_RC_OK;
1438 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1439 return ERROR_INVALID_DATA;
1441 Stream_Read_UINT16(s, pdu.surfaceId);
1442 Stream_Read_UINT16(s, pdu.width);
1443 Stream_Read_UINT16(s, pdu.height);
1444 Stream_Read_UINT8(s, pdu.pixelFormat);
1446 if ((pdu.width == 0) || (pdu.height == 0))
1448 WLog_Print(gfx->base.log, WLOG_ERROR,
1449 "context->RecvCreateSurfacePdu Width[%" PRIu32
"], Height[%" PRIu32
"] is 0",
1450 pdu.width, pdu.height);
1451 return ERROR_INVALID_DATA;
1453 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE))
1455 WLog_Print(gfx->base.log, WLOG_ERROR,
1456 "context->RecvCreateSurfacePdu Width[%" PRIu32
"], Height[%" PRIu32
1458 pdu.width, pdu.height, MAX_SURFACE_SIZE);
1459 return ERROR_INVALID_DATA;
1462 WLog_Print(gfx->base.log, WLOG_DEBUG,
1463 "RecvCreateSurfacePdu: surfaceId: %" PRIu16
" width: %" PRIu16
" height: %" PRIu16
1464 " pixelFormat: 0x%02" PRIX8
"",
1465 pdu.surfaceId, pdu.width, pdu.height, pdu.pixelFormat);
1474 const UINT drc = IFCALLRESULT(CHANNEL_RC_OK, context->DeleteSurface, context, &deletePdu);
1475 if (drc != CHANNEL_RC_OK)
1476 WLog_Print(gfx->base.log, WLOG_WARN,
1477 "context->DeleteSurface failed with error %" PRIu32
", ignoring", drc);
1479 IFCALLRET(context->CreateSurface, error, context, &pdu);
1482 WLog_Print(gfx->base.log, WLOG_ERROR,
1483 "context->CreateSurface failed with error %" PRIu32
"", error);
1497 WINPR_ASSERT(callback);
1500 RdpgfxClientContext* context = gfx->context;
1501 UINT error = CHANNEL_RC_OK;
1503 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1504 return ERROR_INVALID_DATA;
1506 Stream_Read_UINT16(s, pdu.surfaceId);
1507 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvDeleteSurfacePdu: surfaceId: %" PRIu16
"",
1512 IFCALLRET(context->DeleteSurface, error, context, &pdu);
1515 WLog_Print(gfx->base.log, WLOG_ERROR,
1516 "context->DeleteSurface failed with error %" PRIu32
"", error);
1530 WINPR_ASSERT(callback);
1533 RdpgfxClientContext* context = gfx->context;
1534 UINT error = CHANNEL_RC_OK;
1536 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1537 return ERROR_INVALID_DATA;
1539 Stream_Read_UINT32(s, pdu.timestamp);
1540 Stream_Read_UINT32(s, pdu.frameId);
1541 WLog_Print(gfx->base.log, WLOG_TRACE,
1542 "RecvStartFramePdu: frameId: %" PRIu32
" timestamp: 0x%08" PRIX32
"", pdu.frameId,
1544 gfx->StartDecodingTime = GetTickCount64();
1548 IFCALLRET(context->StartFrame, error, context, &pdu);
1551 WLog_Print(gfx->base.log, WLOG_ERROR,
1552 "context->StartFrame failed with error %" PRIu32
"", error);
1555 gfx->UnacknowledgedFrames++;
1556 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
1569 WINPR_ASSERT(callback);
1572 RdpgfxClientContext* context = gfx->context;
1573 UINT error = CHANNEL_RC_OK;
1575 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1576 return ERROR_INVALID_DATA;
1578 Stream_Read_UINT32(s, pdu.frameId);
1579 WLog_Print(gfx->base.log, WLOG_TRACE,
"RecvEndFramePdu: frameId: %" PRIu32
"", pdu.frameId);
1581 const UINT64 start = GetTickCount64();
1584 IFCALLRET(context->EndFrame, error, context, &pdu);
1588 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->EndFrame failed with error %" PRIu32
"",
1593 const UINT64 end = GetTickCount64();
1594 const UINT64 EndFrameTime = end - start;
1595 gfx->TotalDecodedFrames++;
1596 rdpgfx_stats_frame_decoded(context);
1598 if (!gfx->sendFrameAcks)
1601 ack.frameId = pdu.frameId;
1602 ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1604 if (gfx->suspendFrameAcks)
1606 ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1608 if (gfx->TotalDecodedFrames == 1)
1609 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1610 WLog_Print(gfx->base.log, WLOG_ERROR,
1611 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32
"",
1616 ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1618 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1619 WLog_Print(gfx->base.log, WLOG_ERROR,
1620 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32
"", error);
1623 switch (gfx->ConnectionCaps.version)
1625#if defined(WITH_GFX_AV1)
1626 case RDPGFX_CAPVERSION_FRDP_1:
1628#if defined(WITH_GFX_AV2)
1629 case RDPGFX_CAPVERSION_FRDP_2:
1631 case RDPGFX_CAPVERSION_10:
1632 case RDPGFX_CAPVERSION_102:
1633 case RDPGFX_CAPVERSION_103:
1634 case RDPGFX_CAPVERSION_104:
1635 case RDPGFX_CAPVERSION_105:
1636 case RDPGFX_CAPVERSION_106:
1637 case RDPGFX_CAPVERSION_106_ERR:
1638 case RDPGFX_CAPVERSION_107:
1639#if defined(WITH_GFX_AZURE)
1640 case RDPGFX_CAPVERSION_111:
1641 case RDPGFX_CAPVERSION_112:
1642 case RDPGFX_CAPVERSION_113:
1643 case RDPGFX_CAPVERSION_115:
1648 UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1653 qoe.frameId = pdu.frameId;
1654 qoe.timestamp = gfx->StartDecodingTime % UINT32_MAX;
1655 qoe.timeDiffSE = WINPR_ASSERTING_INT_CAST(UINT16, diff);
1656 qoe.timeDiffEDR = WINPR_ASSERTING_INT_CAST(UINT16, EndFrameTime);
1658 if ((error = rdpgfx_send_qoe_frame_acknowledge_pdu(context, &qoe)))
1659 WLog_Print(gfx->base.log, WLOG_ERROR,
1660 "rdpgfx_send_qoe_frame_acknowledge_pdu failed with error %" PRIu32
1683 WINPR_ASSERT(callback);
1688 if (!Stream_SafeSeek(s, Stream_GetRemainingLength(s)))
1689 return ERROR_INVALID_DATA;
1691 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvCapsResetPdu: sending the capabilities again");
1693 free_surfaces(gfx->context, gfx->SurfaceTable);
1694 const UINT error = evict_cache_slots(gfx->context, gfx->MaxCacheSlots, gfx->CacheSlots);
1698 WLog_Print(gfx->base.log, WLOG_ERROR,
"evict_cache_slots failed with error %" PRIu32
"",
1701 gfx->UnacknowledgedFrames = 0;
1702 gfx->TotalDecodedFrames = 0;
1704 return rdpgfx_send_supported_caps(callback);
1708static BOOL rdpgfx_codec_has_region_data_embedded(UINT16 codecId)
1712 case RDPGFX_CODECID_AVC420:
1713 case RDPGFX_CODECID_AVC444:
1714 case RDPGFX_CODECID_AVC444v2:
1715#if defined(WITH_GFX_AZURE)
1716 case RDPGFX_CODECID_HEVC420:
1717 case RDPGFX_CODECID_HEVC444:
1719#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1720 case RDPGFX_CODECID_AV1:
1737 WINPR_ASSERT(callback);
1742 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1743 return ERROR_INVALID_DATA;
1745 Stream_Read_UINT16(s, pdu.surfaceId);
1746 Stream_Read_UINT16(s, pdu.codecId);
1747 Stream_Read_UINT8(s, pdu.pixelFormat);
1749 if ((error = rdpgfx_read_rect16(
1750 gfx->base.log, s, &(pdu.destRect),
1751 rdpgfx_codec_has_region_data_embedded(pdu.codecId))))
1753 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"",
1758 Stream_Read_UINT32(s, pdu.bitmapDataLength);
1760 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1761 return ERROR_INVALID_DATA;
1763 pdu.bitmapData = Stream_Pointer(s);
1764 Stream_Seek(s, pdu.bitmapDataLength);
1766 WLog_Print(gfx->base.log, WLOG_TRACE,
1767 "RecvWireToSurface1Pdu: surfaceId: %" PRIu16
" codecId: %s (0x%04" PRIX16
1768 ") pixelFormat: 0x%02" PRIX8
" "
1769 "destRect: left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
1770 " bitmapDataLength: %" PRIu32
"",
1771 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId, pdu.pixelFormat,
1772 pdu.destRect.left, pdu.destRect.top, pdu.destRect.right, pdu.destRect.bottom,
1773 pdu.bitmapDataLength);
1774 cmd.surfaceId = pdu.surfaceId;
1775 cmd.codecId = pdu.codecId;
1778 switch (pdu.pixelFormat)
1780 case GFX_PIXEL_FORMAT_XRGB_8888:
1781 cmd.format = PIXEL_FORMAT_BGRX32;
1784 case GFX_PIXEL_FORMAT_ARGB_8888:
1785 cmd.format = PIXEL_FORMAT_BGRA32;
1789 return ERROR_INVALID_DATA;
1792 cmd.left = pdu.destRect.left;
1793 cmd.top = pdu.destRect.top;
1794 cmd.right = pdu.destRect.right;
1795 cmd.bottom = pdu.destRect.bottom;
1796 cmd.width = cmd.right - cmd.left;
1797 cmd.height = cmd.bottom - cmd.top;
1798 cmd.length = pdu.bitmapDataLength;
1799 cmd.data = pdu.bitmapData;
1800 cmd.extra =
nullptr;
1802 if (cmd.right < cmd.left)
1804 WLog_Print(gfx->base.log, WLOG_ERROR,
1805 "RecvWireToSurface1Pdu right=%" PRIu32
" < left=%" PRIu32, cmd.right, cmd.left);
1806 return ERROR_INVALID_DATA;
1808 if (cmd.bottom < cmd.top)
1810 WLog_Print(gfx->base.log, WLOG_ERROR,
1811 "RecvWireToSurface1Pdu bottom=%" PRIu32
" < top=%" PRIu32, cmd.bottom, cmd.top);
1812 return ERROR_INVALID_DATA;
1815 if ((error = rdpgfx_decode(gfx, &cmd)))
1816 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_decode failed with error %" PRIu32
"!",
1825 switch (cmd->codecId)
1827 case RDPGFX_CODECID_UNCOMPRESSED:
1828 case RDPGFX_CODECID_CAVIDEO:
1829 case RDPGFX_CODECID_CLEARCODEC:
1830 case RDPGFX_CODECID_PLANAR:
1831 case RDPGFX_CODECID_AVC420:
1832 case RDPGFX_CODECID_ALPHA:
1833 case RDPGFX_CODECID_AVC444:
1834 case RDPGFX_CODECID_AVC444v2:
1835 case RDPGFX_CODECID_CAPROGRESSIVE:
1836 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1837#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1838 case RDPGFX_CODECID_AV1:
1840#if defined(WITH_GFX_AZURE)
1841 case RDPGFX_CODECID_HEVC420:
1842 case RDPGFX_CODECID_HEVC444:
1844 if (!gfx->allowedCodecs[cmd->codecId])
1846 char buffer[128] = WINPR_C_ARRAY_INIT;
1848 gfx->base.log, WLOG_ERROR,
1849 "RDPGFX_SURFACE_COMMAND::codecId %s not allowed with %s [%s]",
1850 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)),
1851 rdpgfx_caps_version_str(gfx->ConnectionCaps.version),
1852 rdpgfx_caps_flags_str(gfx->ConnectionCaps.flags, buffer,
sizeof(buffer)));
1857 WLog_Print(gfx->base.log, WLOG_ERROR,
1858 "Unknown RDPGFX_SURFACE_COMMAND::codecId %" PRIu32, cmd->codecId);
1867 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Got GFX %s",
1868 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)));
1870 RdpgfxClientContext* context = gfx->context;
1872 return CHANNEL_RC_OK;
1874 if (!checkSurfaceCommand(gfx, cmd))
1875 return CHANNEL_RC_NULL_DATA;
1877 const UINT error = IFCALLRESULT(CHANNEL_RC_OK, context->SurfaceCommand, context, cmd);
1880 WLog_Print(gfx->base.log, WLOG_ERROR,
1881 "context->SurfaceCommand failed with error %" PRIu32
"", error);
1883 rdpgfx_codecid_event(context, cmd->codecId, cmd->length);
1897 WINPR_ASSERT(callback);
1901 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1902 return ERROR_INVALID_DATA;
1904 Stream_Read_UINT16(s, pdu.surfaceId);
1905 Stream_Read_UINT16(s, pdu.codecId);
1906 Stream_Read_UINT32(s, pdu.codecContextId);
1907 Stream_Read_UINT8(s, pdu.pixelFormat);
1908 Stream_Read_UINT32(s, pdu.bitmapDataLength);
1909 pdu.bitmapData = Stream_Pointer(s);
1910 if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1911 return ERROR_INVALID_DATA;
1913 WLog_Print(gfx->base.log, WLOG_TRACE,
1914 "RecvWireToSurface2Pdu: surfaceId: %" PRIu16
" codecId: %s (0x%04" PRIX16
") "
1915 "codecContextId: %" PRIu32
" pixelFormat: 0x%02" PRIX8
" bitmapDataLength: %" PRIu32
1917 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1918 pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1920 cmd.surfaceId = pdu.surfaceId;
1921 cmd.codecId = pdu.codecId;
1922 cmd.contextId = pdu.codecContextId;
1924 switch (pdu.pixelFormat)
1926 case GFX_PIXEL_FORMAT_XRGB_8888:
1927 cmd.format = PIXEL_FORMAT_BGRX32;
1930 case GFX_PIXEL_FORMAT_ARGB_8888:
1931 cmd.format = PIXEL_FORMAT_BGRA32;
1935 return ERROR_INVALID_DATA;
1938 cmd.length = pdu.bitmapDataLength;
1939 cmd.data = pdu.bitmapData;
1940 cmd.extra =
nullptr;
1942 if (cmd.codecId != RDPGFX_CODECID_CAPROGRESSIVE)
1943 return ERROR_INVALID_DATA;
1945 return logSurfaceCommand(gfx, &cmd);
1956 WINPR_ASSERT(callback);
1959 RdpgfxClientContext* context = gfx->context;
1960 UINT error = CHANNEL_RC_OK;
1962 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1963 return ERROR_INVALID_DATA;
1965 Stream_Read_UINT16(s, pdu.surfaceId);
1966 Stream_Read_UINT32(s, pdu.codecContextId);
1968 WLog_Print(gfx->base.log, WLOG_DEBUG,
1969 "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16
" codecContextId: %" PRIu32
"",
1970 pdu.surfaceId, pdu.codecContextId);
1974 IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1977 WLog_Print(gfx->base.log, WLOG_ERROR,
1978 "context->DeleteEncodingContext failed with error %" PRIu32
"", error);
1992 WINPR_ASSERT(callback);
1995 RdpgfxClientContext* context = gfx->context;
1997 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1998 return ERROR_INVALID_DATA;
2000 Stream_Read_UINT16(s, pdu.surfaceId);
2002 UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
2003 if (error != CHANNEL_RC_OK)
2005 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_color32 failed with error %" PRIu32
"!",
2010 Stream_Read_UINT16(s, pdu.fillRectCount);
2012 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
2013 return ERROR_INVALID_DATA;
2019 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2020 return CHANNEL_RC_NO_MEMORY;
2023 for (UINT16 index = 0; index < pdu.fillRectCount; index++)
2027 if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect, FALSE)))
2029 WLog_Print(gfx->base.log, WLOG_ERROR,
2030 "rdpgfx_read_rect16 failed with error %" PRIu32
"!", error);
2031 free(pdu.fillRects);
2035 WLog_Print(gfx->base.log, WLOG_TRACE,
2036 "RecvSolidFillPdu: surfaceId: %" PRIu16
" fillRectCount: %" PRIu16
"", pdu.surfaceId,
2041 IFCALLRET(context->SolidFill, error, context, &pdu);
2044 WLog_Print(gfx->base.log, WLOG_ERROR,
2045 "context->SolidFill failed with error %" PRIu32
"", error);
2048 free(pdu.fillRects);
2060 WINPR_ASSERT(callback);
2063 RdpgfxClientContext* context = gfx->context;
2066 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
2067 return ERROR_INVALID_DATA;
2069 Stream_Read_UINT16(s, pdu.surfaceIdSrc);
2070 Stream_Read_UINT16(s, pdu.surfaceIdDest);
2073 rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE)))
2075 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"!",
2080 Stream_Read_UINT16(s, pdu.destPtsCount);
2082 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2083 return ERROR_INVALID_DATA;
2089 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2090 return CHANNEL_RC_NO_MEMORY;
2093 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2097 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2099 WLog_Print(gfx->base.log, WLOG_ERROR,
2100 "rdpgfx_read_point16 failed with error %" PRIu32
"!", error);
2106 WLog_Print(gfx->base.log, WLOG_TRACE,
2107 "RecvSurfaceToSurfacePdu: surfaceIdSrc: %" PRIu16
" surfaceIdDest: %" PRIu16
" "
2108 "left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
2109 " destPtsCount: %" PRIu16
"",
2110 pdu.surfaceIdSrc, pdu.surfaceIdDest, pdu.rectSrc.left, pdu.rectSrc.top,
2111 pdu.rectSrc.right, pdu.rectSrc.bottom, pdu.destPtsCount);
2115 IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
2118 WLog_Print(gfx->base.log, WLOG_ERROR,
2119 "context->SurfaceToSurface failed with error %" PRIu32
"", error);
2134 WINPR_ASSERT(callback);
2138 RdpgfxClientContext* context = gfx->context;
2140 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2141 return ERROR_INVALID_DATA;
2143 Stream_Read_UINT16(s, pdu.surfaceId);
2144 Stream_Read_UINT64(s, pdu.cacheKey);
2145 Stream_Read_UINT16(s, pdu.cacheSlot);
2147 UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE);
2148 if (error != CHANNEL_RC_OK)
2150 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"!",
2155 WLog_Print(gfx->base.log, WLOG_TRACE,
2156 "RecvSurfaceToCachePdu: surfaceId: %" PRIu16
" cacheKey: 0x%016" PRIX64
2157 " cacheSlot: %" PRIu16
" "
2158 "left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
"",
2159 pdu.surfaceId, pdu.cacheKey, pdu.cacheSlot, pdu.rectSrc.left, pdu.rectSrc.top,
2160 pdu.rectSrc.right, pdu.rectSrc.bottom);
2164 IFCALLRET(context->SurfaceToCache, error, context, &pdu);
2167 WLog_Print(gfx->base.log, WLOG_ERROR,
2168 "context->SurfaceToCache failed with error %" PRIu32
"", error);
2182 WINPR_ASSERT(callback);
2186 RdpgfxClientContext* context = gfx->context;
2187 UINT error = CHANNEL_RC_OK;
2189 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
2190 return ERROR_INVALID_DATA;
2192 Stream_Read_UINT16(s, pdu.cacheSlot);
2193 Stream_Read_UINT16(s, pdu.surfaceId);
2194 Stream_Read_UINT16(s, pdu.destPtsCount);
2196 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2197 return ERROR_INVALID_DATA;
2203 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2204 return CHANNEL_RC_NO_MEMORY;
2207 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2211 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2213 WLog_Print(gfx->base.log, WLOG_ERROR,
2214 "rdpgfx_read_point16 failed with error %" PRIu32
"", error);
2220 WLog_Print(gfx->base.log, WLOG_TRACE,
2221 "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16
" surfaceId: %" PRIu16
2222 " destPtsCount: %" PRIu16
"",
2223 pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
2227 IFCALLRET(context->CacheToSurface, error, context, &pdu);
2230 WLog_Print(gfx->base.log, WLOG_ERROR,
2231 "context->CacheToSurface failed with error %" PRIu32
"", error);
2246 WINPR_ASSERT(callback);
2250 RdpgfxClientContext* context = gfx->context;
2251 UINT error = CHANNEL_RC_OK;
2253 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
2254 return ERROR_INVALID_DATA;
2256 Stream_Read_UINT16(s, pdu.surfaceId);
2257 Stream_Read_UINT16(s, pdu.reserved);
2258 Stream_Read_UINT32(s, pdu.outputOriginX);
2259 Stream_Read_UINT32(s, pdu.outputOriginY);
2261 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2263 WLog_Print(gfx->base.log, WLOG_ERROR,
2264 "context->MapSurfaceToOutput outputOriginX[%" PRIu32
"], outputOriginY[%" PRIu32
2266 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2267 return ERROR_INVALID_DATA;
2270 WLog_Print(gfx->base.log, WLOG_DEBUG,
2271 "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16
" outputOriginX: %" PRIu32
2272 " outputOriginY: %" PRIu32
"",
2273 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
2277 IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
2280 WLog_Print(gfx->base.log, WLOG_ERROR,
2281 "context->MapSurfaceToOutput failed with error %" PRIu32
"", error);
2291 WINPR_ASSERT(callback);
2295 RdpgfxClientContext* context = gfx->context;
2296 UINT error = CHANNEL_RC_OK;
2298 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2299 return ERROR_INVALID_DATA;
2301 Stream_Read_UINT16(s, pdu.surfaceId);
2302 Stream_Read_UINT16(s, pdu.reserved);
2303 Stream_Read_UINT32(s, pdu.outputOriginX);
2304 Stream_Read_UINT32(s, pdu.outputOriginY);
2305 Stream_Read_UINT32(s, pdu.targetWidth);
2306 Stream_Read_UINT32(s, pdu.targetHeight);
2308 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2310 WLog_Print(gfx->base.log, WLOG_ERROR,
2311 "context->MapSurfaceToScaledOutput outputOriginX[%" PRIu32
2312 "], outputOriginY[%" PRIu32
"] > %" PRIu32,
2313 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2314 return ERROR_INVALID_DATA;
2316 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2318 WLog_Print(gfx->base.log, WLOG_ERROR,
2319 "context->MapSurfaceToScaledOutput targetWidth[%" PRIu32
2320 "], targetHeight[%" PRIu32
"] > %" PRIu32,
2321 pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2322 return ERROR_INVALID_DATA;
2325 WLog_Print(gfx->base.log, WLOG_DEBUG,
2326 "RecvMapSurfaceToScaledOutputPdu: surfaceId: %" PRIu16
" outputOriginX: %" PRIu32
2327 " outputOriginY: %" PRIu32
" targetWidth: %" PRIu32
" targetHeight: %" PRIu32,
2328 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY, pdu.targetWidth,
2333 IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
2336 WLog_Print(gfx->base.log, WLOG_ERROR,
2337 "context->MapSurfaceToScaledOutput failed with error %" PRIu32
"", error);
2351 WINPR_ASSERT(callback);
2355 RdpgfxClientContext* context = gfx->context;
2356 UINT error = CHANNEL_RC_OK;
2358 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
2359 return ERROR_INVALID_DATA;
2361 Stream_Read_UINT16(s, pdu.surfaceId);
2362 Stream_Read_UINT64(s, pdu.windowId);
2363 Stream_Read_UINT32(s, pdu.mappedWidth);
2364 Stream_Read_UINT32(s, pdu.mappedHeight);
2366 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2368 WLog_Print(gfx->base.log, WLOG_ERROR,
2369 "context->MapSurfaceToWindow mappedWidth[%" PRIu32
"], mappedHeight[%" PRIu32
2371 pdu.mappedWidth, pdu.mappedHeight);
2372 return ERROR_INVALID_DATA;
2374 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2376 WLog_Print(gfx->base.log, WLOG_ERROR,
2377 "context->MapSurfaceToWindow mappedWidth[%" PRIu32
"], mappedHeight[%" PRIu32
2379 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2380 return ERROR_INVALID_DATA;
2383 WLog_Print(gfx->base.log, WLOG_DEBUG,
2384 "RecvMapSurfaceToWindowPdu: surfaceId: %" PRIu16
" windowId: 0x%016" PRIX64
2385 " mappedWidth: %" PRIu32
" mappedHeight: %" PRIu32
"",
2386 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight);
2388 if (context && context->MapSurfaceToWindow)
2390 IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
2393 WLog_Print(gfx->base.log, WLOG_ERROR,
2394 "context->MapSurfaceToWindow failed with error %" PRIu32
"", error);
2404 WINPR_ASSERT(callback);
2407 RdpgfxClientContext* context = gfx->context;
2408 UINT error = CHANNEL_RC_OK;
2410 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
2411 return ERROR_INVALID_DATA;
2413 Stream_Read_UINT16(s, pdu.surfaceId);
2414 Stream_Read_UINT64(s, pdu.windowId);
2415 Stream_Read_UINT32(s, pdu.mappedWidth);
2416 Stream_Read_UINT32(s, pdu.mappedHeight);
2417 Stream_Read_UINT32(s, pdu.targetWidth);
2418 Stream_Read_UINT32(s, pdu.targetHeight);
2420 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2422 WLog_Print(gfx->base.log, WLOG_ERROR,
2423 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2424 "], mappedHeight[%" PRIu32
"] is 0",
2425 pdu.mappedWidth, pdu.mappedHeight);
2426 return ERROR_INVALID_DATA;
2428 if ((pdu.targetWidth == 0) || (pdu.targetHeight == 0))
2430 WLog_Print(gfx->base.log, WLOG_ERROR,
2431 "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2432 "], targetHeight[%" PRIu32
"] is 0",
2433 pdu.targetWidth, pdu.targetHeight);
2434 return ERROR_INVALID_DATA;
2437 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2439 WLog_Print(gfx->base.log, WLOG_ERROR,
2440 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2441 "], mappedHeight[%" PRIu32
"] > %" PRIu32,
2442 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2443 return ERROR_INVALID_DATA;
2445 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2447 WLog_Print(gfx->base.log, WLOG_ERROR,
2448 "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2449 "], targetHeight[%" PRIu32
"] > %" PRIu32,
2450 pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2451 return ERROR_INVALID_DATA;
2454 WLog_Print(gfx->base.log, WLOG_DEBUG,
2455 "RecvMapSurfaceToScaledWindowPdu: surfaceId: %" PRIu16
" windowId: 0x%016" PRIX64
2456 " mappedWidth: %" PRIu32
" mappedHeight: %" PRIu32
" targetWidth: %" PRIu32
2457 " targetHeight: %" PRIu32
"",
2458 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight, pdu.targetWidth,
2461 if (context && context->MapSurfaceToScaledWindow)
2463 IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
2466 WLog_Print(gfx->base.log, WLOG_ERROR,
2467 "context->MapSurfaceToScaledWindow failed with error %" PRIu32
"", error);
2481 wStream sbuffer = WINPR_C_ARRAY_INIT;
2483 WINPR_ASSERT(callback);
2488 size_t packetlen = 0;
2489 UINT error = rdpgfx_read_header(gfx->base.log, data, &header, &packetlen);
2490 if (error != CHANNEL_RC_OK)
2492 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_header failed with error %" PRIu32
"!",
2498 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
2499 Stream_Seek(data, packetlen);
2501 WLog_Print(gfx->base.log, WLOG_TRACE,
2502 "cmdId: %s (0x%04" PRIX16
") flags: 0x%04" PRIX16
" pduLength: %" PRIu32
"",
2503 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
2506 rdpgfx_stats_cmdid_event(gfx->context, header.cmdId);
2507 switch (header.cmdId)
2509 case RDPGFX_CMDID_WIRETOSURFACE_1:
2510 if ((error = rdpgfx_recv_wire_to_surface_1_pdu(callback, s)))
2511 WLog_Print(gfx->base.log, WLOG_ERROR,
2512 "rdpgfx_recv_wire_to_surface_1_pdu failed with error %" PRIu32
"!",
2517 case RDPGFX_CMDID_WIRETOSURFACE_2:
2518 if ((error = rdpgfx_recv_wire_to_surface_2_pdu(callback, s)))
2519 WLog_Print(gfx->base.log, WLOG_ERROR,
2520 "rdpgfx_recv_wire_to_surface_2_pdu failed with error %" PRIu32
"!",
2525 case RDPGFX_CMDID_DELETEENCODINGCONTEXT:
2526 if ((error = rdpgfx_recv_delete_encoding_context_pdu(callback, s)))
2527 WLog_Print(gfx->base.log, WLOG_ERROR,
2528 "rdpgfx_recv_delete_encoding_context_pdu failed with error %" PRIu32
"!",
2533 case RDPGFX_CMDID_SOLIDFILL:
2534 if ((error = rdpgfx_recv_solid_fill_pdu(callback, s)))
2535 WLog_Print(gfx->base.log, WLOG_ERROR,
2536 "rdpgfx_recv_solid_fill_pdu failed with error %" PRIu32
"!", error);
2540 case RDPGFX_CMDID_SURFACETOSURFACE:
2541 if ((error = rdpgfx_recv_surface_to_surface_pdu(callback, s)))
2542 WLog_Print(gfx->base.log, WLOG_ERROR,
2543 "rdpgfx_recv_surface_to_surface_pdu failed with error %" PRIu32
"!",
2548 case RDPGFX_CMDID_SURFACETOCACHE:
2549 if ((error = rdpgfx_recv_surface_to_cache_pdu(callback, s)))
2550 WLog_Print(gfx->base.log, WLOG_ERROR,
2551 "rdpgfx_recv_surface_to_cache_pdu failed with error %" PRIu32
"!",
2556 case RDPGFX_CMDID_CACHETOSURFACE:
2557 if ((error = rdpgfx_recv_cache_to_surface_pdu(callback, s)))
2558 WLog_Print(gfx->base.log, WLOG_ERROR,
2559 "rdpgfx_recv_cache_to_surface_pdu failed with error %" PRIu32
"!",
2564 case RDPGFX_CMDID_EVICTCACHEENTRY:
2565 if ((error = rdpgfx_recv_evict_cache_entry_pdu(callback, s)))
2566 WLog_Print(gfx->base.log, WLOG_ERROR,
2567 "rdpgfx_recv_evict_cache_entry_pdu failed with error %" PRIu32
"!",
2572 case RDPGFX_CMDID_CREATESURFACE:
2573 if ((error = rdpgfx_recv_create_surface_pdu(callback, s)))
2574 WLog_Print(gfx->base.log, WLOG_ERROR,
2575 "rdpgfx_recv_create_surface_pdu failed with error %" PRIu32
"!", error);
2579 case RDPGFX_CMDID_DELETESURFACE:
2580 if ((error = rdpgfx_recv_delete_surface_pdu(callback, s)))
2581 WLog_Print(gfx->base.log, WLOG_ERROR,
2582 "rdpgfx_recv_delete_surface_pdu failed with error %" PRIu32
"!", error);
2586 case RDPGFX_CMDID_STARTFRAME:
2587 if ((error = rdpgfx_recv_start_frame_pdu(callback, s)))
2588 WLog_Print(gfx->base.log, WLOG_ERROR,
2589 "rdpgfx_recv_start_frame_pdu failed with error %" PRIu32
"!", error);
2593 case RDPGFX_CMDID_ENDFRAME:
2594 if ((error = rdpgfx_recv_end_frame_pdu(callback, s)))
2595 WLog_Print(gfx->base.log, WLOG_ERROR,
2596 "rdpgfx_recv_end_frame_pdu failed with error %" PRIu32
"!", error);
2600 case RDPGFX_CMDID_RESETGRAPHICS:
2601 if ((error = rdpgfx_recv_reset_graphics_pdu(callback, s)))
2602 WLog_Print(gfx->base.log, WLOG_ERROR,
2603 "rdpgfx_recv_reset_graphics_pdu failed with error %" PRIu32
"!", error);
2607 case RDPGFX_CMDID_MAPSURFACETOOUTPUT:
2608 if ((error = rdpgfx_recv_map_surface_to_output_pdu(callback, s)))
2609 WLog_Print(gfx->base.log, WLOG_ERROR,
2610 "rdpgfx_recv_map_surface_to_output_pdu failed with error %" PRIu32
"!",
2615 case RDPGFX_CMDID_CACHEIMPORTREPLY:
2616 if ((error = rdpgfx_recv_cache_import_reply_pdu(callback, s)))
2617 WLog_Print(gfx->base.log, WLOG_ERROR,
2618 "rdpgfx_recv_cache_import_reply_pdu failed with error %" PRIu32
"!",
2623 case RDPGFX_CMDID_CAPSCONFIRM:
2624 if ((error = rdpgfx_recv_caps_confirm_pdu(callback, s)))
2625 WLog_Print(gfx->base.log, WLOG_ERROR,
2626 "rdpgfx_recv_caps_confirm_pdu failed with error %" PRIu32
"!", error);
2628 if ((error = rdpgfx_send_cache_offer(gfx)))
2629 WLog_Print(gfx->base.log, WLOG_ERROR,
2630 "rdpgfx_send_cache_offer failed with error %" PRIu32
"!", error);
2634 case RDPGFX_CMDID_MAPSURFACETOWINDOW:
2635 if ((error = rdpgfx_recv_map_surface_to_window_pdu(callback, s)))
2636 WLog_Print(gfx->base.log, WLOG_ERROR,
2637 "rdpgfx_recv_map_surface_to_window_pdu failed with error %" PRIu32
"!",
2642 case RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW:
2643 if ((error = rdpgfx_recv_map_surface_to_scaled_window_pdu(callback, s)))
2644 WLog_Print(gfx->base.log, WLOG_ERROR,
2645 "rdpgfx_recv_map_surface_to_scaled_window_pdu failed with error %" PRIu32
2651 case RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT:
2652 if ((error = rdpgfx_recv_map_surface_to_scaled_output_pdu(callback, s)))
2653 WLog_Print(gfx->base.log, WLOG_ERROR,
2654 "rdpgfx_recv_map_surface_to_scaled_output_pdu failed with error %" PRIu32
2660 case RDPGFX_CMDID_PROTECT_SURFACE:
2661 case RDPGFX_CMDID_WATERMARK:
2662 WLog_Print(gfx->base.log, WLOG_WARN,
2663 "Command %s not implemented, skipping %" PRIu32
" bytes",
2664 rdpgfx_get_cmd_id_string(header.cmdId), header.pduLength);
2665 if (!Stream_SafeSeek(s, packetlen))
2666 error = ERROR_INVALID_DATA;
2669 case RDPGFX_CMDID_UNUSED_0014:
2671 WLog_Print(gfx->base.log, WLOG_DEBUG,
2672 "Diagnostic information from the server, skipping %" PRIu32
" bytes",
2674 if (!Stream_SafeSeek(s, packetlen))
2675 error = ERROR_INVALID_DATA;
2678 case RDPGFX_CMDID_CAPSRESET:
2679 if ((error = rdpgfx_recv_caps_reset_pdu(callback, s)))
2680 WLog_Print(gfx->base.log, WLOG_ERROR,
2681 "rdpgfx_recv_caps_reset_pdu failed with error %" PRIu32
"!", error);
2685 error = CHANNEL_RC_BAD_PROC;
2691 WLog_Print(gfx->base.log, WLOG_ERROR,
2692 "Error while processing GFX cmdId: %s (0x%04" PRIX16
")",
2693 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
2697 const size_t end = Stream_GetPosition(s);
2699 if (end != packetlen)
2701 WLog_Print(gfx->base.log, WLOG_ERROR,
2702 "Unexpected gfx pdu end: Actual: %" PRIuz
", Expected: %" PRIuz, end, packetlen);
2713static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
2716 BYTE* pDstData =
nullptr;
2718 WINPR_ASSERT(callback);
2720 UINT error = CHANNEL_RC_OK;
2723 int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2724 (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2726 if ((status < 0) || (DstSize == 0))
2728 WLog_Print(gfx->base.log, WLOG_ERROR,
2729 "zgfx_decompress failure! status: %d, DstSize=%" PRIu32, status, DstSize);
2731 return ERROR_INTERNAL_ERROR;
2734 wStream sbuffer = WINPR_C_ARRAY_INIT;
2735 wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2739 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2741 return CHANNEL_RC_NO_MEMORY;
2744 while (Stream_GetPosition(s) < Stream_Length(s))
2746 if ((error = rdpgfx_recv_pdu(callback, s)))
2748 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_recv_pdu failed with error %" PRIu32
"!",
2763static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2766 WINPR_ASSERT(callback);
2769 RdpgfxClientContext* context = gfx->context;
2770 UINT error = CHANNEL_RC_OK;
2771 BOOL do_caps_advertise = TRUE;
2773 gfx->sendFrameAcks = TRUE;
2777 IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2780 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->OnOpen failed with error %" PRIu32
"",
2783 RdpgfxClientContextInt* ctx = (RdpgfxClientContextInt*)context;
2784 const RdpgfxClientContextStats empty = WINPR_C_ARRAY_INIT;
2788 if (do_caps_advertise)
2789 error = rdpgfx_send_supported_caps(callback);
2794static void rdpgfx_dump_stats(RdpgfxClientContext* context)
2796 WINPR_ASSERT(context);
2801 wLog* log = gfx->base.log;
2802 const DWORD level = WLOG_TRACE;
2803 if (!WLog_IsLevelActive(log, level))
2806 WLog_Print(log, level,
"RdpgfxClientContextStats");
2807 for (
size_t x = 0; x < rdpgfx_stats_max_index(); x++)
2809 const char* name = rdpgfx_stats_name_for_index(x);
2810 const uint64_t val = rdpgfx_stats_value_for_index(context, x);
2811 WLog_Print(log, level,
"%s: %" PRIu64, name, val);
2820static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2822 UINT error = CHANNEL_RC_OK;
2824 WINPR_ASSERT(callback);
2830 RdpgfxClientContext* context = gfx->context;
2832 WLog_Print(gfx->base.log, WLOG_DEBUG,
"OnClose");
2833 error = rdpgfx_save_persistent_cache(gfx);
2838 WLog_Print(gfx->base.log, WLOG_ERROR,
2839 "rdpgfx_save_persistent_cache failed with error %" PRIu32
"", error);
2842 free_surfaces(context, gfx->SurfaceTable);
2843 error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2847 WLog_Print(gfx->base.log, WLOG_ERROR,
"evict_cache_slots failed with error %" PRIu32
"",
2852 gfx->UnacknowledgedFrames = 0;
2853 gfx->TotalDecodedFrames = 0;
2857 error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2861 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->OnClose failed with error %" PRIu32
"",
2864 rdpgfx_dump_stats(context);
2868 return CHANNEL_RC_OK;
2875 RdpgfxClientContext* context = gfx->context;
2877 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Terminated");
2878 rdpgfx_client_context_free(context);
2886static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId,
void* pData)
2888 WINPR_ASSERT(context);
2891 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2895 if (!HashTable_Insert(gfx->SurfaceTable, (
void*)key, pData))
2896 return ERROR_BAD_ARGUMENTS;
2899 HashTable_Remove(gfx->SurfaceTable, (
void*)key);
2901 return CHANNEL_RC_OK;
2909static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2912 ULONG_PTR* pKeys =
nullptr;
2913 WINPR_ASSERT(context);
2916 size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2918 WINPR_ASSERT(ppSurfaceIds);
2919 WINPR_ASSERT(count_out);
2923 return CHANNEL_RC_OK;
2926 UINT16* pSurfaceIds = (UINT16*)calloc(count,
sizeof(UINT16));
2930 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2932 return CHANNEL_RC_NO_MEMORY;
2935 for (
size_t index = 0; index < count; index++)
2937 pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2941 *ppSurfaceIds = pSurfaceIds;
2942 *count_out = (UINT16)count;
2943 return CHANNEL_RC_OK;
2946static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2948 WINPR_ASSERT(context);
2951 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2952 return HashTable_GetItemValue(gfx->SurfaceTable, (
void*)key);
2960static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot,
void* pData)
2962 WINPR_ASSERT(context);
2967 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2969 WLog_Print(gfx->base.log, WLOG_ERROR,
2970 "invalid cache slot %" PRIu16
", must be between 1 and %" PRIu16
"", cacheSlot,
2971 gfx->MaxCacheSlots);
2972 return ERROR_INVALID_INDEX;
2975 gfx->CacheSlots[cacheSlot - 1] = pData;
2976 return CHANNEL_RC_OK;
2979static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2981 WINPR_ASSERT(context);
2985 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2987 WLog_Print(gfx->base.log, WLOG_ERROR,
2988 "invalid cache slot %" PRIu16
", must be between 1 and %" PRIu16
"", cacheSlot,
2989 gfx->MaxCacheSlots);
2993 return gfx->CacheSlots[cacheSlot - 1];
2997 WINPR_ATTR_UNUSED rdpSettings* settings)
3002 gfx->rdpcontext = rcontext;
3004 gfx->SurfaceTable = HashTable_New(TRUE);
3005 if (!gfx->SurfaceTable)
3007 WLog_Print(gfx->base.log, WLOG_ERROR,
"HashTable_New for surfaces failed !");
3008 return CHANNEL_RC_NO_MEMORY;
3011 gfx->suspendFrameAcks =
3013 gfx->MaxCacheSlots =
3016 RdpgfxClientContextInt* context =
3017 (RdpgfxClientContextInt*)calloc(1,
sizeof(RdpgfxClientContextInt));
3020 WLog_Print(gfx->base.log, WLOG_ERROR,
"context calloc failed!");
3021 HashTable_Free(gfx->SurfaceTable);
3022 gfx->SurfaceTable =
nullptr;
3023 return CHANNEL_RC_NO_MEMORY;
3026 gfx->zgfx = zgfx_context_new(FALSE);
3029 WLog_Print(gfx->base.log, WLOG_ERROR,
"zgfx_context_new failed!");
3030 HashTable_Free(gfx->SurfaceTable);
3031 gfx->SurfaceTable =
nullptr;
3033 return CHANNEL_RC_NO_MEMORY;
3036 context->common.handle = (
void*)gfx;
3037 context->common.GetSurfaceIds = rdpgfx_get_surface_ids;
3038 context->common.SetSurfaceData = rdpgfx_set_surface_data;
3039 context->common.GetSurfaceData = rdpgfx_get_surface_data;
3040 context->common.SetCacheSlotData = rdpgfx_set_cache_slot_data;
3041 context->common.GetCacheSlotData = rdpgfx_get_cache_slot_data;
3042 context->common.CapsAdvertise = rdpgfx_send_caps_advertise_pdu;
3043 context->common.FrameAcknowledge = rdpgfx_send_frame_acknowledge_pdu;
3044 context->common.CacheImportOffer = rdpgfx_send_cache_import_offer_pdu;
3045 context->common.QoeFrameAcknowledge = rdpgfx_send_qoe_frame_acknowledge_pdu;
3047 gfx->base.iface.pInterface = (
void*)context;
3048 gfx->context = &context->common;
3049 return CHANNEL_RC_OK;
3052void rdpgfx_client_context_free(RdpgfxClientContext* context)
3059 free_surfaces(context, gfx->SurfaceTable);
3060 evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
3064 zgfx_context_free(gfx->zgfx);
3065 gfx->zgfx =
nullptr;
3068 HashTable_Free(gfx->SurfaceTable);
3072static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
3073 rdpgfx_on_open, rdpgfx_on_close,
3081FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
3083 return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
3085 &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.