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