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