FreeRDP
Loading...
Searching...
No Matches
server/rdpgfx_main.c
1
19
20#include <freerdp/config.h>
21
22#include <winpr/assert.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <winpr/cast.h>
28#include <winpr/crt.h>
29#include <winpr/synch.h>
30#include <winpr/thread.h>
31#include <winpr/stream.h>
32
33#include <freerdp/freerdp.h>
34#include <freerdp/codec/color.h>
35
36#include <freerdp/channels/wtsvc.h>
37#include <freerdp/channels/log.h>
38
39#include "rdpgfx_common.h"
40#include "rdpgfx_main.h"
41
42#define TAG CHANNELS_TAG("rdpgfx.server")
43
44static BOOL rdpgfx_server_close(RdpgfxServerContext* context);
45
46#define checkCapsAreExchanged(context) \
47 checkCapsAreExchangedInt(context, __FILE__, __func__, __LINE__)
48WINPR_ATTR_NODISCARD static BOOL checkCapsAreExchangedInt(RdpgfxServerContext* context,
49 const char* file, const char* fkt,
50 size_t line)
51{
52 WINPR_ASSERT(context);
53 WINPR_ASSERT(context->priv);
54
55 const DWORD level = WLOG_TRACE;
56 if (WLog_IsLevelActive(context->priv->log, level))
57 {
58 WLog_PrintTextMessage(context->priv->log, level, line, file, fkt,
59 "activeCapSet{Version=0x%08" PRIx32 ", flags=0x%08" PRIx32 "}",
60 context->priv->activeCapSet.version,
61 context->priv->activeCapSet.flags);
62 }
63 return context->priv->activeCapSet.version > 0;
64}
65
75WINPR_ATTR_NODISCARD static inline size_t rdpgfx_pdu_length(size_t dataLen)
76{
77 return RDPGFX_HEADER_SIZE + dataLen;
78}
79
80WINPR_ATTR_NODISCARD static inline UINT rdpgfx_server_packet_init_header(wStream* s, UINT16 cmdId,
81 size_t pduLength)
82{
83 const RDPGFX_HEADER header = { .flags = 0,
84 .cmdId = cmdId,
85 .pduLength = WINPR_ASSERTING_INT_CAST(UINT32, pduLength) };
86 /* Write header. Note that actual length might be changed
87 * after the entire packet has been constructed. */
88 return rdpgfx_write_header(s, &header);
89}
90
98WINPR_ATTR_NODISCARD static inline BOOL rdpgfx_server_packet_complete_header(wStream* s,
99 size_t start)
100{
101 const size_t current = Stream_GetPosition(s);
102 const size_t cap = Stream_Capacity(s);
103 if (cap < start + RDPGFX_HEADER_SIZE)
104 return FALSE;
105 if ((start > UINT32_MAX) || (current < start))
106 return FALSE;
107 /* Fill actual length */
108 if (!Stream_SetPosition(s, start + RDPGFX_HEADER_SIZE - sizeof(UINT32)))
109 return FALSE;
110 Stream_Write_UINT32(s, (UINT32)(current - start)); /* pduLength (4 bytes) */
111 return Stream_SetPosition(s, current);
112}
113
121WINPR_ATTR_NODISCARD static UINT rdpgfx_server_packet_send(RdpgfxServerContext* context, wStream* s)
122{
123 UINT error = 0;
124 UINT32 flags = 0;
125 ULONG written = 0;
126 BYTE* pSrcData = Stream_Buffer(s);
127 const size_t SrcSize = Stream_GetPosition(s);
128 if (SrcSize > UINT32_MAX)
129 return ERROR_INTERNAL_ERROR;
130
131 WINPR_ASSERT(context);
132 WINPR_ASSERT(context->priv);
133
134 /* Allocate new stream with enough capacity. Additional overhead is
135 * descriptor (1 bytes) + segmentCount (2 bytes) + uncompressedSize (4 bytes)
136 * + segmentCount * size (4 bytes) */
137 wStream* fs = Stream_New(nullptr, SrcSize + 7 + (SrcSize / ZGFX_SEGMENTED_MAXSIZE + 1) * 4);
138
139 if (!fs)
140 {
141 WLog_Print(context->priv->log, WLOG_ERROR, "Stream_New failed!");
142 error = CHANNEL_RC_NO_MEMORY;
143 goto out;
144 }
145
146 if (zgfx_compress_to_stream(context->priv->zgfx, fs, pSrcData, (UINT32)SrcSize, &flags) < 0)
147 {
148 WLog_Print(context->priv->log, WLOG_ERROR, "zgfx_compress_to_stream failed!");
149 error = ERROR_INTERNAL_ERROR;
150 goto out;
151 }
152
153 {
154 const size_t pos = Stream_GetPosition(fs);
155 WINPR_ASSERT(pos <= UINT32_MAX);
156 if (!WTSVirtualChannelWrite(context->priv->rdpgfx_channel, Stream_BufferAs(fs, char),
157 (UINT32)pos, &written))
158 {
159 WLog_Print(context->priv->log, WLOG_ERROR, "WTSVirtualChannelWrite failed!");
160 error = ERROR_INTERNAL_ERROR;
161 goto out;
162 }
163 }
164
165 if (written < Stream_GetPosition(fs))
166 {
167 WLog_Print(context->priv->log, WLOG_WARN,
168 "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
169 Stream_GetPosition(fs));
170 }
171
172 error = CHANNEL_RC_OK;
173out:
174 Stream_Free(fs, TRUE);
175 Stream_Free(s, TRUE);
176 return error;
177}
178
191WINPR_ATTR_MALLOC(Stream_Free, 1)
192static wStream* rdpgfx_server_single_packet_new(wLog* log, UINT16 cmdId, size_t dataLen)
193{
194 UINT error = 0;
195 const size_t pduLength = rdpgfx_pdu_length(dataLen);
196 wStream* s = Stream_New(nullptr, pduLength);
197
198 if (!s)
199 {
200 WLog_Print(log, WLOG_ERROR, "Stream_New failed!");
201 goto error;
202 }
203
204 if ((error = rdpgfx_server_packet_init_header(s, cmdId, pduLength)))
205 {
206 WLog_Print(log, WLOG_ERROR, "Failed to init header with error %" PRIu32 "!", error);
207 goto error;
208 }
209
210 return s;
211error:
212 Stream_Free(s, TRUE);
213 return nullptr;
214}
215
224WINPR_ATTR_NODISCARD static inline UINT
225rdpgfx_server_single_packet_send(RdpgfxServerContext* context, wStream* s)
226{
227 /* Fill actual length */
228 if (!rdpgfx_server_packet_complete_header(s, 0))
229 return ERROR_INTERNAL_ERROR;
230 return rdpgfx_server_packet_send(context, s);
231}
232
238WINPR_ATTR_NODISCARD static UINT
239rdpgfx_send_caps_confirm_pdu(RdpgfxServerContext* context,
240 const RDPGFX_CAPS_CONFIRM_PDU* capsConfirm)
241{
242 WINPR_ASSERT(context);
243 WINPR_ASSERT(context->priv);
244 WINPR_ASSERT(capsConfirm);
245
246 RDPGFX_CAPSET* capsSet = capsConfirm->capsSet;
247 WINPR_ASSERT(capsSet);
248
249 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CAPSCONFIRM,
250 RDPGFX_CAPSET_BASE_SIZE + capsSet->length);
251
252 if (!s)
253 {
254 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
255 return CHANNEL_RC_NO_MEMORY;
256 }
257
258 WLog_Print(context->priv->log, WLOG_DEBUG,
259 "CAPS version=0x%04" PRIx32 ", flags=0x%04" PRIx32 ", length=%" PRIu32,
260 capsSet->version, capsSet->flags, capsSet->length);
261 Stream_Write_UINT32(s, capsSet->version); /* version (4 bytes) */
262 Stream_Write_UINT32(s, capsSet->length); /* capsDataLength (4 bytes) */
263
264 if (capsSet->length >= 4)
265 {
266 Stream_Write_UINT32(s, capsSet->flags); /* capsData (4 bytes) */
267 Stream_Zero(s, capsSet->length - 4);
268 }
269 else
270 Stream_Zero(s, capsSet->length);
271
272 context->priv->activeCapSet = *capsSet;
273 return rdpgfx_server_single_packet_send(context, s);
274}
275
281WINPR_ATTR_NODISCARD static UINT
282rdpgfx_send_reset_graphics_pdu(RdpgfxServerContext* context, const RDPGFX_RESET_GRAPHICS_PDU* pdu)
283{
284 const size_t RDPGFX_RESET_GRAPHICS_PDU_SIZE = 340;
285
286 if (!checkCapsAreExchanged(context))
287 return CHANNEL_RC_NOT_INITIALIZED;
288
289 WINPR_ASSERT(pdu);
290 WINPR_ASSERT(context->priv);
291
292 /* Check monitorCount. This ensures total size within 340 bytes) */
293 if (pdu->monitorCount >= 16)
294 {
295 WLog_Print(context->priv->log, WLOG_ERROR,
296 "Monitor count MUST be less than or equal to 16: %" PRIu32 "",
297 pdu->monitorCount);
298 return ERROR_INVALID_DATA;
299 }
300
301 wStream* s =
302 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_RESETGRAPHICS,
303 RDPGFX_RESET_GRAPHICS_PDU_SIZE - RDPGFX_HEADER_SIZE);
304
305 if (!s)
306 {
307 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
308 return CHANNEL_RC_NO_MEMORY;
309 }
310
311 Stream_Write_UINT32(s, pdu->width); /* width (4 bytes) */
312 Stream_Write_UINT32(s, pdu->height); /* height (4 bytes) */
313 Stream_Write_UINT32(s, pdu->monitorCount); /* monitorCount (4 bytes) */
314
315 for (UINT32 index = 0; index < pdu->monitorCount; index++)
316 {
317 const MONITOR_DEF* monitor = &(pdu->monitorDefArray[index]);
318 Stream_Write_INT32(s, monitor->left); /* left (4 bytes) */
319 Stream_Write_INT32(s, monitor->top); /* top (4 bytes) */
320 Stream_Write_INT32(s, monitor->right); /* right (4 bytes) */
321 Stream_Write_INT32(s, monitor->bottom); /* bottom (4 bytes) */
322 Stream_Write_UINT32(s, monitor->flags); /* flags (4 bytes) */
323 }
324
325 /* pad (total size must be 340 bytes) */
326 const size_t pos = Stream_GetPosition(s);
327 if (pos > RDPGFX_RESET_GRAPHICS_PDU_SIZE)
328 {
329 Stream_Free(s, TRUE);
330 return ERROR_INVALID_DATA;
331 }
332 if (!Stream_SafeZero(s, RDPGFX_RESET_GRAPHICS_PDU_SIZE - pos))
333 {
334 Stream_Free(s, TRUE);
335 return ERROR_INVALID_DATA;
336 }
337 return rdpgfx_server_single_packet_send(context, s);
338}
339
345WINPR_ATTR_NODISCARD static UINT
346rdpgfx_send_evict_cache_entry_pdu(RdpgfxServerContext* context,
348{
349 if (!checkCapsAreExchanged(context))
350 return CHANNEL_RC_NOT_INITIALIZED;
351
352 WINPR_ASSERT(pdu);
353 WINPR_ASSERT(context->priv);
354 wStream* s =
355 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_EVICTCACHEENTRY, 2);
356
357 if (!s)
358 {
359 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
360 return CHANNEL_RC_NO_MEMORY;
361 }
362
363 Stream_Write_UINT16(s, pdu->cacheSlot); /* cacheSlot (2 bytes) */
364 return rdpgfx_server_single_packet_send(context, s);
365}
366
372WINPR_ATTR_NODISCARD static UINT
373rdpgfx_send_cache_import_reply_pdu(RdpgfxServerContext* context,
375{
376 if (!checkCapsAreExchanged(context))
377 return CHANNEL_RC_NOT_INITIALIZED;
378 WINPR_ASSERT(context);
379 WINPR_ASSERT(context->priv);
380 WINPR_ASSERT(pdu);
381
382 WLog_Print(context->priv->log, WLOG_DEBUG, "reply with %" PRIu16 " entries",
383 pdu->importedEntriesCount);
384 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CACHEIMPORTREPLY,
385 2 + 2 * pdu->importedEntriesCount);
386
387 if (!s)
388 {
389 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
390 return CHANNEL_RC_NO_MEMORY;
391 }
392
393 /* importedEntriesCount (2 bytes) */
394 Stream_Write_UINT16(s, pdu->importedEntriesCount);
395
396 for (UINT16 index = 0; index < pdu->importedEntriesCount; index++)
397 {
398 Stream_Write_UINT16(s, pdu->cacheSlots[index]); /* cacheSlot (2 bytes) */
399 }
400
401 return rdpgfx_server_single_packet_send(context, s);
402}
403
404WINPR_ATTR_NODISCARD static UINT
405rdpgfx_process_cache_import_offer_pdu(RdpgfxServerContext* context,
406 const RDPGFX_CACHE_IMPORT_OFFER_PDU* cacheImportOffer)
407{
408 if (!checkCapsAreExchanged(context))
409 return CHANNEL_RC_NOT_INITIALIZED;
410 WINPR_ASSERT(context);
411 WINPR_ASSERT(context->priv);
412 WINPR_ASSERT(cacheImportOffer);
413
414 RDPGFX_CACHE_IMPORT_REPLY_PDU reply = WINPR_C_ARRAY_INIT;
415 WLog_Print(context->priv->log, WLOG_DEBUG,
416 "received %" PRIu16 " entries, reply with %" PRIu16 " entries",
417 cacheImportOffer->cacheEntriesCount, reply.importedEntriesCount);
418 return IFCALLRESULT(CHANNEL_RC_OK, context->CacheImportReply, context, &reply);
419}
420
426WINPR_ATTR_NODISCARD static UINT
427rdpgfx_send_create_surface_pdu(RdpgfxServerContext* context, const RDPGFX_CREATE_SURFACE_PDU* pdu)
428{
429 if (!checkCapsAreExchanged(context))
430 return CHANNEL_RC_NOT_INITIALIZED;
431
432 WINPR_ASSERT(context->priv);
433 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CREATESURFACE, 7);
434
435 WINPR_ASSERT(context);
436 WINPR_ASSERT(pdu);
437 WINPR_ASSERT((pdu->pixelFormat == GFX_PIXEL_FORMAT_XRGB_8888) ||
438 (pdu->pixelFormat == GFX_PIXEL_FORMAT_ARGB_8888));
439
440 if (!s)
441 {
442 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
443 return CHANNEL_RC_NO_MEMORY;
444 }
445
446 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
447 Stream_Write_UINT16(s, pdu->width); /* width (2 bytes) */
448 Stream_Write_UINT16(s, pdu->height); /* height (2 bytes) */
449 Stream_Write_UINT8(s, pdu->pixelFormat); /* RDPGFX_PIXELFORMAT (1 byte) */
450 return rdpgfx_server_single_packet_send(context, s);
451}
452
458WINPR_ATTR_NODISCARD static UINT
459rdpgfx_send_delete_surface_pdu(RdpgfxServerContext* context, const RDPGFX_DELETE_SURFACE_PDU* pdu)
460{
461 if (!checkCapsAreExchanged(context))
462 return CHANNEL_RC_NOT_INITIALIZED;
463
464 WINPR_ASSERT(pdu);
465 WINPR_ASSERT(context->priv);
466 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETESURFACE, 2);
467
468 if (!s)
469 {
470 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
471 return CHANNEL_RC_NO_MEMORY;
472 }
473
474 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
475 return rdpgfx_server_single_packet_send(context, s);
476}
477
478WINPR_ATTR_NODISCARD static inline BOOL
479rdpgfx_write_start_frame_pdu(wStream* s, const RDPGFX_START_FRAME_PDU* pdu)
480{
481 if (!Stream_EnsureRemainingCapacity(s, 8))
482 return FALSE;
483
484 WINPR_ASSERT(pdu);
485 Stream_Write_UINT32(s, pdu->timestamp); /* timestamp (4 bytes) */
486 Stream_Write_UINT32(s, pdu->frameId); /* frameId (4 bytes) */
487 return TRUE;
488}
489
490WINPR_ATTR_NODISCARD static inline BOOL rdpgfx_write_end_frame_pdu(wStream* s,
491 const RDPGFX_END_FRAME_PDU* pdu)
492{
493 if (!Stream_EnsureRemainingCapacity(s, 4))
494 return FALSE;
495 WINPR_ASSERT(pdu);
496 Stream_Write_UINT32(s, pdu->frameId); /* frameId (4 bytes) */
497 return TRUE;
498}
499
505WINPR_ATTR_NODISCARD static UINT rdpgfx_send_start_frame_pdu(RdpgfxServerContext* context,
506 const RDPGFX_START_FRAME_PDU* pdu)
507{
508 if (!checkCapsAreExchanged(context))
509 return CHANNEL_RC_NOT_INITIALIZED;
510
511 WINPR_ASSERT(pdu);
512 WINPR_ASSERT(context->priv);
513 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_STARTFRAME,
514 RDPGFX_START_FRAME_PDU_SIZE);
515
516 if (!s)
517 {
518 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
519 return CHANNEL_RC_NO_MEMORY;
520 }
521
522 if (!rdpgfx_write_start_frame_pdu(s, pdu))
523 {
524 Stream_Free(s, TRUE);
525 return ERROR_INTERNAL_ERROR;
526 }
527 return rdpgfx_server_single_packet_send(context, s);
528}
529
535WINPR_ATTR_NODISCARD static UINT rdpgfx_send_end_frame_pdu(RdpgfxServerContext* context,
536 const RDPGFX_END_FRAME_PDU* pdu)
537{
538 if (!checkCapsAreExchanged(context))
539 return CHANNEL_RC_NOT_INITIALIZED;
540
541 WINPR_ASSERT(pdu);
542 WINPR_ASSERT(context->priv);
543 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_ENDFRAME,
544 RDPGFX_END_FRAME_PDU_SIZE);
545
546 if (!s)
547 {
548 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
549 return CHANNEL_RC_NO_MEMORY;
550 }
551
552 if (!rdpgfx_write_end_frame_pdu(s, pdu))
553 {
554 Stream_Free(s, TRUE);
555 return ERROR_INTERNAL_ERROR;
556 }
557 return rdpgfx_server_single_packet_send(context, s);
558}
559
566WINPR_ATTR_NODISCARD static inline UINT32
567rdpgfx_estimate_h264_avc420(const RDPGFX_AVC420_BITMAP_STREAM* havc420)
568{
569 WINPR_ASSERT(havc420);
570 /* H264 metadata + H264 stream. See rdpgfx_write_h264_avc420 */
571 return sizeof(UINT32) /* numRegionRects */
572 + 10ULL /* regionRects + quantQualityVals */
573 * havc420->meta.numRegionRects +
574 havc420->length;
575}
576
577#if defined(WITH_GFX_AZURE)
578WINPR_ATTR_NODISCARD static inline UINT32
579rdpgfx_estimate_hevc(const RDPGFX_HEVC_BITMAP_STREAM* hevc)
580{
581 WINPR_ASSERT(hevc);
582 /* H264 metadata + H264 stream. See rdpgfx_write_h264_avc420 */
583 UINT32 len = sizeof(UINT32) /* numRegionRects */
584 + 10ULL /* regionRects + quantQualityVals */
585 * hevc->meta.numRegionRects +
586 sizeof(UINT32) + /* cbNalSizes */
587 hevc->cbNalSizes + hevc->length;
588 if (hevc->frameAttributes >= 0)
589 len++;
590 return len;
591}
592#endif
593
600WINPR_ATTR_NODISCARD static inline UINT32
601rdpgfx_estimate_surface_command(const RDPGFX_SURFACE_COMMAND* cmd)
602{
603 RDPGFX_AVC420_BITMAP_STREAM* havc420 = nullptr;
604 RDPGFX_AVC444_BITMAP_STREAM* havc444 = nullptr;
605 UINT32 h264Size = 0;
606
607 WINPR_ASSERT(cmd);
608
609 /* Estimate stream size according to codec. */
610 switch (cmd->codecId)
611 {
612 case RDPGFX_CODECID_CAPROGRESSIVE:
613 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
614 return RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE + cmd->length;
615
616#if defined(WITH_GFX_AZURE)
617 case RDPGFX_CODECID_HEVC420:
618 case RDPGFX_CODECID_HEVC444:
619 {
620 const RDPGFX_HEVC_BITMAP_STREAM* hevc = (const RDPGFX_HEVC_BITMAP_STREAM*)cmd->extra;
621 const UINT32 hevcSize = rdpgfx_estimate_hevc(hevc);
622 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + hevcSize;
623 }
624#endif
625
626#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
627 case RDPGFX_CODECID_AV1:
628#endif
629 case RDPGFX_CODECID_AVC420:
630 havc420 = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
631 h264Size = rdpgfx_estimate_h264_avc420(havc420);
632 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
633
634 case RDPGFX_CODECID_AVC444:
635 havc444 = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
636 h264Size = sizeof(UINT32); /* cbAvc420EncodedBitstream1 */
637 /* avc420EncodedBitstream1 */
638 havc420 = &(havc444->bitstream[0]);
639 h264Size += rdpgfx_estimate_h264_avc420(havc420);
640
641 /* avc420EncodedBitstream2 */
642 if (havc444->LC == 0)
643 {
644 havc420 = &(havc444->bitstream[1]);
645 h264Size += rdpgfx_estimate_h264_avc420(havc420);
646 }
647
648 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
649
650 default:
651 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + cmd->length;
652 }
653}
654
662WINPR_ATTR_NODISCARD static inline UINT16
663rdpgfx_surface_command_cmdid(const RDPGFX_SURFACE_COMMAND* cmd)
664{
665 WINPR_ASSERT(cmd);
666
667 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
668 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
669 {
670 return RDPGFX_CMDID_WIRETOSURFACE_2;
671 }
672
673 return RDPGFX_CMDID_WIRETOSURFACE_1;
674}
675
681WINPR_ATTR_NODISCARD static UINT rdpgfx_write_h264_metablock(wLog* log, wStream* s,
682 const RDPGFX_H264_METABLOCK* meta)
683{
684 RDPGFX_H264_QUANT_QUALITY* quantQualityVal = nullptr;
685 UINT error = CHANNEL_RC_OK;
686
687 WINPR_ASSERT(meta);
688 if (!Stream_EnsureRemainingCapacity(s, 4ull + meta->numRegionRects * 10ull))
689 return ERROR_OUTOFMEMORY;
690
691 Stream_Write_UINT32(s, meta->numRegionRects); /* numRegionRects (4 bytes) */
692
693 if (meta->numRegionRects > RDPGFX_MAX_META_REGION_COUNT)
694 {
695 WLog_Print(log, WLOG_ERROR, "unusually high number of update rectangles %" PRIu32,
696 meta->numRegionRects);
697 return ERROR_INVALID_PARAMETER;
698 }
699
700 for (UINT32 index = 0; index < meta->numRegionRects; index++)
701 {
702 const RECTANGLE_16* regionRect = &(meta->regionRects[index]);
703 if (rectangle_is_empty(regionRect))
704 WLog_Print(log, WLOG_WARN, "rectangle at %" PRIu32 " (of %" PRIu32 ") is empty!", index,
705 meta->numRegionRects);
706
707 if ((error = rdpgfx_write_rect16(s, regionRect)))
708 {
709 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_rect16 failed with error %" PRIu32 "!",
710 error);
711 return error;
712 }
713 }
714
715 for (UINT32 index = 0; index < meta->numRegionRects; index++)
716 {
717 quantQualityVal = &(meta->quantQualityVals[index]);
718 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(
719 uint8_t, quantQualityVal->qp | (quantQualityVal->r << 6) |
720 (quantQualityVal->p << 7))); /* qpVal (1 byte) */
721 /* qualityVal (1 byte) */
722 Stream_Write_UINT8(s, quantQualityVal->qualityVal);
723 }
724
725 return error;
726}
727
734WINPR_ATTR_NODISCARD static inline UINT
735rdpgfx_write_h264_avc420(wLog* log, wStream* s, const RDPGFX_AVC420_BITMAP_STREAM* havc420)
736{
737 WINPR_ASSERT(havc420);
738 const UINT error = rdpgfx_write_h264_metablock(log, s, &(havc420->meta));
739
740 if (error != CHANNEL_RC_OK)
741 {
742 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_h264_metablock failed with error %" PRIu32 "!",
743 error);
744 return error;
745 }
746
747 if (!Stream_EnsureRemainingCapacity(s, havc420->length))
748 return ERROR_OUTOFMEMORY;
749
750 Stream_Write(s, havc420->data, havc420->length);
751 return error;
752}
753
754#if defined(WITH_GFX_AZURE)
755WINPR_ATTR_NODISCARD static inline UINT rdpgfx_write_hevc(wLog* log, wStream* s,
756 const RDPGFX_HEVC_BITMAP_STREAM* hevc,
757 const RDPGFX_CAPSET* activeCaps)
758{
759 WINPR_ASSERT(hevc);
760 const UINT error = rdpgfx_write_h264_metablock(log, s, &(hevc->meta));
761
762 if (error != CHANNEL_RC_OK)
763 {
764 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_h264_metablock failed with error %" PRIu32 "!",
765 error);
766 return error;
767 }
768
769 if (!Stream_EnsureRemainingCapacity(s, sizeof(UINT32)))
770 return ERROR_OUTOFMEMORY;
771 Stream_Write_UINT32(s, hevc->cbNalSizes);
772
773 if (!Stream_EnsureRemainingCapacity(s, hevc->cbNalSizes))
774 return ERROR_OUTOFMEMORY;
775
776 for (size_t x = 0; x < hevc->cbNalSizes / 4; x++)
777 Stream_Write_UINT32(s, hevc->nalSize[x]);
778
779 if (activeCaps->version >= RDPGFX_CAPVERSION_115)
780 {
781 if (!Stream_EnsureRemainingCapacity(s, 1))
782 return ERROR_OUTOFMEMORY;
783
784 if (hevc->frameAttributes < 0)
785 Stream_Write_UINT8(s, 1);
786 else
787 Stream_Write_UINT8(s, hevc->frameAttributes & 0xff);
788 }
789
790 if (!Stream_EnsureRemainingCapacity(s, hevc->length))
791 return ERROR_OUTOFMEMORY;
792
793 Stream_Write(s, hevc->data, hevc->length);
794 return error;
795}
796#endif
797
805WINPR_ATTR_NODISCARD static UINT rdpgfx_write_surface_command(wLog* log, wStream* s,
806 const RDPGFX_SURFACE_COMMAND* cmd,
807 const RDPGFX_CAPSET* activeCaps)
808{
809 UINT error = CHANNEL_RC_OK;
810 RDPGFX_AVC420_BITMAP_STREAM* havc420 = nullptr;
811 RDPGFX_AVC444_BITMAP_STREAM* havc444 = nullptr;
812 UINT8 pixelFormat = 0;
813
814 WINPR_ASSERT(cmd);
815 switch (cmd->format)
816 {
817 case PIXEL_FORMAT_BGRX32:
818 pixelFormat = GFX_PIXEL_FORMAT_XRGB_8888;
819 break;
820
821 case PIXEL_FORMAT_BGRA32:
822 pixelFormat = GFX_PIXEL_FORMAT_ARGB_8888;
823 break;
824
825 default:
826 WLog_Print(log, WLOG_ERROR, "Format %s not supported!",
827 FreeRDPGetColorFormatName(cmd->format));
828 return ERROR_INVALID_DATA;
829 }
830
831 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
832 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
833 {
834 if (!Stream_EnsureRemainingCapacity(s, 13ull + cmd->length))
835 return ERROR_INTERNAL_ERROR;
836 /* Write RDPGFX_CMDID_WIRETOSURFACE_2 format for CAPROGRESSIVE */
837 Stream_Write_UINT16(
838 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->surfaceId)); /* surfaceId (2 bytes) */
839 Stream_Write_UINT16(
840 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->codecId)); /* codecId (2 bytes) */
841 Stream_Write_UINT32(s, cmd->contextId); /* codecContextId (4 bytes) */
842 Stream_Write_UINT8(s, pixelFormat); /* pixelFormat (1 byte) */
843 Stream_Write_UINT32(s, cmd->length); /* bitmapDataLength (4 bytes) */
844 Stream_Write(s, cmd->data, cmd->length);
845 }
846 else
847 {
848 /* Write RDPGFX_CMDID_WIRETOSURFACE_1 format for others */
849 if (!Stream_EnsureRemainingCapacity(s, 17))
850 return ERROR_INTERNAL_ERROR;
851 Stream_Write_UINT16(
852 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->surfaceId)); /* surfaceId (2 bytes) */
853 Stream_Write_UINT16(
854 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->codecId)); /* codecId (2 bytes) */
855 Stream_Write_UINT8(s, pixelFormat); /* pixelFormat (1 byte) */
856 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->left)); /* left (2 bytes) */
857 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->top)); /* top (2 bytes) */
858 Stream_Write_UINT16(s,
859 WINPR_ASSERTING_INT_CAST(uint16_t, cmd->right)); /* right (2 bytes) */
860 Stream_Write_UINT16(s,
861 WINPR_ASSERTING_INT_CAST(uint16_t, cmd->bottom)); /* bottom (2 bytes) */
862 Stream_Write_UINT32(s, cmd->length); /* bitmapDataLength (4 bytes) */
863 const size_t bitmapDataStart = Stream_GetPosition(s);
864
865 switch (cmd->codecId)
866 {
867#if defined(WITH_GFX_AZURE)
868 case RDPGFX_CODECID_HEVC420:
869 case RDPGFX_CODECID_HEVC444:
870 {
871 const RDPGFX_HEVC_BITMAP_STREAM* hevc = (RDPGFX_HEVC_BITMAP_STREAM*)cmd->extra;
872 error = rdpgfx_write_hevc(log, s, hevc, activeCaps);
873
874 if (error != CHANNEL_RC_OK)
875 {
876 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_h264_avc420 failed!");
877 return error;
878 }
879 }
880 break;
881#else
882 WINPR_UNUSED(activeCaps);
883#endif
884#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
885 case RDPGFX_CODECID_AV1:
886#endif
887 case RDPGFX_CODECID_AVC420:
888 {
889 havc420 = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
890 error = rdpgfx_write_h264_avc420(log, s, havc420);
891
892 if (error != CHANNEL_RC_OK)
893 {
894 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_h264_avc420 failed!");
895 return error;
896 }
897 }
898 break;
899
900 case RDPGFX_CODECID_AVC444:
901 case RDPGFX_CODECID_AVC444v2:
902 {
903 havc444 = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
904 havc420 = &(havc444->bitstream[0]); /* avc420EncodedBitstreamInfo (4 bytes) */
905 if (!Stream_EnsureRemainingCapacity(s, 4))
906 return ERROR_INTERNAL_ERROR;
907 Stream_Write_UINT32(s, havc444->cbAvc420EncodedBitstream1 |
908 ((uint32_t)havc444->LC << 30UL));
909 /* avc420EncodedBitstream1 */
910 error = rdpgfx_write_h264_avc420(log, s, havc420);
911
912 if (error != CHANNEL_RC_OK)
913 {
914 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_h264_avc420 failed!");
915 return error;
916 }
917
918 /* avc420EncodedBitstream2 */
919 if (havc444->LC == 0)
920 {
921 havc420 = &(havc444->bitstream[1]);
922 error = rdpgfx_write_h264_avc420(log, s, havc420);
923
924 if (error != CHANNEL_RC_OK)
925 {
926 WLog_Print(log, WLOG_ERROR, "rdpgfx_write_h264_avc420 failed!");
927 return error;
928 }
929 }
930 }
931 break;
932 default:
933 if (!Stream_EnsureRemainingCapacity(s, cmd->length))
934 return ERROR_INTERNAL_ERROR;
935 Stream_Write(s, cmd->data, cmd->length);
936 break;
937 }
938
939 /* Fill actual bitmap data length */
940 const size_t bitmapDataLength = Stream_GetPosition(s) - bitmapDataStart;
941 if (bitmapDataLength > UINT32_MAX)
942 return ERROR_INTERNAL_ERROR;
943
944 if (!Stream_SetPosition(s, bitmapDataStart - sizeof(UINT32)))
945 return ERROR_INVALID_DATA;
946 if (!Stream_EnsureRemainingCapacity(s, 4))
947 return ERROR_INTERNAL_ERROR;
948 Stream_Write_UINT32(s, (UINT32)bitmapDataLength); /* bitmapDataLength (4 bytes) */
949 if (!Stream_SafeSeek(s, bitmapDataLength))
950 return ERROR_INTERNAL_ERROR;
951 }
952
953 return error;
954}
955
963WINPR_ATTR_NODISCARD static UINT rdpgfx_send_surface_command(RdpgfxServerContext* context,
964 const RDPGFX_SURFACE_COMMAND* cmd)
965{
966 if (!checkCapsAreExchanged(context))
967 return CHANNEL_RC_NOT_INITIALIZED;
968 UINT error = CHANNEL_RC_OK;
969
970 WINPR_ASSERT(context);
971 WINPR_ASSERT(context->priv);
972 WINPR_ASSERT(cmd);
973
974 wStream* s =
975 rdpgfx_server_single_packet_new(context->priv->log, rdpgfx_surface_command_cmdid(cmd),
976 rdpgfx_estimate_surface_command(cmd));
977
978 if (!s)
979 {
980 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
981 return CHANNEL_RC_NO_MEMORY;
982 }
983
984 error = rdpgfx_write_surface_command(context->priv->log, s, cmd, &context->priv->activeCapSet);
985
986 if (error != CHANNEL_RC_OK)
987 {
988 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_write_surface_command failed!");
989 goto error;
990 }
991
992 return rdpgfx_server_single_packet_send(context, s);
993error:
994 Stream_Free(s, TRUE);
995 return error;
996}
997
1006WINPR_ATTR_NODISCARD static UINT
1007rdpgfx_send_surface_frame_command(RdpgfxServerContext* context, const RDPGFX_SURFACE_COMMAND* cmd,
1008 const RDPGFX_START_FRAME_PDU* startFrame,
1009 const RDPGFX_END_FRAME_PDU* endFrame)
1010
1011{
1012 if (!checkCapsAreExchanged(context))
1013 return CHANNEL_RC_NOT_INITIALIZED;
1014
1015 WINPR_ASSERT(context->priv);
1016 WINPR_ASSERT(cmd);
1017 WINPR_ASSERT(startFrame);
1018 WINPR_ASSERT(endFrame);
1019
1020 UINT error = CHANNEL_RC_OK;
1021 size_t size = rdpgfx_pdu_length(rdpgfx_estimate_surface_command(cmd));
1022
1023 if (startFrame)
1024 {
1025 size += rdpgfx_pdu_length(RDPGFX_START_FRAME_PDU_SIZE);
1026 }
1027
1028 if (endFrame)
1029 {
1030 size += rdpgfx_pdu_length(RDPGFX_END_FRAME_PDU_SIZE);
1031 }
1032
1033 wStream* s = Stream_New(nullptr, size);
1034
1035 if (!s)
1036 {
1037 WLog_Print(context->priv->log, WLOG_ERROR, "Stream_New failed!");
1038 return CHANNEL_RC_NO_MEMORY;
1039 }
1040
1041 /* Write start frame if exists */
1042 if (startFrame)
1043 {
1044 const size_t position = Stream_GetPosition(s);
1045 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_STARTFRAME, 0);
1046
1047 if (error != CHANNEL_RC_OK)
1048 {
1049 WLog_Print(context->priv->log, WLOG_ERROR,
1050 "Failed to init header with error %" PRIu32 "!", error);
1051 goto error;
1052 }
1053
1054 if (!rdpgfx_write_start_frame_pdu(s, startFrame) ||
1055 !rdpgfx_server_packet_complete_header(s, position))
1056 {
1057 error = ERROR_INTERNAL_ERROR;
1058 goto error;
1059 }
1060 }
1061
1062 /* Write RDPGFX_CMDID_WIRETOSURFACE_1 or RDPGFX_CMDID_WIRETOSURFACE_2 */
1063 {
1064 const size_t pos = Stream_GetPosition(s);
1065 error = rdpgfx_server_packet_init_header(s, rdpgfx_surface_command_cmdid(cmd),
1066 0); // Actual length will be filled later
1067
1068 if (error != CHANNEL_RC_OK)
1069 {
1070 WLog_Print(context->priv->log, WLOG_ERROR,
1071 "Failed to init header with error %" PRIu32 "!", error);
1072 goto error;
1073 }
1074
1075 error =
1076 rdpgfx_write_surface_command(context->priv->log, s, cmd, &context->priv->activeCapSet);
1077
1078 if (error != CHANNEL_RC_OK)
1079 {
1080 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_write_surface_command failed!");
1081 goto error;
1082 }
1083
1084 if (!rdpgfx_server_packet_complete_header(s, pos))
1085 {
1086 error = ERROR_INTERNAL_ERROR;
1087 goto error;
1088 }
1089 }
1090
1091 /* Write end frame if exists */
1092 if (endFrame)
1093 {
1094 const size_t position = Stream_GetPosition(s);
1095 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_ENDFRAME, 0);
1096
1097 if (error != CHANNEL_RC_OK)
1098 {
1099 WLog_Print(context->priv->log, WLOG_ERROR,
1100 "Failed to init header with error %" PRIu32 "!", error);
1101 goto error;
1102 }
1103
1104 if (!rdpgfx_write_end_frame_pdu(s, endFrame) ||
1105 !rdpgfx_server_packet_complete_header(s, position))
1106 {
1107 error = ERROR_INTERNAL_ERROR;
1108 goto error;
1109 }
1110 }
1111
1112 return rdpgfx_server_packet_send(context, s);
1113error:
1114 Stream_Free(s, TRUE);
1115 return error;
1116}
1117
1123WINPR_ATTR_NODISCARD static UINT
1124rdpgfx_send_delete_encoding_context_pdu(RdpgfxServerContext* context,
1126{
1127 if (!checkCapsAreExchanged(context))
1128 return CHANNEL_RC_NOT_INITIALIZED;
1129
1130 WINPR_ASSERT(context->priv);
1131 WINPR_ASSERT(pdu);
1132 wStream* s =
1133 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETEENCODINGCONTEXT, 6);
1134
1135 if (!s)
1136 {
1137 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1138 return CHANNEL_RC_NO_MEMORY;
1139 }
1140
1141 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1142 Stream_Write_UINT32(s, pdu->codecContextId); /* codecContextId (4 bytes) */
1143 return rdpgfx_server_single_packet_send(context, s);
1144}
1145
1151WINPR_ATTR_NODISCARD static UINT rdpgfx_send_solid_fill_pdu(RdpgfxServerContext* context,
1152 const RDPGFX_SOLID_FILL_PDU* pdu)
1153{
1154 if (!checkCapsAreExchanged(context))
1155 return CHANNEL_RC_NOT_INITIALIZED;
1156
1157 WINPR_ASSERT(context->priv);
1158 WINPR_ASSERT(pdu);
1159
1160 UINT error = CHANNEL_RC_OK;
1161 RECTANGLE_16* fillRect = nullptr;
1162 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SOLIDFILL,
1163 8 + 8 * pdu->fillRectCount);
1164
1165 if (!s)
1166 {
1167 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1168 return CHANNEL_RC_NO_MEMORY;
1169 }
1170
1171 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1172
1173 /* fillPixel (4 bytes) */
1174 if ((error = rdpgfx_write_color32(s, &(pdu->fillPixel))))
1175 {
1176 WLog_Print(context->priv->log, WLOG_ERROR,
1177 "rdpgfx_write_color32 failed with error %" PRIu32 "!", error);
1178 goto error;
1179 }
1180
1181 Stream_Write_UINT16(s, pdu->fillRectCount); /* fillRectCount (2 bytes) */
1182
1183 for (UINT16 index = 0; index < pdu->fillRectCount; index++)
1184 {
1185 fillRect = &(pdu->fillRects[index]);
1186
1187 if ((error = rdpgfx_write_rect16(s, fillRect)))
1188 {
1189 WLog_Print(context->priv->log, WLOG_ERROR,
1190 "rdpgfx_write_rect16 failed with error %" PRIu32 "!", error);
1191 goto error;
1192 }
1193 }
1194
1195 return rdpgfx_server_single_packet_send(context, s);
1196error:
1197 Stream_Free(s, TRUE);
1198 return error;
1199}
1200
1206WINPR_ATTR_NODISCARD static UINT
1207rdpgfx_send_surface_to_surface_pdu(RdpgfxServerContext* context,
1209{
1210 if (!checkCapsAreExchanged(context))
1211 return CHANNEL_RC_NOT_INITIALIZED;
1212
1213 WINPR_ASSERT(pdu);
1214 WINPR_ASSERT(context->priv);
1215
1216 UINT error = CHANNEL_RC_OK;
1217 RDPGFX_POINT16* destPt = nullptr;
1218 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOSURFACE,
1219 14 + 4 * pdu->destPtsCount);
1220
1221 if (!s)
1222 {
1223 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1224 return CHANNEL_RC_NO_MEMORY;
1225 }
1226
1227 Stream_Write_UINT16(s, pdu->surfaceIdSrc); /* surfaceIdSrc (2 bytes) */
1228 Stream_Write_UINT16(s, pdu->surfaceIdDest); /* surfaceIdDest (2 bytes) */
1229
1230 /* rectSrc (8 bytes ) */
1231 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1232 {
1233 WLog_Print(context->priv->log, WLOG_ERROR,
1234 "rdpgfx_write_rect16 failed with error %" PRIu32 "!", error);
1235 goto error;
1236 }
1237
1238 Stream_Write_UINT16(s, pdu->destPtsCount); /* destPtsCount (2 bytes) */
1239
1240 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1241 {
1242 destPt = &(pdu->destPts[index]);
1243
1244 if ((error = rdpgfx_write_point16(s, destPt)))
1245 {
1246 WLog_Print(context->priv->log, WLOG_ERROR,
1247 "rdpgfx_write_point16 failed with error %" PRIu32 "!", error);
1248 goto error;
1249 }
1250 }
1251
1252 return rdpgfx_server_single_packet_send(context, s);
1253error:
1254 Stream_Free(s, TRUE);
1255 return error;
1256}
1257
1263WINPR_ATTR_NODISCARD static UINT
1264rdpgfx_send_surface_to_cache_pdu(RdpgfxServerContext* context,
1265 const RDPGFX_SURFACE_TO_CACHE_PDU* pdu)
1266{
1267 if (!checkCapsAreExchanged(context))
1268 return CHANNEL_RC_NOT_INITIALIZED;
1269
1270 WINPR_ASSERT(pdu);
1271 WINPR_ASSERT(context->priv);
1272
1273 UINT error = CHANNEL_RC_OK;
1274 wStream* s =
1275 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOCACHE, 20);
1276
1277 if (!s)
1278 {
1279 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1280 return CHANNEL_RC_NO_MEMORY;
1281 }
1282
1283 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1284 Stream_Write_UINT64(s, pdu->cacheKey); /* cacheKey (8 bytes) */
1285 Stream_Write_UINT16(s, pdu->cacheSlot); /* cacheSlot (2 bytes) */
1286
1287 /* rectSrc (8 bytes ) */
1288 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1289 {
1290 WLog_Print(context->priv->log, WLOG_ERROR,
1291 "rdpgfx_write_rect16 failed with error %" PRIu32 "!", error);
1292 goto error;
1293 }
1294
1295 return rdpgfx_server_single_packet_send(context, s);
1296error:
1297 Stream_Free(s, TRUE);
1298 return error;
1299}
1300
1306WINPR_ATTR_NODISCARD static UINT
1307rdpgfx_send_cache_to_surface_pdu(RdpgfxServerContext* context,
1308 const RDPGFX_CACHE_TO_SURFACE_PDU* pdu)
1309{
1310 if (!checkCapsAreExchanged(context))
1311 return CHANNEL_RC_NOT_INITIALIZED;
1312
1313 WINPR_ASSERT(pdu);
1314 WINPR_ASSERT(context->priv);
1315
1316 UINT error = CHANNEL_RC_OK;
1317 RDPGFX_POINT16* destPt = nullptr;
1318 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CACHETOSURFACE,
1319 6 + 4 * pdu->destPtsCount);
1320
1321 if (!s)
1322 {
1323 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1324 return CHANNEL_RC_NO_MEMORY;
1325 }
1326
1327 Stream_Write_UINT16(s, pdu->cacheSlot); /* cacheSlot (2 bytes) */
1328 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1329 Stream_Write_UINT16(s, pdu->destPtsCount); /* destPtsCount (2 bytes) */
1330
1331 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1332 {
1333 destPt = &(pdu->destPts[index]);
1334
1335 if ((error = rdpgfx_write_point16(s, destPt)))
1336 {
1337 WLog_Print(context->priv->log, WLOG_ERROR,
1338 "rdpgfx_write_point16 failed with error %" PRIu32 "", error);
1339 goto error;
1340 }
1341 }
1342
1343 return rdpgfx_server_single_packet_send(context, s);
1344error:
1345 Stream_Free(s, TRUE);
1346 return error;
1347}
1348
1354WINPR_ATTR_NODISCARD static UINT
1355rdpgfx_send_map_surface_to_output_pdu(RdpgfxServerContext* context,
1357{
1358 if (!checkCapsAreExchanged(context))
1359 return CHANNEL_RC_NOT_INITIALIZED;
1360
1361 WINPR_ASSERT(pdu);
1362 WINPR_ASSERT(context->priv);
1363 wStream* s =
1364 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOOUTPUT, 12);
1365
1366 if (!s)
1367 {
1368 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1369 return CHANNEL_RC_NO_MEMORY;
1370 }
1371
1372 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1373 Stream_Write_UINT16(s, 0); /* reserved (2 bytes). Must be 0 */
1374 Stream_Write_UINT32(s, pdu->outputOriginX); /* outputOriginX (4 bytes) */
1375 Stream_Write_UINT32(s, pdu->outputOriginY); /* outputOriginY (4 bytes) */
1376 return rdpgfx_server_single_packet_send(context, s);
1377}
1378
1384WINPR_ATTR_NODISCARD static UINT
1385rdpgfx_send_map_surface_to_window_pdu(RdpgfxServerContext* context,
1387{
1388 if (!checkCapsAreExchanged(context))
1389 return CHANNEL_RC_NOT_INITIALIZED;
1390
1391 WINPR_ASSERT(pdu);
1392 WINPR_ASSERT(context->priv);
1393
1394 wStream* s =
1395 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOWINDOW, 18);
1396
1397 if (!s)
1398 {
1399 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1400 return CHANNEL_RC_NO_MEMORY;
1401 }
1402
1403 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1404 Stream_Write_UINT64(s, pdu->windowId); /* windowId (8 bytes) */
1405 Stream_Write_UINT32(s, pdu->mappedWidth); /* mappedWidth (4 bytes) */
1406 Stream_Write_UINT32(s, pdu->mappedHeight); /* mappedHeight (4 bytes) */
1407 return rdpgfx_server_single_packet_send(context, s);
1408}
1409
1410WINPR_ATTR_NODISCARD static UINT
1411rdpgfx_send_map_surface_to_scaled_window_pdu(RdpgfxServerContext* context,
1413{
1414 if (!checkCapsAreExchanged(context))
1415 return CHANNEL_RC_NOT_INITIALIZED;
1416
1417 WINPR_ASSERT(pdu);
1418 WINPR_ASSERT(context->priv);
1419 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1420 RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW, 26);
1421
1422 if (!s)
1423 {
1424 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1425 return CHANNEL_RC_NO_MEMORY;
1426 }
1427
1428 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1429 Stream_Write_UINT64(s, pdu->windowId); /* windowId (8 bytes) */
1430 Stream_Write_UINT32(s, pdu->mappedWidth); /* mappedWidth (4 bytes) */
1431 Stream_Write_UINT32(s, pdu->mappedHeight); /* mappedHeight (4 bytes) */
1432 Stream_Write_UINT32(s, pdu->targetWidth); /* targetWidth (4 bytes) */
1433 Stream_Write_UINT32(s, pdu->targetHeight); /* targetHeight (4 bytes) */
1434 return rdpgfx_server_single_packet_send(context, s);
1435}
1436
1442WINPR_ATTR_NODISCARD static UINT rdpgfx_recv_frame_acknowledge_pdu(RdpgfxServerContext* context,
1443 wStream* s)
1444{
1445 WINPR_ASSERT(context);
1446
1447 if (!checkCapsAreExchanged(context))
1448 return CHANNEL_RC_NOT_INITIALIZED;
1449
1450 WINPR_ASSERT(context->priv);
1451
1452 RDPGFX_FRAME_ACKNOWLEDGE_PDU pdu = WINPR_C_ARRAY_INIT;
1453 UINT error = CHANNEL_RC_OK;
1454
1455 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 12))
1456 return ERROR_INVALID_DATA;
1457
1458 Stream_Read_UINT32(s, pdu.queueDepth); /* queueDepth (4 bytes) */
1459 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1460 Stream_Read_UINT32(s, pdu.totalFramesDecoded); /* totalFramesDecoded (4 bytes) */
1461
1462 IFCALLRET(context->FrameAcknowledge, error, context, &pdu);
1463
1464 if (error)
1465 WLog_Print(context->priv->log, WLOG_ERROR,
1466 "context->FrameAcknowledge failed with error %" PRIu32 "", error);
1467
1468 return error;
1469}
1470
1476WINPR_ATTR_NODISCARD static UINT rdpgfx_recv_cache_import_offer_pdu(RdpgfxServerContext* context,
1477 wStream* s)
1478{
1479 WINPR_ASSERT(context);
1480 if (!checkCapsAreExchanged(context))
1481 return CHANNEL_RC_NOT_INITIALIZED;
1482
1483 WINPR_ASSERT(context->priv);
1484
1485 RDPGFX_CACHE_IMPORT_OFFER_PDU pdu = WINPR_C_ARRAY_INIT;
1486 RDPGFX_CACHE_ENTRY_METADATA* cacheEntry = nullptr;
1487 UINT error = CHANNEL_RC_OK;
1488
1489 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1490 return ERROR_INVALID_DATA;
1491
1492 /* cacheEntriesCount (2 bytes) */
1493 Stream_Read_UINT16(s, pdu.cacheEntriesCount);
1494
1495 /* 2.2.2.16 RDPGFX_CACHE_IMPORT_OFFER_PDU */
1496 if (pdu.cacheEntriesCount >= 5462)
1497 {
1498 WLog_Print(context->priv->log, WLOG_ERROR, "Invalid cacheEntriesCount: %" PRIu16 "",
1499 pdu.cacheEntriesCount);
1500 return ERROR_INVALID_DATA;
1501 }
1502
1503 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, pdu.cacheEntriesCount,
1504 12ull))
1505 return ERROR_INVALID_DATA;
1506
1507 for (UINT16 index = 0; index < pdu.cacheEntriesCount; index++)
1508 {
1509 cacheEntry = &(pdu.cacheEntries[index]);
1510 Stream_Read_UINT64(s, cacheEntry->cacheKey); /* cacheKey (8 bytes) */
1511 Stream_Read_UINT32(s, cacheEntry->bitmapLength); /* bitmapLength (4 bytes) */
1512 }
1513
1514 IFCALLRET(context->CacheImportOffer, error, context, &pdu);
1515
1516 if (error)
1517 WLog_Print(context->priv->log, WLOG_ERROR,
1518 "context->CacheImportOffer failed with error %" PRIu32 "", error);
1519
1520 return error;
1521}
1522
1528WINPR_ATTR_NODISCARD static UINT rdpgfx_recv_caps_advertise_pdu(RdpgfxServerContext* context,
1529 wStream* s)
1530{
1531 RDPGFX_CAPSET* capsSets = nullptr;
1532 RDPGFX_CAPS_ADVERTISE_PDU pdu = WINPR_C_ARRAY_INIT;
1533 UINT error = ERROR_INVALID_DATA;
1534
1535 if (!context)
1536 return ERROR_BAD_ARGUMENTS;
1537
1538 WINPR_ASSERT(context->priv);
1539 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1540 return ERROR_INVALID_DATA;
1541
1542 Stream_Read_UINT16(s, pdu.capsSetCount); /* capsSetCount (2 bytes) */
1543 if (pdu.capsSetCount > 0)
1544 {
1545 capsSets = calloc(pdu.capsSetCount, (RDPGFX_CAPSET_BASE_SIZE + 4));
1546 if (!capsSets)
1547 return ERROR_OUTOFMEMORY;
1548 }
1549
1550 pdu.capsSets = capsSets;
1551
1552 for (UINT16 index = 0; index < pdu.capsSetCount; index++)
1553 {
1554 RDPGFX_CAPSET* capsSet = &(pdu.capsSets[index]);
1555
1556 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 8))
1557 goto fail;
1558
1559 Stream_Read_UINT32(s, capsSet->version); /* version (4 bytes) */
1560 Stream_Read_UINT32(s, capsSet->length); /* capsDataLength (4 bytes) */
1561
1562 if (capsSet->length >= 4)
1563 {
1564 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
1565 goto fail;
1566
1567 Stream_Peek_UINT32(s, capsSet->flags); /* capsData (4 bytes) */
1568 }
1569
1570 if (!Stream_SafeSeek(s, capsSet->length))
1571 goto fail;
1572 }
1573
1574 error = ERROR_BAD_CONFIGURATION;
1575 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
1576
1577 if (error)
1578 WLog_Print(context->priv->log, WLOG_ERROR,
1579 "context->CapsAdvertise failed with error %" PRIu32 "", error);
1580
1581fail:
1582 free(capsSets);
1583 return error;
1584}
1585
1591WINPR_ATTR_NODISCARD static UINT rdpgfx_recv_qoe_frame_acknowledge_pdu(RdpgfxServerContext* context,
1592 wStream* s)
1593{
1594 WINPR_ASSERT(context);
1595
1596 if (!checkCapsAreExchanged(context))
1597 return CHANNEL_RC_NOT_INITIALIZED;
1598
1599 RDPGFX_QOE_FRAME_ACKNOWLEDGE_PDU pdu = WINPR_C_ARRAY_INIT;
1600 UINT error = CHANNEL_RC_OK;
1601
1602 WINPR_ASSERT(context->priv);
1603
1604 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 12))
1605 return ERROR_INVALID_DATA;
1606
1607 Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1608 Stream_Read_UINT32(s, pdu.timestamp); /* timestamp (4 bytes) */
1609 Stream_Read_UINT16(s, pdu.timeDiffSE); /* timeDiffSE (2 bytes) */
1610 Stream_Read_UINT16(s, pdu.timeDiffEDR); /* timeDiffEDR (2 bytes) */
1611
1612 IFCALLRET(context->QoeFrameAcknowledge, error, context, &pdu);
1613
1614 if (error)
1615 WLog_Print(context->priv->log, WLOG_ERROR,
1616 "context->QoeFrameAcknowledge failed with error %" PRIu32 "", error);
1617
1618 return error;
1619}
1620
1621WINPR_ATTR_NODISCARD static UINT
1622rdpgfx_send_map_surface_to_scaled_output_pdu(RdpgfxServerContext* context,
1624{
1625 if (!checkCapsAreExchanged(context))
1626 return CHANNEL_RC_NOT_INITIALIZED;
1627
1628 WINPR_ASSERT(pdu);
1629 WINPR_ASSERT(context->priv);
1630 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1631 RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT, 20);
1632
1633 if (!s)
1634 {
1635 WLog_Print(context->priv->log, WLOG_ERROR, "rdpgfx_server_single_packet_new failed!");
1636 return CHANNEL_RC_NO_MEMORY;
1637 }
1638
1639 Stream_Write_UINT16(s, pdu->surfaceId); /* surfaceId (2 bytes) */
1640 Stream_Write_UINT16(s, 0); /* reserved (2 bytes). Must be 0 */
1641 Stream_Write_UINT32(s, pdu->outputOriginX); /* outputOriginX (4 bytes) */
1642 Stream_Write_UINT32(s, pdu->outputOriginY); /* outputOriginY (4 bytes) */
1643 Stream_Write_UINT32(s, pdu->targetWidth); /* targetWidth (4 bytes) */
1644 Stream_Write_UINT32(s, pdu->targetHeight); /* targetHeight (4 bytes) */
1645 return rdpgfx_server_single_packet_send(context, s);
1646}
1647
1653WINPR_ATTR_NODISCARD static UINT rdpgfx_server_receive_pdu(RdpgfxServerContext* context,
1654 wStream* data)
1655{
1656 size_t packetlen = 0;
1657 wStream sbuffer = WINPR_C_ARRAY_INIT;
1658 RDPGFX_HEADER header = WINPR_C_ARRAY_INIT;
1659 UINT error = CHANNEL_RC_OK;
1660
1661 WINPR_ASSERT(context);
1662 WINPR_ASSERT(context->priv);
1663
1664 if ((error = rdpgfx_read_header(context->priv->log, data, &header, &packetlen)))
1665 {
1666 WLog_Print(context->priv->log, WLOG_ERROR,
1667 "rdpgfx_read_header failed with error %" PRIu32 "!", error);
1668 return error;
1669 }
1670
1671 /* Limit the PDU parsers to the data of this PDU, the stream might contain more of them */
1672 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
1673 Stream_Seek(data, packetlen);
1674
1675 WLog_Print(context->priv->log, WLOG_TRACE,
1676 "cmdId: %s (0x%04" PRIX16 ") flags: 0x%04" PRIX16 " pduLength: %" PRIu32 "",
1677 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
1678 header.pduLength);
1679
1680 switch (header.cmdId)
1681 {
1682 case RDPGFX_CMDID_FRAMEACKNOWLEDGE:
1683 if ((error = rdpgfx_recv_frame_acknowledge_pdu(context, s)))
1684 WLog_Print(context->priv->log, WLOG_ERROR,
1685 "rdpgfx_recv_frame_acknowledge_pdu "
1686 "failed with error %" PRIu32 "!",
1687 error);
1688
1689 break;
1690
1691 case RDPGFX_CMDID_CACHEIMPORTOFFER:
1692 if ((error = rdpgfx_recv_cache_import_offer_pdu(context, s)))
1693 WLog_Print(context->priv->log, WLOG_ERROR,
1694 "rdpgfx_recv_cache_import_offer_pdu "
1695 "failed with error %" PRIu32 "!",
1696 error);
1697
1698 break;
1699
1700 case RDPGFX_CMDID_CAPSADVERTISE:
1701 if ((error = rdpgfx_recv_caps_advertise_pdu(context, s)))
1702 WLog_Print(context->priv->log, WLOG_ERROR,
1703 "rdpgfx_recv_caps_advertise_pdu "
1704 "failed with error %" PRIu32 "!",
1705 error);
1706
1707 break;
1708
1709 case RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE:
1710 if ((error = rdpgfx_recv_qoe_frame_acknowledge_pdu(context, s)))
1711 WLog_Print(context->priv->log, WLOG_ERROR,
1712 "rdpgfx_recv_qoe_frame_acknowledge_pdu "
1713 "failed with error %" PRIu32 "!",
1714 error);
1715
1716 break;
1717
1718 default:
1719 error = CHANNEL_RC_BAD_PROC;
1720 break;
1721 }
1722
1723 if (error)
1724 {
1725 WLog_Print(context->priv->log, WLOG_ERROR,
1726 "Error while parsing GFX cmdId: %s (0x%04" PRIX16 ")",
1727 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
1728 return error;
1729 }
1730
1731 const size_t end = Stream_GetPosition(s);
1732
1733 if (end != packetlen)
1734 {
1735 WLog_Print(context->priv->log, WLOG_ERROR,
1736 "Unexpected gfx pdu end: Actual: %" PRIuz ", Expected: %" PRIuz "", end,
1737 packetlen);
1738 }
1739
1740 return error;
1741}
1742
1743WINPR_ATTR_NODISCARD static DWORD WINAPI rdpgfx_server_thread_func(LPVOID arg)
1744{
1745 RdpgfxServerContext* context = (RdpgfxServerContext*)arg;
1746 WINPR_ASSERT(context);
1747
1748 RdpgfxServerPrivate* priv = context->priv;
1749 DWORD status = 0;
1750 DWORD nCount = 0;
1751 HANDLE events[8] = WINPR_C_ARRAY_INIT;
1752 UINT error = CHANNEL_RC_OK;
1753
1754 WINPR_ASSERT(priv);
1755
1756 if (priv->ownThread)
1757 {
1758 WINPR_ASSERT(priv->stopEvent);
1759 events[nCount++] = priv->stopEvent;
1760 }
1761
1762 WINPR_ASSERT(priv->channelEvent);
1763 events[nCount++] = priv->channelEvent;
1764
1765 /* Main virtual channel loop. RDPGFX do not need version negotiation */
1766 while (TRUE)
1767 {
1768 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1769
1770 if (status == WAIT_FAILED)
1771 {
1772 error = GetLastError();
1773 WLog_Print(context->priv->log, WLOG_ERROR,
1774 "WaitForMultipleObjects failed with error %" PRIu32 "", error);
1775 break;
1776 }
1777
1778 /* Stop Event */
1779 if (status == WAIT_OBJECT_0)
1780 break;
1781
1782 if ((error = rdpgfx_server_handle_messages(context)))
1783 {
1784 WLog_Print(context->priv->log, WLOG_ERROR,
1785 "rdpgfx_server_handle_messages failed with error %" PRIu32 "", error);
1786 break;
1787 }
1788 }
1789
1790 if (error && context->rdpcontext)
1791 setChannelError(context->rdpcontext, error, "rdpgfx_server_thread_func reported an error");
1792
1793 ExitThread(error);
1794 return error;
1795}
1796
1797WINPR_ATTR_NODISCARD static BOOL rdpgfx_server_open(RdpgfxServerContext* context)
1798{
1799 WINPR_ASSERT(context);
1800 RdpgfxServerPrivate* priv = context->priv;
1801 void* buffer = nullptr;
1802
1803 WINPR_ASSERT(priv);
1804
1805 if (!priv->isOpened)
1806 {
1807 PULONG pSessionId = nullptr;
1808 DWORD BytesReturned = 0;
1809 priv->SessionId = WTS_CURRENT_SESSION;
1810 UINT32 channelId = 0;
1811 BOOL status = TRUE;
1812
1813 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
1814 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
1815 {
1816 WLog_Print(context->priv->log, WLOG_ERROR, "WTSQuerySessionInformationA failed!");
1817 return FALSE;
1818 }
1819
1820 priv->SessionId = (DWORD)*pSessionId;
1821 WTSFreeMemory(pSessionId);
1822 priv->rdpgfx_channel = WTSVirtualChannelOpenEx(priv->SessionId, RDPGFX_DVC_CHANNEL_NAME,
1823 WTS_CHANNEL_OPTION_DYNAMIC);
1824
1825 if (!priv->rdpgfx_channel)
1826 {
1827 WLog_Print(context->priv->log, WLOG_ERROR, "WTSVirtualChannelOpenEx failed!");
1828 return FALSE;
1829 }
1830
1831 channelId = WTSChannelGetIdByHandle(priv->rdpgfx_channel);
1832
1833 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
1834 if (!status)
1835 {
1836 WLog_Print(context->priv->log, WLOG_ERROR, "context->ChannelIdAssigned failed!");
1837 goto fail;
1838 }
1839
1840 /* Query for channel event handle */
1841 if (!WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualEventHandle, &buffer,
1842 &BytesReturned) ||
1843 (BytesReturned != sizeof(HANDLE)))
1844 {
1845 WLog_Print(context->priv->log, WLOG_ERROR,
1846 "WTSVirtualChannelQuery failed "
1847 "or invalid returned size(%" PRIu32 ")",
1848 BytesReturned);
1849
1850 if (buffer)
1851 WTSFreeMemory(buffer);
1852
1853 goto fail;
1854 }
1855
1856 priv->channelEvent = *(HANDLE*)buffer;
1857 WTSFreeMemory(buffer);
1858
1859 if (!(priv->zgfx = zgfx_context_new(TRUE)))
1860 {
1861 WLog_Print(context->priv->log, WLOG_ERROR, "Create zgfx context failed!");
1862 goto fail;
1863 }
1864
1865 priv->isReady = FALSE;
1866 const RDPGFX_CAPSET empty = WINPR_C_ARRAY_INIT;
1867 priv->activeCapSet = empty;
1868 if (priv->ownThread)
1869 {
1870 if (!(priv->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
1871 {
1872 WLog_Print(context->priv->log, WLOG_ERROR, "CreateEvent failed!");
1873 goto fail;
1874 }
1875
1876 if (!(priv->thread = CreateThread(nullptr, 0, rdpgfx_server_thread_func, (void*)context,
1877 0, nullptr)))
1878 {
1879 WLog_Print(context->priv->log, WLOG_ERROR, "CreateThread failed!");
1880 goto fail;
1881 }
1882 }
1883
1884 priv->isOpened = TRUE;
1885 return TRUE;
1886 }
1887
1888 WLog_Print(context->priv->log, WLOG_ERROR, "RDPGFX channel is already opened!");
1889 return FALSE;
1890fail:
1891 (void)rdpgfx_server_close(context);
1892 return FALSE;
1893}
1894
1895BOOL rdpgfx_server_close(RdpgfxServerContext* context)
1896{
1897 WINPR_ASSERT(context);
1898
1899 RdpgfxServerPrivate* priv = context->priv;
1900 WINPR_ASSERT(priv);
1901
1902 if (priv->ownThread && priv->thread)
1903 {
1904 (void)SetEvent(priv->stopEvent);
1905
1906 if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
1907 {
1908 WLog_Print(context->priv->log, WLOG_ERROR,
1909 "WaitForSingleObject failed with error %" PRIu32 "", GetLastError());
1910 return FALSE;
1911 }
1912
1913 (void)CloseHandle(priv->thread);
1914 (void)CloseHandle(priv->stopEvent);
1915 priv->thread = nullptr;
1916 priv->stopEvent = nullptr;
1917 }
1918
1919 zgfx_context_free(priv->zgfx);
1920 priv->zgfx = nullptr;
1921
1922 if (priv->rdpgfx_channel)
1923 {
1924 (void)WTSVirtualChannelClose(priv->rdpgfx_channel);
1925 priv->rdpgfx_channel = nullptr;
1926 }
1927
1928 priv->channelEvent = nullptr;
1929 priv->isOpened = FALSE;
1930 priv->isReady = FALSE;
1931 const RDPGFX_CAPSET empty = WINPR_C_ARRAY_INIT;
1932 priv->activeCapSet = empty;
1933 return TRUE;
1934}
1935
1936WINPR_ATTR_NODISCARD static BOOL rdpgfx_server_initialize(RdpgfxServerContext* context,
1937 BOOL externalThread)
1938{
1939 WINPR_ASSERT(context);
1940 WINPR_ASSERT(context->priv);
1941
1942 if (context->priv->isOpened)
1943 {
1944 WLog_Print(context->priv->log, WLOG_WARN,
1945 "Application error: RDPEGFX channel already initialized, "
1946 "calling in this state is not possible!");
1947 return FALSE;
1948 }
1949
1950 context->priv->ownThread = !externalThread;
1951 return TRUE;
1952}
1953
1954RdpgfxServerContext* rdpgfx_server_context_new(HANDLE vcm)
1955{
1956 RdpgfxServerContext* context = (RdpgfxServerContext*)calloc(1, sizeof(RdpgfxServerContext));
1957
1958 if (!context)
1959 return nullptr;
1960
1961 context->vcm = vcm;
1962 context->Initialize = rdpgfx_server_initialize;
1963 context->Open = rdpgfx_server_open;
1964 context->Close = rdpgfx_server_close;
1965 context->ResetGraphics = rdpgfx_send_reset_graphics_pdu;
1966 context->StartFrame = rdpgfx_send_start_frame_pdu;
1967 context->EndFrame = rdpgfx_send_end_frame_pdu;
1968 context->SurfaceCommand = rdpgfx_send_surface_command;
1969 context->SurfaceFrameCommand = rdpgfx_send_surface_frame_command;
1970 context->DeleteEncodingContext = rdpgfx_send_delete_encoding_context_pdu;
1971 context->CreateSurface = rdpgfx_send_create_surface_pdu;
1972 context->DeleteSurface = rdpgfx_send_delete_surface_pdu;
1973 context->SolidFill = rdpgfx_send_solid_fill_pdu;
1974 context->SurfaceToSurface = rdpgfx_send_surface_to_surface_pdu;
1975 context->SurfaceToCache = rdpgfx_send_surface_to_cache_pdu;
1976 context->CacheToSurface = rdpgfx_send_cache_to_surface_pdu;
1977 context->CacheImportOffer = rdpgfx_process_cache_import_offer_pdu;
1978 context->CacheImportReply = rdpgfx_send_cache_import_reply_pdu;
1979 context->EvictCacheEntry = rdpgfx_send_evict_cache_entry_pdu;
1980 context->MapSurfaceToOutput = rdpgfx_send_map_surface_to_output_pdu;
1981 context->MapSurfaceToWindow = rdpgfx_send_map_surface_to_window_pdu;
1982 context->MapSurfaceToScaledOutput = rdpgfx_send_map_surface_to_scaled_output_pdu;
1983 context->MapSurfaceToScaledWindow = rdpgfx_send_map_surface_to_scaled_window_pdu;
1984 context->CapsAdvertise = nullptr;
1985 context->CapsConfirm = rdpgfx_send_caps_confirm_pdu;
1986 context->FrameAcknowledge = nullptr;
1987 context->QoeFrameAcknowledge = nullptr;
1988 RdpgfxServerPrivate* priv = context->priv =
1989 (RdpgfxServerPrivate*)calloc(1, sizeof(RdpgfxServerPrivate));
1990
1991 if (!priv)
1992 goto fail;
1993
1994 priv->log = WLog_Get(TAG);
1995 if (!priv->log)
1996 goto fail;
1997
1998 /* Create shared input stream */
1999 priv->input_stream = Stream_New(nullptr, 4);
2000
2001 if (!priv->input_stream)
2002 {
2003 WLog_Print(context->priv->log, WLOG_ERROR, "Stream_New failed!");
2004 goto fail;
2005 }
2006
2007 priv->isOpened = FALSE;
2008 priv->isReady = FALSE;
2009 priv->ownThread = TRUE;
2010
2011 {
2012 const RDPGFX_CAPSET empty = WINPR_C_ARRAY_INIT;
2013 priv->activeCapSet = empty;
2014 }
2015
2016 return context;
2017fail:
2018 WINPR_PRAGMA_DIAG_PUSH
2019 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2020 rdpgfx_server_context_free(context);
2021 WINPR_PRAGMA_DIAG_POP
2022 return nullptr;
2023}
2024
2025void rdpgfx_server_context_free(RdpgfxServerContext* context)
2026{
2027 if (!context)
2028 return;
2029
2030 (void)rdpgfx_server_close(context);
2031
2032 if (context->priv)
2033 Stream_Free(context->priv->input_stream, TRUE);
2034
2035 free(context->priv);
2036 free(context);
2037}
2038
2039HANDLE rdpgfx_server_get_event_handle(RdpgfxServerContext* context)
2040{
2041 if (!context)
2042 return nullptr;
2043 if (!context->priv)
2044 return nullptr;
2045 return context->priv->channelEvent;
2046}
2047
2048/*
2049 * Handle rpdgfx messages - server side
2050 *
2051 * @param context side context
2052 *
2053 * @return 0 on success
2054 * ERROR_NO_DATA if no data could be read this time
2055 * otherwise a Win32 error code
2056 */
2057UINT rdpgfx_server_handle_messages(RdpgfxServerContext* context)
2058{
2059 DWORD BytesReturned = 0;
2060 void* buffer = nullptr;
2061 UINT ret = CHANNEL_RC_OK;
2062
2063 WINPR_ASSERT(context);
2064 WINPR_ASSERT(context->priv);
2065
2066 RdpgfxServerPrivate* priv = context->priv;
2067 wStream* s = priv->input_stream;
2068
2069 /* Check whether the dynamic channel is ready */
2070 if (!priv->isReady)
2071 {
2072 if (WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualChannelReady, &buffer,
2073 &BytesReturned) == FALSE)
2074 {
2075 if (GetLastError() == ERROR_NO_DATA)
2076 return ERROR_NO_DATA;
2077
2078 WLog_Print(context->priv->log, WLOG_ERROR, "WTSVirtualChannelQuery failed");
2079 return ERROR_INTERNAL_ERROR;
2080 }
2081
2082 priv->isReady = *((BOOL*)buffer);
2083 WTSFreeMemory(buffer);
2084 }
2085
2086 /* Consume channel event only after the gfx dynamic channel is ready */
2087 if (priv->isReady)
2088 {
2089 Stream_ResetPosition(s);
2090
2091 if (!WTSVirtualChannelRead(priv->rdpgfx_channel, 0, nullptr, 0, &BytesReturned))
2092 {
2093 if (GetLastError() == ERROR_NO_DATA)
2094 return ERROR_NO_DATA;
2095
2096 WLog_Print(context->priv->log, WLOG_ERROR, "WTSVirtualChannelRead failed!");
2097 return ERROR_INTERNAL_ERROR;
2098 }
2099
2100 if (BytesReturned < 1)
2101 return CHANNEL_RC_OK;
2102
2103 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
2104 {
2105 WLog_Print(context->priv->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
2106 return CHANNEL_RC_NO_MEMORY;
2107 }
2108
2109 const size_t len = Stream_Capacity(s);
2110 if (len > UINT32_MAX)
2111 return ERROR_INTERNAL_ERROR;
2112 if (WTSVirtualChannelRead(priv->rdpgfx_channel, 0, Stream_BufferAs(s, char), (UINT32)len,
2113 &BytesReturned) == FALSE)
2114 {
2115 WLog_Print(context->priv->log, WLOG_ERROR, "WTSVirtualChannelRead failed!");
2116 return ERROR_INTERNAL_ERROR;
2117 }
2118
2119 if (!Stream_SetLength(s, BytesReturned))
2120 return ERROR_INTERNAL_ERROR;
2121
2122 Stream_ResetPosition(s);
2123
2124 while (Stream_GetPosition(s) < Stream_Length(s))
2125 {
2126 if ((ret = rdpgfx_server_receive_pdu(context, s)))
2127 {
2128 WLog_Print(context->priv->log, WLOG_ERROR,
2129 "rdpgfx_server_receive_pdu "
2130 "failed with error %" PRIu32 "!",
2131 ret);
2132 return ret;
2133 }
2134 }
2135 }
2136
2137 return ret;
2138}