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>
42#include "rdpgfx_common.h"
43#include "rdpgfx_codec.h"
45#include "rdpgfx_main.h"
47#define GFXTAG CHANNELS_TAG("rdpgfx.client")
49#define RDPGFX_NUMBER_CAPSETS 0x100
51static BOOL delete_surface(
const void* key,
void* value,
void* arg)
53 const UINT16
id = (UINT16)(uintptr_t)(key);
57 RdpgfxClientContext* context = arg;
64 UINT error = CHANNEL_RC_OK;
65 IFCALLRET(context->DeleteSurface, error, context, &pdu);
71 WLog_Print(gfx->base.log, WLOG_ERROR,
72 "context->DeleteSurface failed with error %" PRIu32
"", error);
78static void free_surfaces(RdpgfxClientContext* context, wHashTable* SurfaceTable)
80 WINPR_ASSERT(context);
81 if (!HashTable_Foreach(SurfaceTable, delete_surface, context))
85 WLog_Print(gfx->base.log, WLOG_WARN,
"delete_surface failed");
89static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots,
void** CacheSlots)
91 UINT error = CHANNEL_RC_OK;
93 WINPR_ASSERT(CacheSlots);
94 for (UINT16 index = 0; index < MaxCacheSlots; index++)
96 if (CacheSlots[index])
100 if (context && context->EvictCacheEntry)
102 const UINT rc = context->EvictCacheEntry(context, &pdu);
103 if (rc != CHANNEL_RC_OK)
107 CacheSlots[index] =
nullptr;
118static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
121 UINT error = CHANNEL_RC_OK;
124 WINPR_ASSERT(context);
128 if (!gfx || !gfx->base.listener_callback)
129 return ERROR_BAD_ARGUMENTS;
134 .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
135 .pduLength = RDPGFX_HEADER_SIZE + 2 };
137 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
140 header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
143 WLog_Print(gfx->base.log, WLOG_DEBUG,
"SendCapsAdvertisePdu %" PRIu16
"", pdu->capsSetCount);
144 wStream* s = Stream_New(
nullptr, header.pduLength);
148 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
149 return CHANNEL_RC_NO_MEMORY;
152 if ((error = rdpgfx_write_header(s, &header)))
156 Stream_Write_UINT16(s, pdu->capsSetCount);
158 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
162 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Sending %s [0x%08" PRIx32
"] flags=0x%08" PRIx32,
163 rdpgfx_caps_version_str(capsSet->version), capsSet->version, capsSet->flags);
165 Stream_Write_UINT32(s, capsSet->version);
166 Stream_Write_UINT32(s, capsSet->length);
167 Stream_Write_UINT32(s, capsSet->flags);
168 if (capsSet->length < 4)
170 Stream_Zero(s, capsSet->length - 4);
173 Stream_SealLength(s);
174 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
177 Stream_Free(s, TRUE);
181static BOOL rdpgfx_is_capability_filtered(
RDPGFX_PLUGIN* gfx, UINT32 caps)
184 const UINT32 filter =
186 const UINT32 capList[] = {
187#if defined(WITH_GFX_AV1)
188 RDPGFX_CAPVERSION_FRDP_1,
190 RDPGFX_CAPVERSION_8, RDPGFX_CAPVERSION_81, RDPGFX_CAPVERSION_10,
191 RDPGFX_CAPVERSION_101, RDPGFX_CAPVERSION_102, RDPGFX_CAPVERSION_103,
192 RDPGFX_CAPVERSION_104, RDPGFX_CAPVERSION_105, RDPGFX_CAPVERSION_106,
193 RDPGFX_CAPVERSION_106_ERR, RDPGFX_CAPVERSION_107
196 for (
size_t x = 0; x < ARRAYSIZE(capList); x++)
198 if (caps == capList[x])
199 return (filter & (1u << x)) != 0;
209 WINPR_ASSERT(pdu->capsSets);
210 WINPR_ASSERT(count > 0);
211 WINPR_ASSERT(pdu->capsSetCount < count);
212 if (pdu->capsSetCount >= count)
214 return &pdu->capsSets[pdu->capsSetCount++];
224 RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
227 return ERROR_BAD_ARGUMENTS;
232 return ERROR_BAD_CONFIGURATION;
234 RdpgfxClientContext* context = gfx->context;
237 return ERROR_BAD_CONFIGURATION;
241 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
243 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
245 return ERROR_BAD_CONFIGURATION;
246 capsSet->version = RDPGFX_CAPVERSION_8;
251 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
258 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
261 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
263 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
265 return ERROR_BAD_CONFIGURATION;
266 capsSet->version = RDPGFX_CAPVERSION_81;
271 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
274 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
279 capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
284 UINT32 caps10Flags = 0;
289 caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
294 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
297 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
300 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
302 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
304 return ERROR_BAD_CONFIGURATION;
305 capsSet->version = RDPGFX_CAPVERSION_10;
307 capsSet->flags = caps10Flags;
310 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
312 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
314 return ERROR_BAD_CONFIGURATION;
315 capsSet->version = RDPGFX_CAPVERSION_101;
316 capsSet->length = 0x10;
320 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
322 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
324 return ERROR_BAD_CONFIGURATION;
325 capsSet->version = RDPGFX_CAPVERSION_102;
326 capsSet->length = 0x4;
327 capsSet->flags = caps10Flags;
332 if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
333 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
336 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
338 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
340 return ERROR_BAD_CONFIGURATION;
341 capsSet->version = RDPGFX_CAPVERSION_103;
342 capsSet->length = 0x4;
343 capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
346 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
348 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
350 return ERROR_BAD_CONFIGURATION;
351 capsSet->version = RDPGFX_CAPVERSION_104;
352 capsSet->length = 0x4;
353 capsSet->flags = caps10Flags;
359#if defined(WITH_CAIRO) || defined(WITH_SWSCALE)
360 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
362 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
364 return ERROR_BAD_CONFIGURATION;
365 capsSet->version = RDPGFX_CAPVERSION_105;
366 capsSet->length = 0x4;
367 capsSet->flags = caps10Flags;
370 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
372 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
374 return ERROR_BAD_CONFIGURATION;
375 capsSet->version = RDPGFX_CAPVERSION_106;
376 capsSet->length = 0x4;
377 capsSet->flags = caps10Flags;
380 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
382 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
384 return ERROR_BAD_CONFIGURATION;
385 capsSet->version = RDPGFX_CAPVERSION_106_ERR;
386 capsSet->length = 0x4;
387 capsSet->flags = caps10Flags;
391 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
393 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
395 return ERROR_BAD_CONFIGURATION;
396 capsSet->version = RDPGFX_CAPVERSION_107;
397 capsSet->length = 0x4;
398 capsSet->flags = caps10Flags;
399#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
400 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
405#if defined(WITH_GFX_AV1)
408 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
410 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
412 return ERROR_BAD_CONFIGURATION;
413 capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
414 capsSet->length = 0x4;
416 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
417 const UINT32 profile =
420 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
422#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
423 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
429 return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
439 WINPR_ASSERT(callback);
443 RdpgfxClientContext* context = gfx->context;
448 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
449 return ERROR_INVALID_DATA;
451 Stream_Read_UINT32(s, capsSet.version);
452 Stream_Read_UINT32(s, capsSet.length);
453 Stream_Read_UINT32(s, capsSet.flags);
454 gfx->TotalDecodedFrames = 0;
455 gfx->ConnectionCaps = capsSet;
456 WLog_Print(gfx->base.log, WLOG_DEBUG,
457 "RecvCapsConfirmPdu: version: %s [0x%08" PRIX32
"] flags: 0x%08" PRIX32
"",
458 rdpgfx_caps_version_str(capsSet.version), capsSet.version, capsSet.flags);
461 return ERROR_BAD_CONFIGURATION;
463 return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
471static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
476 if (!context || !pdu)
477 return ERROR_BAD_ARGUMENTS;
481 if (!gfx || !gfx->base.listener_callback)
482 return ERROR_BAD_CONFIGURATION;
487 return ERROR_BAD_CONFIGURATION;
490 .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
491 .pduLength = RDPGFX_HEADER_SIZE + 12 };
493 WLog_Print(gfx->base.log, WLOG_TRACE,
"SendFrameAcknowledgePdu: %" PRIu32
"", pdu->frameId);
495 wStream* s = Stream_New(
nullptr, header.pduLength);
499 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
500 return CHANNEL_RC_NO_MEMORY;
503 if ((error = rdpgfx_write_header(s, &header)))
507 Stream_Write_UINT32(s, pdu->queueDepth);
508 Stream_Write_UINT32(s, pdu->frameId);
509 Stream_Write_UINT32(s, pdu->totalFramesDecoded);
510 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
513 if (error == CHANNEL_RC_OK)
514 gfx->UnacknowledgedFrames--;
517 Stream_Free(s, TRUE);
521static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
524 UINT error = CHANNEL_RC_OK;
526 .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
527 .pduLength = RDPGFX_HEADER_SIZE + 12 };
529 if (!context || !pdu)
530 return ERROR_BAD_ARGUMENTS;
534 if (!gfx || !gfx->base.listener_callback)
535 return ERROR_BAD_CONFIGURATION;
540 return ERROR_BAD_CONFIGURATION;
542 WLog_Print(gfx->base.log, WLOG_TRACE,
"SendQoeFrameAcknowledgePdu: %" PRIu32
"", pdu->frameId);
543 wStream* s = Stream_New(
nullptr, header.pduLength);
547 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
548 return CHANNEL_RC_NO_MEMORY;
551 if ((error = rdpgfx_write_header(s, &header)))
555 Stream_Write_UINT32(s, pdu->frameId);
556 Stream_Write_UINT32(s, pdu->timestamp);
557 Stream_Write_UINT16(s, pdu->timeDiffSE);
558 Stream_Write_UINT16(s, pdu->timeDiffEDR);
559 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
562 Stream_Free(s, TRUE);
574 WINPR_ASSERT(callback);
580 RdpgfxClientContext* context = gfx->context;
581 UINT error = CHANNEL_RC_OK;
582 GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
584 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
585 return ERROR_INVALID_DATA;
587 Stream_Read_UINT32(s, pdu.width);
588 Stream_Read_UINT32(s, pdu.height);
589 Stream_Read_UINT32(s, pdu.monitorCount);
591 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
592 return ERROR_INVALID_DATA;
596 if (!pdu.monitorDefArray)
598 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
599 return CHANNEL_RC_NO_MEMORY;
602 for (UINT32 index = 0; index < pdu.monitorCount; index++)
604 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
605 Stream_Read_INT32(s, monitor->left);
606 Stream_Read_INT32(s, monitor->top);
607 Stream_Read_INT32(s, monitor->right);
608 Stream_Read_INT32(s, monitor->bottom);
609 Stream_Read_UINT32(s, monitor->flags);
612 const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
615 free(pdu.monitorDefArray);
616 return CHANNEL_RC_NULL_DATA;
618 const size_t pad = 340ULL - size;
620 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (
size_t)pad))
622 free(pdu.monitorDefArray);
623 return CHANNEL_RC_NO_MEMORY;
627 WLog_Print(gfx->base.log, WLOG_DEBUG,
628 "RecvResetGraphicsPdu: width: %" PRIu32
" height: %" PRIu32
" count: %" PRIu32
"",
629 pdu.width, pdu.height, pdu.monitorCount);
631 for (UINT32 index = 0; index < pdu.monitorCount; index++)
633 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
634 WLog_Print(gfx->base.log, WLOG_TRACE,
635 "RecvResetGraphicsPdu: monitor left:%" PRIi32
" top:%" PRIi32
" right:%" PRIi32
636 " bottom:%" PRIi32
" flags:0x%" PRIx32
"",
637 monitor->left, monitor->top, monitor->right, monitor->bottom, monitor->flags);
642 IFCALLRET(context->ResetGraphics, error, context, &pdu);
645 WLog_Print(gfx->base.log, WLOG_ERROR,
646 "context->ResetGraphics failed with error %" PRIu32
"", error);
649 free(pdu.monitorDefArray);
652 EventArgsInit(&graphicsReset,
"libfreerdp");
653 graphicsReset.width = pdu.width;
654 graphicsReset.height = pdu.height;
655 if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
656 return ERROR_INTERNAL_ERROR;
668 WINPR_ASSERT(callback);
671 RdpgfxClientContext* context = gfx->context;
672 UINT error = CHANNEL_RC_OK;
674 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
675 return ERROR_INVALID_DATA;
677 Stream_Read_UINT16(s, pdu.cacheSlot);
678 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16
"",
683 IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
686 WLog_Print(gfx->base.log, WLOG_ERROR,
687 "context->EvictCacheEntry failed with error %" PRIu32
"", error);
700 UINT error = CHANNEL_RC_OK;
703 WINPR_ASSERT(gfx->rdpcontext);
704 rdpSettings* settings = gfx->rdpcontext->settings;
705 RdpgfxClientContext* context = gfx->context;
707 WINPR_ASSERT(context);
708 WINPR_ASSERT(settings);
711 return CHANNEL_RC_OK;
713 const char* BitmapCachePersistFile =
715 if (!BitmapCachePersistFile)
716 return CHANNEL_RC_OK;
718 if (!context->ExportCacheEntry)
719 return CHANNEL_RC_INITIALIZATION_ERROR;
721 rdpPersistentCache* persistent = persistent_cache_new();
724 return CHANNEL_RC_NO_MEMORY;
726 if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
728 error = CHANNEL_RC_INITIALIZATION_ERROR;
732 for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
734 if (gfx->CacheSlots[idx])
736 const UINT16 cacheSlot = idx;
739 if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
742 if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
747 persistent_cache_free(persistent);
751 persistent_cache_free(persistent);
760static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
763 UINT error = CHANNEL_RC_OK;
765 if (!context || !pdu)
766 return ERROR_BAD_ARGUMENTS;
770 if (!gfx || !gfx->base.listener_callback)
771 return ERROR_BAD_CONFIGURATION;
776 return ERROR_BAD_CONFIGURATION;
779 .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
781 RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
783 WLog_Print(gfx->base.log, WLOG_DEBUG,
"SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16
"",
784 pdu->cacheEntriesCount);
785 wStream* s = Stream_New(
nullptr, header.pduLength);
789 WLog_Print(gfx->base.log, WLOG_ERROR,
"Stream_New failed!");
790 return CHANNEL_RC_NO_MEMORY;
793 if ((error = rdpgfx_write_header(s, &header)))
796 if (pdu->cacheEntriesCount <= 0)
798 WLog_Print(gfx->base.log, WLOG_ERROR,
"Invalid cacheEntriesCount: %" PRIu16
"",
799 pdu->cacheEntriesCount);
800 error = ERROR_INVALID_DATA;
805 Stream_Write_UINT16(s, pdu->cacheEntriesCount);
807 for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
810 Stream_Write_UINT64(s, cacheEntry->cacheKey);
811 Stream_Write_UINT32(s, cacheEntry->bitmapLength);
814 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
818 Stream_Free(s, TRUE);
830 UINT error = CHANNEL_RC_OK;
835 WINPR_ASSERT(gfx->rdpcontext);
837 RdpgfxClientContext* context = gfx->context;
838 rdpSettings* settings = gfx->rdpcontext->settings;
841 return CHANNEL_RC_OK;
843 const char* BitmapCachePersistFile =
845 if (!BitmapCachePersistFile)
846 return CHANNEL_RC_OK;
848 rdpPersistentCache* persistent = persistent_cache_new();
851 return CHANNEL_RC_NO_MEMORY;
853 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
855 error = CHANNEL_RC_INITIALIZATION_ERROR;
859 if (persistent_cache_get_version(persistent) != 3)
861 error = ERROR_INVALID_DATA;
865 count = persistent_cache_get_count(persistent);
868 error = ERROR_INVALID_DATA;
872 if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
873 count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
875 if (count > gfx->MaxCacheSlots)
876 count = gfx->MaxCacheSlots;
881 error = CHANNEL_RC_NO_MEMORY;
885 WINPR_ASSERT(count <= UINT16_MAX);
886 offer->cacheEntriesCount = (UINT16)count;
888 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Sending Cache Import Offer: %d", count);
890 for (
int idx = 0; idx < count; idx++)
892 if (persistent_cache_read_entry(persistent, &entry) < 1)
894 error = ERROR_INVALID_DATA;
898 offer->cacheEntries[idx].cacheKey = entry.key64;
899 offer->cacheEntries[idx].bitmapLength = entry.size;
902 if (offer->cacheEntriesCount > 0)
904 error = rdpgfx_send_cache_import_offer_pdu(context, offer);
905 if (error != CHANNEL_RC_OK)
907 WLog_Print(gfx->base.log, WLOG_ERROR,
"Failed to send cache import offer PDU");
913 persistent_cache_free(persistent);
923static UINT rdpgfx_load_cache_import_reply(
RDPGFX_PLUGIN* gfx,
926 UINT error = CHANNEL_RC_OK;
927 rdpPersistentCache* persistent =
nullptr;
929 WINPR_ASSERT(gfx->rdpcontext);
930 rdpSettings* settings = gfx->rdpcontext->settings;
931 RdpgfxClientContext* context = gfx->context;
933 WINPR_ASSERT(settings);
936 return CHANNEL_RC_OK;
938 const char* BitmapCachePersistFile =
940 if (!BitmapCachePersistFile)
941 return CHANNEL_RC_OK;
943 persistent = persistent_cache_new();
946 return CHANNEL_RC_NO_MEMORY;
948 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
950 error = CHANNEL_RC_INITIALIZATION_ERROR;
954 if (persistent_cache_get_version(persistent) != 3)
956 error = ERROR_INVALID_DATA;
960 int count = persistent_cache_get_count(persistent);
962 count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
964 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Receiving Cache Import Reply: %d", count);
966 for (
int idx = 0; idx < count; idx++)
969 if (persistent_cache_read_entry(persistent, &entry) < 1)
971 error = ERROR_INVALID_DATA;
975 const UINT16 cacheSlot = reply->cacheSlots[idx];
976 if (context && context->ImportCacheEntry)
978 error = context->ImportCacheEntry(context, cacheSlot, &entry);
979 if (error != CHANNEL_RC_OK)
984 persistent_cache_free(persistent);
988 persistent_cache_free(persistent);
1000 WINPR_ASSERT(callback);
1003 RdpgfxClientContext* context = gfx->context;
1004 UINT error = CHANNEL_RC_OK;
1006 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1007 return ERROR_INVALID_DATA;
1009 Stream_Read_UINT16(s, pdu.importedEntriesCount);
1011 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1013 return ERROR_INVALID_DATA;
1015 if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1016 return ERROR_INVALID_DATA;
1018 for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1020 Stream_Read_UINT16(s, pdu.cacheSlots[idx]);
1023 WLog_Print(gfx->base.log, WLOG_TRACE,
1024 "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16
"",
1025 pdu.importedEntriesCount);
1027 error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1031 WLog_Print(gfx->base.log, WLOG_ERROR,
1032 "rdpgfx_load_cache_import_reply failed with error %" PRIu32
"", error);
1038 IFCALLRET(context->CacheImportReply, error, context, &pdu);
1041 WLog_Print(gfx->base.log, WLOG_ERROR,
1042 "context->CacheImportReply failed with error %" PRIu32
"", error);
1056 WINPR_ASSERT(callback);
1059 RdpgfxClientContext* context = gfx->context;
1060 UINT error = CHANNEL_RC_OK;
1062 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1063 return ERROR_INVALID_DATA;
1065 Stream_Read_UINT16(s, pdu.surfaceId);
1066 Stream_Read_UINT16(s, pdu.width);
1067 Stream_Read_UINT16(s, pdu.height);
1068 Stream_Read_UINT8(s, pdu.pixelFormat);
1069 WLog_Print(gfx->base.log, WLOG_DEBUG,
1070 "RecvCreateSurfacePdu: surfaceId: %" PRIu16
" width: %" PRIu16
" height: %" PRIu16
1071 " pixelFormat: 0x%02" PRIX8
"",
1072 pdu.surfaceId, pdu.width, pdu.height, pdu.pixelFormat);
1081 const UINT drc = IFCALLRESULT(CHANNEL_RC_OK, context->DeleteSurface, context, &deletePdu);
1082 if (drc != CHANNEL_RC_OK)
1083 WLog_Print(gfx->base.log, WLOG_WARN,
1084 "context->DeleteSurface failed with error %" PRIu32
", ignoring", drc);
1086 IFCALLRET(context->CreateSurface, error, context, &pdu);
1089 WLog_Print(gfx->base.log, WLOG_ERROR,
1090 "context->CreateSurface failed with error %" PRIu32
"", error);
1104 WINPR_ASSERT(callback);
1107 RdpgfxClientContext* context = gfx->context;
1108 UINT error = CHANNEL_RC_OK;
1110 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1111 return ERROR_INVALID_DATA;
1113 Stream_Read_UINT16(s, pdu.surfaceId);
1114 WLog_Print(gfx->base.log, WLOG_DEBUG,
"RecvDeleteSurfacePdu: surfaceId: %" PRIu16
"",
1119 IFCALLRET(context->DeleteSurface, error, context, &pdu);
1122 WLog_Print(gfx->base.log, WLOG_ERROR,
1123 "context->DeleteSurface failed with error %" PRIu32
"", error);
1137 WINPR_ASSERT(callback);
1140 RdpgfxClientContext* context = gfx->context;
1141 UINT error = CHANNEL_RC_OK;
1143 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1144 return ERROR_INVALID_DATA;
1146 Stream_Read_UINT32(s, pdu.timestamp);
1147 Stream_Read_UINT32(s, pdu.frameId);
1148 WLog_Print(gfx->base.log, WLOG_TRACE,
1149 "RecvStartFramePdu: frameId: %" PRIu32
" timestamp: 0x%08" PRIX32
"", pdu.frameId,
1151 gfx->StartDecodingTime = GetTickCount64();
1155 IFCALLRET(context->StartFrame, error, context, &pdu);
1158 WLog_Print(gfx->base.log, WLOG_ERROR,
1159 "context->StartFrame failed with error %" PRIu32
"", error);
1162 gfx->UnacknowledgedFrames++;
1175 WINPR_ASSERT(callback);
1178 RdpgfxClientContext* context = gfx->context;
1179 UINT error = CHANNEL_RC_OK;
1181 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1182 return ERROR_INVALID_DATA;
1184 Stream_Read_UINT32(s, pdu.frameId);
1185 WLog_Print(gfx->base.log, WLOG_TRACE,
"RecvEndFramePdu: frameId: %" PRIu32
"", pdu.frameId);
1187 const UINT64 start = GetTickCount64();
1190 IFCALLRET(context->EndFrame, error, context, &pdu);
1194 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->EndFrame failed with error %" PRIu32
"",
1199 const UINT64 end = GetTickCount64();
1200 const UINT64 EndFrameTime = end - start;
1201 gfx->TotalDecodedFrames++;
1203 if (!gfx->sendFrameAcks)
1206 ack.frameId = pdu.frameId;
1207 ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1209 if (gfx->suspendFrameAcks)
1211 ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1213 if (gfx->TotalDecodedFrames == 1)
1214 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1215 WLog_Print(gfx->base.log, WLOG_ERROR,
1216 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32
"",
1221 ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1223 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1224 WLog_Print(gfx->base.log, WLOG_ERROR,
1225 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32
"", error);
1228 switch (gfx->ConnectionCaps.version)
1230#if defined(WITH_GFX_AV1)
1231 case RDPGFX_CAPVERSION_FRDP_1:
1233 case RDPGFX_CAPVERSION_10:
1234 case RDPGFX_CAPVERSION_102:
1235 case RDPGFX_CAPVERSION_103:
1236 case RDPGFX_CAPVERSION_104:
1237 case RDPGFX_CAPVERSION_105:
1238 case RDPGFX_CAPVERSION_106:
1239 case RDPGFX_CAPVERSION_106_ERR:
1240 case RDPGFX_CAPVERSION_107:
1244 UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1249 qoe.frameId = pdu.frameId;
1250 qoe.timestamp = gfx->StartDecodingTime % UINT32_MAX;
1251 qoe.timeDiffSE = WINPR_ASSERTING_INT_CAST(UINT16, diff);
1252 qoe.timeDiffEDR = WINPR_ASSERTING_INT_CAST(UINT16, EndFrameTime);
1254 if ((error = rdpgfx_send_qoe_frame_acknowledge_pdu(context, &qoe)))
1255 WLog_Print(gfx->base.log, WLOG_ERROR,
1256 "rdpgfx_send_qoe_frame_acknowledge_pdu failed with error %" PRIu32
1279 WINPR_ASSERT(callback);
1284 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1285 return ERROR_INVALID_DATA;
1287 Stream_Read_UINT16(s, pdu.surfaceId);
1288 Stream_Read_UINT16(s, pdu.codecId);
1289 Stream_Read_UINT8(s, pdu.pixelFormat);
1291 if ((error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.destRect))))
1293 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"",
1298 Stream_Read_UINT32(s, pdu.bitmapDataLength);
1300 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1301 return ERROR_INVALID_DATA;
1303 pdu.bitmapData = Stream_Pointer(s);
1304 Stream_Seek(s, pdu.bitmapDataLength);
1306 WLog_Print(gfx->base.log, WLOG_TRACE,
1307 "RecvWireToSurface1Pdu: surfaceId: %" PRIu16
" codecId: %s (0x%04" PRIX16
1308 ") pixelFormat: 0x%02" PRIX8
" "
1309 "destRect: left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
1310 " bitmapDataLength: %" PRIu32
"",
1311 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId, pdu.pixelFormat,
1312 pdu.destRect.left, pdu.destRect.top, pdu.destRect.right, pdu.destRect.bottom,
1313 pdu.bitmapDataLength);
1314 cmd.surfaceId = pdu.surfaceId;
1315 cmd.codecId = pdu.codecId;
1318 switch (pdu.pixelFormat)
1320 case GFX_PIXEL_FORMAT_XRGB_8888:
1321 cmd.format = PIXEL_FORMAT_BGRX32;
1324 case GFX_PIXEL_FORMAT_ARGB_8888:
1325 cmd.format = PIXEL_FORMAT_BGRA32;
1329 return ERROR_INVALID_DATA;
1332 cmd.left = pdu.destRect.left;
1333 cmd.top = pdu.destRect.top;
1334 cmd.right = pdu.destRect.right;
1335 cmd.bottom = pdu.destRect.bottom;
1336 cmd.width = cmd.right - cmd.left;
1337 cmd.height = cmd.bottom - cmd.top;
1338 cmd.length = pdu.bitmapDataLength;
1339 cmd.data = pdu.bitmapData;
1340 cmd.extra =
nullptr;
1342 if (cmd.right < cmd.left)
1344 WLog_Print(gfx->base.log, WLOG_ERROR,
1345 "RecvWireToSurface1Pdu right=%" PRIu32
" < left=%" PRIu32, cmd.right, cmd.left);
1346 return ERROR_INVALID_DATA;
1348 if (cmd.bottom < cmd.top)
1350 WLog_Print(gfx->base.log, WLOG_ERROR,
1351 "RecvWireToSurface1Pdu bottom=%" PRIu32
" < top=%" PRIu32, cmd.bottom, cmd.top);
1352 return ERROR_INVALID_DATA;
1355 if ((error = rdpgfx_decode(gfx, &cmd)))
1356 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_decode failed with error %" PRIu32
"!",
1372 WINPR_ASSERT(callback);
1375 RdpgfxClientContext* context = gfx->context;
1376 UINT error = CHANNEL_RC_OK;
1378 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1379 return ERROR_INVALID_DATA;
1381 Stream_Read_UINT16(s, pdu.surfaceId);
1382 Stream_Read_UINT16(s, pdu.codecId);
1383 Stream_Read_UINT32(s, pdu.codecContextId);
1384 Stream_Read_UINT8(s, pdu.pixelFormat);
1385 Stream_Read_UINT32(s, pdu.bitmapDataLength);
1386 pdu.bitmapData = Stream_Pointer(s);
1387 if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1388 return ERROR_INVALID_DATA;
1390 WLog_Print(gfx->base.log, WLOG_TRACE,
1391 "RecvWireToSurface2Pdu: surfaceId: %" PRIu16
" codecId: %s (0x%04" PRIX16
") "
1392 "codecContextId: %" PRIu32
" pixelFormat: 0x%02" PRIX8
" bitmapDataLength: %" PRIu32
1394 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1395 pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1397 cmd.surfaceId = pdu.surfaceId;
1398 cmd.codecId = pdu.codecId;
1399 cmd.contextId = pdu.codecContextId;
1401 switch (pdu.pixelFormat)
1403 case GFX_PIXEL_FORMAT_XRGB_8888:
1404 cmd.format = PIXEL_FORMAT_BGRX32;
1407 case GFX_PIXEL_FORMAT_ARGB_8888:
1408 cmd.format = PIXEL_FORMAT_BGRA32;
1412 return ERROR_INVALID_DATA;
1415 cmd.length = pdu.bitmapDataLength;
1416 cmd.data = pdu.bitmapData;
1417 cmd.extra =
nullptr;
1421 IFCALLRET(context->SurfaceCommand, error, context, &cmd);
1424 WLog_Print(gfx->base.log, WLOG_ERROR,
1425 "context->SurfaceCommand failed with error %" PRIu32
"", error);
1439 WINPR_ASSERT(callback);
1442 RdpgfxClientContext* context = gfx->context;
1443 UINT error = CHANNEL_RC_OK;
1445 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1446 return ERROR_INVALID_DATA;
1448 Stream_Read_UINT16(s, pdu.surfaceId);
1449 Stream_Read_UINT32(s, pdu.codecContextId);
1451 WLog_Print(gfx->base.log, WLOG_DEBUG,
1452 "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16
" codecContextId: %" PRIu32
"",
1453 pdu.surfaceId, pdu.codecContextId);
1457 IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1460 WLog_Print(gfx->base.log, WLOG_ERROR,
1461 "context->DeleteEncodingContext failed with error %" PRIu32
"", error);
1475 WINPR_ASSERT(callback);
1478 RdpgfxClientContext* context = gfx->context;
1480 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1481 return ERROR_INVALID_DATA;
1483 Stream_Read_UINT16(s, pdu.surfaceId);
1485 UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
1486 if (error != CHANNEL_RC_OK)
1488 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_color32 failed with error %" PRIu32
"!",
1493 Stream_Read_UINT16(s, pdu.fillRectCount);
1495 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
1496 return ERROR_INVALID_DATA;
1502 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
1503 return CHANNEL_RC_NO_MEMORY;
1506 for (UINT16 index = 0; index < pdu.fillRectCount; index++)
1510 if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect)))
1512 WLog_Print(gfx->base.log, WLOG_ERROR,
1513 "rdpgfx_read_rect16 failed with error %" PRIu32
"!", error);
1514 free(pdu.fillRects);
1518 WLog_Print(gfx->base.log, WLOG_TRACE,
1519 "RecvSolidFillPdu: surfaceId: %" PRIu16
" fillRectCount: %" PRIu16
"", pdu.surfaceId,
1524 IFCALLRET(context->SolidFill, error, context, &pdu);
1527 WLog_Print(gfx->base.log, WLOG_ERROR,
1528 "context->SolidFill failed with error %" PRIu32
"", error);
1531 free(pdu.fillRects);
1543 WINPR_ASSERT(callback);
1546 RdpgfxClientContext* context = gfx->context;
1549 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
1550 return ERROR_INVALID_DATA;
1552 Stream_Read_UINT16(s, pdu.surfaceIdSrc);
1553 Stream_Read_UINT16(s, pdu.surfaceIdDest);
1555 if ((error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc))))
1557 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"!",
1562 Stream_Read_UINT16(s, pdu.destPtsCount);
1564 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
1565 return ERROR_INVALID_DATA;
1571 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
1572 return CHANNEL_RC_NO_MEMORY;
1575 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
1579 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
1581 WLog_Print(gfx->base.log, WLOG_ERROR,
1582 "rdpgfx_read_point16 failed with error %" PRIu32
"!", error);
1588 WLog_Print(gfx->base.log, WLOG_TRACE,
1589 "RecvSurfaceToSurfacePdu: surfaceIdSrc: %" PRIu16
" surfaceIdDest: %" PRIu16
" "
1590 "left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
1591 " destPtsCount: %" PRIu16
"",
1592 pdu.surfaceIdSrc, pdu.surfaceIdDest, pdu.rectSrc.left, pdu.rectSrc.top,
1593 pdu.rectSrc.right, pdu.rectSrc.bottom, pdu.destPtsCount);
1597 IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
1600 WLog_Print(gfx->base.log, WLOG_ERROR,
1601 "context->SurfaceToSurface failed with error %" PRIu32
"", error);
1616 WINPR_ASSERT(callback);
1620 RdpgfxClientContext* context = gfx->context;
1622 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
1623 return ERROR_INVALID_DATA;
1625 Stream_Read_UINT16(s, pdu.surfaceId);
1626 Stream_Read_UINT64(s, pdu.cacheKey);
1627 Stream_Read_UINT16(s, pdu.cacheSlot);
1629 UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc));
1630 if (error != CHANNEL_RC_OK)
1632 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_rect16 failed with error %" PRIu32
"!",
1637 WLog_Print(gfx->base.log, WLOG_TRACE,
1638 "RecvSurfaceToCachePdu: surfaceId: %" PRIu16
" cacheKey: 0x%016" PRIX64
1639 " cacheSlot: %" PRIu16
" "
1640 "left: %" PRIu16
" top: %" PRIu16
" right: %" PRIu16
" bottom: %" PRIu16
"",
1641 pdu.surfaceId, pdu.cacheKey, pdu.cacheSlot, pdu.rectSrc.left, pdu.rectSrc.top,
1642 pdu.rectSrc.right, pdu.rectSrc.bottom);
1646 IFCALLRET(context->SurfaceToCache, error, context, &pdu);
1649 WLog_Print(gfx->base.log, WLOG_ERROR,
1650 "context->SurfaceToCache failed with error %" PRIu32
"", error);
1664 WINPR_ASSERT(callback);
1668 RdpgfxClientContext* context = gfx->context;
1669 UINT error = CHANNEL_RC_OK;
1671 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1672 return ERROR_INVALID_DATA;
1674 Stream_Read_UINT16(s, pdu.cacheSlot);
1675 Stream_Read_UINT16(s, pdu.surfaceId);
1676 Stream_Read_UINT16(s, pdu.destPtsCount);
1678 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
1679 return ERROR_INVALID_DATA;
1685 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
1686 return CHANNEL_RC_NO_MEMORY;
1689 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
1693 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
1695 WLog_Print(gfx->base.log, WLOG_ERROR,
1696 "rdpgfx_read_point16 failed with error %" PRIu32
"", error);
1702 WLog_Print(gfx->base.log, WLOG_TRACE,
1703 "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16
" surfaceId: %" PRIu16
1704 " destPtsCount: %" PRIu16
"",
1705 pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
1709 IFCALLRET(context->CacheToSurface, error, context, &pdu);
1712 WLog_Print(gfx->base.log, WLOG_ERROR,
1713 "context->CacheToSurface failed with error %" PRIu32
"", error);
1728 WINPR_ASSERT(callback);
1732 RdpgfxClientContext* context = gfx->context;
1733 UINT error = CHANNEL_RC_OK;
1735 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
1736 return ERROR_INVALID_DATA;
1738 Stream_Read_UINT16(s, pdu.surfaceId);
1739 Stream_Read_UINT16(s, pdu.reserved);
1740 Stream_Read_UINT32(s, pdu.outputOriginX);
1741 Stream_Read_UINT32(s, pdu.outputOriginY);
1742 WLog_Print(gfx->base.log, WLOG_DEBUG,
1743 "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16
" outputOriginX: %" PRIu32
1744 " outputOriginY: %" PRIu32
"",
1745 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
1749 IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
1752 WLog_Print(gfx->base.log, WLOG_ERROR,
1753 "context->MapSurfaceToOutput failed with error %" PRIu32
"", error);
1763 WINPR_ASSERT(callback);
1767 RdpgfxClientContext* context = gfx->context;
1768 UINT error = CHANNEL_RC_OK;
1770 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
1771 return ERROR_INVALID_DATA;
1773 Stream_Read_UINT16(s, pdu.surfaceId);
1774 Stream_Read_UINT16(s, pdu.reserved);
1775 Stream_Read_UINT32(s, pdu.outputOriginX);
1776 Stream_Read_UINT32(s, pdu.outputOriginY);
1777 Stream_Read_UINT32(s, pdu.targetWidth);
1778 Stream_Read_UINT32(s, pdu.targetHeight);
1779 WLog_Print(gfx->base.log, WLOG_DEBUG,
1780 "RecvMapSurfaceToScaledOutputPdu: surfaceId: %" PRIu16
" outputOriginX: %" PRIu32
1781 " outputOriginY: %" PRIu32
" targetWidth: %" PRIu32
" targetHeight: %" PRIu32,
1782 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY, pdu.targetWidth,
1787 IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
1790 WLog_Print(gfx->base.log, WLOG_ERROR,
1791 "context->MapSurfaceToScaledOutput failed with error %" PRIu32
"", error);
1805 WINPR_ASSERT(callback);
1809 RdpgfxClientContext* context = gfx->context;
1810 UINT error = CHANNEL_RC_OK;
1812 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
1813 return ERROR_INVALID_DATA;
1815 Stream_Read_UINT16(s, pdu.surfaceId);
1816 Stream_Read_UINT64(s, pdu.windowId);
1817 Stream_Read_UINT32(s, pdu.mappedWidth);
1818 Stream_Read_UINT32(s, pdu.mappedHeight);
1819 WLog_Print(gfx->base.log, WLOG_DEBUG,
1820 "RecvMapSurfaceToWindowPdu: surfaceId: %" PRIu16
" windowId: 0x%016" PRIX64
1821 " mappedWidth: %" PRIu32
" mappedHeight: %" PRIu32
"",
1822 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight);
1824 if (context && context->MapSurfaceToWindow)
1826 IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
1829 WLog_Print(gfx->base.log, WLOG_ERROR,
1830 "context->MapSurfaceToWindow failed with error %" PRIu32
"", error);
1840 WINPR_ASSERT(callback);
1843 RdpgfxClientContext* context = gfx->context;
1844 UINT error = CHANNEL_RC_OK;
1846 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
1847 return ERROR_INVALID_DATA;
1849 Stream_Read_UINT16(s, pdu.surfaceId);
1850 Stream_Read_UINT64(s, pdu.windowId);
1851 Stream_Read_UINT32(s, pdu.mappedWidth);
1852 Stream_Read_UINT32(s, pdu.mappedHeight);
1853 Stream_Read_UINT32(s, pdu.targetWidth);
1854 Stream_Read_UINT32(s, pdu.targetHeight);
1855 WLog_Print(gfx->base.log, WLOG_DEBUG,
1856 "RecvMapSurfaceToScaledWindowPdu: surfaceId: %" PRIu16
" windowId: 0x%016" PRIX64
1857 " mappedWidth: %" PRIu32
" mappedHeight: %" PRIu32
" targetWidth: %" PRIu32
1858 " targetHeight: %" PRIu32
"",
1859 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight, pdu.targetWidth,
1862 if (context && context->MapSurfaceToScaledWindow)
1864 IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
1867 WLog_Print(gfx->base.log, WLOG_ERROR,
1868 "context->MapSurfaceToScaledWindow failed with error %" PRIu32
"", error);
1883 WINPR_ASSERT(callback);
1885 const size_t beg = Stream_GetPosition(s);
1889 UINT error = rdpgfx_read_header(gfx->base.log, s, &header);
1890 if (error != CHANNEL_RC_OK)
1892 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_read_header failed with error %" PRIu32
"!",
1897 WLog_Print(gfx->base.log, WLOG_TRACE,
1898 "cmdId: %s (0x%04" PRIX16
") flags: 0x%04" PRIX16
" pduLength: %" PRIu32
"",
1899 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
1902 switch (header.cmdId)
1904 case RDPGFX_CMDID_WIRETOSURFACE_1:
1905 if ((error = rdpgfx_recv_wire_to_surface_1_pdu(callback, s)))
1906 WLog_Print(gfx->base.log, WLOG_ERROR,
1907 "rdpgfx_recv_wire_to_surface_1_pdu failed with error %" PRIu32
"!",
1912 case RDPGFX_CMDID_WIRETOSURFACE_2:
1913 if ((error = rdpgfx_recv_wire_to_surface_2_pdu(callback, s)))
1914 WLog_Print(gfx->base.log, WLOG_ERROR,
1915 "rdpgfx_recv_wire_to_surface_2_pdu failed with error %" PRIu32
"!",
1920 case RDPGFX_CMDID_DELETEENCODINGCONTEXT:
1921 if ((error = rdpgfx_recv_delete_encoding_context_pdu(callback, s)))
1922 WLog_Print(gfx->base.log, WLOG_ERROR,
1923 "rdpgfx_recv_delete_encoding_context_pdu failed with error %" PRIu32
"!",
1928 case RDPGFX_CMDID_SOLIDFILL:
1929 if ((error = rdpgfx_recv_solid_fill_pdu(callback, s)))
1930 WLog_Print(gfx->base.log, WLOG_ERROR,
1931 "rdpgfx_recv_solid_fill_pdu failed with error %" PRIu32
"!", error);
1935 case RDPGFX_CMDID_SURFACETOSURFACE:
1936 if ((error = rdpgfx_recv_surface_to_surface_pdu(callback, s)))
1937 WLog_Print(gfx->base.log, WLOG_ERROR,
1938 "rdpgfx_recv_surface_to_surface_pdu failed with error %" PRIu32
"!",
1943 case RDPGFX_CMDID_SURFACETOCACHE:
1944 if ((error = rdpgfx_recv_surface_to_cache_pdu(callback, s)))
1945 WLog_Print(gfx->base.log, WLOG_ERROR,
1946 "rdpgfx_recv_surface_to_cache_pdu failed with error %" PRIu32
"!",
1951 case RDPGFX_CMDID_CACHETOSURFACE:
1952 if ((error = rdpgfx_recv_cache_to_surface_pdu(callback, s)))
1953 WLog_Print(gfx->base.log, WLOG_ERROR,
1954 "rdpgfx_recv_cache_to_surface_pdu failed with error %" PRIu32
"!",
1959 case RDPGFX_CMDID_EVICTCACHEENTRY:
1960 if ((error = rdpgfx_recv_evict_cache_entry_pdu(callback, s)))
1961 WLog_Print(gfx->base.log, WLOG_ERROR,
1962 "rdpgfx_recv_evict_cache_entry_pdu failed with error %" PRIu32
"!",
1967 case RDPGFX_CMDID_CREATESURFACE:
1968 if ((error = rdpgfx_recv_create_surface_pdu(callback, s)))
1969 WLog_Print(gfx->base.log, WLOG_ERROR,
1970 "rdpgfx_recv_create_surface_pdu failed with error %" PRIu32
"!", error);
1974 case RDPGFX_CMDID_DELETESURFACE:
1975 if ((error = rdpgfx_recv_delete_surface_pdu(callback, s)))
1976 WLog_Print(gfx->base.log, WLOG_ERROR,
1977 "rdpgfx_recv_delete_surface_pdu failed with error %" PRIu32
"!", error);
1981 case RDPGFX_CMDID_STARTFRAME:
1982 if ((error = rdpgfx_recv_start_frame_pdu(callback, s)))
1983 WLog_Print(gfx->base.log, WLOG_ERROR,
1984 "rdpgfx_recv_start_frame_pdu failed with error %" PRIu32
"!", error);
1988 case RDPGFX_CMDID_ENDFRAME:
1989 if ((error = rdpgfx_recv_end_frame_pdu(callback, s)))
1990 WLog_Print(gfx->base.log, WLOG_ERROR,
1991 "rdpgfx_recv_end_frame_pdu failed with error %" PRIu32
"!", error);
1995 case RDPGFX_CMDID_RESETGRAPHICS:
1996 if ((error = rdpgfx_recv_reset_graphics_pdu(callback, s)))
1997 WLog_Print(gfx->base.log, WLOG_ERROR,
1998 "rdpgfx_recv_reset_graphics_pdu failed with error %" PRIu32
"!", error);
2002 case RDPGFX_CMDID_MAPSURFACETOOUTPUT:
2003 if ((error = rdpgfx_recv_map_surface_to_output_pdu(callback, s)))
2004 WLog_Print(gfx->base.log, WLOG_ERROR,
2005 "rdpgfx_recv_map_surface_to_output_pdu failed with error %" PRIu32
"!",
2010 case RDPGFX_CMDID_CACHEIMPORTREPLY:
2011 if ((error = rdpgfx_recv_cache_import_reply_pdu(callback, s)))
2012 WLog_Print(gfx->base.log, WLOG_ERROR,
2013 "rdpgfx_recv_cache_import_reply_pdu failed with error %" PRIu32
"!",
2018 case RDPGFX_CMDID_CAPSCONFIRM:
2019 if ((error = rdpgfx_recv_caps_confirm_pdu(callback, s)))
2020 WLog_Print(gfx->base.log, WLOG_ERROR,
2021 "rdpgfx_recv_caps_confirm_pdu failed with error %" PRIu32
"!", error);
2023 if ((error = rdpgfx_send_cache_offer(gfx)))
2024 WLog_Print(gfx->base.log, WLOG_ERROR,
2025 "rdpgfx_send_cache_offer failed with error %" PRIu32
"!", error);
2029 case RDPGFX_CMDID_MAPSURFACETOWINDOW:
2030 if ((error = rdpgfx_recv_map_surface_to_window_pdu(callback, s)))
2031 WLog_Print(gfx->base.log, WLOG_ERROR,
2032 "rdpgfx_recv_map_surface_to_window_pdu failed with error %" PRIu32
"!",
2037 case RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW:
2038 if ((error = rdpgfx_recv_map_surface_to_scaled_window_pdu(callback, s)))
2039 WLog_Print(gfx->base.log, WLOG_ERROR,
2040 "rdpgfx_recv_map_surface_to_scaled_window_pdu failed with error %" PRIu32
2046 case RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT:
2047 if ((error = rdpgfx_recv_map_surface_to_scaled_output_pdu(callback, s)))
2048 WLog_Print(gfx->base.log, WLOG_ERROR,
2049 "rdpgfx_recv_map_surface_to_scaled_output_pdu failed with error %" PRIu32
2056 error = CHANNEL_RC_BAD_PROC;
2062 WLog_Print(gfx->base.log, WLOG_ERROR,
2063 "Error while processing GFX cmdId: %s (0x%04" PRIX16
")",
2064 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
2065 if (!Stream_SetPosition(s, (beg + header.pduLength)))
2066 return ERROR_INVALID_DATA;
2070 const size_t end = Stream_GetPosition(s);
2072 if (end != (beg + header.pduLength))
2074 WLog_Print(gfx->base.log, WLOG_ERROR,
2075 "Unexpected gfx pdu end: Actual: %" PRIuz
", Expected: %" PRIuz, end,
2076 (beg + header.pduLength));
2077 if (!Stream_SetPosition(s, (beg + header.pduLength)))
2078 return ERROR_INVALID_DATA;
2089static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
2092 BYTE* pDstData =
nullptr;
2094 WINPR_ASSERT(callback);
2096 UINT error = CHANNEL_RC_OK;
2099 int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2100 (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2104 WLog_Print(gfx->base.log, WLOG_ERROR,
"zgfx_decompress failure! status: %d", status);
2106 return ERROR_INTERNAL_ERROR;
2109 wStream sbuffer = WINPR_C_ARRAY_INIT;
2110 wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2114 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2116 return CHANNEL_RC_NO_MEMORY;
2119 while (Stream_GetPosition(s) < Stream_Length(s))
2121 if ((error = rdpgfx_recv_pdu(callback, s)))
2123 WLog_Print(gfx->base.log, WLOG_ERROR,
"rdpgfx_recv_pdu failed with error %" PRIu32
"!",
2138static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2141 WINPR_ASSERT(callback);
2144 RdpgfxClientContext* context = gfx->context;
2145 UINT error = CHANNEL_RC_OK;
2146 BOOL do_caps_advertise = TRUE;
2148 gfx->sendFrameAcks = TRUE;
2152 IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2155 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->OnOpen failed with error %" PRIu32
"",
2159 if (do_caps_advertise)
2160 error = rdpgfx_send_supported_caps(callback);
2170static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2172 UINT error = CHANNEL_RC_OK;
2174 WINPR_ASSERT(callback);
2180 RdpgfxClientContext* context = gfx->context;
2182 WLog_Print(gfx->base.log, WLOG_DEBUG,
"OnClose");
2183 error = rdpgfx_save_persistent_cache(gfx);
2188 WLog_Print(gfx->base.log, WLOG_ERROR,
2189 "rdpgfx_save_persistent_cache failed with error %" PRIu32
"", error);
2192 free_surfaces(context, gfx->SurfaceTable);
2193 error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2197 WLog_Print(gfx->base.log, WLOG_ERROR,
"evict_cache_slots failed with error %" PRIu32
"",
2202 gfx->UnacknowledgedFrames = 0;
2203 gfx->TotalDecodedFrames = 0;
2207 error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2211 WLog_Print(gfx->base.log, WLOG_ERROR,
"context->OnClose failed with error %" PRIu32
"",
2217 return CHANNEL_RC_OK;
2224 RdpgfxClientContext* context = gfx->context;
2226 WLog_Print(gfx->base.log, WLOG_DEBUG,
"Terminated");
2227 rdpgfx_client_context_free(context);
2235static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId,
void* pData)
2237 WINPR_ASSERT(context);
2240 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2244 if (!HashTable_Insert(gfx->SurfaceTable, (
void*)key, pData))
2245 return ERROR_BAD_ARGUMENTS;
2248 HashTable_Remove(gfx->SurfaceTable, (
void*)key);
2250 return CHANNEL_RC_OK;
2258static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2261 ULONG_PTR* pKeys =
nullptr;
2262 WINPR_ASSERT(context);
2265 size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2267 WINPR_ASSERT(ppSurfaceIds);
2268 WINPR_ASSERT(count_out);
2272 return CHANNEL_RC_OK;
2275 UINT16* pSurfaceIds = (UINT16*)calloc(count,
sizeof(UINT16));
2279 WLog_Print(gfx->base.log, WLOG_ERROR,
"calloc failed!");
2281 return CHANNEL_RC_NO_MEMORY;
2284 for (
size_t index = 0; index < count; index++)
2286 pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2290 *ppSurfaceIds = pSurfaceIds;
2291 *count_out = (UINT16)count;
2292 return CHANNEL_RC_OK;
2295static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2297 WINPR_ASSERT(context);
2300 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2301 return HashTable_GetItemValue(gfx->SurfaceTable, (
void*)key);
2309static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot,
void* pData)
2311 WINPR_ASSERT(context);
2316 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2318 WLog_Print(gfx->base.log, WLOG_ERROR,
2319 "invalid cache slot %" PRIu16
", must be between 1 and %" PRIu16
"", cacheSlot,
2320 gfx->MaxCacheSlots);
2321 return ERROR_INVALID_INDEX;
2324 gfx->CacheSlots[cacheSlot - 1] = pData;
2325 return CHANNEL_RC_OK;
2328static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2330 WINPR_ASSERT(context);
2334 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2336 WLog_Print(gfx->base.log, WLOG_ERROR,
2337 "invalid cache slot %" PRIu16
", must be between 1 and %" PRIu16
"", cacheSlot,
2338 gfx->MaxCacheSlots);
2342 return gfx->CacheSlots[cacheSlot - 1];
2346 WINPR_ATTR_UNUSED rdpSettings* settings)
2351 gfx->rdpcontext = rcontext;
2353 gfx->SurfaceTable = HashTable_New(TRUE);
2354 if (!gfx->SurfaceTable)
2356 WLog_Print(gfx->base.log, WLOG_ERROR,
"HashTable_New for surfaces failed !");
2357 return CHANNEL_RC_NO_MEMORY;
2360 gfx->suspendFrameAcks =
2362 gfx->MaxCacheSlots =
2365 RdpgfxClientContext* context = (RdpgfxClientContext*)calloc(1,
sizeof(RdpgfxClientContext));
2368 WLog_Print(gfx->base.log, WLOG_ERROR,
"context calloc failed!");
2369 HashTable_Free(gfx->SurfaceTable);
2370 gfx->SurfaceTable =
nullptr;
2371 return CHANNEL_RC_NO_MEMORY;
2374 gfx->zgfx = zgfx_context_new(FALSE);
2377 WLog_Print(gfx->base.log, WLOG_ERROR,
"zgfx_context_new failed!");
2378 HashTable_Free(gfx->SurfaceTable);
2379 gfx->SurfaceTable =
nullptr;
2381 return CHANNEL_RC_NO_MEMORY;
2384 context->handle = (
void*)gfx;
2385 context->GetSurfaceIds = rdpgfx_get_surface_ids;
2386 context->SetSurfaceData = rdpgfx_set_surface_data;
2387 context->GetSurfaceData = rdpgfx_get_surface_data;
2388 context->SetCacheSlotData = rdpgfx_set_cache_slot_data;
2389 context->GetCacheSlotData = rdpgfx_get_cache_slot_data;
2390 context->CapsAdvertise = rdpgfx_send_caps_advertise_pdu;
2391 context->FrameAcknowledge = rdpgfx_send_frame_acknowledge_pdu;
2392 context->CacheImportOffer = rdpgfx_send_cache_import_offer_pdu;
2393 context->QoeFrameAcknowledge = rdpgfx_send_qoe_frame_acknowledge_pdu;
2395 gfx->base.iface.pInterface = (
void*)context;
2396 gfx->context = context;
2397 return CHANNEL_RC_OK;
2400void rdpgfx_client_context_free(RdpgfxClientContext* context)
2407 free_surfaces(context, gfx->SurfaceTable);
2408 evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2412 zgfx_context_free(gfx->zgfx);
2413 gfx->zgfx =
nullptr;
2416 HashTable_Free(gfx->SurfaceTable);
2420static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
2421 rdpgfx_on_open, rdpgfx_on_close,
2429FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
2431 return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
2433 &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.