FreeRDP
Loading...
Searching...
No Matches
client/rdpgfx_main.c
1
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 WINPR_ASSERT(context);
226 if (!HashTable_Foreach(SurfaceTable, delete_surface, context))
227 {
228 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
229 WINPR_ASSERT(gfx);
230 WLog_Print(gfx->base.log, WLOG_WARN, "delete_surface failed");
231 }
232}
233
234static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots, void** CacheSlots)
235{
236 UINT error = CHANNEL_RC_OK;
237
238 WINPR_ASSERT(CacheSlots);
239 for (UINT16 index = 0; index < MaxCacheSlots; index++)
240 {
241 if (CacheSlots[index])
242 {
243 const RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = { .cacheSlot = index + 1 };
244
245 if (context && context->EvictCacheEntry)
246 {
247 const UINT rc = context->EvictCacheEntry(context, &pdu);
248 if (rc != CHANNEL_RC_OK)
249 error = rc;
250 }
251
252 CacheSlots[index] = nullptr;
253 }
254 }
255 return error;
256}
257
263static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
264 const RDPGFX_CAPS_ADVERTISE_PDU* pdu)
265{
266 UINT error = CHANNEL_RC_OK;
267
268 WINPR_ASSERT(pdu);
269 WINPR_ASSERT(context);
270
271 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
272
273 if (!gfx || !gfx->base.listener_callback)
274 return ERROR_BAD_ARGUMENTS;
275
276 memset(gfx->allowedCodecs, FALSE, sizeof(gfx->allowedCodecs));
277
278 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
279
280 RDPGFX_HEADER header = { .flags = 0,
281 .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
282 .pduLength = RDPGFX_HEADER_SIZE + 2 };
283
284 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
285 {
286 const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
287 header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
288 }
289
290 WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCapsAdvertisePdu %" PRIu16 "", pdu->capsSetCount);
291 wStream* s = Stream_New(nullptr, header.pduLength);
292
293 if (!s)
294 {
295 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
296 return CHANNEL_RC_NO_MEMORY;
297 }
298
299 if ((error = rdpgfx_write_header(s, &header)))
300 goto fail;
301
302 /* RDPGFX_CAPS_ADVERTISE_PDU */
303 Stream_Write_UINT16(s, pdu->capsSetCount); /* capsSetCount (2 bytes) */
304
305 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
306 {
307 const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
308
309 char buffer[128] = WINPR_C_ARRAY_INIT;
310 WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending %s [0x%08" PRIx32 "] flags=%s",
311 rdpgfx_caps_version_str(capsSet->version), capsSet->version,
312 rdpgfx_caps_flags_str(capsSet->flags, buffer, sizeof(buffer)));
313
314 Stream_Write_UINT32(s, capsSet->version); /* version (4 bytes) */
315 Stream_Write_UINT32(s, capsSet->length); /* capsDataLength (4 bytes) */
316 Stream_Write_UINT32(s, capsSet->flags); /* capsData (4 bytes) */
317 if (capsSet->length < 4)
318 goto fail;
319 Stream_Zero(s, capsSet->length - 4);
320 }
321
322 Stream_SealLength(s);
323 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
324 nullptr);
325fail:
326 Stream_Free(s, TRUE);
327 return error;
328}
329
330static BOOL rdpgfx_is_capability_filtered(RDPGFX_PLUGIN* gfx, UINT32 caps)
331{
332 WINPR_ASSERT(gfx);
333 const UINT32 filter =
334 freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCapsFilter);
335 const UINT32 capList[] = {
336#if defined(WITH_GFX_AV1)
337 RDPGFX_CAPVERSION_FRDP_1,
338#endif
339#if defined(WITH_GFX_AV2)
340 RDPGFX_CAPVERSION_FRDP_2,
341#endif
342 RDPGFX_CAPVERSION_8,
343 RDPGFX_CAPVERSION_81,
344 RDPGFX_CAPVERSION_10,
345 RDPGFX_CAPVERSION_101,
346 RDPGFX_CAPVERSION_102,
347 RDPGFX_CAPVERSION_103,
348 RDPGFX_CAPVERSION_104,
349 RDPGFX_CAPVERSION_105,
350 RDPGFX_CAPVERSION_106,
351 RDPGFX_CAPVERSION_106_ERR,
352 RDPGFX_CAPVERSION_107
353#if defined(WITH_GFX_AZURE)
354 ,
355 RDPGFX_CAPVERSION_111,
356 RDPGFX_CAPVERSION_112,
357 RDPGFX_CAPVERSION_113,
358 RDPGFX_CAPVERSION_115
359#endif
360 };
361
362 for (size_t x = 0; x < ARRAYSIZE(capList); x++)
363 {
364 if (caps == capList[x])
365 return (filter & (1u << x)) != 0;
366 }
367
368 return TRUE;
369}
370
371WINPR_ATTR_NODISCARD
372static RDPGFX_CAPSET* nextCapset(RDPGFX_CAPS_ADVERTISE_PDU* pdu, size_t count)
373{
374 WINPR_ASSERT(pdu);
375 WINPR_ASSERT(pdu->capsSets);
376 WINPR_ASSERT(count > 0);
377 WINPR_ASSERT(pdu->capsSetCount < count);
378 if (pdu->capsSetCount >= count)
379 return nullptr;
380 return &pdu->capsSets[pdu->capsSetCount++];
381}
382
383#if defined(WITH_GFX_AZURE)
384/* Only offer HEVC if it can be decoded: a server confirming it sends HEVC420 frames. */
385WINPR_ATTR_NODISCARD
386static BOOL rdpgfx_hevc_enabled(RDPGFX_PLUGIN* gfx)
387{
388 WINPR_ASSERT(gfx);
389
390 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecHEVC))
391 return FALSE;
392 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I420, FALSE))
393 return TRUE;
394
395 WLog_Print(gfx->base.log, WLOG_WARN,
396 "HEVC mode enabled, but decoder does not support it. Disabling.");
397 return FALSE;
398}
399#endif
400
406static UINT rdpgfx_send_supported_caps(GENERIC_CHANNEL_CALLBACK* callback)
407{
408 RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
409
410 if (!callback)
411 return ERROR_BAD_ARGUMENTS;
412
413 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
414
415 if (!gfx)
416 return ERROR_BAD_CONFIGURATION;
417
418 RdpgfxClientContext* context = gfx->context;
419
420 if (!context)
421 return ERROR_BAD_CONFIGURATION;
422
423 RDPGFX_CAPS_ADVERTISE_PDU pdu = { .capsSetCount = 0, .capsSets = capsSets };
424
425 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
426 {
427 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
428 if (!capsSet)
429 return ERROR_BAD_CONFIGURATION;
430 capsSet->version = RDPGFX_CAPVERSION_8;
431 capsSet->length = 4;
432 capsSet->flags = 0;
433
434 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
435 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
436
437 /* in CAPVERSION_8 the spec says that we should not have both
438 * thinclient and smallcache (and thinclient implies a small cache)
439 */
440 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) &&
441 !freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
442 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
443 }
444
445 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
446 {
447 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
448 if (!capsSet)
449 return ERROR_BAD_CONFIGURATION;
450 capsSet->version = RDPGFX_CAPVERSION_81;
451 capsSet->length = 4;
452 capsSet->flags = 0;
453
454 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
455 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
456
457 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
458 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
459
460 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264) &&
461 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC420, FALSE))
462 capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
463 }
464
465 UINT32 caps10Flags = 0;
466 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264) ||
467 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444))
468 {
469 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
470 caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
471
472 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444) ||
473 !freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AVC444, FALSE))
474 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
475 }
476
477 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
478 {
479 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
480 if (!capsSet)
481 return ERROR_BAD_CONFIGURATION;
482 capsSet->version = RDPGFX_CAPVERSION_10;
483 capsSet->length = 4;
484 capsSet->flags = caps10Flags;
485 }
486
487 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
488 {
489 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
490 if (!capsSet)
491 return ERROR_BAD_CONFIGURATION;
492 capsSet->version = RDPGFX_CAPVERSION_101;
493 capsSet->length = 0x10;
494 capsSet->flags = 0;
495 }
496
497 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
498 {
499 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
500 if (!capsSet)
501 return ERROR_BAD_CONFIGURATION;
502 capsSet->version = RDPGFX_CAPVERSION_102;
503 capsSet->length = 0x4;
504 capsSet->flags = caps10Flags;
505 }
506
507 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
508 {
509 if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
510 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
511 }
512
513 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
514 {
515 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
516 if (!capsSet)
517 return ERROR_BAD_CONFIGURATION;
518 capsSet->version = RDPGFX_CAPVERSION_103;
519 capsSet->length = 0x4;
520 capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
521 }
522
523 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
524 {
525 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
526 if (!capsSet)
527 return ERROR_BAD_CONFIGURATION;
528 capsSet->version = RDPGFX_CAPVERSION_104;
529 capsSet->length = 0x4;
530 capsSet->flags = caps10Flags;
531 }
532
533 /* The following capabilities expect support for image scaling.
534 * Disable these for builds that do not have support for that.
535 */
536 if (freerdp_image_scaling_supported())
537 {
538 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
539 {
540 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
541 if (!capsSet)
542 return ERROR_BAD_CONFIGURATION;
543 capsSet->version = RDPGFX_CAPVERSION_105;
544 capsSet->length = 0x4;
545 capsSet->flags = caps10Flags;
546 }
547
548 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
549 {
550 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
551 if (!capsSet)
552 return ERROR_BAD_CONFIGURATION;
553 capsSet->version = RDPGFX_CAPVERSION_106;
554 capsSet->length = 0x4;
555 capsSet->flags = caps10Flags;
556 }
557
558 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
559 {
560 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
561 if (!capsSet)
562 return ERROR_BAD_CONFIGURATION;
563 capsSet->version = RDPGFX_CAPVERSION_106_ERR;
564 capsSet->length = 0x4;
565 capsSet->flags = caps10Flags;
566 }
567 }
568
569 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
570 {
571 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
572 if (!capsSet)
573 return ERROR_BAD_CONFIGURATION;
574 capsSet->version = RDPGFX_CAPVERSION_107;
575 capsSet->length = 0x4;
576 capsSet->flags = caps10Flags;
577 if (!freerdp_image_scaling_supported())
578 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
579 }
580
581#if defined(WITH_GFX_AZURE)
582 if (freerdp_image_scaling_supported())
583 {
584 const UINT32 caps[] = { RDPGFX_CAPVERSION_111, RDPGFX_CAPVERSION_112, RDPGFX_CAPVERSION_113,
585 RDPGFX_CAPVERSION_115 };
586 const BOOL hevc = rdpgfx_hevc_enabled(gfx);
587 const BOOL hevc444 =
588 hevc && freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecHEVC444) &&
589 freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_HEVC_I444, FALSE);
590
591 for (size_t x = 0; x < ARRAYSIZE(caps); x++)
592 {
593 const UINT32 cap = caps[x];
594
595 if (rdpgfx_is_capability_filtered(gfx, cap))
596 continue;
597
598 /* the flags differ between the versions, keep them out of caps10Flags */
599 UINT32 flags = caps10Flags;
600
601 /* HEVC420 is valid from RDPGFX_CAPVERSION_112 on. AVC stays enabled: the server
602 * falls back to it if it has no HEVC encoder. */
603 if (hevc && (cap >= RDPGFX_CAPVERSION_112))
604 flags |= RDPGFX_CAPS_FLAG_HEVC_ENABLE;
605
606 /* HEVC444 is only valid in RDPGFX_CAPVERSION_115 */
607 if (hevc444 && (cap >= RDPGFX_CAPVERSION_115))
608 flags |= RDPGFX_CAPS_FLAG_HEVC_I444;
609
610 /* AVC mixed mode is valid in RDPGFX_CAPVERSION_113 and RDPGFX_CAPVERSION_115.
611 * The server then mixes AVC420 or AVC444 regions with the other codecs on a
612 * surface, all of which are decoded already, so offer it whenever AVC is. */
613 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAVCMM))
614 {
615 if ((cap >= RDPGFX_CAPVERSION_113) &&
616 ((flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0))
617 flags |= RDPGFX_CAPS_FLAG_AVCMM_ENABLE;
618 }
619
620 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
621 if (!capsSet)
622 return ERROR_BAD_CONFIGURATION;
623 capsSet->version = cap;
624 capsSet->length = 0x4;
625 capsSet->flags = flags;
626 if (!freerdp_image_scaling_supported())
627 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
628 }
629 }
630#endif
631
632#if defined(WITH_GFX_AV1)
633 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1))
634 {
635 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I420, FALSE) &&
636 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
637 {
638 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
639 if (!capsSet)
640 return ERROR_BAD_CONFIGURATION;
641 capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
642 capsSet->length = 0x4;
643
644 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV1_I444, FALSE))
645 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
646 else
647 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
648
649 const UINT32 profile =
650 freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1Profile);
651 if (profile == 0)
652 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
653
654 if (!freerdp_image_scaling_supported())
655 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
656 }
657 }
658#endif
659
660#if defined(WITH_GFX_AV2)
661 /* A capability set of its own: a server that confirms it may send AV2, and one that does
662 * not know it never confirms it. */
663 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV2))
664 {
665 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I420, FALSE) &&
666 !rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_2))
667 {
668 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
669 if (!capsSet)
670 return ERROR_BAD_CONFIGURATION;
671 capsSet->version = RDPGFX_CAPVERSION_FRDP_2;
672 capsSet->length = 0x4;
673
674 if (freerdp_codec_supported(gfx->rdpcontext, FREERDP_CODEC_AV2_I444, FALSE))
675 {
676 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
677
678 const UINT32 profile = freerdp_settings_get_uint32(gfx->rdpcontext->settings,
679 FreeRDP_GfxCodecAV2Profile);
680 if (profile == 0)
681 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
682 }
683 else
684 {
685 /* the decoder (dav2d) has no AV2 4:4:4: always ask for 4:2:0 */
686 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
687 }
688
689 if (!freerdp_image_scaling_supported())
690 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
691 }
692 }
693#endif
694
695 return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
696}
697
703static UINT rdpgfx_recv_caps_confirm_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
704{
705 WINPR_ASSERT(callback);
706 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
707
708 WINPR_ASSERT(gfx);
709 RdpgfxClientContext* context = gfx->context;
710
711 RDPGFX_CAPSET capsSet = WINPR_C_ARRAY_INIT;
712 RDPGFX_CAPS_CONFIRM_PDU pdu = { .capsSet = &capsSet };
713
714 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
715 return ERROR_INVALID_DATA;
716
717 Stream_Read_UINT32(s, capsSet.version); /* version (4 bytes) */
718 Stream_Read_UINT32(s, capsSet.length); /* capsDataLength (4 bytes) */
719 Stream_Read_UINT32(s, capsSet.flags); /* capsData (4 bytes) */
720 gfx->TotalDecodedFrames = 0;
721 gfx->ConnectionCaps = capsSet;
722 if (context)
723 context->CapsVersion = capsSet.version;
724 {
725 char buffer[128] = WINPR_C_ARRAY_INIT;
726
727 WLog_Print(gfx->base.log, WLOG_DEBUG,
728 "RecvCapsConfirmPdu: version: %s [0x%08" PRIX32 "] flags: %s",
729 rdpgfx_caps_version_str(capsSet.version), capsSet.version,
730 rdpgfx_caps_flags_str(capsSet.flags, buffer, sizeof(buffer)));
731 }
732 memset(gfx->allowedCodecs, TRUE, sizeof(gfx->allowedCodecs));
733
734 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = FALSE;
735 gfx->allowedCodecs[RDPGFX_CODECID_AVC444] = FALSE;
736 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = FALSE;
737#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
738 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = FALSE;
739#endif
740
741 if (capsSet.version >= RDPGFX_CAPVERSION_10)
742 {
743 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
744 {
745 WLog_Print(gfx->base.log, WLOG_INFO, "AVC not disabled by server");
746 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
747 gfx->allowedCodecs[RDPGFX_CODECID_AVC444] = TRUE;
748 }
749 }
750
751 if (capsSet.version >= RDPGFX_CAPVERSION_103)
752 {
753 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
754 gfx->allowedCodecs[RDPGFX_CODECID_AVC444v2] = TRUE;
755 }
756
757 if (capsSet.version == RDPGFX_CAPVERSION_81)
758 {
759 if ((capsSet.flags & RDPGFX_CAPS_FLAG_AVC420_ENABLED) != 0)
760 {
761 WLog_Print(gfx->base.log, WLOG_INFO, "AVC420 enabled by server");
762 gfx->allowedCodecs[RDPGFX_CODECID_AVC420] = TRUE;
763 }
764 }
765
766#if defined(WITH_GFX_AV1)
767 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_1)
768 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
769#endif
770#if defined(WITH_GFX_AV2)
771 if (capsSet.version == RDPGFX_CAPVERSION_FRDP_2)
772 gfx->allowedCodecs[RDPGFX_CODECID_AV1] = TRUE;
773#endif
774
775#if defined(WITH_GFX_AZURE)
776 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = FALSE;
777 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = FALSE;
778
779 if (capsSet.version >= RDPGFX_CAPVERSION_112)
780 {
781 if ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_ENABLE) != 0)
782 {
783 WLog_Print(gfx->base.log, WLOG_INFO, "HEVC confirmed by server");
784 gfx->allowedCodecs[RDPGFX_CODECID_HEVC420] = TRUE;
785
786 /* HEVC444 is only valid in RDPGFX_CAPVERSION_115 and needs HEVC enabled */
787 if ((capsSet.version >= RDPGFX_CAPVERSION_115) &&
788 ((capsSet.flags & RDPGFX_CAPS_FLAG_HEVC_I444) != 0))
789 {
790 WLog_Print(gfx->base.log, WLOG_INFO, "HEVC I444 confirmed by server");
791 gfx->allowedCodecs[RDPGFX_CODECID_HEVC444] = TRUE;
792 }
793 }
794 }
795#endif
796
797 if (!context)
798 return ERROR_BAD_CONFIGURATION;
799
800 return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
801}
802
808static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
810{
811 UINT error = 0;
812
813 if (!context || !pdu)
814 return ERROR_BAD_ARGUMENTS;
815
816 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
817
818 if (!gfx || !gfx->base.listener_callback)
819 return ERROR_BAD_CONFIGURATION;
820
821 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
822
823 if (!callback)
824 return ERROR_BAD_CONFIGURATION;
825
826 const RDPGFX_HEADER header = { .flags = 0,
827 .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
828 .pduLength = RDPGFX_HEADER_SIZE + 12 };
829
830 WLog_Print(gfx->base.log, WLOG_TRACE, "SendFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
831
832 wStream* s = Stream_New(nullptr, header.pduLength);
833
834 if (!s)
835 {
836 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
837 return CHANNEL_RC_NO_MEMORY;
838 }
839
840 if ((error = rdpgfx_write_header(s, &header)))
841 goto fail;
842
843 /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
844 Stream_Write_UINT32(s, pdu->queueDepth); /* queueDepth (4 bytes) */
845 Stream_Write_UINT32(s, pdu->frameId); /* frameId (4 bytes) */
846 Stream_Write_UINT32(s, pdu->totalFramesDecoded); /* totalFramesDecoded (4 bytes) */
847 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
848 nullptr);
849
850 if (error == CHANNEL_RC_OK) /* frame successfully acked */
851 {
852 gfx->UnacknowledgedFrames--;
853 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
854 }
855
856fail:
857 Stream_Free(s, TRUE);
858 return error;
859}
860
861static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
863{
864 UINT error = CHANNEL_RC_OK;
865 const RDPGFX_HEADER header = { .flags = 0,
866 .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
867 .pduLength = RDPGFX_HEADER_SIZE + 12 };
868
869 if (!context || !pdu)
870 return ERROR_BAD_ARGUMENTS;
871
872 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
873
874 if (!gfx || !gfx->base.listener_callback)
875 return ERROR_BAD_CONFIGURATION;
876
877 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
878
879 if (!callback)
880 return ERROR_BAD_CONFIGURATION;
881
882 WLog_Print(gfx->base.log, WLOG_TRACE, "SendQoeFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
883 wStream* s = Stream_New(nullptr, header.pduLength);
884
885 if (!s)
886 {
887 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
888 return CHANNEL_RC_NO_MEMORY;
889 }
890
891 if ((error = rdpgfx_write_header(s, &header)))
892 goto fail;
893
894 /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
895 Stream_Write_UINT32(s, pdu->frameId);
896 Stream_Write_UINT32(s, pdu->timestamp);
897 Stream_Write_UINT16(s, pdu->timeDiffSE);
898 Stream_Write_UINT16(s, pdu->timeDiffEDR);
899 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
900 nullptr);
901fail:
902 Stream_Free(s, TRUE);
903 return error;
904}
905
911static UINT rdpgfx_recv_reset_graphics_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
912{
913 RDPGFX_RESET_GRAPHICS_PDU pdu = WINPR_C_ARRAY_INIT;
914 WINPR_ASSERT(callback);
915
916 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
917
918 WINPR_ASSERT(gfx);
919
920 RdpgfxClientContext* context = gfx->context;
921 UINT error = CHANNEL_RC_OK;
922 GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
923
924 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
925 return ERROR_INVALID_DATA;
926
927 Stream_Read_UINT32(s, pdu.width); /* width (4 bytes) */
928 Stream_Read_UINT32(s, pdu.height); /* height (4 bytes) */
929 Stream_Read_UINT32(s, pdu.monitorCount); /* monitorCount (4 bytes) */
930
931 if ((pdu.width == 0) || (pdu.height == 0) || (pdu.monitorCount == 0))
932 {
933 WLog_Print(gfx->base.log, WLOG_ERROR,
934 "context->ResetGraphics Width[%" PRIu32 "], Height[%" PRIu32
935 "] or monitorCount[%" PRIu32 " is 0",
936 pdu.width, pdu.height, pdu.monitorCount);
937 return ERROR_INVALID_DATA;
938 }
939 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE) ||
940 (pdu.monitorCount > MAX_MONITOR_COUNT))
941 {
942 WLog_Print(gfx->base.log, WLOG_ERROR,
943 "context->ResetGraphics Width[%" PRIu32 "], Height[%" PRIu32 "] > %" PRIu32
944 " or monitorCount[%" PRIu32 " > %" PRIu32,
945 pdu.width, pdu.height, MAX_SURFACE_SIZE, pdu.monitorCount, MAX_MONITOR_COUNT);
946 return ERROR_INVALID_DATA;
947 }
948
949 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
950 return ERROR_INVALID_DATA;
951
952 pdu.monitorDefArray = (MONITOR_DEF*)calloc(pdu.monitorCount, sizeof(MONITOR_DEF));
953
954 if (!pdu.monitorDefArray)
955 {
956 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
957 return CHANNEL_RC_NO_MEMORY;
958 }
959
960 for (UINT32 index = 0; index < pdu.monitorCount; index++)
961 {
962 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
963 Stream_Read_INT32(s, monitor->left); /* left (4 bytes) */
964 Stream_Read_INT32(s, monitor->top); /* top (4 bytes) */
965 Stream_Read_INT32(s, monitor->right); /* right (4 bytes) */
966 Stream_Read_INT32(s, monitor->bottom); /* bottom (4 bytes) */
967 Stream_Read_UINT32(s, monitor->flags); /* flags (4 bytes) */
968 }
969
970 const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
971 if (size > 340)
972 {
973 free(pdu.monitorDefArray);
974 return CHANNEL_RC_NULL_DATA;
975 }
976 const size_t pad = 340ULL - size;
977
978 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (size_t)pad))
979 {
980 free(pdu.monitorDefArray);
981 return CHANNEL_RC_NO_MEMORY;
982 }
983
984 Stream_Seek(s, pad); /* pad (total size is 340 bytes) */
985 WLog_Print(gfx->base.log, WLOG_DEBUG,
986 "RecvResetGraphicsPdu: width: %" PRIu32 " height: %" PRIu32 " count: %" PRIu32 "",
987 pdu.width, pdu.height, pdu.monitorCount);
988
989 for (UINT32 index = 0; index < pdu.monitorCount; index++)
990 {
991 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
992 WLog_Print(gfx->base.log, WLOG_TRACE,
993 "RecvResetGraphicsPdu: monitor left:%" PRIi32 " top:%" PRIi32 " right:%" PRIi32
994 " bottom:%" PRIi32 " flags:0x%" PRIx32 "",
995 monitor->left, monitor->top, monitor->right, monitor->bottom, monitor->flags);
996 }
997
998 if (context)
999 {
1000 IFCALLRET(context->ResetGraphics, error, context, &pdu);
1001
1002 if (error)
1003 WLog_Print(gfx->base.log, WLOG_ERROR,
1004 "context->ResetGraphics failed with error %" PRIu32 "", error);
1005 }
1006
1007 free(pdu.monitorDefArray);
1008
1009 /* some listeners may be interested (namely the display channel) */
1010 if (error == CHANNEL_RC_OK)
1011 {
1012 EventArgsInit(&graphicsReset, "libfreerdp");
1013 graphicsReset.width = pdu.width;
1014 graphicsReset.height = pdu.height;
1015 if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
1016 return ERROR_INTERNAL_ERROR;
1017 }
1018 return error;
1019}
1020
1026static UINT rdpgfx_recv_evict_cache_entry_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1027{
1028 RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = WINPR_C_ARRAY_INIT;
1029 WINPR_ASSERT(callback);
1030 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1031 WINPR_ASSERT(gfx);
1032 RdpgfxClientContext* context = gfx->context;
1033 UINT error = CHANNEL_RC_OK;
1034
1035 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1036 return ERROR_INVALID_DATA;
1037
1038 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
1039 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16 "",
1040 pdu.cacheSlot);
1041
1042 if (context)
1043 {
1044 IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
1045
1046 if (error)
1047 WLog_Print(gfx->base.log, WLOG_ERROR,
1048 "context->EvictCacheEntry failed with error %" PRIu32 "", error);
1049 }
1050
1051 return error;
1052}
1053
1059static UINT rdpgfx_save_persistent_cache(RDPGFX_PLUGIN* gfx)
1060{
1061 UINT error = CHANNEL_RC_OK;
1062
1063 WINPR_ASSERT(gfx);
1064 WINPR_ASSERT(gfx->rdpcontext);
1065 rdpSettings* settings = gfx->rdpcontext->settings;
1066 RdpgfxClientContext* context = gfx->context;
1067
1068 WINPR_ASSERT(context);
1069 WINPR_ASSERT(settings);
1070
1071 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1072 return CHANNEL_RC_OK;
1073
1074 const char* BitmapCachePersistFile =
1075 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1076 if (!BitmapCachePersistFile)
1077 return CHANNEL_RC_OK;
1078
1079 if (!context->ExportCacheEntry)
1080 return CHANNEL_RC_INITIALIZATION_ERROR;
1081
1082 rdpPersistentCache* persistent = persistent_cache_new();
1083
1084 if (!persistent)
1085 return CHANNEL_RC_NO_MEMORY;
1086
1087 if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
1088 {
1089 error = CHANNEL_RC_INITIALIZATION_ERROR;
1090 goto fail;
1091 }
1092
1093 for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
1094 {
1095 if (gfx->CacheSlots[idx])
1096 {
1097 const UINT16 cacheSlot = idx;
1098 PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
1099
1100 if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
1101 continue;
1102
1103 if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
1104 goto fail;
1105 }
1106 }
1107
1108 persistent_cache_free(persistent);
1109
1110 return error;
1111fail:
1112 persistent_cache_free(persistent);
1113 return error;
1114}
1115
1121static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
1123{
1124 UINT error = CHANNEL_RC_OK;
1125
1126 if (!context || !pdu)
1127 return ERROR_BAD_ARGUMENTS;
1128
1129 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
1130
1131 if (!gfx || !gfx->base.listener_callback)
1132 return ERROR_BAD_CONFIGURATION;
1133
1134 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
1135
1136 if (!callback)
1137 return ERROR_BAD_CONFIGURATION;
1138
1139 const RDPGFX_HEADER header = { .flags = 0,
1140 .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
1141 .pduLength =
1142 RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
1143
1144 WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16 "",
1145 pdu->cacheEntriesCount);
1146 wStream* s = Stream_New(nullptr, header.pduLength);
1147
1148 if (!s)
1149 {
1150 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
1151 return CHANNEL_RC_NO_MEMORY;
1152 }
1153
1154 if ((error = rdpgfx_write_header(s, &header)))
1155 goto fail;
1156
1157 if (pdu->cacheEntriesCount <= 0)
1158 {
1159 WLog_Print(gfx->base.log, WLOG_ERROR, "Invalid cacheEntriesCount: %" PRIu16 "",
1160 pdu->cacheEntriesCount);
1161 error = ERROR_INVALID_DATA;
1162 goto fail;
1163 }
1164
1165 /* cacheEntriesCount (2 bytes) */
1166 Stream_Write_UINT16(s, pdu->cacheEntriesCount);
1167
1168 for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
1169 {
1170 const RDPGFX_CACHE_ENTRY_METADATA* cacheEntry = &(pdu->cacheEntries[index]);
1171 Stream_Write_UINT64(s, cacheEntry->cacheKey); /* cacheKey (8 bytes) */
1172 Stream_Write_UINT32(s, cacheEntry->bitmapLength); /* bitmapLength (4 bytes) */
1173 }
1174
1175 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
1176 nullptr);
1177
1178fail:
1179 Stream_Free(s, TRUE);
1180 return error;
1181}
1182
1188static UINT rdpgfx_send_cache_offer(RDPGFX_PLUGIN* gfx)
1189{
1190 int count = 0;
1191 UINT error = CHANNEL_RC_OK;
1192 PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1193 RDPGFX_CACHE_IMPORT_OFFER_PDU* offer = nullptr;
1194
1195 WINPR_ASSERT(gfx);
1196 WINPR_ASSERT(gfx->rdpcontext);
1197
1198 RdpgfxClientContext* context = gfx->context;
1199 rdpSettings* settings = gfx->rdpcontext->settings;
1200
1201 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1202 return CHANNEL_RC_OK;
1203
1204 const char* BitmapCachePersistFile =
1205 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1206 if (!BitmapCachePersistFile)
1207 return CHANNEL_RC_OK;
1208
1209 rdpPersistentCache* persistent = persistent_cache_new();
1210
1211 if (!persistent)
1212 return CHANNEL_RC_NO_MEMORY;
1213
1214 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1215 {
1216 error = CHANNEL_RC_INITIALIZATION_ERROR;
1217 goto fail;
1218 }
1219
1220 if (persistent_cache_get_version(persistent) != 3)
1221 {
1222 error = ERROR_INVALID_DATA;
1223 goto fail;
1224 }
1225
1226 count = persistent_cache_get_count(persistent);
1227 if (count < 0)
1228 {
1229 error = ERROR_INVALID_DATA;
1230 goto fail;
1231 }
1232
1233 if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
1234 count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
1235
1236 if (count > gfx->MaxCacheSlots)
1237 count = gfx->MaxCacheSlots;
1238
1240 if (!offer)
1241 {
1242 error = CHANNEL_RC_NO_MEMORY;
1243 goto fail;
1244 }
1245
1246 WINPR_ASSERT(count <= UINT16_MAX);
1247 offer->cacheEntriesCount = (UINT16)count;
1248
1249 WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending Cache Import Offer: %d", count);
1250
1251 for (int idx = 0; idx < count; idx++)
1252 {
1253 if (persistent_cache_read_entry(persistent, &entry) < 1)
1254 {
1255 error = ERROR_INVALID_DATA;
1256 goto fail;
1257 }
1258
1259 offer->cacheEntries[idx].cacheKey = entry.key64;
1260 offer->cacheEntries[idx].bitmapLength = entry.size;
1261 }
1262
1263 if (offer->cacheEntriesCount > 0)
1264 {
1265 error = rdpgfx_send_cache_import_offer_pdu(context, offer);
1266 if (error != CHANNEL_RC_OK)
1267 {
1268 WLog_Print(gfx->base.log, WLOG_ERROR, "Failed to send cache import offer PDU");
1269 goto fail;
1270 }
1271 }
1272
1273fail:
1274 persistent_cache_free(persistent);
1275 free(offer);
1276 return error;
1277}
1278
1284static UINT rdpgfx_load_cache_import_reply(RDPGFX_PLUGIN* gfx,
1285 const RDPGFX_CACHE_IMPORT_REPLY_PDU* reply)
1286{
1287 UINT error = CHANNEL_RC_OK;
1288 rdpPersistentCache* persistent = nullptr;
1289 WINPR_ASSERT(gfx);
1290 WINPR_ASSERT(gfx->rdpcontext);
1291 rdpSettings* settings = gfx->rdpcontext->settings;
1292 RdpgfxClientContext* context = gfx->context;
1293
1294 WINPR_ASSERT(settings);
1295 WINPR_ASSERT(reply);
1296 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1297 return CHANNEL_RC_OK;
1298
1299 const char* BitmapCachePersistFile =
1300 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1301 if (!BitmapCachePersistFile)
1302 return CHANNEL_RC_OK;
1303
1304 persistent = persistent_cache_new();
1305
1306 if (!persistent)
1307 return CHANNEL_RC_NO_MEMORY;
1308
1309 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1310 {
1311 error = CHANNEL_RC_INITIALIZATION_ERROR;
1312 goto fail;
1313 }
1314
1315 if (persistent_cache_get_version(persistent) != 3)
1316 {
1317 error = ERROR_INVALID_DATA;
1318 goto fail;
1319 }
1320
1321 int count = persistent_cache_get_count(persistent);
1322
1323 count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
1324
1325 WLog_Print(gfx->base.log, WLOG_DEBUG, "Receiving Cache Import Reply: %d", count);
1326
1327 for (int idx = 0; idx < count; idx++)
1328 {
1329 PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1330 if (persistent_cache_read_entry(persistent, &entry) < 1)
1331 {
1332 error = ERROR_INVALID_DATA;
1333 goto fail;
1334 }
1335
1336 const UINT16 cacheSlot = reply->cacheSlots[idx];
1337 if (context && context->ImportCacheEntry)
1338 {
1339 error = context->ImportCacheEntry(context, cacheSlot, &entry);
1340 if (error != CHANNEL_RC_OK)
1341 break;
1342 }
1343 }
1344
1345 persistent_cache_free(persistent);
1346
1347 return error;
1348fail:
1349 persistent_cache_free(persistent);
1350 return error;
1351}
1352
1358static UINT rdpgfx_recv_cache_import_reply_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1359{
1360 RDPGFX_CACHE_IMPORT_REPLY_PDU pdu = WINPR_C_ARRAY_INIT;
1361 WINPR_ASSERT(callback);
1362 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1363 WINPR_ASSERT(gfx);
1364 RdpgfxClientContext* context = gfx->context;
1365 UINT error = CHANNEL_RC_OK;
1366
1367 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1368 return ERROR_INVALID_DATA;
1369
1370 Stream_Read_UINT16(s, pdu.importedEntriesCount); /* cacheSlot (2 bytes) */
1371
1372 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1373 2ull))
1374 return ERROR_INVALID_DATA;
1375
1376 if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1377 return ERROR_INVALID_DATA;
1378
1379 for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1380 {
1381 Stream_Read_UINT16(s, pdu.cacheSlots[idx]); /* cacheSlot (2 bytes) */
1382 }
1383
1384 WLog_Print(gfx->base.log, WLOG_TRACE,
1385 "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16 "",
1386 pdu.importedEntriesCount);
1387
1388 error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1389
1390 if (error)
1391 {
1392 WLog_Print(gfx->base.log, WLOG_ERROR,
1393 "rdpgfx_load_cache_import_reply failed with error %" PRIu32 "", error);
1394 return error;
1395 }
1396
1397 if (context)
1398 {
1399 IFCALLRET(context->CacheImportReply, error, context, &pdu);
1400
1401 if (error)
1402 WLog_Print(gfx->base.log, WLOG_ERROR,
1403 "context->CacheImportReply failed with error %" PRIu32 "", error);
1404 }
1405
1406 return error;
1407}
1408
1414static UINT rdpgfx_recv_create_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1415{
1416 RDPGFX_CREATE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1417 WINPR_ASSERT(callback);
1418 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1419 WINPR_ASSERT(gfx);
1420 RdpgfxClientContext* context = gfx->context;
1421 UINT error = CHANNEL_RC_OK;
1422
1423 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1424 return ERROR_INVALID_DATA;
1425
1426 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1427 Stream_Read_UINT16(s, pdu.width); /* width (2 bytes) */
1428 Stream_Read_UINT16(s, pdu.height); /* height (2 bytes) */
1429 Stream_Read_UINT8(s, pdu.pixelFormat); /* RDPGFX_PIXELFORMAT (1 byte) */
1430
1431 if ((pdu.width == 0) || (pdu.height == 0))
1432 {
1433 WLog_Print(gfx->base.log, WLOG_ERROR,
1434 "context->RecvCreateSurfacePdu Width[%" PRIu32 "], Height[%" PRIu32 "] is 0",
1435 pdu.width, pdu.height);
1436 return ERROR_INVALID_DATA;
1437 }
1438 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE))
1439 {
1440 WLog_Print(gfx->base.log, WLOG_ERROR,
1441 "context->RecvCreateSurfacePdu Width[%" PRIu32 "], Height[%" PRIu32
1442 "] > %" PRIu32,
1443 pdu.width, pdu.height, MAX_SURFACE_SIZE);
1444 return ERROR_INVALID_DATA;
1445 }
1446
1447 WLog_Print(gfx->base.log, WLOG_DEBUG,
1448 "RecvCreateSurfacePdu: surfaceId: %" PRIu16 " width: %" PRIu16 " height: %" PRIu16
1449 " pixelFormat: 0x%02" PRIX8 "",
1450 pdu.surfaceId, pdu.width, pdu.height, pdu.pixelFormat);
1451
1452 if (context)
1453 {
1454 /* create surface PDU sometimes happens for surface ID that are already in use and have not
1455 * been removed yet. Ensure that there is no surface with the new ID by trying to remove it
1456 * manually.
1457 */
1458 RDPGFX_DELETE_SURFACE_PDU deletePdu = { pdu.surfaceId };
1459 const UINT drc = IFCALLRESULT(CHANNEL_RC_OK, context->DeleteSurface, context, &deletePdu);
1460 if (drc != CHANNEL_RC_OK)
1461 WLog_Print(gfx->base.log, WLOG_WARN,
1462 "context->DeleteSurface failed with error %" PRIu32 ", ignoring", drc);
1463
1464 IFCALLRET(context->CreateSurface, error, context, &pdu);
1465
1466 if (error)
1467 WLog_Print(gfx->base.log, WLOG_ERROR,
1468 "context->CreateSurface failed with error %" PRIu32 "", error);
1469 }
1470
1471 return error;
1472}
1473
1479static UINT rdpgfx_recv_delete_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1480{
1481 RDPGFX_DELETE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1482 WINPR_ASSERT(callback);
1483 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1484 WINPR_ASSERT(gfx);
1485 RdpgfxClientContext* context = gfx->context;
1486 UINT error = CHANNEL_RC_OK;
1487
1488 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1489 return ERROR_INVALID_DATA;
1490
1491 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1492 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvDeleteSurfacePdu: surfaceId: %" PRIu16 "",
1493 pdu.surfaceId);
1494
1495 if (context)
1496 {
1497 IFCALLRET(context->DeleteSurface, error, context, &pdu);
1498
1499 if (error)
1500 WLog_Print(gfx->base.log, WLOG_ERROR,
1501 "context->DeleteSurface failed with error %" PRIu32 "", error);
1502 }
1503
1504 return error;
1505}
1506
1512static UINT rdpgfx_recv_start_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1513{
1514 RDPGFX_START_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1515 WINPR_ASSERT(callback);
1516 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1517 WINPR_ASSERT(gfx);
1518 RdpgfxClientContext* context = gfx->context;
1519 UINT error = CHANNEL_RC_OK;
1520
1521 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1522 return ERROR_INVALID_DATA;
1523
1524 Stream_Read_UINT32(s, pdu.timestamp); /* timestamp (4 bytes) */
1525 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1526 WLog_Print(gfx->base.log, WLOG_TRACE,
1527 "RecvStartFramePdu: frameId: %" PRIu32 " timestamp: 0x%08" PRIX32 "", pdu.frameId,
1528 pdu.timestamp);
1529 gfx->StartDecodingTime = GetTickCount64();
1530
1531 if (context)
1532 {
1533 IFCALLRET(context->StartFrame, error, context, &pdu);
1534
1535 if (error)
1536 WLog_Print(gfx->base.log, WLOG_ERROR,
1537 "context->StartFrame failed with error %" PRIu32 "", error);
1538 }
1539
1540 gfx->UnacknowledgedFrames++;
1541 rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
1542 return error;
1543}
1544
1550static UINT rdpgfx_recv_end_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1551{
1552 RDPGFX_END_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1553 RDPGFX_FRAME_ACKNOWLEDGE_PDU ack = WINPR_C_ARRAY_INIT;
1554 WINPR_ASSERT(callback);
1555 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1556 WINPR_ASSERT(gfx);
1557 RdpgfxClientContext* context = gfx->context;
1558 UINT error = CHANNEL_RC_OK;
1559
1560 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1561 return ERROR_INVALID_DATA;
1562
1563 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1564 WLog_Print(gfx->base.log, WLOG_TRACE, "RecvEndFramePdu: frameId: %" PRIu32 "", pdu.frameId);
1565
1566 const UINT64 start = GetTickCount64();
1567 if (context)
1568 {
1569 IFCALLRET(context->EndFrame, error, context, &pdu);
1570
1571 if (error)
1572 {
1573 WLog_Print(gfx->base.log, WLOG_ERROR, "context->EndFrame failed with error %" PRIu32 "",
1574 error);
1575 return error;
1576 }
1577 }
1578 const UINT64 end = GetTickCount64();
1579 const UINT64 EndFrameTime = end - start;
1580 gfx->TotalDecodedFrames++;
1581 rdpgfx_stats_frame_decoded(context);
1582
1583 if (!gfx->sendFrameAcks)
1584 return error;
1585
1586 ack.frameId = pdu.frameId;
1587 ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1588
1589 if (gfx->suspendFrameAcks)
1590 {
1591 ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1592
1593 if (gfx->TotalDecodedFrames == 1)
1594 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1595 WLog_Print(gfx->base.log, WLOG_ERROR,
1596 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32 "",
1597 error);
1598 }
1599 else
1600 {
1601 ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1602
1603 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1604 WLog_Print(gfx->base.log, WLOG_ERROR,
1605 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32 "", error);
1606 }
1607
1608 switch (gfx->ConnectionCaps.version)
1609 {
1610#if defined(WITH_GFX_AV1)
1611 case RDPGFX_CAPVERSION_FRDP_1:
1612#endif
1613#if defined(WITH_GFX_AV2)
1614 case RDPGFX_CAPVERSION_FRDP_2:
1615#endif
1616 case RDPGFX_CAPVERSION_10:
1617 case RDPGFX_CAPVERSION_102:
1618 case RDPGFX_CAPVERSION_103:
1619 case RDPGFX_CAPVERSION_104:
1620 case RDPGFX_CAPVERSION_105:
1621 case RDPGFX_CAPVERSION_106:
1622 case RDPGFX_CAPVERSION_106_ERR:
1623 case RDPGFX_CAPVERSION_107:
1624#if defined(WITH_GFX_AZURE)
1625 case RDPGFX_CAPVERSION_111:
1626 case RDPGFX_CAPVERSION_112:
1627 case RDPGFX_CAPVERSION_113:
1628 case RDPGFX_CAPVERSION_115:
1629#endif
1630 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSendQoeAck))
1631 {
1632 RDPGFX_QOE_FRAME_ACKNOWLEDGE_PDU qoe = WINPR_C_ARRAY_INIT;
1633 UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1634
1635 if (diff > 65000)
1636 diff = 0;
1637
1638 qoe.frameId = pdu.frameId;
1639 qoe.timestamp = gfx->StartDecodingTime % UINT32_MAX;
1640 qoe.timeDiffSE = WINPR_ASSERTING_INT_CAST(UINT16, diff);
1641 qoe.timeDiffEDR = WINPR_ASSERTING_INT_CAST(UINT16, EndFrameTime);
1642
1643 if ((error = rdpgfx_send_qoe_frame_acknowledge_pdu(context, &qoe)))
1644 WLog_Print(gfx->base.log, WLOG_ERROR,
1645 "rdpgfx_send_qoe_frame_acknowledge_pdu failed with error %" PRIu32
1646 "",
1647 error);
1648 }
1649
1650 break;
1651
1652 default:
1653 break;
1654 }
1655
1656 return error;
1657}
1658
1666static UINT rdpgfx_recv_caps_reset_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1667{
1668 WINPR_ASSERT(callback);
1669 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1670 WINPR_ASSERT(gfx);
1671
1672 /* the PDU has no body, tolerate one */
1673 if (!Stream_SafeSeek(s, Stream_GetRemainingLength(s)))
1674 return ERROR_INVALID_DATA;
1675
1676 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvCapsResetPdu: sending the capabilities again");
1677
1678 free_surfaces(gfx->context, gfx->SurfaceTable);
1679 const UINT error = evict_cache_slots(gfx->context, gfx->MaxCacheSlots, gfx->CacheSlots);
1680 if (error)
1681 {
1682 // print error, but don't consider this a hard failure
1683 WLog_Print(gfx->base.log, WLOG_ERROR, "evict_cache_slots failed with error %" PRIu32 "",
1684 error);
1685 }
1686 gfx->UnacknowledgedFrames = 0;
1687 gfx->TotalDecodedFrames = 0;
1688
1689 return rdpgfx_send_supported_caps(callback);
1690}
1691
1692WINPR_ATTR_NODISCARD
1693static BOOL rdpgfx_codec_has_region_data_embedded(UINT16 codecId)
1694{
1695 switch (codecId)
1696 {
1697 case RDPGFX_CODECID_AVC420:
1698 case RDPGFX_CODECID_AVC444:
1699 case RDPGFX_CODECID_AVC444v2:
1700#if defined(WITH_GFX_AZURE)
1701 case RDPGFX_CODECID_HEVC420:
1702 case RDPGFX_CODECID_HEVC444:
1703#endif
1704#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1705 case RDPGFX_CODECID_AV1:
1706#endif
1707 return TRUE;
1708 default:
1709 return FALSE;
1710 }
1711}
1712
1718static UINT rdpgfx_recv_wire_to_surface_1_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1719{
1720 RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1721 RDPGFX_WIRE_TO_SURFACE_PDU_1 pdu = WINPR_C_ARRAY_INIT;
1722 WINPR_ASSERT(callback);
1723 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1724 UINT error = 0;
1725
1726 WINPR_ASSERT(gfx);
1727 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1728 return ERROR_INVALID_DATA;
1729
1730 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1731 Stream_Read_UINT16(s, pdu.codecId); /* codecId (2 bytes) */
1732 Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1733
1734 if ((error = rdpgfx_read_rect16(
1735 gfx->base.log, s, &(pdu.destRect),
1736 rdpgfx_codec_has_region_data_embedded(pdu.codecId)))) /* destRect (8 bytes) */
1737 {
1738 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "",
1739 error);
1740 return error;
1741 }
1742
1743 Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1744
1745 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1746 return ERROR_INVALID_DATA;
1747
1748 pdu.bitmapData = Stream_Pointer(s);
1749 Stream_Seek(s, pdu.bitmapDataLength);
1750
1751 WLog_Print(gfx->base.log, WLOG_TRACE,
1752 "RecvWireToSurface1Pdu: surfaceId: %" PRIu16 " codecId: %s (0x%04" PRIX16
1753 ") pixelFormat: 0x%02" PRIX8 " "
1754 "destRect: left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16
1755 " bitmapDataLength: %" PRIu32 "",
1756 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId, pdu.pixelFormat,
1757 pdu.destRect.left, pdu.destRect.top, pdu.destRect.right, pdu.destRect.bottom,
1758 pdu.bitmapDataLength);
1759 cmd.surfaceId = pdu.surfaceId;
1760 cmd.codecId = pdu.codecId;
1761 cmd.contextId = 0;
1762
1763 switch (pdu.pixelFormat)
1764 {
1765 case GFX_PIXEL_FORMAT_XRGB_8888:
1766 cmd.format = PIXEL_FORMAT_BGRX32;
1767 break;
1768
1769 case GFX_PIXEL_FORMAT_ARGB_8888:
1770 cmd.format = PIXEL_FORMAT_BGRA32;
1771 break;
1772
1773 default:
1774 return ERROR_INVALID_DATA;
1775 }
1776
1777 cmd.left = pdu.destRect.left;
1778 cmd.top = pdu.destRect.top;
1779 cmd.right = pdu.destRect.right;
1780 cmd.bottom = pdu.destRect.bottom;
1781 cmd.width = cmd.right - cmd.left;
1782 cmd.height = cmd.bottom - cmd.top;
1783 cmd.length = pdu.bitmapDataLength;
1784 cmd.data = pdu.bitmapData;
1785 cmd.extra = nullptr;
1786
1787 if (cmd.right < cmd.left)
1788 {
1789 WLog_Print(gfx->base.log, WLOG_ERROR,
1790 "RecvWireToSurface1Pdu right=%" PRIu32 " < left=%" PRIu32, cmd.right, cmd.left);
1791 return ERROR_INVALID_DATA;
1792 }
1793 if (cmd.bottom < cmd.top)
1794 {
1795 WLog_Print(gfx->base.log, WLOG_ERROR,
1796 "RecvWireToSurface1Pdu bottom=%" PRIu32 " < top=%" PRIu32, cmd.bottom, cmd.top);
1797 return ERROR_INVALID_DATA;
1798 }
1799
1800 if ((error = rdpgfx_decode(gfx, &cmd)))
1801 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_decode failed with error %" PRIu32 "!",
1802 error);
1803
1804 return error;
1805}
1806
1807WINPR_ATTR_NODISCARD
1808static BOOL checkSurfaceCommand(const RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1809{
1810 switch (cmd->codecId)
1811 {
1812 case RDPGFX_CODECID_UNCOMPRESSED:
1813 case RDPGFX_CODECID_CAVIDEO:
1814 case RDPGFX_CODECID_CLEARCODEC:
1815 case RDPGFX_CODECID_PLANAR:
1816 case RDPGFX_CODECID_AVC420:
1817 case RDPGFX_CODECID_ALPHA:
1818 case RDPGFX_CODECID_AVC444:
1819 case RDPGFX_CODECID_AVC444v2:
1820 case RDPGFX_CODECID_CAPROGRESSIVE:
1821 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1822#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
1823 case RDPGFX_CODECID_AV1:
1824#endif
1825#if defined(WITH_GFX_AZURE)
1826 case RDPGFX_CODECID_HEVC420:
1827 case RDPGFX_CODECID_HEVC444:
1828#endif
1829 if (!gfx->allowedCodecs[cmd->codecId])
1830 {
1831 char buffer[128] = WINPR_C_ARRAY_INIT;
1832 WLog_Print(
1833 gfx->base.log, WLOG_ERROR,
1834 "RDPGFX_SURFACE_COMMAND::codecId %s not allowed with %s [%s]",
1835 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)),
1836 rdpgfx_caps_version_str(gfx->ConnectionCaps.version),
1837 rdpgfx_caps_flags_str(gfx->ConnectionCaps.flags, buffer, sizeof(buffer)));
1838 return FALSE;
1839 }
1840 return TRUE;
1841 default:
1842 WLog_Print(gfx->base.log, WLOG_ERROR,
1843 "Unknown RDPGFX_SURFACE_COMMAND::codecId %" PRIu32, cmd->codecId);
1844 return FALSE;
1845 }
1846}
1847
1848UINT logSurfaceCommand(RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1849{
1850 WINPR_ASSERT(gfx);
1851
1852 WLog_Print(gfx->base.log, WLOG_DEBUG, "Got GFX %s",
1853 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)));
1854
1855 RdpgfxClientContext* context = gfx->context;
1856 if (!context)
1857 return CHANNEL_RC_OK;
1858
1859 if (!checkSurfaceCommand(gfx, cmd))
1860 return CHANNEL_RC_NULL_DATA;
1861
1862 const UINT error = IFCALLRESULT(CHANNEL_RC_OK, context->SurfaceCommand, context, cmd);
1863
1864 if (error)
1865 WLog_Print(gfx->base.log, WLOG_ERROR,
1866 "context->SurfaceCommand failed with error %" PRIu32 "", error);
1867
1868 rdpgfx_codecid_event(context, cmd->codecId, cmd->length);
1869 return error;
1870}
1871
1877static UINT rdpgfx_recv_wire_to_surface_2_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1878{
1879 RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1880 RDPGFX_WIRE_TO_SURFACE_PDU_2 pdu = WINPR_C_ARRAY_INIT;
1881
1882 WINPR_ASSERT(callback);
1883 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1884 WINPR_ASSERT(gfx);
1885
1886 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1887 return ERROR_INVALID_DATA;
1888
1889 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1890 Stream_Read_UINT16(s, pdu.codecId); /* codecId (2 bytes) */
1891 Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1892 Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1893 Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1894 pdu.bitmapData = Stream_Pointer(s);
1895 if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1896 return ERROR_INVALID_DATA;
1897
1898 WLog_Print(gfx->base.log, WLOG_TRACE,
1899 "RecvWireToSurface2Pdu: surfaceId: %" PRIu16 " codecId: %s (0x%04" PRIX16 ") "
1900 "codecContextId: %" PRIu32 " pixelFormat: 0x%02" PRIX8 " bitmapDataLength: %" PRIu32
1901 "",
1902 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1903 pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1904
1905 cmd.surfaceId = pdu.surfaceId;
1906 cmd.codecId = pdu.codecId;
1907 cmd.contextId = pdu.codecContextId;
1908
1909 switch (pdu.pixelFormat)
1910 {
1911 case GFX_PIXEL_FORMAT_XRGB_8888:
1912 cmd.format = PIXEL_FORMAT_BGRX32;
1913 break;
1914
1915 case GFX_PIXEL_FORMAT_ARGB_8888:
1916 cmd.format = PIXEL_FORMAT_BGRA32;
1917 break;
1918
1919 default:
1920 return ERROR_INVALID_DATA;
1921 }
1922
1923 cmd.length = pdu.bitmapDataLength;
1924 cmd.data = pdu.bitmapData;
1925 cmd.extra = nullptr;
1926
1927 if (cmd.codecId != RDPGFX_CODECID_CAPROGRESSIVE)
1928 return ERROR_INVALID_DATA;
1929
1930 return logSurfaceCommand(gfx, &cmd);
1931}
1932
1938static UINT rdpgfx_recv_delete_encoding_context_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1939{
1940 RDPGFX_DELETE_ENCODING_CONTEXT_PDU pdu = WINPR_C_ARRAY_INIT;
1941 WINPR_ASSERT(callback);
1942 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1943 WINPR_ASSERT(gfx);
1944 RdpgfxClientContext* context = gfx->context;
1945 UINT error = CHANNEL_RC_OK;
1946
1947 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1948 return ERROR_INVALID_DATA;
1949
1950 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1951 Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1952
1953 WLog_Print(gfx->base.log, WLOG_DEBUG,
1954 "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16 " codecContextId: %" PRIu32 "",
1955 pdu.surfaceId, pdu.codecContextId);
1956
1957 if (context)
1958 {
1959 IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1960
1961 if (error)
1962 WLog_Print(gfx->base.log, WLOG_ERROR,
1963 "context->DeleteEncodingContext failed with error %" PRIu32 "", error);
1964 }
1965
1966 return error;
1967}
1968
1974static UINT rdpgfx_recv_solid_fill_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1975{
1976 RDPGFX_SOLID_FILL_PDU pdu = WINPR_C_ARRAY_INIT;
1977 WINPR_ASSERT(callback);
1978 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1979 WINPR_ASSERT(gfx);
1980 RdpgfxClientContext* context = gfx->context;
1981
1982 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1983 return ERROR_INVALID_DATA;
1984
1985 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1986
1987 UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
1988 if (error != CHANNEL_RC_OK) /* fillPixel (4 bytes) */
1989 {
1990 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_color32 failed with error %" PRIu32 "!",
1991 error);
1992 return error;
1993 }
1994
1995 Stream_Read_UINT16(s, pdu.fillRectCount); /* fillRectCount (2 bytes) */
1996
1997 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
1998 return ERROR_INVALID_DATA;
1999
2000 pdu.fillRects = (RECTANGLE_16*)calloc(pdu.fillRectCount, sizeof(RECTANGLE_16));
2001
2002 if (!pdu.fillRects)
2003 {
2004 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2005 return CHANNEL_RC_NO_MEMORY;
2006 }
2007
2008 for (UINT16 index = 0; index < pdu.fillRectCount; index++)
2009 {
2010 RECTANGLE_16* fillRect = &(pdu.fillRects[index]);
2011
2012 if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect, FALSE)))
2013 {
2014 WLog_Print(gfx->base.log, WLOG_ERROR,
2015 "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error);
2016 free(pdu.fillRects);
2017 return error;
2018 }
2019 }
2020 WLog_Print(gfx->base.log, WLOG_TRACE,
2021 "RecvSolidFillPdu: surfaceId: %" PRIu16 " fillRectCount: %" PRIu16 "", pdu.surfaceId,
2022 pdu.fillRectCount);
2023
2024 if (context)
2025 {
2026 IFCALLRET(context->SolidFill, error, context, &pdu);
2027
2028 if (error)
2029 WLog_Print(gfx->base.log, WLOG_ERROR,
2030 "context->SolidFill failed with error %" PRIu32 "", error);
2031 }
2032
2033 free(pdu.fillRects);
2034 return error;
2035}
2036
2042static UINT rdpgfx_recv_surface_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2043{
2044 RDPGFX_SURFACE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
2045 WINPR_ASSERT(callback);
2046 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2047 WINPR_ASSERT(gfx);
2048 RdpgfxClientContext* context = gfx->context;
2049 UINT error = 0;
2050
2051 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
2052 return ERROR_INVALID_DATA;
2053
2054 Stream_Read_UINT16(s, pdu.surfaceIdSrc); /* surfaceIdSrc (2 bytes) */
2055 Stream_Read_UINT16(s, pdu.surfaceIdDest); /* surfaceIdDest (2 bytes) */
2056
2057 if ((error =
2058 rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE))) /* rectSrc (8 bytes ) */
2059 {
2060 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
2061 error);
2062 return error;
2063 }
2064
2065 Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
2066
2067 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2068 return ERROR_INVALID_DATA;
2069
2070 pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
2071
2072 if (!pdu.destPts)
2073 {
2074 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2075 return CHANNEL_RC_NO_MEMORY;
2076 }
2077
2078 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2079 {
2080 RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
2081
2082 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2083 {
2084 WLog_Print(gfx->base.log, WLOG_ERROR,
2085 "rdpgfx_read_point16 failed with error %" PRIu32 "!", error);
2086 free(pdu.destPts);
2087 return error;
2088 }
2089 }
2090
2091 WLog_Print(gfx->base.log, WLOG_TRACE,
2092 "RecvSurfaceToSurfacePdu: surfaceIdSrc: %" PRIu16 " surfaceIdDest: %" PRIu16 " "
2093 "left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16
2094 " destPtsCount: %" PRIu16 "",
2095 pdu.surfaceIdSrc, pdu.surfaceIdDest, pdu.rectSrc.left, pdu.rectSrc.top,
2096 pdu.rectSrc.right, pdu.rectSrc.bottom, pdu.destPtsCount);
2097
2098 if (context)
2099 {
2100 IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
2101
2102 if (error)
2103 WLog_Print(gfx->base.log, WLOG_ERROR,
2104 "context->SurfaceToSurface failed with error %" PRIu32 "", error);
2105 }
2106
2107 free(pdu.destPts);
2108 return error;
2109}
2110
2116static UINT rdpgfx_recv_surface_to_cache_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2117{
2118 RDPGFX_SURFACE_TO_CACHE_PDU pdu = WINPR_C_ARRAY_INIT;
2119 WINPR_ASSERT(callback);
2120 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2121
2122 WINPR_ASSERT(gfx);
2123 RdpgfxClientContext* context = gfx->context;
2124
2125 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2126 return ERROR_INVALID_DATA;
2127
2128 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2129 Stream_Read_UINT64(s, pdu.cacheKey); /* cacheKey (8 bytes) */
2130 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
2131
2132 UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc), FALSE);
2133 if (error != CHANNEL_RC_OK) /* rectSrc (8 bytes ) */
2134 {
2135 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
2136 error);
2137 return error;
2138 }
2139
2140 WLog_Print(gfx->base.log, WLOG_TRACE,
2141 "RecvSurfaceToCachePdu: surfaceId: %" PRIu16 " cacheKey: 0x%016" PRIX64
2142 " cacheSlot: %" PRIu16 " "
2143 "left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16 "",
2144 pdu.surfaceId, pdu.cacheKey, pdu.cacheSlot, pdu.rectSrc.left, pdu.rectSrc.top,
2145 pdu.rectSrc.right, pdu.rectSrc.bottom);
2146
2147 if (context)
2148 {
2149 IFCALLRET(context->SurfaceToCache, error, context, &pdu);
2150
2151 if (error)
2152 WLog_Print(gfx->base.log, WLOG_ERROR,
2153 "context->SurfaceToCache failed with error %" PRIu32 "", error);
2154 }
2155
2156 return error;
2157}
2158
2164static UINT rdpgfx_recv_cache_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2165{
2166 RDPGFX_CACHE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
2167 WINPR_ASSERT(callback);
2168 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2169
2170 WINPR_ASSERT(gfx);
2171 RdpgfxClientContext* context = gfx->context;
2172 UINT error = CHANNEL_RC_OK;
2173
2174 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
2175 return ERROR_INVALID_DATA;
2176
2177 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
2178 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2179 Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
2180
2181 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
2182 return ERROR_INVALID_DATA;
2183
2184 pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
2185
2186 if (!pdu.destPts)
2187 {
2188 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2189 return CHANNEL_RC_NO_MEMORY;
2190 }
2191
2192 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
2193 {
2194 RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
2195
2196 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
2197 {
2198 WLog_Print(gfx->base.log, WLOG_ERROR,
2199 "rdpgfx_read_point16 failed with error %" PRIu32 "", error);
2200 free(pdu.destPts);
2201 return error;
2202 }
2203 }
2204
2205 WLog_Print(gfx->base.log, WLOG_TRACE,
2206 "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16 " surfaceId: %" PRIu16
2207 " destPtsCount: %" PRIu16 "",
2208 pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
2209
2210 if (context)
2211 {
2212 IFCALLRET(context->CacheToSurface, error, context, &pdu);
2213
2214 if (error)
2215 WLog_Print(gfx->base.log, WLOG_ERROR,
2216 "context->CacheToSurface failed with error %" PRIu32 "", error);
2217 }
2218
2219 free(pdu.destPts);
2220 return error;
2221}
2222
2228static UINT rdpgfx_recv_map_surface_to_output_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2229{
2230 RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
2231 WINPR_ASSERT(callback);
2232 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2233
2234 WINPR_ASSERT(gfx);
2235 RdpgfxClientContext* context = gfx->context;
2236 UINT error = CHANNEL_RC_OK;
2237
2238 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
2239 return ERROR_INVALID_DATA;
2240
2241 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2242 Stream_Read_UINT16(s, pdu.reserved); /* reserved (2 bytes) */
2243 Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
2244 Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
2245
2246 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2247 {
2248 WLog_Print(gfx->base.log, WLOG_ERROR,
2249 "context->MapSurfaceToOutput outputOriginX[%" PRIu32 "], outputOriginY[%" PRIu32
2250 "] > %" PRIu32,
2251 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2252 return ERROR_INVALID_DATA;
2253 }
2254
2255 WLog_Print(gfx->base.log, WLOG_DEBUG,
2256 "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
2257 " outputOriginY: %" PRIu32 "",
2258 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
2259
2260 if (context)
2261 {
2262 IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
2263
2264 if (error)
2265 WLog_Print(gfx->base.log, WLOG_ERROR,
2266 "context->MapSurfaceToOutput failed with error %" PRIu32 "", error);
2267 }
2268
2269 return error;
2270}
2271
2272static UINT rdpgfx_recv_map_surface_to_scaled_output_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2273 wStream* s)
2274{
2275 RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
2276 WINPR_ASSERT(callback);
2277 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2278
2279 WINPR_ASSERT(gfx);
2280 RdpgfxClientContext* context = gfx->context;
2281 UINT error = CHANNEL_RC_OK;
2282
2283 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2284 return ERROR_INVALID_DATA;
2285
2286 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2287 Stream_Read_UINT16(s, pdu.reserved); /* reserved (2 bytes) */
2288 Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
2289 Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
2290 Stream_Read_UINT32(s, pdu.targetWidth); /* targetWidth (4 bytes) */
2291 Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
2292
2293 if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2294 {
2295 WLog_Print(gfx->base.log, WLOG_ERROR,
2296 "context->MapSurfaceToScaledOutput outputOriginX[%" PRIu32
2297 "], outputOriginY[%" PRIu32 "] > %" PRIu32,
2298 pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2299 return ERROR_INVALID_DATA;
2300 }
2301 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2302 {
2303 WLog_Print(gfx->base.log, WLOG_ERROR,
2304 "context->MapSurfaceToScaledOutput targetWidth[%" PRIu32
2305 "], targetHeight[%" PRIu32 "] > %" PRIu32,
2306 pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2307 return ERROR_INVALID_DATA;
2308 }
2309
2310 WLog_Print(gfx->base.log, WLOG_DEBUG,
2311 "RecvMapSurfaceToScaledOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
2312 " outputOriginY: %" PRIu32 " targetWidth: %" PRIu32 " targetHeight: %" PRIu32,
2313 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY, pdu.targetWidth,
2314 pdu.targetHeight);
2315
2316 if (context)
2317 {
2318 IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
2319
2320 if (error)
2321 WLog_Print(gfx->base.log, WLOG_ERROR,
2322 "context->MapSurfaceToScaledOutput failed with error %" PRIu32 "", error);
2323 }
2324
2325 return error;
2326}
2327
2333static UINT rdpgfx_recv_map_surface_to_window_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2334{
2335 RDPGFX_MAP_SURFACE_TO_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2336 WINPR_ASSERT(callback);
2337 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2338
2339 WINPR_ASSERT(gfx);
2340 RdpgfxClientContext* context = gfx->context;
2341 UINT error = CHANNEL_RC_OK;
2342
2343 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
2344 return ERROR_INVALID_DATA;
2345
2346 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2347 Stream_Read_UINT64(s, pdu.windowId); /* windowId (8 bytes) */
2348 Stream_Read_UINT32(s, pdu.mappedWidth); /* mappedWidth (4 bytes) */
2349 Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2350
2351 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2352 {
2353 WLog_Print(gfx->base.log, WLOG_ERROR,
2354 "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2355 "] is 0",
2356 pdu.mappedWidth, pdu.mappedHeight);
2357 return ERROR_INVALID_DATA;
2358 }
2359 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2360 {
2361 WLog_Print(gfx->base.log, WLOG_ERROR,
2362 "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2363 "] > %" PRIu32,
2364 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2365 return ERROR_INVALID_DATA;
2366 }
2367
2368 WLog_Print(gfx->base.log, WLOG_DEBUG,
2369 "RecvMapSurfaceToWindowPdu: surfaceId: %" PRIu16 " windowId: 0x%016" PRIX64
2370 " mappedWidth: %" PRIu32 " mappedHeight: %" PRIu32 "",
2371 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight);
2372
2373 if (context && context->MapSurfaceToWindow)
2374 {
2375 IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
2376
2377 if (error)
2378 WLog_Print(gfx->base.log, WLOG_ERROR,
2379 "context->MapSurfaceToWindow failed with error %" PRIu32 "", error);
2380 }
2381
2382 return error;
2383}
2384
2385static UINT rdpgfx_recv_map_surface_to_scaled_window_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2386 wStream* s)
2387{
2388 RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2389 WINPR_ASSERT(callback);
2390 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2391 WINPR_ASSERT(gfx);
2392 RdpgfxClientContext* context = gfx->context;
2393 UINT error = CHANNEL_RC_OK;
2394
2395 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
2396 return ERROR_INVALID_DATA;
2397
2398 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2399 Stream_Read_UINT64(s, pdu.windowId); /* windowId (8 bytes) */
2400 Stream_Read_UINT32(s, pdu.mappedWidth); /* mappedWidth (4 bytes) */
2401 Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2402 Stream_Read_UINT32(s, pdu.targetWidth); /* targetWidth (4 bytes) */
2403 Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
2404
2405 if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2406 {
2407 WLog_Print(gfx->base.log, WLOG_ERROR,
2408 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2409 "], mappedHeight[%" PRIu32 "] is 0",
2410 pdu.mappedWidth, pdu.mappedHeight);
2411 return ERROR_INVALID_DATA;
2412 }
2413 if ((pdu.targetWidth == 0) || (pdu.targetHeight == 0))
2414 {
2415 WLog_Print(gfx->base.log, WLOG_ERROR,
2416 "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2417 "], targetHeight[%" PRIu32 "] is 0",
2418 pdu.targetWidth, pdu.targetHeight);
2419 return ERROR_INVALID_DATA;
2420 }
2421
2422 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2423 {
2424 WLog_Print(gfx->base.log, WLOG_ERROR,
2425 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2426 "], mappedHeight[%" PRIu32 "] > %" PRIu32,
2427 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2428 return ERROR_INVALID_DATA;
2429 }
2430 if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2431 {
2432 WLog_Print(gfx->base.log, WLOG_ERROR,
2433 "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2434 "], targetHeight[%" PRIu32 "] > %" PRIu32,
2435 pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2436 return ERROR_INVALID_DATA;
2437 }
2438
2439 WLog_Print(gfx->base.log, WLOG_DEBUG,
2440 "RecvMapSurfaceToScaledWindowPdu: surfaceId: %" PRIu16 " windowId: 0x%016" PRIX64
2441 " mappedWidth: %" PRIu32 " mappedHeight: %" PRIu32 " targetWidth: %" PRIu32
2442 " targetHeight: %" PRIu32 "",
2443 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight, pdu.targetWidth,
2444 pdu.targetHeight);
2445
2446 if (context && context->MapSurfaceToScaledWindow)
2447 {
2448 IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
2449
2450 if (error)
2451 WLog_Print(gfx->base.log, WLOG_ERROR,
2452 "context->MapSurfaceToScaledWindow failed with error %" PRIu32 "", error);
2453 }
2454
2455 return error;
2456}
2457
2463static UINT rdpgfx_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
2464{
2465 RDPGFX_HEADER header = WINPR_C_ARRAY_INIT;
2466 wStream sbuffer = WINPR_C_ARRAY_INIT;
2467
2468 WINPR_ASSERT(callback);
2469 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2470
2471 WINPR_ASSERT(gfx);
2472
2473 size_t packetlen = 0;
2474 UINT error = rdpgfx_read_header(gfx->base.log, data, &header, &packetlen);
2475 if (error != CHANNEL_RC_OK)
2476 {
2477 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_header failed with error %" PRIu32 "!",
2478 error);
2479 return error;
2480 }
2481
2482 /* Limit the PDU parsers to the data of this PDU, the stream might contain more of them */
2483 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
2484 Stream_Seek(data, packetlen);
2485
2486 WLog_Print(gfx->base.log, WLOG_TRACE,
2487 "cmdId: %s (0x%04" PRIX16 ") flags: 0x%04" PRIX16 " pduLength: %" PRIu32 "",
2488 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
2489 header.pduLength);
2490
2491 rdpgfx_stats_cmdid_event(gfx->context, header.cmdId);
2492 switch (header.cmdId)
2493 {
2494 case RDPGFX_CMDID_WIRETOSURFACE_1:
2495 if ((error = rdpgfx_recv_wire_to_surface_1_pdu(callback, s)))
2496 WLog_Print(gfx->base.log, WLOG_ERROR,
2497 "rdpgfx_recv_wire_to_surface_1_pdu failed with error %" PRIu32 "!",
2498 error);
2499
2500 break;
2501
2502 case RDPGFX_CMDID_WIRETOSURFACE_2:
2503 if ((error = rdpgfx_recv_wire_to_surface_2_pdu(callback, s)))
2504 WLog_Print(gfx->base.log, WLOG_ERROR,
2505 "rdpgfx_recv_wire_to_surface_2_pdu failed with error %" PRIu32 "!",
2506 error);
2507
2508 break;
2509
2510 case RDPGFX_CMDID_DELETEENCODINGCONTEXT:
2511 if ((error = rdpgfx_recv_delete_encoding_context_pdu(callback, s)))
2512 WLog_Print(gfx->base.log, WLOG_ERROR,
2513 "rdpgfx_recv_delete_encoding_context_pdu failed with error %" PRIu32 "!",
2514 error);
2515
2516 break;
2517
2518 case RDPGFX_CMDID_SOLIDFILL:
2519 if ((error = rdpgfx_recv_solid_fill_pdu(callback, s)))
2520 WLog_Print(gfx->base.log, WLOG_ERROR,
2521 "rdpgfx_recv_solid_fill_pdu failed with error %" PRIu32 "!", error);
2522
2523 break;
2524
2525 case RDPGFX_CMDID_SURFACETOSURFACE:
2526 if ((error = rdpgfx_recv_surface_to_surface_pdu(callback, s)))
2527 WLog_Print(gfx->base.log, WLOG_ERROR,
2528 "rdpgfx_recv_surface_to_surface_pdu failed with error %" PRIu32 "!",
2529 error);
2530
2531 break;
2532
2533 case RDPGFX_CMDID_SURFACETOCACHE:
2534 if ((error = rdpgfx_recv_surface_to_cache_pdu(callback, s)))
2535 WLog_Print(gfx->base.log, WLOG_ERROR,
2536 "rdpgfx_recv_surface_to_cache_pdu failed with error %" PRIu32 "!",
2537 error);
2538
2539 break;
2540
2541 case RDPGFX_CMDID_CACHETOSURFACE:
2542 if ((error = rdpgfx_recv_cache_to_surface_pdu(callback, s)))
2543 WLog_Print(gfx->base.log, WLOG_ERROR,
2544 "rdpgfx_recv_cache_to_surface_pdu failed with error %" PRIu32 "!",
2545 error);
2546
2547 break;
2548
2549 case RDPGFX_CMDID_EVICTCACHEENTRY:
2550 if ((error = rdpgfx_recv_evict_cache_entry_pdu(callback, s)))
2551 WLog_Print(gfx->base.log, WLOG_ERROR,
2552 "rdpgfx_recv_evict_cache_entry_pdu failed with error %" PRIu32 "!",
2553 error);
2554
2555 break;
2556
2557 case RDPGFX_CMDID_CREATESURFACE:
2558 if ((error = rdpgfx_recv_create_surface_pdu(callback, s)))
2559 WLog_Print(gfx->base.log, WLOG_ERROR,
2560 "rdpgfx_recv_create_surface_pdu failed with error %" PRIu32 "!", error);
2561
2562 break;
2563
2564 case RDPGFX_CMDID_DELETESURFACE:
2565 if ((error = rdpgfx_recv_delete_surface_pdu(callback, s)))
2566 WLog_Print(gfx->base.log, WLOG_ERROR,
2567 "rdpgfx_recv_delete_surface_pdu failed with error %" PRIu32 "!", error);
2568
2569 break;
2570
2571 case RDPGFX_CMDID_STARTFRAME:
2572 if ((error = rdpgfx_recv_start_frame_pdu(callback, s)))
2573 WLog_Print(gfx->base.log, WLOG_ERROR,
2574 "rdpgfx_recv_start_frame_pdu failed with error %" PRIu32 "!", error);
2575
2576 break;
2577
2578 case RDPGFX_CMDID_ENDFRAME:
2579 if ((error = rdpgfx_recv_end_frame_pdu(callback, s)))
2580 WLog_Print(gfx->base.log, WLOG_ERROR,
2581 "rdpgfx_recv_end_frame_pdu failed with error %" PRIu32 "!", error);
2582
2583 break;
2584
2585 case RDPGFX_CMDID_RESETGRAPHICS:
2586 if ((error = rdpgfx_recv_reset_graphics_pdu(callback, s)))
2587 WLog_Print(gfx->base.log, WLOG_ERROR,
2588 "rdpgfx_recv_reset_graphics_pdu failed with error %" PRIu32 "!", error);
2589
2590 break;
2591
2592 case RDPGFX_CMDID_MAPSURFACETOOUTPUT:
2593 if ((error = rdpgfx_recv_map_surface_to_output_pdu(callback, s)))
2594 WLog_Print(gfx->base.log, WLOG_ERROR,
2595 "rdpgfx_recv_map_surface_to_output_pdu failed with error %" PRIu32 "!",
2596 error);
2597
2598 break;
2599
2600 case RDPGFX_CMDID_CACHEIMPORTREPLY:
2601 if ((error = rdpgfx_recv_cache_import_reply_pdu(callback, s)))
2602 WLog_Print(gfx->base.log, WLOG_ERROR,
2603 "rdpgfx_recv_cache_import_reply_pdu failed with error %" PRIu32 "!",
2604 error);
2605
2606 break;
2607
2608 case RDPGFX_CMDID_CAPSCONFIRM:
2609 if ((error = rdpgfx_recv_caps_confirm_pdu(callback, s)))
2610 WLog_Print(gfx->base.log, WLOG_ERROR,
2611 "rdpgfx_recv_caps_confirm_pdu failed with error %" PRIu32 "!", error);
2612
2613 if ((error = rdpgfx_send_cache_offer(gfx)))
2614 WLog_Print(gfx->base.log, WLOG_ERROR,
2615 "rdpgfx_send_cache_offer failed with error %" PRIu32 "!", error);
2616
2617 break;
2618
2619 case RDPGFX_CMDID_MAPSURFACETOWINDOW:
2620 if ((error = rdpgfx_recv_map_surface_to_window_pdu(callback, s)))
2621 WLog_Print(gfx->base.log, WLOG_ERROR,
2622 "rdpgfx_recv_map_surface_to_window_pdu failed with error %" PRIu32 "!",
2623 error);
2624
2625 break;
2626
2627 case RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW:
2628 if ((error = rdpgfx_recv_map_surface_to_scaled_window_pdu(callback, s)))
2629 WLog_Print(gfx->base.log, WLOG_ERROR,
2630 "rdpgfx_recv_map_surface_to_scaled_window_pdu failed with error %" PRIu32
2631 "!",
2632 error);
2633
2634 break;
2635
2636 case RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT:
2637 if ((error = rdpgfx_recv_map_surface_to_scaled_output_pdu(callback, s)))
2638 WLog_Print(gfx->base.log, WLOG_ERROR,
2639 "rdpgfx_recv_map_surface_to_scaled_output_pdu failed with error %" PRIu32
2640 "!",
2641 error);
2642
2643 break;
2644
2645 case RDPGFX_CMDID_PROTECT_SURFACE:
2646 case RDPGFX_CMDID_WATERMARK:
2647 WLog_Print(gfx->base.log, WLOG_WARN,
2648 "Command %s not implemented, skipping %" PRIu32 " bytes",
2649 rdpgfx_get_cmd_id_string(header.cmdId), header.pduLength);
2650 if (!Stream_SafeSeek(s, packetlen))
2651 error = ERROR_INVALID_DATA;
2652 break;
2653
2654 case RDPGFX_CMDID_UNUSED_0014:
2655 /* servers send diagnostic information with this command, it carries no graphics */
2656 WLog_Print(gfx->base.log, WLOG_DEBUG,
2657 "Diagnostic information from the server, skipping %" PRIu32 " bytes",
2658 header.pduLength);
2659 if (!Stream_SafeSeek(s, packetlen))
2660 error = ERROR_INVALID_DATA;
2661 break;
2662
2663 case RDPGFX_CMDID_CAPSRESET:
2664 if ((error = rdpgfx_recv_caps_reset_pdu(callback, s)))
2665 WLog_Print(gfx->base.log, WLOG_ERROR,
2666 "rdpgfx_recv_caps_reset_pdu failed with error %" PRIu32 "!", error);
2667 break;
2668
2669 default:
2670 error = CHANNEL_RC_BAD_PROC;
2671 break;
2672 }
2673
2674 if (error)
2675 {
2676 WLog_Print(gfx->base.log, WLOG_ERROR,
2677 "Error while processing GFX cmdId: %s (0x%04" PRIX16 ")",
2678 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
2679 return error;
2680 }
2681
2682 const size_t end = Stream_GetPosition(s);
2683
2684 if (end != packetlen)
2685 {
2686 WLog_Print(gfx->base.log, WLOG_ERROR,
2687 "Unexpected gfx pdu end: Actual: %" PRIuz ", Expected: %" PRIuz, end, packetlen);
2688 }
2689
2690 return error;
2691}
2692
2698static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
2699{
2700 UINT32 DstSize = 0;
2701 BYTE* pDstData = nullptr;
2702 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2703 WINPR_ASSERT(callback);
2704 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2705 UINT error = CHANNEL_RC_OK;
2706
2707 WINPR_ASSERT(gfx);
2708 int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2709 (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2710
2711 if ((status < 0) || (DstSize == 0))
2712 {
2713 WLog_Print(gfx->base.log, WLOG_ERROR,
2714 "zgfx_decompress failure! status: %d, DstSize=%" PRIu32, status, DstSize);
2715 free(pDstData);
2716 return ERROR_INTERNAL_ERROR;
2717 }
2718
2719 wStream sbuffer = WINPR_C_ARRAY_INIT;
2720 wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2721
2722 if (!s)
2723 {
2724 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2725 free(pDstData);
2726 return CHANNEL_RC_NO_MEMORY;
2727 }
2728
2729 while (Stream_GetPosition(s) < Stream_Length(s))
2730 {
2731 if ((error = rdpgfx_recv_pdu(callback, s)))
2732 {
2733 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_recv_pdu failed with error %" PRIu32 "!",
2734 error);
2735 break;
2736 }
2737 }
2738
2739 free(pDstData);
2740 return error;
2741}
2742
2748static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2749{
2750 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2751 WINPR_ASSERT(callback);
2752 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2753 WINPR_ASSERT(gfx);
2754 RdpgfxClientContext* context = gfx->context;
2755 UINT error = CHANNEL_RC_OK;
2756 BOOL do_caps_advertise = TRUE;
2757
2758 gfx->sendFrameAcks = TRUE;
2759
2760 if (context)
2761 {
2762 IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2763
2764 if (error)
2765 WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnOpen failed with error %" PRIu32 "",
2766 error);
2767
2768 RdpgfxClientContextInt* ctx = (RdpgfxClientContextInt*)context;
2769 const RdpgfxClientContextStats empty = WINPR_C_ARRAY_INIT;
2770 ctx->stats = empty;
2771 }
2772
2773 if (do_caps_advertise)
2774 error = rdpgfx_send_supported_caps(callback);
2775
2776 return error;
2777}
2778
2779static void rdpgfx_dump_stats(RdpgfxClientContext* context)
2780{
2781 WINPR_ASSERT(context);
2782
2783 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2784 WINPR_ASSERT(gfx);
2785
2786 wLog* log = gfx->base.log;
2787 const DWORD level = WLOG_TRACE;
2788 if (!WLog_IsLevelActive(log, level))
2789 return;
2790
2791 WLog_Print(log, level, "RdpgfxClientContextStats");
2792 for (size_t x = 0; x < rdpgfx_stats_max_index(); x++)
2793 {
2794 const char* name = rdpgfx_stats_name_for_index(x);
2795 const uint64_t val = rdpgfx_stats_value_for_index(context, x);
2796 WLog_Print(log, level, "%s: %" PRIu64, name, val);
2797 }
2798}
2799
2805static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2806{
2807 UINT error = CHANNEL_RC_OK;
2808 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2809 WINPR_ASSERT(callback);
2810 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2811
2812 if (!gfx)
2813 goto fail;
2814
2815 RdpgfxClientContext* context = gfx->context;
2816
2817 WLog_Print(gfx->base.log, WLOG_DEBUG, "OnClose");
2818 error = rdpgfx_save_persistent_cache(gfx);
2819
2820 if (error)
2821 {
2822 // print error, but don't consider this a hard failure
2823 WLog_Print(gfx->base.log, WLOG_ERROR,
2824 "rdpgfx_save_persistent_cache failed with error %" PRIu32 "", error);
2825 }
2826
2827 free_surfaces(context, gfx->SurfaceTable);
2828 error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2829 if (error)
2830 {
2831 // print error, but don't consider this a hard failure
2832 WLog_Print(gfx->base.log, WLOG_ERROR, "evict_cache_slots failed with error %" PRIu32 "",
2833 error);
2834 }
2835
2836 free(callback);
2837 gfx->UnacknowledgedFrames = 0;
2838 gfx->TotalDecodedFrames = 0;
2839
2840 if (context)
2841 {
2842 error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2843 if (error)
2844 {
2845 // print error, but don't consider this a hard failure
2846 WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnClose failed with error %" PRIu32 "",
2847 error);
2848 }
2849 rdpgfx_dump_stats(context);
2850 }
2851
2852fail:
2853 return CHANNEL_RC_OK;
2854}
2855
2856static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
2857{
2858 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2859 WINPR_ASSERT(gfx);
2860 RdpgfxClientContext* context = gfx->context;
2861
2862 WLog_Print(gfx->base.log, WLOG_DEBUG, "Terminated");
2863 rdpgfx_client_context_free(context);
2864}
2865
2871static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId, void* pData)
2872{
2873 WINPR_ASSERT(context);
2874 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2875 WINPR_ASSERT(gfx);
2876 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2877
2878 if (pData)
2879 {
2880 if (!HashTable_Insert(gfx->SurfaceTable, (void*)key, pData))
2881 return ERROR_BAD_ARGUMENTS;
2882 }
2883 else
2884 HashTable_Remove(gfx->SurfaceTable, (void*)key);
2885
2886 return CHANNEL_RC_OK;
2887}
2888
2894static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2895 UINT16* count_out)
2896{
2897 ULONG_PTR* pKeys = nullptr;
2898 WINPR_ASSERT(context);
2899 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2900 WINPR_ASSERT(gfx);
2901 size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2902
2903 WINPR_ASSERT(ppSurfaceIds);
2904 WINPR_ASSERT(count_out);
2905 if (count < 1)
2906 {
2907 *count_out = 0;
2908 return CHANNEL_RC_OK;
2909 }
2910
2911 UINT16* pSurfaceIds = (UINT16*)calloc(count, sizeof(UINT16));
2912
2913 if (!pSurfaceIds)
2914 {
2915 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2916 free(pKeys);
2917 return CHANNEL_RC_NO_MEMORY;
2918 }
2919
2920 for (size_t index = 0; index < count; index++)
2921 {
2922 pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2923 }
2924
2925 free(pKeys);
2926 *ppSurfaceIds = pSurfaceIds;
2927 *count_out = (UINT16)count;
2928 return CHANNEL_RC_OK;
2929}
2930
2931static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2932{
2933 WINPR_ASSERT(context);
2934 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2935 WINPR_ASSERT(gfx);
2936 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2937 return HashTable_GetItemValue(gfx->SurfaceTable, (void*)key);
2938}
2939
2945static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot, void* pData)
2946{
2947 WINPR_ASSERT(context);
2948 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2949
2950 WINPR_ASSERT(gfx);
2951 /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2952 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2953 {
2954 WLog_Print(gfx->base.log, WLOG_ERROR,
2955 "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2956 gfx->MaxCacheSlots);
2957 return ERROR_INVALID_INDEX;
2958 }
2959
2960 gfx->CacheSlots[cacheSlot - 1] = pData;
2961 return CHANNEL_RC_OK;
2962}
2963
2964static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2965{
2966 WINPR_ASSERT(context);
2967 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2968 WINPR_ASSERT(gfx);
2969 /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2970 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2971 {
2972 WLog_Print(gfx->base.log, WLOG_ERROR,
2973 "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2974 gfx->MaxCacheSlots);
2975 return nullptr;
2976 }
2977
2978 return gfx->CacheSlots[cacheSlot - 1];
2979}
2980
2981static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext,
2982 WINPR_ATTR_UNUSED rdpSettings* settings)
2983{
2984 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2985
2986 WINPR_ASSERT(base);
2987 gfx->rdpcontext = rcontext;
2988
2989 gfx->SurfaceTable = HashTable_New(TRUE);
2990 if (!gfx->SurfaceTable)
2991 {
2992 WLog_Print(gfx->base.log, WLOG_ERROR, "HashTable_New for surfaces failed !");
2993 return CHANNEL_RC_NO_MEMORY;
2994 }
2995
2996 gfx->suspendFrameAcks =
2997 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSuspendFrameAck);
2998 gfx->MaxCacheSlots =
2999 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) ? 4096 : 25600;
3000
3001 RdpgfxClientContextInt* context =
3002 (RdpgfxClientContextInt*)calloc(1, sizeof(RdpgfxClientContextInt));
3003 if (!context)
3004 {
3005 WLog_Print(gfx->base.log, WLOG_ERROR, "context calloc failed!");
3006 HashTable_Free(gfx->SurfaceTable);
3007 gfx->SurfaceTable = nullptr;
3008 return CHANNEL_RC_NO_MEMORY;
3009 }
3010
3011 gfx->zgfx = zgfx_context_new(FALSE);
3012 if (!gfx->zgfx)
3013 {
3014 WLog_Print(gfx->base.log, WLOG_ERROR, "zgfx_context_new failed!");
3015 HashTable_Free(gfx->SurfaceTable);
3016 gfx->SurfaceTable = nullptr;
3017 free(context);
3018 return CHANNEL_RC_NO_MEMORY;
3019 }
3020
3021 context->common.handle = (void*)gfx;
3022 context->common.GetSurfaceIds = rdpgfx_get_surface_ids;
3023 context->common.SetSurfaceData = rdpgfx_set_surface_data;
3024 context->common.GetSurfaceData = rdpgfx_get_surface_data;
3025 context->common.SetCacheSlotData = rdpgfx_set_cache_slot_data;
3026 context->common.GetCacheSlotData = rdpgfx_get_cache_slot_data;
3027 context->common.CapsAdvertise = rdpgfx_send_caps_advertise_pdu;
3028 context->common.FrameAcknowledge = rdpgfx_send_frame_acknowledge_pdu;
3029 context->common.CacheImportOffer = rdpgfx_send_cache_import_offer_pdu;
3030 context->common.QoeFrameAcknowledge = rdpgfx_send_qoe_frame_acknowledge_pdu;
3031
3032 gfx->base.iface.pInterface = (void*)context;
3033 gfx->context = &context->common;
3034 return CHANNEL_RC_OK;
3035}
3036
3037void rdpgfx_client_context_free(RdpgfxClientContext* context)
3038{
3039 if (!context)
3040 return;
3041
3042 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
3043
3044 free_surfaces(context, gfx->SurfaceTable);
3045 evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
3046
3047 if (gfx->zgfx)
3048 {
3049 zgfx_context_free(gfx->zgfx);
3050 gfx->zgfx = nullptr;
3051 }
3052
3053 HashTable_Free(gfx->SurfaceTable);
3054 free(context);
3055}
3056
3057static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
3058 rdpgfx_on_open, rdpgfx_on_close,
3059 nullptr };
3060
3066FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
3067{
3068 return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
3069 sizeof(RDPGFX_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
3070 &rdpgfx_callbacks, init_plugin_cb, terminate_plugin_cb);
3071}
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