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