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
42#include "rdpgfx_common.h"
43#include "rdpgfx_codec.h"
44
45#include "rdpgfx_main.h"
46
47#define GFXTAG CHANNELS_TAG("rdpgfx.client")
48
49#define RDPGFX_NUMBER_CAPSETS 0x100
50
51typedef struct
52{
53 uint64_t cntGfxCodecID[RDPGFX_CODECID_MAX];
54 uint64_t cntGfxCommandID[RDPGFX_CMDID_MAX + 1];
55} RdpgfxClientContextStats;
56
57typedef struct
58{
59 RdpgfxClientContext common;
60 RdpgfxClientContextStats stats;
61} RdpgfxClientContextInt;
62
63static const UINT32 MAX_SURFACE_SIZE = 32766;
64static const UINT32 MAX_MONITOR_COUNT = 16;
65
66size_t rdpgfx_stats_max_index(void)
67{
68 return sizeof(RdpgfxClientContextStats) / sizeof(uint64_t);
69}
70
71const char* rdpgfx_stats_name_for_index(size_t index)
72{
73 if (index < RDPGFX_CODECID_MAX)
74 return rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
75
76 index -= RDPGFX_CODECID_MAX;
77 if (index < RDPGFX_CMDID_MAX)
78 return rdpgfx_get_cmd_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
79 return "RDPGFX_STATS_UNUSED";
80}
81
82uint64_t rdpgfx_stats_value_for_index(RdpgfxClientContext* context, size_t index)
83{
84 WINPR_ASSERT(context);
85
86 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
87
88 if (index < RDPGFX_CODECID_MAX)
89 return intCtx->stats.cntGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
90
91 index -= RDPGFX_CODECID_MAX;
92 if (index < RDPGFX_CMDID_MAX)
93 return intCtx->stats.cntGfxCommandID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
94 return 0;
95}
96
97static void rdpgfx_codecid_event(RdpgfxClientContext* context, uint32_t index)
98{
99 if (!context)
100 return;
101 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
102 WINPR_ASSERT(intCtx);
103 if (index < RDPGFX_CODECID_MAX)
104 intCtx->stats.cntGfxCodecID[index]++;
105 else
106 {
107 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
108 WINPR_ASSERT(gfx);
109 WLog_Print(gfx->base.log, WLOG_WARN, "Invalid event index %" PRIu32, index);
110 }
111}
112
113static void rdpgfx_stats_cmdid_event(RdpgfxClientContext* context, uint32_t index)
114{
115 if (!context)
116 return;
117 RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
118 WINPR_ASSERT(intCtx);
119
120 if (index >= RDPGFX_CMDID_MAX)
121 {
122 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
123 WINPR_ASSERT(gfx);
124 WLog_Print(gfx->base.log, WLOG_WARN, "Invalid codecID %" PRIu32, index);
125 index = RDPGFX_CMDID_MAX;
126 }
127 intCtx->stats.cntGfxCommandID[index]++;
128}
129
130static BOOL delete_surface(const void* key, void* value, void* arg)
131{
132 const UINT16 id = (UINT16)(uintptr_t)(key);
133 if (id < 1)
134 return FALSE;
135
136 RdpgfxClientContext* context = arg;
137 const RDPGFX_DELETE_SURFACE_PDU pdu = { .surfaceId = id - 1 };
138
139 WINPR_UNUSED(value);
140
141 if (context)
142 {
143 UINT error = CHANNEL_RC_OK;
144 IFCALLRET(context->DeleteSurface, error, context, &pdu);
145
146 if (error)
147 {
148 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
149 WINPR_ASSERT(gfx);
150 WLog_Print(gfx->base.log, WLOG_ERROR,
151 "context->DeleteSurface failed with error %" PRIu32 "", error);
152 }
153 }
154 return TRUE;
155}
156
157static void free_surfaces(RdpgfxClientContext* context, wHashTable* SurfaceTable)
158{
159 WINPR_ASSERT(context);
160 if (!HashTable_Foreach(SurfaceTable, delete_surface, context))
161 {
162 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
163 WINPR_ASSERT(gfx);
164 WLog_Print(gfx->base.log, WLOG_WARN, "delete_surface failed");
165 }
166}
167
168static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots, void** CacheSlots)
169{
170 UINT error = CHANNEL_RC_OK;
171
172 WINPR_ASSERT(CacheSlots);
173 for (UINT16 index = 0; index < MaxCacheSlots; index++)
174 {
175 if (CacheSlots[index])
176 {
177 const RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = { .cacheSlot = index + 1 };
178
179 if (context && context->EvictCacheEntry)
180 {
181 const UINT rc = context->EvictCacheEntry(context, &pdu);
182 if (rc != CHANNEL_RC_OK)
183 error = rc;
184 }
185
186 CacheSlots[index] = nullptr;
187 }
188 }
189 return error;
190}
191
197static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
198 const RDPGFX_CAPS_ADVERTISE_PDU* pdu)
199{
200 UINT error = CHANNEL_RC_OK;
201
202 WINPR_ASSERT(pdu);
203 WINPR_ASSERT(context);
204
205 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
206
207 if (!gfx || !gfx->base.listener_callback)
208 return ERROR_BAD_ARGUMENTS;
209
210 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
211
212 RDPGFX_HEADER header = { .flags = 0,
213 .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
214 .pduLength = RDPGFX_HEADER_SIZE + 2 };
215
216 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
217 {
218 const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
219 header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
220 }
221
222 WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCapsAdvertisePdu %" PRIu16 "", pdu->capsSetCount);
223 wStream* s = Stream_New(nullptr, header.pduLength);
224
225 if (!s)
226 {
227 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
228 return CHANNEL_RC_NO_MEMORY;
229 }
230
231 if ((error = rdpgfx_write_header(s, &header)))
232 goto fail;
233
234 /* RDPGFX_CAPS_ADVERTISE_PDU */
235 Stream_Write_UINT16(s, pdu->capsSetCount); /* capsSetCount (2 bytes) */
236
237 for (UINT16 index = 0; index < pdu->capsSetCount; index++)
238 {
239 const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
240
241 WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending %s [0x%08" PRIx32 "] flags=0x%08" PRIx32,
242 rdpgfx_caps_version_str(capsSet->version), capsSet->version, capsSet->flags);
243
244 Stream_Write_UINT32(s, capsSet->version); /* version (4 bytes) */
245 Stream_Write_UINT32(s, capsSet->length); /* capsDataLength (4 bytes) */
246 Stream_Write_UINT32(s, capsSet->flags); /* capsData (4 bytes) */
247 if (capsSet->length < 4)
248 goto fail;
249 Stream_Zero(s, capsSet->length - 4);
250 }
251
252 Stream_SealLength(s);
253 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
254 nullptr);
255fail:
256 Stream_Free(s, TRUE);
257 return error;
258}
259
260static BOOL rdpgfx_is_capability_filtered(RDPGFX_PLUGIN* gfx, UINT32 caps)
261{
262 WINPR_ASSERT(gfx);
263 const UINT32 filter =
264 freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCapsFilter);
265 const UINT32 capList[] = {
266#if defined(WITH_GFX_AV1)
267 RDPGFX_CAPVERSION_FRDP_1,
268#endif
269 RDPGFX_CAPVERSION_8,
270 RDPGFX_CAPVERSION_81,
271 RDPGFX_CAPVERSION_10,
272 RDPGFX_CAPVERSION_101,
273 RDPGFX_CAPVERSION_102,
274 RDPGFX_CAPVERSION_103,
275 RDPGFX_CAPVERSION_104,
276 RDPGFX_CAPVERSION_105,
277 RDPGFX_CAPVERSION_106,
278 RDPGFX_CAPVERSION_106_ERR,
279 RDPGFX_CAPVERSION_107
280#if defined(WITH_GFX_AZURE)
281 ,
282 RDPGFX_CAPVERSION_111,
283 RDPGFX_CAPVERSION_112,
284 RDPGFX_CAPVERSION_113
285#endif
286 };
287
288 for (size_t x = 0; x < ARRAYSIZE(capList); x++)
289 {
290 if (caps == capList[x])
291 return (filter & (1u << x)) != 0;
292 }
293
294 return TRUE;
295}
296
297WINPR_ATTR_NODISCARD
298static RDPGFX_CAPSET* nextCapset(RDPGFX_CAPS_ADVERTISE_PDU* pdu, size_t count)
299{
300 WINPR_ASSERT(pdu);
301 WINPR_ASSERT(pdu->capsSets);
302 WINPR_ASSERT(count > 0);
303 WINPR_ASSERT(pdu->capsSetCount < count);
304 if (pdu->capsSetCount >= count)
305 return nullptr;
306 return &pdu->capsSets[pdu->capsSetCount++];
307}
308
314static UINT rdpgfx_send_supported_caps(GENERIC_CHANNEL_CALLBACK* callback)
315{
316 RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
317
318 if (!callback)
319 return ERROR_BAD_ARGUMENTS;
320
321 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
322
323 if (!gfx)
324 return ERROR_BAD_CONFIGURATION;
325
326 RdpgfxClientContext* context = gfx->context;
327
328 if (!context)
329 return ERROR_BAD_CONFIGURATION;
330
331 RDPGFX_CAPS_ADVERTISE_PDU pdu = { .capsSetCount = 0, .capsSets = capsSets };
332
333 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
334 {
335 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
336 if (!capsSet)
337 return ERROR_BAD_CONFIGURATION;
338 capsSet->version = RDPGFX_CAPVERSION_8;
339 capsSet->length = 4;
340 capsSet->flags = 0;
341
342 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
343 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
344
345 /* in CAPVERSION_8 the spec says that we should not have both
346 * thinclient and smallcache (and thinclient implies a small cache)
347 */
348 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) &&
349 !freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
350 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
351 }
352
353 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
354 {
355 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
356 if (!capsSet)
357 return ERROR_BAD_CONFIGURATION;
358 capsSet->version = RDPGFX_CAPVERSION_81;
359 capsSet->length = 4;
360 capsSet->flags = 0;
361
362 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
363 capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
364
365 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
366 capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
367
368#ifdef WITH_GFX_H264
369
370 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264))
371 capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
372
373#endif
374 }
375
376 UINT32 caps10Flags = 0;
377 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264) ||
378 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444))
379 {
380 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
381 caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
382
383#ifdef WITH_GFX_H264
384
385 if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444))
386 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
387
388#else
389 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
390#endif
391
392 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
393 {
394 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
395 if (!capsSet)
396 return ERROR_BAD_CONFIGURATION;
397 capsSet->version = RDPGFX_CAPVERSION_10;
398 capsSet->length = 4;
399 capsSet->flags = caps10Flags;
400 }
401
402 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
403 {
404 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
405 if (!capsSet)
406 return ERROR_BAD_CONFIGURATION;
407 capsSet->version = RDPGFX_CAPVERSION_101;
408 capsSet->length = 0x10;
409 capsSet->flags = 0;
410 }
411
412 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
413 {
414 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
415 if (!capsSet)
416 return ERROR_BAD_CONFIGURATION;
417 capsSet->version = RDPGFX_CAPVERSION_102;
418 capsSet->length = 0x4;
419 capsSet->flags = caps10Flags;
420 }
421
422 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
423 {
424 if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
425 caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
426 }
427
428 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
429 {
430 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
431 if (!capsSet)
432 return ERROR_BAD_CONFIGURATION;
433 capsSet->version = RDPGFX_CAPVERSION_103;
434 capsSet->length = 0x4;
435 capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
436 }
437
438 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
439 {
440 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
441 if (!capsSet)
442 return ERROR_BAD_CONFIGURATION;
443 capsSet->version = RDPGFX_CAPVERSION_104;
444 capsSet->length = 0x4;
445 capsSet->flags = caps10Flags;
446 }
447
448 /* The following capabilities expect support for image scaling.
449 * Disable these for builds that do not have support for that.
450 */
451#if defined(WITH_CAIRO) || defined(WITH_SWSCALE)
452 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
453 {
454 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
455 if (!capsSet)
456 return ERROR_BAD_CONFIGURATION;
457 capsSet->version = RDPGFX_CAPVERSION_105;
458 capsSet->length = 0x4;
459 capsSet->flags = caps10Flags;
460 }
461
462 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
463 {
464 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
465 if (!capsSet)
466 return ERROR_BAD_CONFIGURATION;
467 capsSet->version = RDPGFX_CAPVERSION_106;
468 capsSet->length = 0x4;
469 capsSet->flags = caps10Flags;
470 }
471
472 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
473 {
474 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
475 if (!capsSet)
476 return ERROR_BAD_CONFIGURATION;
477 capsSet->version = RDPGFX_CAPVERSION_106_ERR;
478 capsSet->length = 0x4;
479 capsSet->flags = caps10Flags;
480 }
481#endif
482
483 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
484 {
485 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
486 if (!capsSet)
487 return ERROR_BAD_CONFIGURATION;
488 capsSet->version = RDPGFX_CAPVERSION_107;
489 capsSet->length = 0x4;
490 capsSet->flags = caps10Flags;
491#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
492 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
493#endif
494 }
495
496#if defined(WITH_GFX_AZURE)
497 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_111))
498 {
499 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
500 if (!capsSet)
501 return ERROR_BAD_CONFIGURATION;
502 capsSet->version = RDPGFX_CAPVERSION_111;
503 capsSet->length = 0x4;
504 capsSet->flags = caps10Flags;
505#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
506 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
507#endif
508 }
509
510 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_112))
511 {
512 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
513 if (!capsSet)
514 return ERROR_BAD_CONFIGURATION;
515 capsSet->version = RDPGFX_CAPVERSION_112;
516 capsSet->length = 0x4;
517 capsSet->flags = caps10Flags;
518#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
519 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
520#endif
521 }
522
523 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_113))
524 {
525 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
526 if (!capsSet)
527 return ERROR_BAD_CONFIGURATION;
528 capsSet->version = RDPGFX_CAPVERSION_113;
529 capsSet->length = 0x4;
530 capsSet->flags = caps10Flags;
531#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
532 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
533#endif
534 }
535#endif
536 }
537
538#if defined(WITH_GFX_AV1)
539 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1))
540 {
541 if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
542 {
543 RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
544 if (!capsSet)
545 return ERROR_BAD_CONFIGURATION;
546 capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
547 capsSet->length = 0x4;
548
549 capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
550 const UINT32 profile =
551 freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1Profile);
552 if (profile == 0)
553 capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
554
555#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
556 capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
557#endif
558 }
559 }
560#endif
561
562 return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
563}
564
570static UINT rdpgfx_recv_caps_confirm_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
571{
572 WINPR_ASSERT(callback);
573 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
574
575 WINPR_ASSERT(gfx);
576 RdpgfxClientContext* context = gfx->context;
577
578 RDPGFX_CAPSET capsSet = WINPR_C_ARRAY_INIT;
579 RDPGFX_CAPS_CONFIRM_PDU pdu = { .capsSet = &capsSet };
580
581 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
582 return ERROR_INVALID_DATA;
583
584 Stream_Read_UINT32(s, capsSet.version); /* version (4 bytes) */
585 Stream_Read_UINT32(s, capsSet.length); /* capsDataLength (4 bytes) */
586 Stream_Read_UINT32(s, capsSet.flags); /* capsData (4 bytes) */
587 gfx->TotalDecodedFrames = 0;
588 gfx->ConnectionCaps = capsSet;
589 WLog_Print(gfx->base.log, WLOG_DEBUG,
590 "RecvCapsConfirmPdu: version: %s [0x%08" PRIX32 "] flags: 0x%08" PRIX32 "",
591 rdpgfx_caps_version_str(capsSet.version), capsSet.version, capsSet.flags);
592
593 if (!context)
594 return ERROR_BAD_CONFIGURATION;
595
596 return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
597}
598
604static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
606{
607 UINT error = 0;
608
609 if (!context || !pdu)
610 return ERROR_BAD_ARGUMENTS;
611
612 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
613
614 if (!gfx || !gfx->base.listener_callback)
615 return ERROR_BAD_CONFIGURATION;
616
617 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
618
619 if (!callback)
620 return ERROR_BAD_CONFIGURATION;
621
622 const RDPGFX_HEADER header = { .flags = 0,
623 .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
624 .pduLength = RDPGFX_HEADER_SIZE + 12 };
625
626 WLog_Print(gfx->base.log, WLOG_TRACE, "SendFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
627
628 wStream* s = Stream_New(nullptr, header.pduLength);
629
630 if (!s)
631 {
632 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
633 return CHANNEL_RC_NO_MEMORY;
634 }
635
636 if ((error = rdpgfx_write_header(s, &header)))
637 goto fail;
638
639 /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
640 Stream_Write_UINT32(s, pdu->queueDepth); /* queueDepth (4 bytes) */
641 Stream_Write_UINT32(s, pdu->frameId); /* frameId (4 bytes) */
642 Stream_Write_UINT32(s, pdu->totalFramesDecoded); /* totalFramesDecoded (4 bytes) */
643 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
644 nullptr);
645
646 if (error == CHANNEL_RC_OK) /* frame successfully acked */
647 gfx->UnacknowledgedFrames--;
648
649fail:
650 Stream_Free(s, TRUE);
651 return error;
652}
653
654static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
656{
657 UINT error = CHANNEL_RC_OK;
658 const RDPGFX_HEADER header = { .flags = 0,
659 .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
660 .pduLength = RDPGFX_HEADER_SIZE + 12 };
661
662 if (!context || !pdu)
663 return ERROR_BAD_ARGUMENTS;
664
665 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
666
667 if (!gfx || !gfx->base.listener_callback)
668 return ERROR_BAD_CONFIGURATION;
669
670 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
671
672 if (!callback)
673 return ERROR_BAD_CONFIGURATION;
674
675 WLog_Print(gfx->base.log, WLOG_TRACE, "SendQoeFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
676 wStream* s = Stream_New(nullptr, header.pduLength);
677
678 if (!s)
679 {
680 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
681 return CHANNEL_RC_NO_MEMORY;
682 }
683
684 if ((error = rdpgfx_write_header(s, &header)))
685 goto fail;
686
687 /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
688 Stream_Write_UINT32(s, pdu->frameId);
689 Stream_Write_UINT32(s, pdu->timestamp);
690 Stream_Write_UINT16(s, pdu->timeDiffSE);
691 Stream_Write_UINT16(s, pdu->timeDiffEDR);
692 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
693 nullptr);
694fail:
695 Stream_Free(s, TRUE);
696 return error;
697}
698
704static UINT rdpgfx_recv_reset_graphics_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
705{
706 RDPGFX_RESET_GRAPHICS_PDU pdu = WINPR_C_ARRAY_INIT;
707 WINPR_ASSERT(callback);
708
709 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
710
711 WINPR_ASSERT(gfx);
712
713 RdpgfxClientContext* context = gfx->context;
714 UINT error = CHANNEL_RC_OK;
715 GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
716
717 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
718 return ERROR_INVALID_DATA;
719
720 Stream_Read_UINT32(s, pdu.width); /* width (4 bytes) */
721 Stream_Read_UINT32(s, pdu.height); /* height (4 bytes) */
722 Stream_Read_UINT32(s, pdu.monitorCount); /* monitorCount (4 bytes) */
723
724 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE) ||
725 (pdu.monitorCount > MAX_MONITOR_COUNT))
726 {
727 WLog_Print(gfx->base.log, WLOG_ERROR,
728 "context->ResetGraphics Width[%" PRIu32 "], Height[%" PRIu32 "] > %" PRIu32
729 " or monitorCount[%" PRIu32 " > %" PRIu32,
730 pdu.width, pdu.width, MAX_SURFACE_SIZE, pdu.monitorCount, MAX_MONITOR_COUNT);
731 return ERROR_INVALID_DATA;
732 }
733
734 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
735 return ERROR_INVALID_DATA;
736
737 pdu.monitorDefArray = (MONITOR_DEF*)calloc(pdu.monitorCount, sizeof(MONITOR_DEF));
738
739 if (!pdu.monitorDefArray)
740 {
741 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
742 return CHANNEL_RC_NO_MEMORY;
743 }
744
745 for (UINT32 index = 0; index < pdu.monitorCount; index++)
746 {
747 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
748 Stream_Read_INT32(s, monitor->left); /* left (4 bytes) */
749 Stream_Read_INT32(s, monitor->top); /* top (4 bytes) */
750 Stream_Read_INT32(s, monitor->right); /* right (4 bytes) */
751 Stream_Read_INT32(s, monitor->bottom); /* bottom (4 bytes) */
752 Stream_Read_UINT32(s, monitor->flags); /* flags (4 bytes) */
753 }
754
755 const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
756 if (size > 340)
757 {
758 free(pdu.monitorDefArray);
759 return CHANNEL_RC_NULL_DATA;
760 }
761 const size_t pad = 340ULL - size;
762
763 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (size_t)pad))
764 {
765 free(pdu.monitorDefArray);
766 return CHANNEL_RC_NO_MEMORY;
767 }
768
769 Stream_Seek(s, pad); /* pad (total size is 340 bytes) */
770 WLog_Print(gfx->base.log, WLOG_DEBUG,
771 "RecvResetGraphicsPdu: width: %" PRIu32 " height: %" PRIu32 " count: %" PRIu32 "",
772 pdu.width, pdu.height, pdu.monitorCount);
773
774 for (UINT32 index = 0; index < pdu.monitorCount; index++)
775 {
776 MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
777 WLog_Print(gfx->base.log, WLOG_TRACE,
778 "RecvResetGraphicsPdu: monitor left:%" PRIi32 " top:%" PRIi32 " right:%" PRIi32
779 " bottom:%" PRIi32 " flags:0x%" PRIx32 "",
780 monitor->left, monitor->top, monitor->right, monitor->bottom, monitor->flags);
781 }
782
783 if (context)
784 {
785 IFCALLRET(context->ResetGraphics, error, context, &pdu);
786
787 if (error)
788 WLog_Print(gfx->base.log, WLOG_ERROR,
789 "context->ResetGraphics failed with error %" PRIu32 "", error);
790 }
791
792 free(pdu.monitorDefArray);
793
794 /* some listeners may be interested (namely the display channel) */
795 EventArgsInit(&graphicsReset, "libfreerdp");
796 graphicsReset.width = pdu.width;
797 graphicsReset.height = pdu.height;
798 if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
799 return ERROR_INTERNAL_ERROR;
800 return error;
801}
802
808static UINT rdpgfx_recv_evict_cache_entry_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
809{
810 RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = WINPR_C_ARRAY_INIT;
811 WINPR_ASSERT(callback);
812 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
813 WINPR_ASSERT(gfx);
814 RdpgfxClientContext* context = gfx->context;
815 UINT error = CHANNEL_RC_OK;
816
817 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
818 return ERROR_INVALID_DATA;
819
820 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
821 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16 "",
822 pdu.cacheSlot);
823
824 if (context)
825 {
826 IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
827
828 if (error)
829 WLog_Print(gfx->base.log, WLOG_ERROR,
830 "context->EvictCacheEntry failed with error %" PRIu32 "", error);
831 }
832
833 return error;
834}
835
841static UINT rdpgfx_save_persistent_cache(RDPGFX_PLUGIN* gfx)
842{
843 UINT error = CHANNEL_RC_OK;
844
845 WINPR_ASSERT(gfx);
846 WINPR_ASSERT(gfx->rdpcontext);
847 rdpSettings* settings = gfx->rdpcontext->settings;
848 RdpgfxClientContext* context = gfx->context;
849
850 WINPR_ASSERT(context);
851 WINPR_ASSERT(settings);
852
853 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
854 return CHANNEL_RC_OK;
855
856 const char* BitmapCachePersistFile =
857 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
858 if (!BitmapCachePersistFile)
859 return CHANNEL_RC_OK;
860
861 if (!context->ExportCacheEntry)
862 return CHANNEL_RC_INITIALIZATION_ERROR;
863
864 rdpPersistentCache* persistent = persistent_cache_new();
865
866 if (!persistent)
867 return CHANNEL_RC_NO_MEMORY;
868
869 if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
870 {
871 error = CHANNEL_RC_INITIALIZATION_ERROR;
872 goto fail;
873 }
874
875 for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
876 {
877 if (gfx->CacheSlots[idx])
878 {
879 const UINT16 cacheSlot = idx;
880 PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
881
882 if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
883 continue;
884
885 if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
886 goto fail;
887 }
888 }
889
890 persistent_cache_free(persistent);
891
892 return error;
893fail:
894 persistent_cache_free(persistent);
895 return error;
896}
897
903static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
905{
906 UINT error = CHANNEL_RC_OK;
907
908 if (!context || !pdu)
909 return ERROR_BAD_ARGUMENTS;
910
911 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
912
913 if (!gfx || !gfx->base.listener_callback)
914 return ERROR_BAD_CONFIGURATION;
915
916 GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
917
918 if (!callback)
919 return ERROR_BAD_CONFIGURATION;
920
921 const RDPGFX_HEADER header = { .flags = 0,
922 .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
923 .pduLength =
924 RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
925
926 WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16 "",
927 pdu->cacheEntriesCount);
928 wStream* s = Stream_New(nullptr, header.pduLength);
929
930 if (!s)
931 {
932 WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
933 return CHANNEL_RC_NO_MEMORY;
934 }
935
936 if ((error = rdpgfx_write_header(s, &header)))
937 goto fail;
938
939 if (pdu->cacheEntriesCount <= 0)
940 {
941 WLog_Print(gfx->base.log, WLOG_ERROR, "Invalid cacheEntriesCount: %" PRIu16 "",
942 pdu->cacheEntriesCount);
943 error = ERROR_INVALID_DATA;
944 goto fail;
945 }
946
947 /* cacheEntriesCount (2 bytes) */
948 Stream_Write_UINT16(s, pdu->cacheEntriesCount);
949
950 for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
951 {
952 const RDPGFX_CACHE_ENTRY_METADATA* cacheEntry = &(pdu->cacheEntries[index]);
953 Stream_Write_UINT64(s, cacheEntry->cacheKey); /* cacheKey (8 bytes) */
954 Stream_Write_UINT32(s, cacheEntry->bitmapLength); /* bitmapLength (4 bytes) */
955 }
956
957 error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
958 nullptr);
959
960fail:
961 Stream_Free(s, TRUE);
962 return error;
963}
964
970static UINT rdpgfx_send_cache_offer(RDPGFX_PLUGIN* gfx)
971{
972 int count = 0;
973 UINT error = CHANNEL_RC_OK;
974 PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
975 RDPGFX_CACHE_IMPORT_OFFER_PDU* offer = nullptr;
976
977 WINPR_ASSERT(gfx);
978 WINPR_ASSERT(gfx->rdpcontext);
979
980 RdpgfxClientContext* context = gfx->context;
981 rdpSettings* settings = gfx->rdpcontext->settings;
982
983 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
984 return CHANNEL_RC_OK;
985
986 const char* BitmapCachePersistFile =
987 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
988 if (!BitmapCachePersistFile)
989 return CHANNEL_RC_OK;
990
991 rdpPersistentCache* persistent = persistent_cache_new();
992
993 if (!persistent)
994 return CHANNEL_RC_NO_MEMORY;
995
996 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
997 {
998 error = CHANNEL_RC_INITIALIZATION_ERROR;
999 goto fail;
1000 }
1001
1002 if (persistent_cache_get_version(persistent) != 3)
1003 {
1004 error = ERROR_INVALID_DATA;
1005 goto fail;
1006 }
1007
1008 count = persistent_cache_get_count(persistent);
1009 if (count < 0)
1010 {
1011 error = ERROR_INVALID_DATA;
1012 goto fail;
1013 }
1014
1015 if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
1016 count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
1017
1018 if (count > gfx->MaxCacheSlots)
1019 count = gfx->MaxCacheSlots;
1020
1022 if (!offer)
1023 {
1024 error = CHANNEL_RC_NO_MEMORY;
1025 goto fail;
1026 }
1027
1028 WINPR_ASSERT(count <= UINT16_MAX);
1029 offer->cacheEntriesCount = (UINT16)count;
1030
1031 WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending Cache Import Offer: %d", count);
1032
1033 for (int idx = 0; idx < count; idx++)
1034 {
1035 if (persistent_cache_read_entry(persistent, &entry) < 1)
1036 {
1037 error = ERROR_INVALID_DATA;
1038 goto fail;
1039 }
1040
1041 offer->cacheEntries[idx].cacheKey = entry.key64;
1042 offer->cacheEntries[idx].bitmapLength = entry.size;
1043 }
1044
1045 if (offer->cacheEntriesCount > 0)
1046 {
1047 error = rdpgfx_send_cache_import_offer_pdu(context, offer);
1048 if (error != CHANNEL_RC_OK)
1049 {
1050 WLog_Print(gfx->base.log, WLOG_ERROR, "Failed to send cache import offer PDU");
1051 goto fail;
1052 }
1053 }
1054
1055fail:
1056 persistent_cache_free(persistent);
1057 free(offer);
1058 return error;
1059}
1060
1066static UINT rdpgfx_load_cache_import_reply(RDPGFX_PLUGIN* gfx,
1067 const RDPGFX_CACHE_IMPORT_REPLY_PDU* reply)
1068{
1069 UINT error = CHANNEL_RC_OK;
1070 rdpPersistentCache* persistent = nullptr;
1071 WINPR_ASSERT(gfx);
1072 WINPR_ASSERT(gfx->rdpcontext);
1073 rdpSettings* settings = gfx->rdpcontext->settings;
1074 RdpgfxClientContext* context = gfx->context;
1075
1076 WINPR_ASSERT(settings);
1077 WINPR_ASSERT(reply);
1078 if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1079 return CHANNEL_RC_OK;
1080
1081 const char* BitmapCachePersistFile =
1082 freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1083 if (!BitmapCachePersistFile)
1084 return CHANNEL_RC_OK;
1085
1086 persistent = persistent_cache_new();
1087
1088 if (!persistent)
1089 return CHANNEL_RC_NO_MEMORY;
1090
1091 if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1092 {
1093 error = CHANNEL_RC_INITIALIZATION_ERROR;
1094 goto fail;
1095 }
1096
1097 if (persistent_cache_get_version(persistent) != 3)
1098 {
1099 error = ERROR_INVALID_DATA;
1100 goto fail;
1101 }
1102
1103 int count = persistent_cache_get_count(persistent);
1104
1105 count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
1106
1107 WLog_Print(gfx->base.log, WLOG_DEBUG, "Receiving Cache Import Reply: %d", count);
1108
1109 for (int idx = 0; idx < count; idx++)
1110 {
1111 PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1112 if (persistent_cache_read_entry(persistent, &entry) < 1)
1113 {
1114 error = ERROR_INVALID_DATA;
1115 goto fail;
1116 }
1117
1118 const UINT16 cacheSlot = reply->cacheSlots[idx];
1119 if (context && context->ImportCacheEntry)
1120 {
1121 error = context->ImportCacheEntry(context, cacheSlot, &entry);
1122 if (error != CHANNEL_RC_OK)
1123 break;
1124 }
1125 }
1126
1127 persistent_cache_free(persistent);
1128
1129 return error;
1130fail:
1131 persistent_cache_free(persistent);
1132 return error;
1133}
1134
1140static UINT rdpgfx_recv_cache_import_reply_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1141{
1142 RDPGFX_CACHE_IMPORT_REPLY_PDU pdu = WINPR_C_ARRAY_INIT;
1143 WINPR_ASSERT(callback);
1144 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1145 WINPR_ASSERT(gfx);
1146 RdpgfxClientContext* context = gfx->context;
1147 UINT error = CHANNEL_RC_OK;
1148
1149 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1150 return ERROR_INVALID_DATA;
1151
1152 Stream_Read_UINT16(s, pdu.importedEntriesCount); /* cacheSlot (2 bytes) */
1153
1154 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1155 2ull))
1156 return ERROR_INVALID_DATA;
1157
1158 if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1159 return ERROR_INVALID_DATA;
1160
1161 for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1162 {
1163 Stream_Read_UINT16(s, pdu.cacheSlots[idx]); /* cacheSlot (2 bytes) */
1164 }
1165
1166 WLog_Print(gfx->base.log, WLOG_TRACE,
1167 "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16 "",
1168 pdu.importedEntriesCount);
1169
1170 error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1171
1172 if (error)
1173 {
1174 WLog_Print(gfx->base.log, WLOG_ERROR,
1175 "rdpgfx_load_cache_import_reply failed with error %" PRIu32 "", error);
1176 return error;
1177 }
1178
1179 if (context)
1180 {
1181 IFCALLRET(context->CacheImportReply, error, context, &pdu);
1182
1183 if (error)
1184 WLog_Print(gfx->base.log, WLOG_ERROR,
1185 "context->CacheImportReply failed with error %" PRIu32 "", error);
1186 }
1187
1188 return error;
1189}
1190
1196static UINT rdpgfx_recv_create_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1197{
1198 RDPGFX_CREATE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1199 WINPR_ASSERT(callback);
1200 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1201 WINPR_ASSERT(gfx);
1202 RdpgfxClientContext* context = gfx->context;
1203 UINT error = CHANNEL_RC_OK;
1204
1205 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1206 return ERROR_INVALID_DATA;
1207
1208 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1209 Stream_Read_UINT16(s, pdu.width); /* width (2 bytes) */
1210 Stream_Read_UINT16(s, pdu.height); /* height (2 bytes) */
1211 Stream_Read_UINT8(s, pdu.pixelFormat); /* RDPGFX_PIXELFORMAT (1 byte) */
1212
1213 if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE))
1214 {
1215 WLog_Print(gfx->base.log, WLOG_ERROR,
1216 "context->RecvCreateSurfacePdu Width[%" PRIu32 "], Height[%" PRIu32
1217 "] > %" PRIu32,
1218 pdu.width, pdu.height, MAX_SURFACE_SIZE);
1219 return ERROR_INVALID_DATA;
1220 }
1221
1222 WLog_Print(gfx->base.log, WLOG_DEBUG,
1223 "RecvCreateSurfacePdu: surfaceId: %" PRIu16 " width: %" PRIu16 " height: %" PRIu16
1224 " pixelFormat: 0x%02" PRIX8 "",
1225 pdu.surfaceId, pdu.width, pdu.height, pdu.pixelFormat);
1226
1227 if (context)
1228 {
1229 /* create surface PDU sometimes happens for surface ID that are already in use and have not
1230 * been removed yet. Ensure that there is no surface with the new ID by trying to remove it
1231 * manually.
1232 */
1233 RDPGFX_DELETE_SURFACE_PDU deletePdu = { pdu.surfaceId };
1234 const UINT drc = IFCALLRESULT(CHANNEL_RC_OK, context->DeleteSurface, context, &deletePdu);
1235 if (drc != CHANNEL_RC_OK)
1236 WLog_Print(gfx->base.log, WLOG_WARN,
1237 "context->DeleteSurface failed with error %" PRIu32 ", ignoring", drc);
1238
1239 IFCALLRET(context->CreateSurface, error, context, &pdu);
1240
1241 if (error)
1242 WLog_Print(gfx->base.log, WLOG_ERROR,
1243 "context->CreateSurface failed with error %" PRIu32 "", error);
1244 }
1245
1246 return error;
1247}
1248
1254static UINT rdpgfx_recv_delete_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1255{
1256 RDPGFX_DELETE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1257 WINPR_ASSERT(callback);
1258 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1259 WINPR_ASSERT(gfx);
1260 RdpgfxClientContext* context = gfx->context;
1261 UINT error = CHANNEL_RC_OK;
1262
1263 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1264 return ERROR_INVALID_DATA;
1265
1266 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1267 WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvDeleteSurfacePdu: surfaceId: %" PRIu16 "",
1268 pdu.surfaceId);
1269
1270 if (context)
1271 {
1272 IFCALLRET(context->DeleteSurface, error, context, &pdu);
1273
1274 if (error)
1275 WLog_Print(gfx->base.log, WLOG_ERROR,
1276 "context->DeleteSurface failed with error %" PRIu32 "", error);
1277 }
1278
1279 return error;
1280}
1281
1287static UINT rdpgfx_recv_start_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1288{
1289 RDPGFX_START_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1290 WINPR_ASSERT(callback);
1291 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1292 WINPR_ASSERT(gfx);
1293 RdpgfxClientContext* context = gfx->context;
1294 UINT error = CHANNEL_RC_OK;
1295
1296 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1297 return ERROR_INVALID_DATA;
1298
1299 Stream_Read_UINT32(s, pdu.timestamp); /* timestamp (4 bytes) */
1300 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1301 WLog_Print(gfx->base.log, WLOG_TRACE,
1302 "RecvStartFramePdu: frameId: %" PRIu32 " timestamp: 0x%08" PRIX32 "", pdu.frameId,
1303 pdu.timestamp);
1304 gfx->StartDecodingTime = GetTickCount64();
1305
1306 if (context)
1307 {
1308 IFCALLRET(context->StartFrame, error, context, &pdu);
1309
1310 if (error)
1311 WLog_Print(gfx->base.log, WLOG_ERROR,
1312 "context->StartFrame failed with error %" PRIu32 "", error);
1313 }
1314
1315 gfx->UnacknowledgedFrames++;
1316 return error;
1317}
1318
1324static UINT rdpgfx_recv_end_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1325{
1326 RDPGFX_END_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1327 RDPGFX_FRAME_ACKNOWLEDGE_PDU ack = WINPR_C_ARRAY_INIT;
1328 WINPR_ASSERT(callback);
1329 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1330 WINPR_ASSERT(gfx);
1331 RdpgfxClientContext* context = gfx->context;
1332 UINT error = CHANNEL_RC_OK;
1333
1334 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1335 return ERROR_INVALID_DATA;
1336
1337 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1338 WLog_Print(gfx->base.log, WLOG_TRACE, "RecvEndFramePdu: frameId: %" PRIu32 "", pdu.frameId);
1339
1340 const UINT64 start = GetTickCount64();
1341 if (context)
1342 {
1343 IFCALLRET(context->EndFrame, error, context, &pdu);
1344
1345 if (error)
1346 {
1347 WLog_Print(gfx->base.log, WLOG_ERROR, "context->EndFrame failed with error %" PRIu32 "",
1348 error);
1349 return error;
1350 }
1351 }
1352 const UINT64 end = GetTickCount64();
1353 const UINT64 EndFrameTime = end - start;
1354 gfx->TotalDecodedFrames++;
1355
1356 if (!gfx->sendFrameAcks)
1357 return error;
1358
1359 ack.frameId = pdu.frameId;
1360 ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1361
1362 if (gfx->suspendFrameAcks)
1363 {
1364 ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1365
1366 if (gfx->TotalDecodedFrames == 1)
1367 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1368 WLog_Print(gfx->base.log, WLOG_ERROR,
1369 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32 "",
1370 error);
1371 }
1372 else
1373 {
1374 ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1375
1376 if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1377 WLog_Print(gfx->base.log, WLOG_ERROR,
1378 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32 "", error);
1379 }
1380
1381 switch (gfx->ConnectionCaps.version)
1382 {
1383#if defined(WITH_GFX_AV1)
1384 case RDPGFX_CAPVERSION_FRDP_1:
1385#endif
1386 case RDPGFX_CAPVERSION_10:
1387 case RDPGFX_CAPVERSION_102:
1388 case RDPGFX_CAPVERSION_103:
1389 case RDPGFX_CAPVERSION_104:
1390 case RDPGFX_CAPVERSION_105:
1391 case RDPGFX_CAPVERSION_106:
1392 case RDPGFX_CAPVERSION_106_ERR:
1393 case RDPGFX_CAPVERSION_107:
1394#if defined(WITH_GFX_AZURE)
1395 case RDPGFX_CAPVERSION_111:
1396 case RDPGFX_CAPVERSION_112:
1397 case RDPGFX_CAPVERSION_113:
1398#endif
1399 if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSendQoeAck))
1400 {
1401 RDPGFX_QOE_FRAME_ACKNOWLEDGE_PDU qoe = WINPR_C_ARRAY_INIT;
1402 UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1403
1404 if (diff > 65000)
1405 diff = 0;
1406
1407 qoe.frameId = pdu.frameId;
1408 qoe.timestamp = gfx->StartDecodingTime % UINT32_MAX;
1409 qoe.timeDiffSE = WINPR_ASSERTING_INT_CAST(UINT16, diff);
1410 qoe.timeDiffEDR = WINPR_ASSERTING_INT_CAST(UINT16, EndFrameTime);
1411
1412 if ((error = rdpgfx_send_qoe_frame_acknowledge_pdu(context, &qoe)))
1413 WLog_Print(gfx->base.log, WLOG_ERROR,
1414 "rdpgfx_send_qoe_frame_acknowledge_pdu failed with error %" PRIu32
1415 "",
1416 error);
1417 }
1418
1419 break;
1420
1421 default:
1422 break;
1423 }
1424
1425 return error;
1426}
1427
1433static UINT rdpgfx_recv_wire_to_surface_1_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1434{
1435 RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1436 RDPGFX_WIRE_TO_SURFACE_PDU_1 pdu = WINPR_C_ARRAY_INIT;
1437 WINPR_ASSERT(callback);
1438 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1439 UINT error = 0;
1440
1441 WINPR_ASSERT(gfx);
1442 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1443 return ERROR_INVALID_DATA;
1444
1445 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1446 Stream_Read_UINT16(s, pdu.codecId); /* codecId (2 bytes) */
1447 Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1448
1449 if ((error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.destRect)))) /* destRect (8 bytes) */
1450 {
1451 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "",
1452 error);
1453 return error;
1454 }
1455
1456 Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1457
1458 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1459 return ERROR_INVALID_DATA;
1460
1461 pdu.bitmapData = Stream_Pointer(s);
1462 Stream_Seek(s, pdu.bitmapDataLength);
1463
1464 WLog_Print(gfx->base.log, WLOG_TRACE,
1465 "RecvWireToSurface1Pdu: surfaceId: %" PRIu16 " codecId: %s (0x%04" PRIX16
1466 ") pixelFormat: 0x%02" PRIX8 " "
1467 "destRect: left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16
1468 " bitmapDataLength: %" PRIu32 "",
1469 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId, pdu.pixelFormat,
1470 pdu.destRect.left, pdu.destRect.top, pdu.destRect.right, pdu.destRect.bottom,
1471 pdu.bitmapDataLength);
1472 cmd.surfaceId = pdu.surfaceId;
1473 cmd.codecId = pdu.codecId;
1474 cmd.contextId = 0;
1475
1476 switch (pdu.pixelFormat)
1477 {
1478 case GFX_PIXEL_FORMAT_XRGB_8888:
1479 cmd.format = PIXEL_FORMAT_BGRX32;
1480 break;
1481
1482 case GFX_PIXEL_FORMAT_ARGB_8888:
1483 cmd.format = PIXEL_FORMAT_BGRA32;
1484 break;
1485
1486 default:
1487 return ERROR_INVALID_DATA;
1488 }
1489
1490 cmd.left = pdu.destRect.left;
1491 cmd.top = pdu.destRect.top;
1492 cmd.right = pdu.destRect.right;
1493 cmd.bottom = pdu.destRect.bottom;
1494 cmd.width = cmd.right - cmd.left;
1495 cmd.height = cmd.bottom - cmd.top;
1496 cmd.length = pdu.bitmapDataLength;
1497 cmd.data = pdu.bitmapData;
1498 cmd.extra = nullptr;
1499
1500 if (cmd.right < cmd.left)
1501 {
1502 WLog_Print(gfx->base.log, WLOG_ERROR,
1503 "RecvWireToSurface1Pdu right=%" PRIu32 " < left=%" PRIu32, cmd.right, cmd.left);
1504 return ERROR_INVALID_DATA;
1505 }
1506 if (cmd.bottom < cmd.top)
1507 {
1508 WLog_Print(gfx->base.log, WLOG_ERROR,
1509 "RecvWireToSurface1Pdu bottom=%" PRIu32 " < top=%" PRIu32, cmd.bottom, cmd.top);
1510 return ERROR_INVALID_DATA;
1511 }
1512
1513 if ((error = rdpgfx_decode(gfx, &cmd)))
1514 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_decode failed with error %" PRIu32 "!",
1515 error);
1516
1517 return error;
1518}
1519
1520WINPR_ATTR_NODISCARD
1521static BOOL checkSurfaceCommand(const RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1522{
1523 switch (cmd->codecId)
1524 {
1525 case RDPGFX_CODECID_UNCOMPRESSED:
1526 case RDPGFX_CODECID_CAVIDEO:
1527 case RDPGFX_CODECID_CLEARCODEC:
1528 case RDPGFX_CODECID_PLANAR:
1529 case RDPGFX_CODECID_AVC420:
1530 case RDPGFX_CODECID_ALPHA:
1531 case RDPGFX_CODECID_AVC444:
1532 case RDPGFX_CODECID_AVC444v2:
1533 case RDPGFX_CODECID_CAPROGRESSIVE:
1534 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1535 return TRUE;
1536#if defined(WITH_GFX_AV1)
1537 case RDPGFX_CODECID_AV1:
1538 if (gfx->ConnectionCaps.version != RDPGFX_CAPVERSION_FRDP_1)
1539 {
1540 WLog_Print(gfx->base.log, WLOG_ERROR,
1541 "RDPGFX_SURFACE_COMMAND::codecId %" PRIu32
1542 " only supported with %s but connection uses %s [0x%08" PRIx32 "]",
1543 cmd->codecId, rdpgfx_caps_version_str(RDPGFX_CAPVERSION_FRDP_1),
1544 rdpgfx_caps_version_str(gfx->ConnectionCaps.version),
1545 gfx->ConnectionCaps.version);
1546 return FALSE;
1547 }
1548 return TRUE;
1549#endif
1550 default:
1551 WLog_Print(gfx->base.log, WLOG_ERROR,
1552 "Unknown RDPGFX_SURFACE_COMMAND::codecId %" PRIu32, cmd->codecId);
1553 return FALSE;
1554 }
1555}
1556
1557UINT logSurfaceCommand(RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1558{
1559 WINPR_ASSERT(gfx);
1560
1561 WLog_Print(gfx->base.log, WLOG_DEBUG, "Got GFX %s",
1562 rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)));
1563
1564 RdpgfxClientContext* context = gfx->context;
1565 if (!context)
1566 return CHANNEL_RC_OK;
1567
1568 if (!checkSurfaceCommand(gfx, cmd))
1569 return CHANNEL_RC_NULL_DATA;
1570
1571 const UINT error = IFCALLRESULT(CHANNEL_RC_OK, context->SurfaceCommand, context, cmd);
1572
1573 if (error)
1574 WLog_Print(gfx->base.log, WLOG_ERROR,
1575 "context->SurfaceCommand failed with error %" PRIu32 "", error);
1576
1577 rdpgfx_codecid_event(context, cmd->codecId);
1578 return error;
1579}
1580
1586static UINT rdpgfx_recv_wire_to_surface_2_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1587{
1588 RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1589 RDPGFX_WIRE_TO_SURFACE_PDU_2 pdu = WINPR_C_ARRAY_INIT;
1590
1591 WINPR_ASSERT(callback);
1592 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1593 WINPR_ASSERT(gfx);
1594
1595 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1596 return ERROR_INVALID_DATA;
1597
1598 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1599 Stream_Read_UINT16(s, pdu.codecId); /* codecId (2 bytes) */
1600 Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1601 Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1602 Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1603 pdu.bitmapData = Stream_Pointer(s);
1604 if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1605 return ERROR_INVALID_DATA;
1606
1607 WLog_Print(gfx->base.log, WLOG_TRACE,
1608 "RecvWireToSurface2Pdu: surfaceId: %" PRIu16 " codecId: %s (0x%04" PRIX16 ") "
1609 "codecContextId: %" PRIu32 " pixelFormat: 0x%02" PRIX8 " bitmapDataLength: %" PRIu32
1610 "",
1611 pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1612 pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1613
1614 cmd.surfaceId = pdu.surfaceId;
1615 cmd.codecId = pdu.codecId;
1616 cmd.contextId = pdu.codecContextId;
1617
1618 switch (pdu.pixelFormat)
1619 {
1620 case GFX_PIXEL_FORMAT_XRGB_8888:
1621 cmd.format = PIXEL_FORMAT_BGRX32;
1622 break;
1623
1624 case GFX_PIXEL_FORMAT_ARGB_8888:
1625 cmd.format = PIXEL_FORMAT_BGRA32;
1626 break;
1627
1628 default:
1629 return ERROR_INVALID_DATA;
1630 }
1631
1632 cmd.length = pdu.bitmapDataLength;
1633 cmd.data = pdu.bitmapData;
1634 cmd.extra = nullptr;
1635
1636 return logSurfaceCommand(gfx, &cmd);
1637}
1638
1644static UINT rdpgfx_recv_delete_encoding_context_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1645{
1646 RDPGFX_DELETE_ENCODING_CONTEXT_PDU pdu = WINPR_C_ARRAY_INIT;
1647 WINPR_ASSERT(callback);
1648 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1649 WINPR_ASSERT(gfx);
1650 RdpgfxClientContext* context = gfx->context;
1651 UINT error = CHANNEL_RC_OK;
1652
1653 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1654 return ERROR_INVALID_DATA;
1655
1656 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1657 Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1658
1659 WLog_Print(gfx->base.log, WLOG_DEBUG,
1660 "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16 " codecContextId: %" PRIu32 "",
1661 pdu.surfaceId, pdu.codecContextId);
1662
1663 if (context)
1664 {
1665 IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1666
1667 if (error)
1668 WLog_Print(gfx->base.log, WLOG_ERROR,
1669 "context->DeleteEncodingContext failed with error %" PRIu32 "", error);
1670 }
1671
1672 return error;
1673}
1674
1680static UINT rdpgfx_recv_solid_fill_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1681{
1682 RDPGFX_SOLID_FILL_PDU pdu = WINPR_C_ARRAY_INIT;
1683 WINPR_ASSERT(callback);
1684 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1685 WINPR_ASSERT(gfx);
1686 RdpgfxClientContext* context = gfx->context;
1687
1688 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1689 return ERROR_INVALID_DATA;
1690
1691 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1692
1693 UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
1694 if (error != CHANNEL_RC_OK) /* fillPixel (4 bytes) */
1695 {
1696 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_color32 failed with error %" PRIu32 "!",
1697 error);
1698 return error;
1699 }
1700
1701 Stream_Read_UINT16(s, pdu.fillRectCount); /* fillRectCount (2 bytes) */
1702
1703 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
1704 return ERROR_INVALID_DATA;
1705
1706 pdu.fillRects = (RECTANGLE_16*)calloc(pdu.fillRectCount, sizeof(RECTANGLE_16));
1707
1708 if (!pdu.fillRects)
1709 {
1710 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
1711 return CHANNEL_RC_NO_MEMORY;
1712 }
1713
1714 for (UINT16 index = 0; index < pdu.fillRectCount; index++)
1715 {
1716 RECTANGLE_16* fillRect = &(pdu.fillRects[index]);
1717
1718 if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect)))
1719 {
1720 WLog_Print(gfx->base.log, WLOG_ERROR,
1721 "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error);
1722 free(pdu.fillRects);
1723 return error;
1724 }
1725 }
1726 WLog_Print(gfx->base.log, WLOG_TRACE,
1727 "RecvSolidFillPdu: surfaceId: %" PRIu16 " fillRectCount: %" PRIu16 "", pdu.surfaceId,
1728 pdu.fillRectCount);
1729
1730 if (context)
1731 {
1732 IFCALLRET(context->SolidFill, error, context, &pdu);
1733
1734 if (error)
1735 WLog_Print(gfx->base.log, WLOG_ERROR,
1736 "context->SolidFill failed with error %" PRIu32 "", error);
1737 }
1738
1739 free(pdu.fillRects);
1740 return error;
1741}
1742
1748static UINT rdpgfx_recv_surface_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1749{
1750 RDPGFX_SURFACE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1751 WINPR_ASSERT(callback);
1752 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1753 WINPR_ASSERT(gfx);
1754 RdpgfxClientContext* context = gfx->context;
1755 UINT error = 0;
1756
1757 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
1758 return ERROR_INVALID_DATA;
1759
1760 Stream_Read_UINT16(s, pdu.surfaceIdSrc); /* surfaceIdSrc (2 bytes) */
1761 Stream_Read_UINT16(s, pdu.surfaceIdDest); /* surfaceIdDest (2 bytes) */
1762
1763 if ((error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc)))) /* rectSrc (8 bytes ) */
1764 {
1765 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
1766 error);
1767 return error;
1768 }
1769
1770 Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
1771
1772 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
1773 return ERROR_INVALID_DATA;
1774
1775 pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
1776
1777 if (!pdu.destPts)
1778 {
1779 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
1780 return CHANNEL_RC_NO_MEMORY;
1781 }
1782
1783 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
1784 {
1785 RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
1786
1787 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
1788 {
1789 WLog_Print(gfx->base.log, WLOG_ERROR,
1790 "rdpgfx_read_point16 failed with error %" PRIu32 "!", error);
1791 free(pdu.destPts);
1792 return error;
1793 }
1794 }
1795
1796 WLog_Print(gfx->base.log, WLOG_TRACE,
1797 "RecvSurfaceToSurfacePdu: surfaceIdSrc: %" PRIu16 " surfaceIdDest: %" PRIu16 " "
1798 "left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16
1799 " destPtsCount: %" PRIu16 "",
1800 pdu.surfaceIdSrc, pdu.surfaceIdDest, pdu.rectSrc.left, pdu.rectSrc.top,
1801 pdu.rectSrc.right, pdu.rectSrc.bottom, pdu.destPtsCount);
1802
1803 if (context)
1804 {
1805 IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
1806
1807 if (error)
1808 WLog_Print(gfx->base.log, WLOG_ERROR,
1809 "context->SurfaceToSurface failed with error %" PRIu32 "", error);
1810 }
1811
1812 free(pdu.destPts);
1813 return error;
1814}
1815
1821static UINT rdpgfx_recv_surface_to_cache_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1822{
1823 RDPGFX_SURFACE_TO_CACHE_PDU pdu = WINPR_C_ARRAY_INIT;
1824 WINPR_ASSERT(callback);
1825 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1826
1827 WINPR_ASSERT(gfx);
1828 RdpgfxClientContext* context = gfx->context;
1829
1830 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
1831 return ERROR_INVALID_DATA;
1832
1833 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1834 Stream_Read_UINT64(s, pdu.cacheKey); /* cacheKey (8 bytes) */
1835 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
1836
1837 UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc));
1838 if (error != CHANNEL_RC_OK) /* rectSrc (8 bytes ) */
1839 {
1840 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
1841 error);
1842 return error;
1843 }
1844
1845 WLog_Print(gfx->base.log, WLOG_TRACE,
1846 "RecvSurfaceToCachePdu: surfaceId: %" PRIu16 " cacheKey: 0x%016" PRIX64
1847 " cacheSlot: %" PRIu16 " "
1848 "left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16 "",
1849 pdu.surfaceId, pdu.cacheKey, pdu.cacheSlot, pdu.rectSrc.left, pdu.rectSrc.top,
1850 pdu.rectSrc.right, pdu.rectSrc.bottom);
1851
1852 if (context)
1853 {
1854 IFCALLRET(context->SurfaceToCache, error, context, &pdu);
1855
1856 if (error)
1857 WLog_Print(gfx->base.log, WLOG_ERROR,
1858 "context->SurfaceToCache failed with error %" PRIu32 "", error);
1859 }
1860
1861 return error;
1862}
1863
1869static UINT rdpgfx_recv_cache_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1870{
1871 RDPGFX_CACHE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1872 WINPR_ASSERT(callback);
1873 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1874
1875 WINPR_ASSERT(gfx);
1876 RdpgfxClientContext* context = gfx->context;
1877 UINT error = CHANNEL_RC_OK;
1878
1879 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1880 return ERROR_INVALID_DATA;
1881
1882 Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
1883 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1884 Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
1885
1886 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
1887 return ERROR_INVALID_DATA;
1888
1889 pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
1890
1891 if (!pdu.destPts)
1892 {
1893 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
1894 return CHANNEL_RC_NO_MEMORY;
1895 }
1896
1897 for (UINT16 index = 0; index < pdu.destPtsCount; index++)
1898 {
1899 RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
1900
1901 if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
1902 {
1903 WLog_Print(gfx->base.log, WLOG_ERROR,
1904 "rdpgfx_read_point16 failed with error %" PRIu32 "", error);
1905 free(pdu.destPts);
1906 return error;
1907 }
1908 }
1909
1910 WLog_Print(gfx->base.log, WLOG_TRACE,
1911 "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16 " surfaceId: %" PRIu16
1912 " destPtsCount: %" PRIu16 "",
1913 pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
1914
1915 if (context)
1916 {
1917 IFCALLRET(context->CacheToSurface, error, context, &pdu);
1918
1919 if (error)
1920 WLog_Print(gfx->base.log, WLOG_ERROR,
1921 "context->CacheToSurface failed with error %" PRIu32 "", error);
1922 }
1923
1924 free(pdu.destPts);
1925 return error;
1926}
1927
1933static UINT rdpgfx_recv_map_surface_to_output_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1934{
1935 RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
1936 WINPR_ASSERT(callback);
1937 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1938
1939 WINPR_ASSERT(gfx);
1940 RdpgfxClientContext* context = gfx->context;
1941 UINT error = CHANNEL_RC_OK;
1942
1943 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
1944 return ERROR_INVALID_DATA;
1945
1946 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1947 Stream_Read_UINT16(s, pdu.reserved); /* reserved (2 bytes) */
1948 Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
1949 Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
1950 WLog_Print(gfx->base.log, WLOG_DEBUG,
1951 "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
1952 " outputOriginY: %" PRIu32 "",
1953 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
1954
1955 if (context)
1956 {
1957 IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
1958
1959 if (error)
1960 WLog_Print(gfx->base.log, WLOG_ERROR,
1961 "context->MapSurfaceToOutput failed with error %" PRIu32 "", error);
1962 }
1963
1964 return error;
1965}
1966
1967static UINT rdpgfx_recv_map_surface_to_scaled_output_pdu(GENERIC_CHANNEL_CALLBACK* callback,
1968 wStream* s)
1969{
1970 RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
1971 WINPR_ASSERT(callback);
1972 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1973
1974 WINPR_ASSERT(gfx);
1975 RdpgfxClientContext* context = gfx->context;
1976 UINT error = CHANNEL_RC_OK;
1977
1978 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
1979 return ERROR_INVALID_DATA;
1980
1981 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1982 Stream_Read_UINT16(s, pdu.reserved); /* reserved (2 bytes) */
1983 Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
1984 Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
1985 Stream_Read_UINT32(s, pdu.targetWidth); /* targetWidth (4 bytes) */
1986 Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
1987 WLog_Print(gfx->base.log, WLOG_DEBUG,
1988 "RecvMapSurfaceToScaledOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
1989 " outputOriginY: %" PRIu32 " targetWidth: %" PRIu32 " targetHeight: %" PRIu32,
1990 pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY, pdu.targetWidth,
1991 pdu.targetHeight);
1992
1993 if (context)
1994 {
1995 IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
1996
1997 if (error)
1998 WLog_Print(gfx->base.log, WLOG_ERROR,
1999 "context->MapSurfaceToScaledOutput failed with error %" PRIu32 "", error);
2000 }
2001
2002 return error;
2003}
2004
2010static UINT rdpgfx_recv_map_surface_to_window_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2011{
2012 RDPGFX_MAP_SURFACE_TO_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2013 WINPR_ASSERT(callback);
2014 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2015
2016 WINPR_ASSERT(gfx);
2017 RdpgfxClientContext* context = gfx->context;
2018 UINT error = CHANNEL_RC_OK;
2019
2020 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
2021 return ERROR_INVALID_DATA;
2022
2023 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2024 Stream_Read_UINT64(s, pdu.windowId); /* windowId (8 bytes) */
2025 Stream_Read_UINT32(s, pdu.mappedWidth); /* mappedWidth (4 bytes) */
2026 Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2027
2028 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2029 {
2030 WLog_Print(gfx->base.log, WLOG_ERROR,
2031 "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2032 "] > %" PRIu32,
2033 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2034 return ERROR_INVALID_DATA;
2035 }
2036
2037 WLog_Print(gfx->base.log, WLOG_DEBUG,
2038 "RecvMapSurfaceToWindowPdu: surfaceId: %" PRIu16 " windowId: 0x%016" PRIX64
2039 " mappedWidth: %" PRIu32 " mappedHeight: %" PRIu32 "",
2040 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight);
2041
2042 if (context && context->MapSurfaceToWindow)
2043 {
2044 IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
2045
2046 if (error)
2047 WLog_Print(gfx->base.log, WLOG_ERROR,
2048 "context->MapSurfaceToWindow failed with error %" PRIu32 "", error);
2049 }
2050
2051 return error;
2052}
2053
2054static UINT rdpgfx_recv_map_surface_to_scaled_window_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2055 wStream* s)
2056{
2057 RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2058 WINPR_ASSERT(callback);
2059 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2060 WINPR_ASSERT(gfx);
2061 RdpgfxClientContext* context = gfx->context;
2062 UINT error = CHANNEL_RC_OK;
2063
2064 if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
2065 return ERROR_INVALID_DATA;
2066
2067 Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
2068 Stream_Read_UINT64(s, pdu.windowId); /* windowId (8 bytes) */
2069 Stream_Read_UINT32(s, pdu.mappedWidth); /* mappedWidth (4 bytes) */
2070 Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2071 Stream_Read_UINT32(s, pdu.targetWidth); /* targetWidth (4 bytes) */
2072 Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
2073
2074 if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2075 {
2076 WLog_Print(gfx->base.log, WLOG_ERROR,
2077 "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2078 "], mappedHeight[%" PRIu32 "] > %" PRIu32,
2079 pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2080 return ERROR_INVALID_DATA;
2081 }
2082
2083 WLog_Print(gfx->base.log, WLOG_DEBUG,
2084 "RecvMapSurfaceToScaledWindowPdu: surfaceId: %" PRIu16 " windowId: 0x%016" PRIX64
2085 " mappedWidth: %" PRIu32 " mappedHeight: %" PRIu32 " targetWidth: %" PRIu32
2086 " targetHeight: %" PRIu32 "",
2087 pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight, pdu.targetWidth,
2088 pdu.targetHeight);
2089
2090 if (context && context->MapSurfaceToScaledWindow)
2091 {
2092 IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
2093
2094 if (error)
2095 WLog_Print(gfx->base.log, WLOG_ERROR,
2096 "context->MapSurfaceToScaledWindow failed with error %" PRIu32 "", error);
2097 }
2098
2099 return error;
2100}
2101
2107static UINT rdpgfx_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2108{
2109 RDPGFX_HEADER header = WINPR_C_ARRAY_INIT;
2110
2111 WINPR_ASSERT(callback);
2112 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2113 const size_t beg = Stream_GetPosition(s);
2114
2115 WINPR_ASSERT(gfx);
2116
2117 size_t packetlen = 0;
2118 UINT error = rdpgfx_read_header(gfx->base.log, s, &header, &packetlen);
2119 if (error != CHANNEL_RC_OK)
2120 {
2121 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_header failed with error %" PRIu32 "!",
2122 error);
2123 return error;
2124 }
2125
2126 WLog_Print(gfx->base.log, WLOG_TRACE,
2127 "cmdId: %s (0x%04" PRIX16 ") flags: 0x%04" PRIX16 " pduLength: %" PRIu32 "",
2128 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
2129 header.pduLength);
2130
2131 rdpgfx_stats_cmdid_event(gfx->context, header.cmdId);
2132 switch (header.cmdId)
2133 {
2134 case RDPGFX_CMDID_WIRETOSURFACE_1:
2135 if ((error = rdpgfx_recv_wire_to_surface_1_pdu(callback, s)))
2136 WLog_Print(gfx->base.log, WLOG_ERROR,
2137 "rdpgfx_recv_wire_to_surface_1_pdu failed with error %" PRIu32 "!",
2138 error);
2139
2140 break;
2141
2142 case RDPGFX_CMDID_WIRETOSURFACE_2:
2143 if ((error = rdpgfx_recv_wire_to_surface_2_pdu(callback, s)))
2144 WLog_Print(gfx->base.log, WLOG_ERROR,
2145 "rdpgfx_recv_wire_to_surface_2_pdu failed with error %" PRIu32 "!",
2146 error);
2147
2148 break;
2149
2150 case RDPGFX_CMDID_DELETEENCODINGCONTEXT:
2151 if ((error = rdpgfx_recv_delete_encoding_context_pdu(callback, s)))
2152 WLog_Print(gfx->base.log, WLOG_ERROR,
2153 "rdpgfx_recv_delete_encoding_context_pdu failed with error %" PRIu32 "!",
2154 error);
2155
2156 break;
2157
2158 case RDPGFX_CMDID_SOLIDFILL:
2159 if ((error = rdpgfx_recv_solid_fill_pdu(callback, s)))
2160 WLog_Print(gfx->base.log, WLOG_ERROR,
2161 "rdpgfx_recv_solid_fill_pdu failed with error %" PRIu32 "!", error);
2162
2163 break;
2164
2165 case RDPGFX_CMDID_SURFACETOSURFACE:
2166 if ((error = rdpgfx_recv_surface_to_surface_pdu(callback, s)))
2167 WLog_Print(gfx->base.log, WLOG_ERROR,
2168 "rdpgfx_recv_surface_to_surface_pdu failed with error %" PRIu32 "!",
2169 error);
2170
2171 break;
2172
2173 case RDPGFX_CMDID_SURFACETOCACHE:
2174 if ((error = rdpgfx_recv_surface_to_cache_pdu(callback, s)))
2175 WLog_Print(gfx->base.log, WLOG_ERROR,
2176 "rdpgfx_recv_surface_to_cache_pdu failed with error %" PRIu32 "!",
2177 error);
2178
2179 break;
2180
2181 case RDPGFX_CMDID_CACHETOSURFACE:
2182 if ((error = rdpgfx_recv_cache_to_surface_pdu(callback, s)))
2183 WLog_Print(gfx->base.log, WLOG_ERROR,
2184 "rdpgfx_recv_cache_to_surface_pdu failed with error %" PRIu32 "!",
2185 error);
2186
2187 break;
2188
2189 case RDPGFX_CMDID_EVICTCACHEENTRY:
2190 if ((error = rdpgfx_recv_evict_cache_entry_pdu(callback, s)))
2191 WLog_Print(gfx->base.log, WLOG_ERROR,
2192 "rdpgfx_recv_evict_cache_entry_pdu failed with error %" PRIu32 "!",
2193 error);
2194
2195 break;
2196
2197 case RDPGFX_CMDID_CREATESURFACE:
2198 if ((error = rdpgfx_recv_create_surface_pdu(callback, s)))
2199 WLog_Print(gfx->base.log, WLOG_ERROR,
2200 "rdpgfx_recv_create_surface_pdu failed with error %" PRIu32 "!", error);
2201
2202 break;
2203
2204 case RDPGFX_CMDID_DELETESURFACE:
2205 if ((error = rdpgfx_recv_delete_surface_pdu(callback, s)))
2206 WLog_Print(gfx->base.log, WLOG_ERROR,
2207 "rdpgfx_recv_delete_surface_pdu failed with error %" PRIu32 "!", error);
2208
2209 break;
2210
2211 case RDPGFX_CMDID_STARTFRAME:
2212 if ((error = rdpgfx_recv_start_frame_pdu(callback, s)))
2213 WLog_Print(gfx->base.log, WLOG_ERROR,
2214 "rdpgfx_recv_start_frame_pdu failed with error %" PRIu32 "!", error);
2215
2216 break;
2217
2218 case RDPGFX_CMDID_ENDFRAME:
2219 if ((error = rdpgfx_recv_end_frame_pdu(callback, s)))
2220 WLog_Print(gfx->base.log, WLOG_ERROR,
2221 "rdpgfx_recv_end_frame_pdu failed with error %" PRIu32 "!", error);
2222
2223 break;
2224
2225 case RDPGFX_CMDID_RESETGRAPHICS:
2226 if ((error = rdpgfx_recv_reset_graphics_pdu(callback, s)))
2227 WLog_Print(gfx->base.log, WLOG_ERROR,
2228 "rdpgfx_recv_reset_graphics_pdu failed with error %" PRIu32 "!", error);
2229
2230 break;
2231
2232 case RDPGFX_CMDID_MAPSURFACETOOUTPUT:
2233 if ((error = rdpgfx_recv_map_surface_to_output_pdu(callback, s)))
2234 WLog_Print(gfx->base.log, WLOG_ERROR,
2235 "rdpgfx_recv_map_surface_to_output_pdu failed with error %" PRIu32 "!",
2236 error);
2237
2238 break;
2239
2240 case RDPGFX_CMDID_CACHEIMPORTREPLY:
2241 if ((error = rdpgfx_recv_cache_import_reply_pdu(callback, s)))
2242 WLog_Print(gfx->base.log, WLOG_ERROR,
2243 "rdpgfx_recv_cache_import_reply_pdu failed with error %" PRIu32 "!",
2244 error);
2245
2246 break;
2247
2248 case RDPGFX_CMDID_CAPSCONFIRM:
2249 if ((error = rdpgfx_recv_caps_confirm_pdu(callback, s)))
2250 WLog_Print(gfx->base.log, WLOG_ERROR,
2251 "rdpgfx_recv_caps_confirm_pdu failed with error %" PRIu32 "!", error);
2252
2253 if ((error = rdpgfx_send_cache_offer(gfx)))
2254 WLog_Print(gfx->base.log, WLOG_ERROR,
2255 "rdpgfx_send_cache_offer failed with error %" PRIu32 "!", error);
2256
2257 break;
2258
2259 case RDPGFX_CMDID_MAPSURFACETOWINDOW:
2260 if ((error = rdpgfx_recv_map_surface_to_window_pdu(callback, s)))
2261 WLog_Print(gfx->base.log, WLOG_ERROR,
2262 "rdpgfx_recv_map_surface_to_window_pdu failed with error %" PRIu32 "!",
2263 error);
2264
2265 break;
2266
2267 case RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW:
2268 if ((error = rdpgfx_recv_map_surface_to_scaled_window_pdu(callback, s)))
2269 WLog_Print(gfx->base.log, WLOG_ERROR,
2270 "rdpgfx_recv_map_surface_to_scaled_window_pdu failed with error %" PRIu32
2271 "!",
2272 error);
2273
2274 break;
2275
2276 case RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT:
2277 if ((error = rdpgfx_recv_map_surface_to_scaled_output_pdu(callback, s)))
2278 WLog_Print(gfx->base.log, WLOG_ERROR,
2279 "rdpgfx_recv_map_surface_to_scaled_output_pdu failed with error %" PRIu32
2280 "!",
2281 error);
2282
2283 break;
2284
2285 case RDPGFX_CMDID_PROTECT_SURFACE:
2286 case RDPGFX_CMDID_WATERMARK:
2287 WLog_Print(gfx->base.log, WLOG_WARN,
2288 "Command %s not implemented, skipping %" PRIu32 " bytes",
2289 rdpgfx_get_cmd_id_string(header.cmdId), header.pduLength);
2290 if (!Stream_SafeSeek(s, packetlen))
2291 error = ERROR_INVALID_DATA;
2292 break;
2293 default:
2294 error = CHANNEL_RC_BAD_PROC;
2295 break;
2296 }
2297
2298 if (error)
2299 {
2300 WLog_Print(gfx->base.log, WLOG_ERROR,
2301 "Error while processing GFX cmdId: %s (0x%04" PRIX16 ")",
2302 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
2303 if (!Stream_SetPosition(s, (beg + header.pduLength)))
2304 return ERROR_INVALID_DATA;
2305 return error;
2306 }
2307
2308 const size_t end = Stream_GetPosition(s);
2309
2310 if (end != (beg + header.pduLength))
2311 {
2312 WLog_Print(gfx->base.log, WLOG_ERROR,
2313 "Unexpected gfx pdu end: Actual: %" PRIuz ", Expected: %" PRIuz, end,
2314 (beg + header.pduLength));
2315 if (!Stream_SetPosition(s, (beg + header.pduLength)))
2316 return ERROR_INVALID_DATA;
2317 }
2318
2319 return error;
2320}
2321
2327static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
2328{
2329 UINT32 DstSize = 0;
2330 BYTE* pDstData = nullptr;
2331 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2332 WINPR_ASSERT(callback);
2333 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2334 UINT error = CHANNEL_RC_OK;
2335
2336 WINPR_ASSERT(gfx);
2337 int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2338 (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2339
2340 if (status < 0)
2341 {
2342 WLog_Print(gfx->base.log, WLOG_ERROR, "zgfx_decompress failure! status: %d", status);
2343 free(pDstData);
2344 return ERROR_INTERNAL_ERROR;
2345 }
2346
2347 wStream sbuffer = WINPR_C_ARRAY_INIT;
2348 wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2349
2350 if (!s)
2351 {
2352 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2353 free(pDstData);
2354 return CHANNEL_RC_NO_MEMORY;
2355 }
2356
2357 while (Stream_GetPosition(s) < Stream_Length(s))
2358 {
2359 if ((error = rdpgfx_recv_pdu(callback, s)))
2360 {
2361 WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_recv_pdu failed with error %" PRIu32 "!",
2362 error);
2363 break;
2364 }
2365 }
2366
2367 free(pDstData);
2368 return error;
2369}
2370
2376static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2377{
2378 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2379 WINPR_ASSERT(callback);
2380 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2381 WINPR_ASSERT(gfx);
2382 RdpgfxClientContext* context = gfx->context;
2383 UINT error = CHANNEL_RC_OK;
2384 BOOL do_caps_advertise = TRUE;
2385
2386 gfx->sendFrameAcks = TRUE;
2387
2388 if (context)
2389 {
2390 IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2391
2392 if (error)
2393 WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnOpen failed with error %" PRIu32 "",
2394 error);
2395
2396 RdpgfxClientContextInt* ctx = (RdpgfxClientContextInt*)context;
2397 const RdpgfxClientContextStats empty = WINPR_C_ARRAY_INIT;
2398 ctx->stats = empty;
2399 }
2400
2401 if (do_caps_advertise)
2402 error = rdpgfx_send_supported_caps(callback);
2403
2404 return error;
2405}
2406
2407static void rdpgfx_dump_stats(RdpgfxClientContext* context)
2408{
2409 WINPR_ASSERT(context);
2410
2411 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2412 WINPR_ASSERT(gfx);
2413
2414 wLog* log = gfx->base.log;
2415 const DWORD level = WLOG_TRACE;
2416 if (!WLog_IsLevelActive(log, level))
2417 return;
2418
2419 WLog_Print(log, level, "RdpgfxClientContextStats");
2420 for (size_t x = 0; x < rdpgfx_stats_max_index(); x++)
2421 {
2422 const char* name = rdpgfx_stats_name_for_index(x);
2423 const uint64_t val = rdpgfx_stats_value_for_index(context, x);
2424 WLog_Print(log, level, "%s: %" PRIu64, name, val);
2425 }
2426}
2427
2433static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2434{
2435 UINT error = CHANNEL_RC_OK;
2436 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2437 WINPR_ASSERT(callback);
2438 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2439
2440 if (!gfx)
2441 goto fail;
2442
2443 RdpgfxClientContext* context = gfx->context;
2444
2445 WLog_Print(gfx->base.log, WLOG_DEBUG, "OnClose");
2446 error = rdpgfx_save_persistent_cache(gfx);
2447
2448 if (error)
2449 {
2450 // print error, but don't consider this a hard failure
2451 WLog_Print(gfx->base.log, WLOG_ERROR,
2452 "rdpgfx_save_persistent_cache failed with error %" PRIu32 "", error);
2453 }
2454
2455 free_surfaces(context, gfx->SurfaceTable);
2456 error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2457 if (error)
2458 {
2459 // print error, but don't consider this a hard failure
2460 WLog_Print(gfx->base.log, WLOG_ERROR, "evict_cache_slots failed with error %" PRIu32 "",
2461 error);
2462 }
2463
2464 free(callback);
2465 gfx->UnacknowledgedFrames = 0;
2466 gfx->TotalDecodedFrames = 0;
2467
2468 if (context)
2469 {
2470 error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2471 if (error)
2472 {
2473 // print error, but don't consider this a hard failure
2474 WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnClose failed with error %" PRIu32 "",
2475 error);
2476 }
2477 rdpgfx_dump_stats(context);
2478 }
2479
2480fail:
2481 return CHANNEL_RC_OK;
2482}
2483
2484static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
2485{
2486 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2487 WINPR_ASSERT(gfx);
2488 RdpgfxClientContext* context = gfx->context;
2489
2490 WLog_Print(gfx->base.log, WLOG_DEBUG, "Terminated");
2491 rdpgfx_client_context_free(context);
2492}
2493
2499static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId, void* pData)
2500{
2501 WINPR_ASSERT(context);
2502 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2503 WINPR_ASSERT(gfx);
2504 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2505
2506 if (pData)
2507 {
2508 if (!HashTable_Insert(gfx->SurfaceTable, (void*)key, pData))
2509 return ERROR_BAD_ARGUMENTS;
2510 }
2511 else
2512 HashTable_Remove(gfx->SurfaceTable, (void*)key);
2513
2514 return CHANNEL_RC_OK;
2515}
2516
2522static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2523 UINT16* count_out)
2524{
2525 ULONG_PTR* pKeys = nullptr;
2526 WINPR_ASSERT(context);
2527 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2528 WINPR_ASSERT(gfx);
2529 size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2530
2531 WINPR_ASSERT(ppSurfaceIds);
2532 WINPR_ASSERT(count_out);
2533 if (count < 1)
2534 {
2535 *count_out = 0;
2536 return CHANNEL_RC_OK;
2537 }
2538
2539 UINT16* pSurfaceIds = (UINT16*)calloc(count, sizeof(UINT16));
2540
2541 if (!pSurfaceIds)
2542 {
2543 WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2544 free(pKeys);
2545 return CHANNEL_RC_NO_MEMORY;
2546 }
2547
2548 for (size_t index = 0; index < count; index++)
2549 {
2550 pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2551 }
2552
2553 free(pKeys);
2554 *ppSurfaceIds = pSurfaceIds;
2555 *count_out = (UINT16)count;
2556 return CHANNEL_RC_OK;
2557}
2558
2559static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2560{
2561 WINPR_ASSERT(context);
2562 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2563 WINPR_ASSERT(gfx);
2564 ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2565 return HashTable_GetItemValue(gfx->SurfaceTable, (void*)key);
2566}
2567
2573static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot, void* pData)
2574{
2575 WINPR_ASSERT(context);
2576 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2577
2578 WINPR_ASSERT(gfx);
2579 /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2580 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2581 {
2582 WLog_Print(gfx->base.log, WLOG_ERROR,
2583 "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2584 gfx->MaxCacheSlots);
2585 return ERROR_INVALID_INDEX;
2586 }
2587
2588 gfx->CacheSlots[cacheSlot - 1] = pData;
2589 return CHANNEL_RC_OK;
2590}
2591
2592static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2593{
2594 WINPR_ASSERT(context);
2595 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2596 WINPR_ASSERT(gfx);
2597 /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2598 if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2599 {
2600 WLog_Print(gfx->base.log, WLOG_ERROR,
2601 "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2602 gfx->MaxCacheSlots);
2603 return nullptr;
2604 }
2605
2606 return gfx->CacheSlots[cacheSlot - 1];
2607}
2608
2609static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext,
2610 WINPR_ATTR_UNUSED rdpSettings* settings)
2611{
2612 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2613
2614 WINPR_ASSERT(base);
2615 gfx->rdpcontext = rcontext;
2616
2617 gfx->SurfaceTable = HashTable_New(TRUE);
2618 if (!gfx->SurfaceTable)
2619 {
2620 WLog_Print(gfx->base.log, WLOG_ERROR, "HashTable_New for surfaces failed !");
2621 return CHANNEL_RC_NO_MEMORY;
2622 }
2623
2624 gfx->suspendFrameAcks =
2625 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSuspendFrameAck);
2626 gfx->MaxCacheSlots =
2627 freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) ? 4096 : 25600;
2628
2629 RdpgfxClientContextInt* context =
2630 (RdpgfxClientContextInt*)calloc(1, sizeof(RdpgfxClientContextInt));
2631 if (!context)
2632 {
2633 WLog_Print(gfx->base.log, WLOG_ERROR, "context calloc failed!");
2634 HashTable_Free(gfx->SurfaceTable);
2635 gfx->SurfaceTable = nullptr;
2636 return CHANNEL_RC_NO_MEMORY;
2637 }
2638
2639 gfx->zgfx = zgfx_context_new(FALSE);
2640 if (!gfx->zgfx)
2641 {
2642 WLog_Print(gfx->base.log, WLOG_ERROR, "zgfx_context_new failed!");
2643 HashTable_Free(gfx->SurfaceTable);
2644 gfx->SurfaceTable = nullptr;
2645 free(context);
2646 return CHANNEL_RC_NO_MEMORY;
2647 }
2648
2649 context->common.handle = (void*)gfx;
2650 context->common.GetSurfaceIds = rdpgfx_get_surface_ids;
2651 context->common.SetSurfaceData = rdpgfx_set_surface_data;
2652 context->common.GetSurfaceData = rdpgfx_get_surface_data;
2653 context->common.SetCacheSlotData = rdpgfx_set_cache_slot_data;
2654 context->common.GetCacheSlotData = rdpgfx_get_cache_slot_data;
2655 context->common.CapsAdvertise = rdpgfx_send_caps_advertise_pdu;
2656 context->common.FrameAcknowledge = rdpgfx_send_frame_acknowledge_pdu;
2657 context->common.CacheImportOffer = rdpgfx_send_cache_import_offer_pdu;
2658 context->common.QoeFrameAcknowledge = rdpgfx_send_qoe_frame_acknowledge_pdu;
2659
2660 gfx->base.iface.pInterface = (void*)context;
2661 gfx->context = &context->common;
2662 return CHANNEL_RC_OK;
2663}
2664
2665void rdpgfx_client_context_free(RdpgfxClientContext* context)
2666{
2667 if (!context)
2668 return;
2669
2670 RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2671
2672 free_surfaces(context, gfx->SurfaceTable);
2673 evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2674
2675 if (gfx->zgfx)
2676 {
2677 zgfx_context_free(gfx->zgfx);
2678 gfx->zgfx = nullptr;
2679 }
2680
2681 HashTable_Free(gfx->SurfaceTable);
2682 free(context);
2683}
2684
2685static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
2686 rdpgfx_on_open, rdpgfx_on_close,
2687 nullptr };
2688
2694FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
2695{
2696 return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
2697 sizeof(RDPGFX_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
2698 &rdpgfx_callbacks, init_plugin_cb, terminate_plugin_cb);
2699}
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