FreeRDP
Loading...
Searching...
No Matches
client/rdpgfx_main.c
1
23
24#include <freerdp/config.h>
25
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28
29#include <winpr/crt.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>
38
39#include <freerdp/addin.h>
40#include <freerdp/channels/log.h>
41#include <freerdp/codec/h264.h>
42
43#include "rdpgfx_common.h"
44#include "rdpgfx_codec.h"
45
46#include "rdpgfx_main.h"
47
48#define GFXTAG CHANNELS_TAG("rdpgfx.client")
49
50#define RDPGFX_NUMBER_CAPSETS 0x100
51
52typedef struct
53{
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;
60
61typedef struct
62{
63 RdpgfxClientContext common;
64 RdpgfxClientContextStats stats;
65} RdpgfxClientContextInt;
66
67/* The name of a byte counter is the name of its codec ID with _BYTES added */
68static char bytesGfxCodecNames[RDPGFX_CODECID_MAX][64];
69static INIT_ONCE bytesGfxCodecNamesOnce = INIT_ONCE_STATIC_INIT;
70
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)
74{
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));
78 return TRUE;
79}
80
81static const UINT32 MAX_SURFACE_SIZE = 32766;
82static const UINT32 MAX_MONITOR_COUNT = 16;
83
84size_t rdpgfx_stats_max_index(void)
85{
86 return sizeof(RdpgfxClientContextStats) / sizeof(uint64_t);
87}
88
89const char* rdpgfx_stats_name_for_index(size_t index)
90{
91 if (index < RDPGFX_CODECID_MAX)
92 return rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
93
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";
99
100 index -= RDPGFX_CMDID_MAX + 1;
101 if (index < RDPGFX_CODECID_MAX)
102 {
103 if (!InitOnceExecuteOnce(&bytesGfxCodecNamesOnce, rdpgfx_stats_init_names, nullptr,
104 nullptr))
105 return "RDPGFX_STATS_UNUSED";
106 return bytesGfxCodecNames[index];
107 }
108
109 index -= RDPGFX_CODECID_MAX;
110 if (index == 0)
111 return "RDPGFX_FRAMES_DECODED";
112 if (index == 1)
113 return "RDPGFX_FRAMES_UNACKNOWLEDGED";
114 return "RDPGFX_STATS_UNUSED";
115}
116
117uint64_t rdpgfx_stats_value_for_index(RdpgfxClientContext* context, size_t index)
118{
119 WINPR_ASSERT(context);
120
121 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
122
123 if (index < RDPGFX_CODECID_MAX)
124 return intCtx->stats.cntGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
125
126 index -= RDPGFX_CODECID_MAX;
127 if (index <= RDPGFX_CMDID_MAX)
128 return intCtx->stats.cntGfxCommandID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
129
130 index -= RDPGFX_CMDID_MAX + 1;
131 if (index < RDPGFX_CODECID_MAX)
132 return intCtx->stats.bytesGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
133
134 index -= RDPGFX_CODECID_MAX;
135 if (index == 0)
136 return intCtx->stats.framesDecoded;
137 if (index == 1)
138 return intCtx->stats.framesUnacknowledged;
139 return 0;
140}
141
142static void rdpgfx_codecid_event(RdpgfxClientContext* context, uint32_t index, uint32_t length)
143{
144 if (!context)
145 return;
146 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
147 WINPR_ASSERT(intCtx);
148 if (index < RDPGFX_CODECID_MAX)
149 {
150 intCtx->stats.cntGfxCodecID[index]++;
151 intCtx->stats.bytesGfxCodecID[index] += length;
152 }
153 else
154 {
155 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
156 WINPR_ASSERT(gfx);
157 WLog_Print(gfx->base.log, WLOG_WARN, "Invalid event index %" PRIu32, index);
158 }
159}
160
161static void rdpgfx_stats_cmdid_event(RdpgfxClientContext* context, uint32_t index)
162{
163 if (!context)
164 return;
165 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
166 WINPR_ASSERT(intCtx);
167
168 if (index >= RDPGFX_CMDID_MAX)
169 {
170 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
171 WINPR_ASSERT(gfx);
172 WLog_Print(gfx->base.log, WLOG_WARN, "Invalid codecID %" PRIu32, index);
173 index = RDPGFX_CMDID_MAX;
174 }
175 intCtx->stats.cntGfxCommandID[index]++;
176}
177
178static void rdpgfx_stats_frame_decoded(RdpgfxClientContext* context)
179{
180 if (!context)
181 return;
182 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
183 intCtx->stats.framesDecoded++;
184}
185
186/* A copy: the plugin's own counter is reset before the stats are dumped on close */
187static void rdpgfx_stats_frames_unacknowledged(RdpgfxClientContext* context, UINT32 frames)
188{
189 if (!context)
190 return;
191 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
192 intCtx->stats.framesUnacknowledged = frames;
193}
194
195static BOOL delete_surface(const void* key, void* value, void* arg)
196{
197 const ULONG_PTR id = (ULONG_PTR)(key);
198 if ((id < 1) || (id - 1 > UINT16_MAX))
199 return FALSE;
200 const UINT16 uid = WINPR_ASSERTING_INT_CAST(UINT16, id - 1);
201
202 RdpgfxClientContext* context = arg;
203 const RDPGFX_DELETE_SURFACE_PDU pdu = { .surfaceId = uid };
204
205 WINPR_UNUSED(value);
206
207 if (context)
208 {
209 UINT error = CHANNEL_RC_OK;
210 IFCALLRET(context->DeleteSurface, error, context, &pdu);
211
212 if (error)
213 {
214 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
215 WINPR_ASSERT(gfx);
216 WLog_Print(gfx->base.log, WLOG_ERROR,
217 "context->DeleteSurface failed with error %" PRIu32 "", error);
218 }
219 }
220 return TRUE;
221}
222
223static void free_surfaces(RdpgfxClientContext* context, wHashTable* SurfaceTable)
224{
225 ULONG_PTR* pKeys = nullptr;
226
227 WINPR_ASSERT(context);
228
229 /* Do not use HashTable_Foreach here: it keeps the (synchronized) table locked while
230 * context->DeleteSurface is called, which acquires context->mux. All other users
231 * (GetSurfaceData, GetSurfaceIds, SetSurfaceData) take context->mux first and the
232 * table lock second, so iterate over a snapshot of the keys instead. */
233 const size_t count = HashTable_GetKeys(SurfaceTable, &pKeys);
234
235 for (size_t index = 0; index < count; index++)
236 {
237 if (!delete_surface((const void*)pKeys[index], nullptr, context))
238 {
239 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
240 WINPR_ASSERT(gfx);
241 WLog_Print(gfx->base.log, WLOG_WARN, "delete_surface failed");
242 break;
243 }
244 }
245
246 free(pKeys);
247}
248
249static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots, void** CacheSlots)
250{
251 UINT error = CHANNEL_RC_OK;
252
253 WINPR_ASSERT(CacheSlots);
254 for (UINT16 index = 0; index < MaxCacheSlots; index++)
255 {
256 if (CacheSlots[index])
257 {
258 const RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = { .cacheSlot = index + 1 };
259
260 if (context && context->EvictCacheEntry)
261 {
262 const UINT rc = context->EvictCacheEntry(context, &pdu);
263 if (rc != CHANNEL_RC_OK)
264 error = rc;
265 }
266
267 CacheSlots[index] = nullptr;
268 }
269 }
270 return error;
271}
272
278static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
279 const RDPGFX_CAPS_ADVERTISE_PDU* pdu)
280{
281 UINT error = CHANNEL_RC_OK;
282
283 WINPR_ASSERT(pdu);
284 WINPR_ASSERT(context);
285
286 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
287
288 if (!gfx || !gfx->base.listener_callback)
289 return ERROR_BAD_ARGUMENTS;
290
291 memset(gfx->allowedCodecs, FALSE, sizeof(gfx->allowedCodecs));
292
293 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
294
295 RDPGFX_HEADER header = { .flags = 0,
296 .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
297 .pduLength = RDPGFX_HEADER_SIZE + 2 };
298
299 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
300 {
301 const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
302 header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
303 }
304
305 WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCapsAdvertisePdu %" PRIu16 "", pdu->capsSetCount);
306 wStream* s = Stream_New(nullptr, header.pduLength);
307
308 if (!s)
309 {
310 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
311 return CHANNEL_RC_NO_MEMORY;
312 }
313
314 if ((error = rdpgfx_write_header(s, &header)))
315 goto fail;
316
317 /* RDPGFX_CAPS_ADVERTISE_PDU */
318 Stream_Write_UINT16(s, pdu->capsSetCount); /* capsSetCount (2 bytes) */
319
320 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
321 {
322 const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
323
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)));
328
329 Stream_Write_UINT32(s, capsSet->version); /* version (4 bytes) */
330 Stream_Write_UINT32(s, capsSet->length); /* capsDataLength (4 bytes) */
331 Stream_Write_UINT32(s, capsSet->flags); /* capsData (4 bytes) */
332 if (capsSet->length < 4)
333 goto fail;
334 Stream_Zero(s, capsSet->length - 4);
335 }
336
337 Stream_SealLength(s);
338 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
339 nullptr);
340fail:
341 Stream_Free(s, TRUE);
342 return error;
343}
344
345static BOOL rdpgfx_is_capability_filtered(RDPGFX_PLUGIN* gfx, UINT32 caps)
346{
347 WINPR_ASSERT(gfx);
348 const UINT32 filter =
349 freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCapsFilter);
350 const UINT32 capList[] = {
351#if defined(WITH_GFX_AV1)
352 RDPGFX_CAPVERSION_FRDP_1,
353#endif
354#if defined(WITH_GFX_AV2)
355 RDPGFX_CAPVERSION_FRDP_2,
356#endif
357 RDPGFX_CAPVERSION_8,
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)
369 ,
370 RDPGFX_CAPVERSION_111,
371 RDPGFX_CAPVERSION_112,
372 RDPGFX_CAPVERSION_113,
373 RDPGFX_CAPVERSION_115
374#endif
375 };
376
377 for (size_t x = 0; x < ARRAYSIZE(capList); x++)
378 {
379 if (caps == capList[x])
380 return (filter & (1u << x)) != 0;
381 }
382
383 return TRUE;
384}
385
386WINPR_ATTR_NODISCARD
387static RDPGFX_CAPSET* nextCapset(RDPGFX_CAPS_ADVERTISE_PDU* pdu, size_t count)
388{
389 WINPR_ASSERT(pdu);
390 WINPR_ASSERT(pdu->capsSets);
391 WINPR_ASSERT(count > 0);
392 WINPR_ASSERT(pdu->capsSetCount < count);
393 if (pdu->capsSetCount >= count)
394 return nullptr;
395 return &pdu->capsSets[pdu->capsSetCount++];
396}
397
398#if defined(WITH_GFX_AZURE)
399/* Only offer HEVC if it can be decoded: a server confirming it sends HEVC420 frames. */
400WINPR_ATTR_NODISCARD
401static BOOL rdpgfx_hevc_enabled(RDPGFX_PLUGIN* gfx)
402{
403 WINPR_ASSERT(gfx);
404
405 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecHEVC))
406 return FALSE;
407 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I420, FALSE))
408 return TRUE;
409
410 WLog_Print(gfx->base.log, WLOG_WARN,
411 "HEVC mode enabled, but decoder does not support it. Disabling.");
412 return FALSE;
413}
414#endif
415
421static UINT rdpgfx_send_supported_caps(GENERIC_CHANNEL_CALLBACK* callback)
422{
423 RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
424
425 if (!callback)
426 return ERROR_BAD_ARGUMENTS;
427
428 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
429
430 if (!gfx)
431 return ERROR_BAD_CONFIGURATION;
432
433 RdpgfxClientContext* context = gfx->context;
434
435 if (!context)
436 return ERROR_BAD_CONFIGURATION;
437
438 RDPGFX_CAPS_ADVERTISE_PDU pdu = { .capsSetCount = 0, .capsSets = capsSets };
439
440 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
441 {
442 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
443 if (!capsSet)
444 return ERROR_BAD_CONFIGURATION;
445 capsSet->version = RDPGFX_CAPVERSION_8;
446 capsSet->length = 4;
447 capsSet->flags = 0;
448
449 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
450 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
451
452 /* in CAPVERSION_8 the spec says that we should not have both
453 * thinclient and smallcache (and thinclient implies a small cache)
454 */
455 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) &&
456 !freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
457 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
458 }
459
460 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
461 {
462 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
463 if (!capsSet)
464 return ERROR_BAD_CONFIGURATION;
465 capsSet->version = RDPGFX_CAPVERSION_81;
466 capsSet->length = 4;
467 capsSet->flags = 0;
468
469 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
470 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
471
472 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
473 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
474
475 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264) &&
476 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC420, FALSE))
477 capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
478 }
479
480 UINT32 caps10Flags = 0;
481 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264) ||
482 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444))
483 {
484 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
485 caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
486
487 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444) ||
488 !freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC444, FALSE))
489 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
490 }
491
492 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
493 {
494 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
495 if (!capsSet)
496 return ERROR_BAD_CONFIGURATION;
497 capsSet->version = RDPGFX_CAPVERSION_10;
498 capsSet->length = 4;
499 capsSet->flags = caps10Flags;
500 }
501
502 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
503 {
504 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
505 if (!capsSet)
506 return ERROR_BAD_CONFIGURATION;
507 capsSet->version = RDPGFX_CAPVERSION_101;
508 capsSet->length = 0x10;
509 capsSet->flags = 0;
510 }
511
512 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
513 {
514 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
515 if (!capsSet)
516 return ERROR_BAD_CONFIGURATION;
517 capsSet->version = RDPGFX_CAPVERSION_102;
518 capsSet->length = 0x4;
519 capsSet->flags = caps10Flags;
520 }
521
522 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
523 {
524 if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
525 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
526 }
527
528 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
529 {
530 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
531 if (!capsSet)
532 return ERROR_BAD_CONFIGURATION;
533 capsSet->version = RDPGFX_CAPVERSION_103;
534 capsSet->length = 0x4;
535 capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
536 }
537
538 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
539 {
540 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
541 if (!capsSet)
542 return ERROR_BAD_CONFIGURATION;
543 capsSet->version = RDPGFX_CAPVERSION_104;
544 capsSet->length = 0x4;
545 capsSet->flags = caps10Flags;
546 }
547
548 /* The following capabilities expect support for image scaling.
549 * Disable these for builds that do not have support for that.
550 */
551 if (freerdp_image_scaling_supported())
552 {
553 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
554 {
555 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
556 if (!capsSet)
557 return ERROR_BAD_CONFIGURATION;
558 capsSet->version = RDPGFX_CAPVERSION_105;
559 capsSet->length = 0x4;
560 capsSet->flags = caps10Flags;
561 }
562
563 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
564 {
565 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
566 if (!capsSet)
567 return ERROR_BAD_CONFIGURATION;
568 capsSet->version = RDPGFX_CAPVERSION_106;
569 capsSet->length = 0x4;
570 capsSet->flags = caps10Flags;
571 }
572
573 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
574 {
575 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
576 if (!capsSet)
577 return ERROR_BAD_CONFIGURATION;
578 capsSet->version = RDPGFX_CAPVERSION_106_ERR;
579 capsSet->length = 0x4;
580 capsSet->flags = caps10Flags;
581 }
582 }
583
584 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
585 {
586 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
587 if (!capsSet)
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;
594 }
595
596#if defined(WITH_GFX_AZURE)
597 if (freerdp_image_scaling_supported())
598 {
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);
602 const BOOL hevc444 =
603 hevc && freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecHEVC444) &&
604 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I444, FALSE);
605
606 for (size_t x = 0; x < ARRAYSIZE(caps); x++)
607 {
608 const UINT32 cap = caps[x];
609
610 if (rdpgfx_is_capability_filtered(gfx, cap))
611 continue;
612
613 /* the flags differ between the versions, keep them out of caps10Flags */
614 UINT32 flags = caps10Flags;
615
616 /* HEVC420 is valid from RDPGFX_CAPVERSION_112 on. AVC stays enabled: the server
617 * falls back to it if it has no HEVC encoder. */
618 if (hevc && (cap >= RDPGFX_CAPVERSION_112))
619 flags |= RDPGFX_CAPS_FLAG_HEVC_ENABLE;
620
621 /* HEVC444 is only valid in RDPGFX_CAPVERSION_115 */
622 if (hevc444 && (cap >= RDPGFX_CAPVERSION_115))
623 flags |= RDPGFX_CAPS_FLAG_HEVC_I444;
624
625 /* AVC mixed mode is valid in RDPGFX_CAPVERSION_113 and RDPGFX_CAPVERSION_115.
626 * The server then mixes AVC420 or AVC444 regions with the other codecs on a
627 * surface, all of which are decoded already, so offer it whenever AVC is. */
628 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAVCMM))
629 {
630 if ((cap >= RDPGFX_CAPVERSION_113) &&
631 ((flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0))
632 flags |= RDPGFX_CAPS_FLAG_AVCMM_ENABLE;
633 }
634
635 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
636 if (!capsSet)
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;
643 }
644 }
645#endif
646
647#if defined(WITH_GFX_AV1)
648 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1))
649 {
650 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I420, FALSE) &&
651 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
652 {
653 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
654 if (!capsSet)
655 return ERROR_BAD_CONFIGURATION;
656 capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
657 capsSet->length = 0x4;
658
659 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I444, FALSE))
660 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
661 else
662 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
663
664 const UINT32 profile =
665 freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1Profile);
666 if (profile == 0)
667 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
668
669 if (!freerdp_image_scaling_supported())
670 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
671 }
672 }
673#endif
674
675#if defined(WITH_GFX_AV2)
676 /* A capability set of its own: a server that confirms it may send AV2, and one that does
677 * not know it never confirms it. */
678 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV2))
679 {
680 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I420, FALSE) &&
681 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_2))
682 {
683 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
684 if (!capsSet)
685 return ERROR_BAD_CONFIGURATION;
686 capsSet->version = RDPGFX_CAPVERSION_FRDP_2;
687 capsSet->length = 0x4;
688
689 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I444, FALSE))
690 {
691 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
692
693 const UINT32 profile = freerdp_settings_get_uint32(gfx->rdpcontext->settings,
694 FreeRDP_GfxCodecAV2Profile);
695 if (profile == 0)
696 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
697 }
698 else
699 {
700 /* the decoder (dav2d) has no AV2 4:4:4: always ask for 4:2:0 */
701 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
702 }
703
704 if (!freerdp_image_scaling_supported())
705 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
706 }
707 }
708#endif
709
710 return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
711}
712
718static UINT rdpgfx_recv_caps_confirm_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
719{
720 WINPR_ASSERT(callback);
721 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
722
723 WINPR_ASSERT(gfx);
724 RdpgfxClientContext* context = gfx->context;
725
726 RDPGFX_CAPSET capsSet = WINPR_C_ARRAY_INIT;
727 RDPGFX_CAPS_CONFIRM_PDU pdu = { .capsSet = &capsSet };
728
729 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
730 return ERROR_INVALID_DATA;
731
732 Stream_Read_UINT32(s, capsSet.version); /* version (4 bytes) */
733 Stream_Read_UINT32(s, capsSet.length); /* capsDataLength (4 bytes) */
734 Stream_Read_UINT32(s, capsSet.flags); /* capsData (4 bytes) */
735 gfx->TotalDecodedFrames = 0;
736 gfx->ConnectionCaps = capsSet;
737 if (context)
738 context->ConfirmedCaps = capsSet;
739 {
740 char buffer[128] = WINPR_C_ARRAY_INIT;
741
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)));
746 }
747 memset(gfx->allowedCodecs, TRUE, sizeof(gfx->allowedCodecs));
748
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;
754#endif
755
756 if (capsSet.version >= RDPGFX_CAPVERSION_10)
757 {
758 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
759 {
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;
763 }
764 }
765
766 if (capsSet.version >= RDPGFX_CAPVERSION_103)
767 {
768 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
769 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = TRUE;
770 }
771
772 if (capsSet.version == RDPGFX_CAPVERSION_81)
773 {
774 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC420_ENABLED) != 0)
775 {
776 WLog_Print(gfx->base.log, WLOG_INFO, "AVC420 enabled by server");
777 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
778 }
779 }
780
781#if defined(WITH_GFX_AV1)
782 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_1)
783 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
784#endif
785#if defined(WITH_GFX_AV2)
786 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_2)
787 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
788#endif
789
790#if defined(WITH_GFX_AZURE)
791 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = FALSE;
792 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = FALSE;
793
794 if (capsSet.version >= RDPGFX_CAPVERSION_112)
795 {
796 if ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_ENABLE) != 0)
797 {
798 WLog_Print(gfx->base.log, WLOG_INFO, "HEVC confirmed by server");
799 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = TRUE;
800
801 /* HEVC444 is only valid in RDPGFX_CAPVERSION_115 and needs HEVC enabled */
802 if ((capsSet.version >= RDPGFX_CAPVERSION_115) &&
803 ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_I444) != 0))
804 {
805 WLog_Print(gfx->base.log, WLOG_INFO, "HEVC I444 confirmed by server");
806 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = TRUE;
807 }
808 }
809 }
810#endif
811
812 if (!context)
813 return ERROR_BAD_CONFIGURATION;
814
815 return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
816}
817
823static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
825{
826 UINT error = 0;
827
828 if (!context || !pdu)
829 return ERROR_BAD_ARGUMENTS;
830
831 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
832
833 if (!gfx || !gfx->base.listener_callback)
834 return ERROR_BAD_CONFIGURATION;
835
836 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
837
838 if (!callback)
839 return ERROR_BAD_CONFIGURATION;
840
841 const RDPGFX_HEADER header = { .flags = 0,
842 .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
843 .pduLength = RDPGFX_HEADER_SIZE + 12 };
844
845 WLog_Print(gfx->base.log, WLOG_TRACE, "SendFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
846
847 wStream* s = Stream_New(nullptr, header.pduLength);
848
849 if (!s)
850 {
851 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
852 return CHANNEL_RC_NO_MEMORY;
853 }
854
855 if ((error = rdpgfx_write_header(s, &header)))
856 goto fail;
857
858 /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
859 Stream_Write_UINT32(s, pdu->queueDepth); /* queueDepth (4 bytes) */
860 Stream_Write_UINT32(s, pdu->frameId); /* frameId (4 bytes) */
861 Stream_Write_UINT32(s, pdu->totalFramesDecoded); /* totalFramesDecoded (4 bytes) */
862 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
863 nullptr);
864
865 if (error == CHANNEL_RC_OK) /* frame successfully acked */
866 {
867 gfx->UnacknowledgedFrames--;
868 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
869 }
870
871fail:
872 Stream_Free(s, TRUE);
873 return error;
874}
875
876static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
878{
879 UINT error = CHANNEL_RC_OK;
880 const RDPGFX_HEADER header = { .flags = 0,
881 .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
882 .pduLength = RDPGFX_HEADER_SIZE + 12 };
883
884 if (!context || !pdu)
885 return ERROR_BAD_ARGUMENTS;
886
887 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
888
889 if (!gfx || !gfx->base.listener_callback)
890 return ERROR_BAD_CONFIGURATION;
891
892 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
893
894 if (!callback)
895 return ERROR_BAD_CONFIGURATION;
896
897 WLog_Print(gfx->base.log, WLOG_TRACE, "SendQoeFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
898 wStream* s = Stream_New(nullptr, header.pduLength);
899
900 if (!s)
901 {
902 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
903 return CHANNEL_RC_NO_MEMORY;
904 }
905
906 if ((error = rdpgfx_write_header(s, &header)))
907 goto fail;
908
909 /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
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),
915 nullptr);
916fail:
917 Stream_Free(s, TRUE);
918 return error;
919}
920
926static UINT rdpgfx_recv_reset_graphics_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
927{
928 RDPGFX_RESET_GRAPHICS_PDU pdu = WINPR_C_ARRAY_INIT;
929 WINPR_ASSERT(callback);
930
931 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
932
933 WINPR_ASSERT(gfx);
934
935 RdpgfxClientContext* context = gfx->context;
936 UINT error = CHANNEL_RC_OK;
937 GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
938
939 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
940 return ERROR_INVALID_DATA;
941
942 Stream_Read_UINT32(s, pdu.width); /* width (4 bytes) */
943 Stream_Read_UINT32(s, pdu.height); /* height (4 bytes) */
944 Stream_Read_UINT32(s, pdu.monitorCount); /* monitorCount (4 bytes) */
945
946 if ((pdu.width == 0) || (pdu.height == 0) || (pdu.monitorCount == 0))
947 {
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;
953 }
954 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE) ||
955 (pdu.monitorCount > MAX_MONITOR_COUNT))
956 {
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;
962 }
963
964 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
965 return ERROR_INVALID_DATA;
966
967 pdu.monitorDefArray = (MONITOR_DEF*)calloc(pdu.monitorCount, sizeof(MONITOR_DEF));
968
969 if (!pdu.monitorDefArray)
970 {
971 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
972 return CHANNEL_RC_NO_MEMORY;
973 }
974
975 for (UINT32 index = 0; index < pdu.monitorCount; index++)
976 {
977 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
978 Stream_Read_INT32(s, monitor->left); /* left (4 bytes) */
979 Stream_Read_INT32(s, monitor->top); /* top (4 bytes) */
980 Stream_Read_INT32(s, monitor->right); /* right (4 bytes) */
981 Stream_Read_INT32(s, monitor->bottom); /* bottom (4 bytes) */
982 Stream_Read_UINT32(s, monitor->flags); /* flags (4 bytes) */
983 }
984
985 const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
986 if (size > 340)
987 {
988 free(pdu.monitorDefArray);
989 return CHANNEL_RC_NULL_DATA;
990 }
991 const size_t pad = 340ULL - size;
992
993 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (size_t)pad))
994 {
995 free(pdu.monitorDefArray);
996 return CHANNEL_RC_NO_MEMORY;
997 }
998
999 Stream_Seek(s, pad); /* pad (total size is 340 bytes) */
1000 WLog_Print(gfx->base.log, WLOG_DEBUG,
1001 "RecvResetGraphicsPdu: width: %" PRIu32 " height: %" PRIu32 " count: %" PRIu32 "",
1002 pdu.width, pdu.height, pdu.monitorCount);
1003
1004 for (UINT32 index = 0; index < pdu.monitorCount; index++)
1005 {
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);
1011 }
1012
1013 if (context)
1014 {
1015 IFCALLRET(context->ResetGraphics, error, context, &pdu);
1016
1017 if (error)
1018 WLog_Print(gfx->base.log, WLOG_ERROR,
1019 "context->ResetGraphics failed with error %" PRIu32 "", error);
1020 }
1021
1022 free(pdu.monitorDefArray);
1023
1024 /* some listeners may be interested (namely the display channel) */
1025 if (error == CHANNEL_RC_OK)
1026 {
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;
1032 }
1033 return error;
1034}
1035
1041static UINT rdpgfx_recv_evict_cache_entry_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1042{
1043 RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = WINPR_C_ARRAY_INIT;
1044 WINPR_ASSERT(callback);
1045 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1046 WINPR_ASSERT(gfx);
1047 RdpgfxClientContext* context = gfx->context;
1048 UINT error = CHANNEL_RC_OK;
1049
1050 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1051 return ERROR_INVALID_DATA;
1052
1053 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
1054 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16 "",
1055 pdu.cacheSlot);
1056
1057 if (context)
1058 {
1059 IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
1060
1061 if (error)
1062 WLog_Print(gfx->base.log, WLOG_ERROR,
1063 "context->EvictCacheEntry failed with error %" PRIu32 "", error);
1064 }
1065
1066 return error;
1067}
1068
1074static UINT rdpgfx_save_persistent_cache(RDPGFX_PLUGIN* gfx)
1075{
1076 UINT error = CHANNEL_RC_OK;
1077
1078 WINPR_ASSERT(gfx);
1079 WINPR_ASSERT(gfx->rdpcontext);
1080 rdpSettings* settings = gfx->rdpcontext->settings;
1081 RdpgfxClientContext* context = gfx->context;
1082
1083 WINPR_ASSERT(context);
1084 WINPR_ASSERT(settings);
1085
1086 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1087 return CHANNEL_RC_OK;
1088
1089 const char* BitmapCachePersistFile =
1090 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1091 if (!BitmapCachePersistFile)
1092 return CHANNEL_RC_OK;
1093
1094 if (!context->ExportCacheEntry)
1095 return CHANNEL_RC_INITIALIZATION_ERROR;
1096
1097 rdpPersistentCache* persistent = persistent_cache_new();
1098
1099 if (!persistent)
1100 return CHANNEL_RC_NO_MEMORY;
1101
1102 if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
1103 {
1104 error = CHANNEL_RC_INITIALIZATION_ERROR;
1105 goto fail;
1106 }
1107
1108 for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
1109 {
1110 if (gfx->CacheSlots[idx])
1111 {
1112 const UINT16 cacheSlot = idx;
1113 PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
1114
1115 if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
1116 continue;
1117
1118 if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
1119 goto fail;
1120 }
1121 }
1122
1123 persistent_cache_free(persistent);
1124
1125 return error;
1126fail:
1127 persistent_cache_free(persistent);
1128 return error;
1129}
1130
1136static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
1138{
1139 UINT error = CHANNEL_RC_OK;
1140
1141 if (!context || !pdu)
1142 return ERROR_BAD_ARGUMENTS;
1143
1144 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
1145
1146 if (!gfx || !gfx->base.listener_callback)
1147 return ERROR_BAD_CONFIGURATION;
1148
1149 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
1150
1151 if (!callback)
1152 return ERROR_BAD_CONFIGURATION;
1153
1154 const RDPGFX_HEADER header = { .flags = 0,
1155 .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
1156 .pduLength =
1157 RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
1158
1159 WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16 "",
1160 pdu->cacheEntriesCount);
1161 wStream* s = Stream_New(nullptr, header.pduLength);
1162
1163 if (!s)
1164 {
1165 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
1166 return CHANNEL_RC_NO_MEMORY;
1167 }
1168
1169 if ((error = rdpgfx_write_header(s, &header)))
1170 goto fail;
1171
1172 if (pdu->cacheEntriesCount <= 0)
1173 {
1174 WLog_Print(gfx->base.log, WLOG_ERROR, "Invalid cacheEntriesCount: %" PRIu16 "",
1175 pdu->cacheEntriesCount);
1176 error = ERROR_INVALID_DATA;
1177 goto fail;
1178 }
1179
1180 /* cacheEntriesCount (2 bytes) */
1181 Stream_Write_UINT16(s, pdu->cacheEntriesCount);
1182
1183 for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
1184 {
1185 const RDPGFX_CACHE_ENTRY_METADATA* cacheEntry = &(pdu->cacheEntries[index]);
1186 Stream_Write_UINT64(s, cacheEntry->cacheKey); /* cacheKey (8 bytes) */
1187 Stream_Write_UINT32(s, cacheEntry->bitmapLength); /* bitmapLength (4 bytes) */
1188 }
1189
1190 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
1191 nullptr);
1192
1193fail:
1194 Stream_Free(s, TRUE);
1195 return error;
1196}
1197
1203static UINT rdpgfx_send_cache_offer(RDPGFX_PLUGIN* gfx)
1204{
1205 int count = 0;
1206 UINT error = CHANNEL_RC_OK;
1207 PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1208 RDPGFX_CACHE_IMPORT_OFFER_PDU* offer = nullptr;
1209
1210 WINPR_ASSERT(gfx);
1211 WINPR_ASSERT(gfx->rdpcontext);
1212
1213 RdpgfxClientContext* context = gfx->context;
1214 rdpSettings* settings = gfx->rdpcontext->settings;
1215
1216 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1217 return CHANNEL_RC_OK;
1218
1219 const char* BitmapCachePersistFile =
1220 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1221 if (!BitmapCachePersistFile)
1222 return CHANNEL_RC_OK;
1223
1224 rdpPersistentCache* persistent = persistent_cache_new();
1225
1226 if (!persistent)
1227 return CHANNEL_RC_NO_MEMORY;
1228
1229 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1230 {
1231 error = CHANNEL_RC_INITIALIZATION_ERROR;
1232 goto fail;
1233 }
1234
1235 if (persistent_cache_get_version(persistent) != 3)
1236 {
1237 error = ERROR_INVALID_DATA;
1238 goto fail;
1239 }
1240
1241 count = persistent_cache_get_count(persistent);
1242 if (count < 0)
1243 {
1244 error = ERROR_INVALID_DATA;
1245 goto fail;
1246 }
1247
1248 if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
1249 count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
1250
1251 if (count > gfx->MaxCacheSlots)
1252 count = gfx->MaxCacheSlots;
1253
1255 if (!offer)
1256 {
1257 error = CHANNEL_RC_NO_MEMORY;
1258 goto fail;
1259 }
1260
1261 WINPR_ASSERT(count <= UINT16_MAX);
1262 offer->cacheEntriesCount = (UINT16)count;
1263
1264 WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending Cache Import Offer: %d", count);
1265
1266 for (int idx = 0; idx < count; idx++)
1267 {
1268 if (persistent_cache_read_entry(persistent, &entry) < 1)
1269 {
1270 error = ERROR_INVALID_DATA;
1271 goto fail;
1272 }
1273
1274 offer->cacheEntries[idx].cacheKey = entry.key64;
1275 offer->cacheEntries[idx].bitmapLength = entry.size;
1276 }
1277
1278 if (offer->cacheEntriesCount > 0)
1279 {
1280 error = rdpgfx_send_cache_import_offer_pdu(context, offer);
1281 if (error != CHANNEL_RC_OK)
1282 {
1283 WLog_Print(gfx->base.log, WLOG_ERROR, "Failed to send cache import offer PDU");
1284 goto fail;
1285 }
1286 }
1287
1288fail:
1289 persistent_cache_free(persistent);
1290 free(offer);
1291 return error;
1292}
1293
1299static UINT rdpgfx_load_cache_import_reply(RDPGFX_PLUGIN* gfx,
1300 const RDPGFX_CACHE_IMPORT_REPLY_PDU* reply)
1301{
1302 UINT error = CHANNEL_RC_OK;
1303 rdpPersistentCache* persistent = nullptr;
1304 WINPR_ASSERT(gfx);
1305 WINPR_ASSERT(gfx->rdpcontext);
1306 rdpSettings* settings = gfx->rdpcontext->settings;
1307 RdpgfxClientContext* context = gfx->context;
1308
1309 WINPR_ASSERT(settings);
1310 WINPR_ASSERT(reply);
1311 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1312 return CHANNEL_RC_OK;
1313
1314 const char* BitmapCachePersistFile =
1315 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1316 if (!BitmapCachePersistFile)
1317 return CHANNEL_RC_OK;
1318
1319 persistent = persistent_cache_new();
1320
1321 if (!persistent)
1322 return CHANNEL_RC_NO_MEMORY;
1323
1324 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1325 {
1326 error = CHANNEL_RC_INITIALIZATION_ERROR;
1327 goto fail;
1328 }
1329
1330 if (persistent_cache_get_version(persistent) != 3)
1331 {
1332 error = ERROR_INVALID_DATA;
1333 goto fail;
1334 }
1335
1336 int count = persistent_cache_get_count(persistent);
1337
1338 count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
1339
1340 WLog_Print(gfx->base.log, WLOG_DEBUG, "Receiving Cache Import Reply: %d", count);
1341
1342 for (int idx = 0; idx < count; idx++)
1343 {
1344 PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1345 if (persistent_cache_read_entry(persistent, &entry) < 1)
1346 {
1347 error = ERROR_INVALID_DATA;
1348 goto fail;
1349 }
1350
1351 const UINT16 cacheSlot = reply->cacheSlots[idx];
1352 if (context && context->ImportCacheEntry)
1353 {
1354 error = context->ImportCacheEntry(context, cacheSlot, &entry);
1355 if (error != CHANNEL_RC_OK)
1356 break;
1357 }
1358 }
1359
1360 persistent_cache_free(persistent);
1361
1362 return error;
1363fail:
1364 persistent_cache_free(persistent);
1365 return error;
1366}
1367
1373static UINT rdpgfx_recv_cache_import_reply_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1374{
1375 RDPGFX_CACHE_IMPORT_REPLY_PDU pdu = WINPR_C_ARRAY_INIT;
1376 WINPR_ASSERT(callback);
1377 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1378 WINPR_ASSERT(gfx);
1379 RdpgfxClientContext* context = gfx->context;
1380 UINT error = CHANNEL_RC_OK;
1381
1382 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1383 return ERROR_INVALID_DATA;
1384
1385 Stream_Read_UINT16(s, pdu.importedEntriesCount); /* cacheSlot (2 bytes) */
1386
1387 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1388 2ull))
1389 return ERROR_INVALID_DATA;
1390
1391 if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1392 return ERROR_INVALID_DATA;
1393
1394 for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1395 {
1396 Stream_Read_UINT16(s, pdu.cacheSlots[idx]); /* cacheSlot (2 bytes) */
1397 }
1398
1399 WLog_Print(gfx->base.log, WLOG_TRACE,
1400 "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16 "",
1401 pdu.importedEntriesCount);
1402
1403 error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1404
1405 if (error)
1406 {
1407 WLog_Print(gfx->base.log, WLOG_ERROR,
1408 "rdpgfx_load_cache_import_reply failed with error %" PRIu32 "", error);
1409 return error;
1410 }
1411
1412 if (context)
1413 {
1414 IFCALLRET(context->CacheImportReply, error, context, &pdu);
1415
1416 if (error)
1417 WLog_Print(gfx->base.log, WLOG_ERROR,
1418 "context->CacheImportReply failed with error %" PRIu32 "", error);
1419 }
1420
1421 return error;
1422}
1423
1429static UINT rdpgfx_recv_create_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1430{
1431 RDPGFX_CREATE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1432 WINPR_ASSERT(callback);
1433 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1434 WINPR_ASSERT(gfx);
1435 RdpgfxClientContext* context = gfx->context;
1436 UINT error = CHANNEL_RC_OK;
1437
1438 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1439 return ERROR_INVALID_DATA;
1440
1441 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1442 Stream_Read_UINT16(s, pdu.width); /* width (2 bytes) */
1443 Stream_Read_UINT16(s, pdu.height); /* height (2 bytes) */
1444 Stream_Read_UINT8(s, pdu.pixelFormat); /* RDPGFX_PIXELFORMAT (1 byte) */
1445
1446 if ((pdu.width == 0) || (pdu.height == 0))
1447 {
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;
1452 }
1453 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE))
1454 {
1455 WLog_Print(gfx->base.log, WLOG_ERROR,
1456 "context->RecvCreateSurfacePdu Width[%" PRIu32 "], Height[%" PRIu32
1457 "] > %" PRIu32,
1458 pdu.width, pdu.height, MAX_SURFACE_SIZE);
1459 return ERROR_INVALID_DATA;
1460 }
1461
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);
1466
1467 if (context)
1468 {
1469 /* create surface PDU sometimes happens for surface ID that are already in use and have not
1470 * been removed yet. Ensure that there is no surface with the new ID by trying to remove it
1471 * manually.
1472 */
1473 RDPGFX_DELETE_SURFACE_PDU deletePdu = { pdu.surfaceId };
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);
1478
1479 IFCALLRET(context->CreateSurface, error, context, &pdu);
1480
1481 if (error)
1482 WLog_Print(gfx->base.log, WLOG_ERROR,
1483 "context->CreateSurface failed with error %" PRIu32 "", error);
1484 }
1485
1486 return error;
1487}
1488
1494static UINT rdpgfx_recv_delete_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1495{
1496 RDPGFX_DELETE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1497 WINPR_ASSERT(callback);
1498 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1499 WINPR_ASSERT(gfx);
1500 RdpgfxClientContext* context = gfx->context;
1501 UINT error = CHANNEL_RC_OK;
1502
1503 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1504 return ERROR_INVALID_DATA;
1505
1506 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1507 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvDeleteSurfacePdu: surfaceId: %" PRIu16 "",
1508 pdu.surfaceId);
1509
1510 if (context)
1511 {
1512 IFCALLRET(context->DeleteSurface, error, context, &pdu);
1513
1514 if (error)
1515 WLog_Print(gfx->base.log, WLOG_ERROR,
1516 "context->DeleteSurface failed with error %" PRIu32 "", error);
1517 }
1518
1519 return error;
1520}
1521
1527static UINT rdpgfx_recv_start_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1528{
1529 RDPGFX_START_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1530 WINPR_ASSERT(callback);
1531 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1532 WINPR_ASSERT(gfx);
1533 RdpgfxClientContext* context = gfx->context;
1534 UINT error = CHANNEL_RC_OK;
1535
1536 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1537 return ERROR_INVALID_DATA;
1538
1539 Stream_Read_UINT32(s, pdu.timestamp); /* timestamp (4 bytes) */
1540 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1541 WLog_Print(gfx->base.log, WLOG_TRACE,
1542 "RecvStartFramePdu: frameId: %" PRIu32 " timestamp: 0x%08" PRIX32 "", pdu.frameId,
1543 pdu.timestamp);
1544 gfx->StartDecodingTime = GetTickCount64();
1545
1546 if (context)
1547 {
1548 IFCALLRET(context->StartFrame, error, context, &pdu);
1549
1550 if (error)
1551 WLog_Print(gfx->base.log, WLOG_ERROR,
1552 "context->StartFrame failed with error %" PRIu32 "", error);
1553 }
1554
1555 gfx->UnacknowledgedFrames++;
1556 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
1557 return error;
1558}
1559
1565static UINT rdpgfx_recv_end_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1566{
1567 RDPGFX_END_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1568 RDPGFX_FRAME_ACKNOWLEDGE_PDU ack = WINPR_C_ARRAY_INIT;
1569 WINPR_ASSERT(callback);
1570 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1571 WINPR_ASSERT(gfx);
1572 RdpgfxClientContext* context = gfx->context;
1573 UINT error = CHANNEL_RC_OK;
1574
1575 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1576 return ERROR_INVALID_DATA;
1577
1578 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1579 WLog_Print(gfx->base.log, WLOG_TRACE, "RecvEndFramePdu: frameId: %" PRIu32 "", pdu.frameId);
1580
1581 const UINT64 start = GetTickCount64();
1582 if (context)
1583 {
1584 IFCALLRET(context->EndFrame, error, context, &pdu);
1585
1586 if (error)
1587 {
1588 WLog_Print(gfx->base.log, WLOG_ERROR, "context->EndFrame failed with error %" PRIu32 "",
1589 error);
1590 return error;
1591 }
1592 }
1593 const UINT64 end = GetTickCount64();
1594 const UINT64 EndFrameTime = end - start;
1595 gfx->TotalDecodedFrames++;
1596 rdpgfx_stats_frame_decoded(context);
1597
1598 if (!gfx->sendFrameAcks)
1599 return error;
1600
1601 ack.frameId = pdu.frameId;
1602 ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1603
1604 if (gfx->suspendFrameAcks)
1605 {
1606 ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1607
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 "",
1612 error);
1613 }
1614 else
1615 {
1616 ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1617
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);
1621 }
1622
1623 switch (gfx->ConnectionCaps.version)
1624 {
1625#if defined(WITH_GFX_AV1)
1626 case RDPGFX_CAPVERSION_FRDP_1:
1627#endif
1628#if defined(WITH_GFX_AV2)
1629 case RDPGFX_CAPVERSION_FRDP_2:
1630#endif
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:
1644#endif
1645 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSendQoeAck))
1646 {
1647 RDPGFX_QOE_FRAME_ACKNOWLEDGE_PDU qoe = WINPR_C_ARRAY_INIT;
1648 UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1649
1650 if (diff > 65000)
1651 diff = 0;
1652
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);
1657
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
1661 "",
1662 error);
1663 }
1664
1665 break;
1666
1667 default:
1668 break;
1669 }
1670
1671 return error;
1672}
1673
1681static UINT rdpgfx_recv_caps_reset_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1682{
1683 WINPR_ASSERT(callback);
1684 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1685 WINPR_ASSERT(gfx);
1686
1687 /* the PDU has no body, tolerate one */
1688 if (!Stream_SafeSeek(s, Stream_GetRemainingLength(s)))
1689 return ERROR_INVALID_DATA;
1690
1691 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvCapsResetPdu: sending the capabilities again");
1692
1693 free_surfaces(gfx->context, gfx->SurfaceTable);
1694 const UINT error = evict_cache_slots(gfx->context, gfx->MaxCacheSlots, gfx->CacheSlots);
1695 if (error)
1696 {
1697 // print error, but don't consider this a hard failure
1698 WLog_Print(gfx->base.log, WLOG_ERROR, "evict_cache_slots failed with error %" PRIu32 "",
1699 error);
1700 }
1701 gfx->UnacknowledgedFrames = 0;
1702 gfx->TotalDecodedFrames = 0;
1703
1704 return rdpgfx_send_supported_caps(callback);
1705}
1706
1707WINPR_ATTR_NODISCARD
1708static BOOL rdpgfx_codec_has_region_data_embedded(UINT16 codecId)
1709{
1710 switch (codecId)
1711 {
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:
1718#endif
1719#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1720 case RDPGFX_CODECID_AV1:
1721#endif
1722 return TRUE;
1723 default:
1724 return FALSE;
1725 }
1726}
1727
1733static UINT rdpgfx_recv_wire_to_surface_1_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1734{
1735 RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1736 RDPGFX_WIRE_TO_SURFACE_PDU_1 pdu = WINPR_C_ARRAY_INIT;
1737 WINPR_ASSERT(callback);
1738 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1739 UINT error = 0;
1740
1741 WINPR_ASSERT(gfx);
1742 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1743 return ERROR_INVALID_DATA;
1744
1745 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1746 Stream_Read_UINT16(s, pdu.codecId); /* codecId (2 bytes) */
1747 Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1748
1749 if ((error = rdpgfx_read_rect16(
1750 gfx->base.log, s, &(pdu.destRect),
1751 rdpgfx_codec_has_region_data_embedded(pdu.codecId)))) /* destRect (8 bytes) */
1752 {
1753 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "",
1754 error);
1755 return error;
1756 }
1757
1758 Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1759
1760 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1761 return ERROR_INVALID_DATA;
1762
1763 pdu.bitmapData = Stream_Pointer(s);
1764 Stream_Seek(s, pdu.bitmapDataLength);
1765
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;
1776 cmd.contextId = 0;
1777
1778 switch (pdu.pixelFormat)
1779 {
1780 case GFX_PIXEL_FORMAT_XRGB_8888:
1781 cmd.format = PIXEL_FORMAT_BGRX32;
1782 break;
1783
1784 case GFX_PIXEL_FORMAT_ARGB_8888:
1785 cmd.format = PIXEL_FORMAT_BGRA32;
1786 break;
1787
1788 default:
1789 return ERROR_INVALID_DATA;
1790 }
1791
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;
1801
1802 if (cmd.right < cmd.left)
1803 {
1804 WLog_Print(gfx->base.log, WLOG_ERROR,
1805 "RecvWireToSurface1Pdu right=%" PRIu32 " < left=%" PRIu32, cmd.right, cmd.left);
1806 return ERROR_INVALID_DATA;
1807 }
1808 if (cmd.bottom < cmd.top)
1809 {
1810 WLog_Print(gfx->base.log, WLOG_ERROR,
1811 "RecvWireToSurface1Pdu bottom=%" PRIu32 " < top=%" PRIu32, cmd.bottom, cmd.top);
1812 return ERROR_INVALID_DATA;
1813 }
1814
1815 if ((error = rdpgfx_decode(gfx, &cmd)))
1816 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_decode failed with error %" PRIu32 "!",
1817 error);
1818
1819 return error;
1820}
1821
1822WINPR_ATTR_NODISCARD
1823static BOOL checkSurfaceCommand(const RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1824{
1825 switch (cmd->codecId)
1826 {
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:
1839#endif
1840#if defined(WITH_GFX_AZURE)
1841 case RDPGFX_CODECID_HEVC420:
1842 case RDPGFX_CODECID_HEVC444:
1843#endif
1844 if (!gfx->allowedCodecs[cmd->codecId])
1845 {
1846 char buffer[128] = WINPR_C_ARRAY_INIT;
1847 WLog_Print(
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)));
1853 return FALSE;
1854 }
1855 return TRUE;
1856 default:
1857 WLog_Print(gfx->base.log, WLOG_ERROR,
1858 "Unknown RDPGFX_SURFACE_COMMAND::codecId %" PRIu32, cmd->codecId);
1859 return FALSE;
1860 }
1861}
1862
1863UINT logSurfaceCommand(RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1864{
1865 WINPR_ASSERT(gfx);
1866
1867 WLog_Print(gfx->base.log, WLOG_DEBUG, "Got GFX %s",
1868 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)));
1869
1870 RdpgfxClientContext* context = gfx->context;
1871 if (!context)
1872 return CHANNEL_RC_OK;
1873
1874 if (!checkSurfaceCommand(gfx, cmd))
1875 return CHANNEL_RC_NULL_DATA;
1876
1877 const UINT error = IFCALLRESULT(CHANNEL_RC_OK, context->SurfaceCommand, context, cmd);
1878
1879 if (error)
1880 WLog_Print(gfx->base.log, WLOG_ERROR,
1881 "context->SurfaceCommand failed with error %" PRIu32 "", error);
1882
1883 rdpgfx_codecid_event(context, cmd->codecId, cmd->length);
1884 return error;
1885}
1886
1892static UINT rdpgfx_recv_wire_to_surface_2_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1893{
1894 RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1895 RDPGFX_WIRE_TO_SURFACE_PDU_2 pdu = WINPR_C_ARRAY_INIT;
1896
1897 WINPR_ASSERT(callback);
1898 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1899 WINPR_ASSERT(gfx);
1900
1901 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1902 return ERROR_INVALID_DATA;
1903
1904 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1905 Stream_Read_UINT16(s, pdu.codecId); /* codecId (2 bytes) */
1906 Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1907 Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1908 Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1909 pdu.bitmapData = Stream_Pointer(s);
1910 if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1911 return ERROR_INVALID_DATA;
1912
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
1916 "",
1917 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1918 pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1919
1920 cmd.surfaceId = pdu.surfaceId;
1921 cmd.codecId = pdu.codecId;
1922 cmd.contextId = pdu.codecContextId;
1923
1924 switch (pdu.pixelFormat)
1925 {
1926 case GFX_PIXEL_FORMAT_XRGB_8888:
1927 cmd.format = PIXEL_FORMAT_BGRX32;
1928 break;
1929
1930 case GFX_PIXEL_FORMAT_ARGB_8888:
1931 cmd.format = PIXEL_FORMAT_BGRA32;
1932 break;
1933
1934 default:
1935 return ERROR_INVALID_DATA;
1936 }
1937
1938 cmd.length = pdu.bitmapDataLength;
1939 cmd.data = pdu.bitmapData;
1940 cmd.extra = nullptr;
1941
1942 if (cmd.codecId != RDPGFX_CODECID_CAPROGRESSIVE)
1943 return ERROR_INVALID_DATA;
1944
1945 return logSurfaceCommand(gfx, &cmd);
1946}
1947
1953static UINT rdpgfx_recv_delete_encoding_context_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1954{
1955 RDPGFX_DELETE_ENCODING_CONTEXT_PDU pdu = WINPR_C_ARRAY_INIT;
1956 WINPR_ASSERT(callback);
1957 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1958 WINPR_ASSERT(gfx);
1959 RdpgfxClientContext* context = gfx->context;
1960 UINT error = CHANNEL_RC_OK;
1961
1962 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1963 return ERROR_INVALID_DATA;
1964
1965 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1966 Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1967
1968 WLog_Print(gfx->base.log, WLOG_DEBUG,
1969 "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16 " codecContextId: %" PRIu32 "",
1970 pdu.surfaceId, pdu.codecContextId);
1971
1972 if (context)
1973 {
1974 IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1975
1976 if (error)
1977 WLog_Print(gfx->base.log, WLOG_ERROR,
1978 "context->DeleteEncodingContext failed with error %" PRIu32 "", error);
1979 }
1980
1981 return error;
1982}
1983
1989static UINT rdpgfx_recv_solid_fill_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1990{
1991 RDPGFX_SOLID_FILL_PDU pdu = WINPR_C_ARRAY_INIT;
1992 WINPR_ASSERT(callback);
1993 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1994 WINPR_ASSERT(gfx);
1995 RdpgfxClientContext* context = gfx->context;
1996
1997 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1998 return ERROR_INVALID_DATA;
1999
2000 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2001
2002 UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
2003 if (error != CHANNEL_RC_OK) /* fillPixel (4 bytes) */
2004 {
2005 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_color32 failed with error %" PRIu32 "!",
2006 error);
2007 return error;
2008 }
2009
2010 Stream_Read_UINT16(s, pdu.fillRectCount); /* fillRectCount (2 bytes) */
2011
2012 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
2013 return ERROR_INVALID_DATA;
2014
2015 pdu.fillRects = (RECTANGLE_16*)calloc(pdu.fillRectCount, sizeof(RECTANGLE_16));
2016
2017 if (!pdu.fillRects)
2018 {
2019 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2020 return CHANNEL_RC_NO_MEMORY;
2021 }
2022
2023 for (UINT16 index = 0; index < pdu.fillRectCount; index++)
2024 {
2025 RECTANGLE_16* fillRect = &(pdu.fillRects[index]);
2026
2027 if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect, FALSE)))
2028 {
2029 WLog_Print(gfx->base.log, WLOG_ERROR,
2030 "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error);
2031 free(pdu.fillRects);
2032 return error;
2033 }
2034 }
2035 WLog_Print(gfx->base.log, WLOG_TRACE,
2036 "RecvSolidFillPdu: surfaceId: %" PRIu16 " fillRectCount: %" PRIu16 "", pdu.surfaceId,
2037 pdu.fillRectCount);
2038
2039 if (context)
2040 {
2041 IFCALLRET(context->SolidFill, error, context, &pdu);
2042
2043 if (error)
2044 WLog_Print(gfx->base.log, WLOG_ERROR,
2045 "context->SolidFill failed with error %" PRIu32 "", error);
2046 }
2047
2048 free(pdu.fillRects);
2049 return error;
2050}
2051
2057static UINT rdpgfx_recv_surface_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2058{
2059 RDPGFX_SURFACE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
2060 WINPR_ASSERT(callback);
2061 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2062 WINPR_ASSERT(gfx);
2063 RdpgfxClientContext* context = gfx->context;
2064 UINT error = 0;
2065
2066 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
2067 return ERROR_INVALID_DATA;
2068
2069 Stream_Read_UINT16(s, pdu.surfaceIdSrc); /* surfaceIdSrc (2 bytes) */
2070 Stream_Read_UINT16(s, pdu.surfaceIdDest); /* surfaceIdDest (2 bytes) */
2071
2072 if ((error =
2073 rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE))) /* rectSrc (8 bytes ) */
2074 {
2075 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
2076 error);
2077 return error;
2078 }
2079
2080 Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
2081
2082 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2083 return ERROR_INVALID_DATA;
2084
2085 pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
2086
2087 if (!pdu.destPts)
2088 {
2089 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2090 return CHANNEL_RC_NO_MEMORY;
2091 }
2092
2093 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2094 {
2095 RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
2096
2097 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2098 {
2099 WLog_Print(gfx->base.log, WLOG_ERROR,
2100 "rdpgfx_read_point16 failed with error %" PRIu32 "!", error);
2101 free(pdu.destPts);
2102 return error;
2103 }
2104 }
2105
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);
2112
2113 if (context)
2114 {
2115 IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
2116
2117 if (error)
2118 WLog_Print(gfx->base.log, WLOG_ERROR,
2119 "context->SurfaceToSurface failed with error %" PRIu32 "", error);
2120 }
2121
2122 free(pdu.destPts);
2123 return error;
2124}
2125
2131static UINT rdpgfx_recv_surface_to_cache_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2132{
2133 RDPGFX_SURFACE_TO_CACHE_PDU pdu = WINPR_C_ARRAY_INIT;
2134 WINPR_ASSERT(callback);
2135 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2136
2137 WINPR_ASSERT(gfx);
2138 RdpgfxClientContext* context = gfx->context;
2139
2140 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2141 return ERROR_INVALID_DATA;
2142
2143 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2144 Stream_Read_UINT64(s, pdu.cacheKey); /* cacheKey (8 bytes) */
2145 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
2146
2147 UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE);
2148 if (error != CHANNEL_RC_OK) /* rectSrc (8 bytes ) */
2149 {
2150 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
2151 error);
2152 return error;
2153 }
2154
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);
2161
2162 if (context)
2163 {
2164 IFCALLRET(context->SurfaceToCache, error, context, &pdu);
2165
2166 if (error)
2167 WLog_Print(gfx->base.log, WLOG_ERROR,
2168 "context->SurfaceToCache failed with error %" PRIu32 "", error);
2169 }
2170
2171 return error;
2172}
2173
2179static UINT rdpgfx_recv_cache_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2180{
2181 RDPGFX_CACHE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
2182 WINPR_ASSERT(callback);
2183 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2184
2185 WINPR_ASSERT(gfx);
2186 RdpgfxClientContext* context = gfx->context;
2187 UINT error = CHANNEL_RC_OK;
2188
2189 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
2190 return ERROR_INVALID_DATA;
2191
2192 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
2193 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2194 Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
2195
2196 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2197 return ERROR_INVALID_DATA;
2198
2199 pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
2200
2201 if (!pdu.destPts)
2202 {
2203 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2204 return CHANNEL_RC_NO_MEMORY;
2205 }
2206
2207 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2208 {
2209 RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
2210
2211 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2212 {
2213 WLog_Print(gfx->base.log, WLOG_ERROR,
2214 "rdpgfx_read_point16 failed with error %" PRIu32 "", error);
2215 free(pdu.destPts);
2216 return error;
2217 }
2218 }
2219
2220 WLog_Print(gfx->base.log, WLOG_TRACE,
2221 "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16 " surfaceId: %" PRIu16
2222 " destPtsCount: %" PRIu16 "",
2223 pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
2224
2225 if (context)
2226 {
2227 IFCALLRET(context->CacheToSurface, error, context, &pdu);
2228
2229 if (error)
2230 WLog_Print(gfx->base.log, WLOG_ERROR,
2231 "context->CacheToSurface failed with error %" PRIu32 "", error);
2232 }
2233
2234 free(pdu.destPts);
2235 return error;
2236}
2237
2243static UINT rdpgfx_recv_map_surface_to_output_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2244{
2245 RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
2246 WINPR_ASSERT(callback);
2247 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2248
2249 WINPR_ASSERT(gfx);
2250 RdpgfxClientContext* context = gfx->context;
2251 UINT error = CHANNEL_RC_OK;
2252
2253 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
2254 return ERROR_INVALID_DATA;
2255
2256 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2257 Stream_Read_UINT16(s, pdu.reserved); /* reserved (2 bytes) */
2258 Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
2259 Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
2260
2261 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2262 {
2263 WLog_Print(gfx->base.log, WLOG_ERROR,
2264 "context->MapSurfaceToOutput outputOriginX[%" PRIu32 "], outputOriginY[%" PRIu32
2265 "] > %" PRIu32,
2266 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2267 return ERROR_INVALID_DATA;
2268 }
2269
2270 WLog_Print(gfx->base.log, WLOG_DEBUG,
2271 "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
2272 " outputOriginY: %" PRIu32 "",
2273 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
2274
2275 if (context)
2276 {
2277 IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
2278
2279 if (error)
2280 WLog_Print(gfx->base.log, WLOG_ERROR,
2281 "context->MapSurfaceToOutput failed with error %" PRIu32 "", error);
2282 }
2283
2284 return error;
2285}
2286
2287static UINT rdpgfx_recv_map_surface_to_scaled_output_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2288 wStream* s)
2289{
2290 RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
2291 WINPR_ASSERT(callback);
2292 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2293
2294 WINPR_ASSERT(gfx);
2295 RdpgfxClientContext* context = gfx->context;
2296 UINT error = CHANNEL_RC_OK;
2297
2298 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2299 return ERROR_INVALID_DATA;
2300
2301 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2302 Stream_Read_UINT16(s, pdu.reserved); /* reserved (2 bytes) */
2303 Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
2304 Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
2305 Stream_Read_UINT32(s, pdu.targetWidth); /* targetWidth (4 bytes) */
2306 Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
2307
2308 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2309 {
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;
2315 }
2316 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2317 {
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;
2323 }
2324
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,
2329 pdu.targetHeight);
2330
2331 if (context)
2332 {
2333 IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
2334
2335 if (error)
2336 WLog_Print(gfx->base.log, WLOG_ERROR,
2337 "context->MapSurfaceToScaledOutput failed with error %" PRIu32 "", error);
2338 }
2339
2340 return error;
2341}
2342
2348static UINT rdpgfx_recv_map_surface_to_window_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2349{
2350 RDPGFX_MAP_SURFACE_TO_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2351 WINPR_ASSERT(callback);
2352 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2353
2354 WINPR_ASSERT(gfx);
2355 RdpgfxClientContext* context = gfx->context;
2356 UINT error = CHANNEL_RC_OK;
2357
2358 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
2359 return ERROR_INVALID_DATA;
2360
2361 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2362 Stream_Read_UINT64(s, pdu.windowId); /* windowId (8 bytes) */
2363 Stream_Read_UINT32(s, pdu.mappedWidth); /* mappedWidth (4 bytes) */
2364 Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2365
2366 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2367 {
2368 WLog_Print(gfx->base.log, WLOG_ERROR,
2369 "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2370 "] is 0",
2371 pdu.mappedWidth, pdu.mappedHeight);
2372 return ERROR_INVALID_DATA;
2373 }
2374 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2375 {
2376 WLog_Print(gfx->base.log, WLOG_ERROR,
2377 "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2378 "] > %" PRIu32,
2379 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2380 return ERROR_INVALID_DATA;
2381 }
2382
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);
2387
2388 if (context && context->MapSurfaceToWindow)
2389 {
2390 IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
2391
2392 if (error)
2393 WLog_Print(gfx->base.log, WLOG_ERROR,
2394 "context->MapSurfaceToWindow failed with error %" PRIu32 "", error);
2395 }
2396
2397 return error;
2398}
2399
2400static UINT rdpgfx_recv_map_surface_to_scaled_window_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2401 wStream* s)
2402{
2403 RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2404 WINPR_ASSERT(callback);
2405 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2406 WINPR_ASSERT(gfx);
2407 RdpgfxClientContext* context = gfx->context;
2408 UINT error = CHANNEL_RC_OK;
2409
2410 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
2411 return ERROR_INVALID_DATA;
2412
2413 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2414 Stream_Read_UINT64(s, pdu.windowId); /* windowId (8 bytes) */
2415 Stream_Read_UINT32(s, pdu.mappedWidth); /* mappedWidth (4 bytes) */
2416 Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2417 Stream_Read_UINT32(s, pdu.targetWidth); /* targetWidth (4 bytes) */
2418 Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
2419
2420 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2421 {
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;
2427 }
2428 if ((pdu.targetWidth == 0) || (pdu.targetHeight == 0))
2429 {
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;
2435 }
2436
2437 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2438 {
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;
2444 }
2445 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2446 {
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;
2452 }
2453
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,
2459 pdu.targetHeight);
2460
2461 if (context && context->MapSurfaceToScaledWindow)
2462 {
2463 IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
2464
2465 if (error)
2466 WLog_Print(gfx->base.log, WLOG_ERROR,
2467 "context->MapSurfaceToScaledWindow failed with error %" PRIu32 "", error);
2468 }
2469
2470 return error;
2471}
2472
2478static UINT rdpgfx_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
2479{
2480 RDPGFX_HEADER header = WINPR_C_ARRAY_INIT;
2481 wStream sbuffer = WINPR_C_ARRAY_INIT;
2482
2483 WINPR_ASSERT(callback);
2484 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2485
2486 WINPR_ASSERT(gfx);
2487
2488 size_t packetlen = 0;
2489 UINT error = rdpgfx_read_header(gfx->base.log, data, &header, &packetlen);
2490 if (error != CHANNEL_RC_OK)
2491 {
2492 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_header failed with error %" PRIu32 "!",
2493 error);
2494 return error;
2495 }
2496
2497 /* Limit the PDU parsers to the data of this PDU, the stream might contain more of them */
2498 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
2499 Stream_Seek(data, packetlen);
2500
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,
2504 header.pduLength);
2505
2506 rdpgfx_stats_cmdid_event(gfx->context, header.cmdId);
2507 switch (header.cmdId)
2508 {
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 "!",
2513 error);
2514
2515 break;
2516
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 "!",
2521 error);
2522
2523 break;
2524
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 "!",
2529 error);
2530
2531 break;
2532
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);
2537
2538 break;
2539
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 "!",
2544 error);
2545
2546 break;
2547
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 "!",
2552 error);
2553
2554 break;
2555
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 "!",
2560 error);
2561
2562 break;
2563
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 "!",
2568 error);
2569
2570 break;
2571
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);
2576
2577 break;
2578
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);
2583
2584 break;
2585
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);
2590
2591 break;
2592
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);
2597
2598 break;
2599
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);
2604
2605 break;
2606
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 "!",
2611 error);
2612
2613 break;
2614
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 "!",
2619 error);
2620
2621 break;
2622
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);
2627
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);
2631
2632 break;
2633
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 "!",
2638 error);
2639
2640 break;
2641
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
2646 "!",
2647 error);
2648
2649 break;
2650
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
2655 "!",
2656 error);
2657
2658 break;
2659
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;
2667 break;
2668
2669 case RDPGFX_CMDID_UNUSED_0014:
2670 /* servers send diagnostic information with this command, it carries no graphics */
2671 WLog_Print(gfx->base.log, WLOG_DEBUG,
2672 "Diagnostic information from the server, skipping %" PRIu32 " bytes",
2673 header.pduLength);
2674 if (!Stream_SafeSeek(s, packetlen))
2675 error = ERROR_INVALID_DATA;
2676 break;
2677
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);
2682 break;
2683
2684 default:
2685 error = CHANNEL_RC_BAD_PROC;
2686 break;
2687 }
2688
2689 if (error)
2690 {
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);
2694 return error;
2695 }
2696
2697 const size_t end = Stream_GetPosition(s);
2698
2699 if (end != packetlen)
2700 {
2701 WLog_Print(gfx->base.log, WLOG_ERROR,
2702 "Unexpected gfx pdu end: Actual: %" PRIuz ", Expected: %" PRIuz, end, packetlen);
2703 }
2704
2705 return error;
2706}
2707
2713static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
2714{
2715 UINT32 DstSize = 0;
2716 BYTE* pDstData = nullptr;
2717 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2718 WINPR_ASSERT(callback);
2719 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2720 UINT error = CHANNEL_RC_OK;
2721
2722 WINPR_ASSERT(gfx);
2723 int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2724 (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2725
2726 if ((status < 0) || (DstSize == 0))
2727 {
2728 WLog_Print(gfx->base.log, WLOG_ERROR,
2729 "zgfx_decompress failure! status: %d, DstSize=%" PRIu32, status, DstSize);
2730 free(pDstData);
2731 return ERROR_INTERNAL_ERROR;
2732 }
2733
2734 wStream sbuffer = WINPR_C_ARRAY_INIT;
2735 wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2736
2737 if (!s)
2738 {
2739 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2740 free(pDstData);
2741 return CHANNEL_RC_NO_MEMORY;
2742 }
2743
2744 while (Stream_GetPosition(s) < Stream_Length(s))
2745 {
2746 if ((error = rdpgfx_recv_pdu(callback, s)))
2747 {
2748 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_recv_pdu failed with error %" PRIu32 "!",
2749 error);
2750 break;
2751 }
2752 }
2753
2754 free(pDstData);
2755 return error;
2756}
2757
2763static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2764{
2765 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2766 WINPR_ASSERT(callback);
2767 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2768 WINPR_ASSERT(gfx);
2769 RdpgfxClientContext* context = gfx->context;
2770 UINT error = CHANNEL_RC_OK;
2771 BOOL do_caps_advertise = TRUE;
2772
2773 gfx->sendFrameAcks = TRUE;
2774
2775 if (context)
2776 {
2777 IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2778
2779 if (error)
2780 WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnOpen failed with error %" PRIu32 "",
2781 error);
2782
2783 RdpgfxClientContextInt* ctx = (RdpgfxClientContextInt*)context;
2784 const RdpgfxClientContextStats empty = WINPR_C_ARRAY_INIT;
2785 ctx->stats = empty;
2786 }
2787
2788 if (do_caps_advertise)
2789 error = rdpgfx_send_supported_caps(callback);
2790
2791 return error;
2792}
2793
2794static void rdpgfx_dump_stats(RdpgfxClientContext* context)
2795{
2796 WINPR_ASSERT(context);
2797
2798 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2799 WINPR_ASSERT(gfx);
2800
2801 wLog* log = gfx->base.log;
2802 const DWORD level = WLOG_TRACE;
2803 if (!WLog_IsLevelActive(log, level))
2804 return;
2805
2806 WLog_Print(log, level, "RdpgfxClientContextStats");
2807 for (size_t x = 0; x < rdpgfx_stats_max_index(); x++)
2808 {
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);
2812 }
2813}
2814
2820static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2821{
2822 UINT error = CHANNEL_RC_OK;
2823 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2824 WINPR_ASSERT(callback);
2825 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2826
2827 if (!gfx)
2828 goto fail;
2829
2830 RdpgfxClientContext* context = gfx->context;
2831
2832 WLog_Print(gfx->base.log, WLOG_DEBUG, "OnClose");
2833 error = rdpgfx_save_persistent_cache(gfx);
2834
2835 if (error)
2836 {
2837 // print error, but don't consider this a hard failure
2838 WLog_Print(gfx->base.log, WLOG_ERROR,
2839 "rdpgfx_save_persistent_cache failed with error %" PRIu32 "", error);
2840 }
2841
2842 free_surfaces(context, gfx->SurfaceTable);
2843 error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2844 if (error)
2845 {
2846 // print error, but don't consider this a hard failure
2847 WLog_Print(gfx->base.log, WLOG_ERROR, "evict_cache_slots failed with error %" PRIu32 "",
2848 error);
2849 }
2850
2851 free(callback);
2852 gfx->UnacknowledgedFrames = 0;
2853 gfx->TotalDecodedFrames = 0;
2854
2855 if (context)
2856 {
2857 error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2858 if (error)
2859 {
2860 // print error, but don't consider this a hard failure
2861 WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnClose failed with error %" PRIu32 "",
2862 error);
2863 }
2864 rdpgfx_dump_stats(context);
2865 }
2866
2867fail:
2868 return CHANNEL_RC_OK;
2869}
2870
2871static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
2872{
2873 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2874 WINPR_ASSERT(gfx);
2875 RdpgfxClientContext* context = gfx->context;
2876
2877 WLog_Print(gfx->base.log, WLOG_DEBUG, "Terminated");
2878 rdpgfx_client_context_free(context);
2879}
2880
2886static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId, void* pData)
2887{
2888 WINPR_ASSERT(context);
2889 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2890 WINPR_ASSERT(gfx);
2891 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2892
2893 if (pData)
2894 {
2895 if (!HashTable_Insert(gfx->SurfaceTable, (void*)key, pData))
2896 return ERROR_BAD_ARGUMENTS;
2897 }
2898 else
2899 HashTable_Remove(gfx->SurfaceTable, (void*)key);
2900
2901 return CHANNEL_RC_OK;
2902}
2903
2909static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2910 UINT16* count_out)
2911{
2912 ULONG_PTR* pKeys = nullptr;
2913 WINPR_ASSERT(context);
2914 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2915 WINPR_ASSERT(gfx);
2916 size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2917
2918 WINPR_ASSERT(ppSurfaceIds);
2919 WINPR_ASSERT(count_out);
2920 if (count < 1)
2921 {
2922 *count_out = 0;
2923 return CHANNEL_RC_OK;
2924 }
2925
2926 UINT16* pSurfaceIds = (UINT16*)calloc(count, sizeof(UINT16));
2927
2928 if (!pSurfaceIds)
2929 {
2930 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2931 free(pKeys);
2932 return CHANNEL_RC_NO_MEMORY;
2933 }
2934
2935 for (size_t index = 0; index < count; index++)
2936 {
2937 pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2938 }
2939
2940 free(pKeys);
2941 *ppSurfaceIds = pSurfaceIds;
2942 *count_out = (UINT16)count;
2943 return CHANNEL_RC_OK;
2944}
2945
2946static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2947{
2948 WINPR_ASSERT(context);
2949 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2950 WINPR_ASSERT(gfx);
2951 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2952 return HashTable_GetItemValue(gfx->SurfaceTable, (void*)key);
2953}
2954
2960static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot, void* pData)
2961{
2962 WINPR_ASSERT(context);
2963 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2964
2965 WINPR_ASSERT(gfx);
2966 /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2967 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2968 {
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;
2973 }
2974
2975 gfx->CacheSlots[cacheSlot - 1] = pData;
2976 return CHANNEL_RC_OK;
2977}
2978
2979static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2980{
2981 WINPR_ASSERT(context);
2982 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2983 WINPR_ASSERT(gfx);
2984 /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2985 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2986 {
2987 WLog_Print(gfx->base.log, WLOG_ERROR,
2988 "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2989 gfx->MaxCacheSlots);
2990 return nullptr;
2991 }
2992
2993 return gfx->CacheSlots[cacheSlot - 1];
2994}
2995
2996static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext,
2997 WINPR_ATTR_UNUSED rdpSettings* settings)
2998{
2999 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
3000
3001 WINPR_ASSERT(base);
3002 gfx->rdpcontext = rcontext;
3003
3004 gfx->SurfaceTable = HashTable_New(TRUE);
3005 if (!gfx->SurfaceTable)
3006 {
3007 WLog_Print(gfx->base.log, WLOG_ERROR, "HashTable_New for surfaces failed !");
3008 return CHANNEL_RC_NO_MEMORY;
3009 }
3010
3011 gfx->suspendFrameAcks =
3012 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSuspendFrameAck);
3013 gfx->MaxCacheSlots =
3014 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) ? 4096 : 25600;
3015
3016 RdpgfxClientContextInt* context =
3017 (RdpgfxClientContextInt*)calloc(1, sizeof(RdpgfxClientContextInt));
3018 if (!context)
3019 {
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;
3024 }
3025
3026 gfx->zgfx = zgfx_context_new(FALSE);
3027 if (!gfx->zgfx)
3028 {
3029 WLog_Print(gfx->base.log, WLOG_ERROR, "zgfx_context_new failed!");
3030 HashTable_Free(gfx->SurfaceTable);
3031 gfx->SurfaceTable = nullptr;
3032 free(context);
3033 return CHANNEL_RC_NO_MEMORY;
3034 }
3035
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;
3046
3047 gfx->base.iface.pInterface = (void*)context;
3048 gfx->context = &context->common;
3049 return CHANNEL_RC_OK;
3050}
3051
3052void rdpgfx_client_context_free(RdpgfxClientContext* context)
3053{
3054 if (!context)
3055 return;
3056
3057 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
3058
3059 free_surfaces(context, gfx->SurfaceTable);
3060 evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
3061
3062 if (gfx->zgfx)
3063 {
3064 zgfx_context_free(gfx->zgfx);
3065 gfx->zgfx = nullptr;
3066 }
3067
3068 HashTable_Free(gfx->SurfaceTable);
3069 free(context);
3070}
3071
3072static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
3073 rdpgfx_on_open, rdpgfx_on_close,
3074 nullptr };
3075
3081FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
3082{
3083 return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
3084 sizeof(RDPGFX_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
3085 &rdpgfx_callbacks, init_plugin_cb, terminate_plugin_cb);
3086}
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.
Definition persistent.h:70