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