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