20#include <freerdp/config.h>
22#include <winpr/assert.h>
27#include <winpr/cast.h>
29#include <winpr/synch.h>
30#include <winpr/thread.h>
31#include <winpr/stream.h>
33#include <freerdp/freerdp.h>
34#include <freerdp/codec/color.h>
36#include <freerdp/channels/wtsvc.h>
37#include <freerdp/channels/log.h>
39#include "rdpgfx_common.h"
40#include "rdpgfx_main.h"
42#define TAG CHANNELS_TAG("rdpgfx.server")
44static BOOL rdpgfx_server_close(RdpgfxServerContext* context);
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,
52 WINPR_ASSERT(context);
53 WINPR_ASSERT(context->priv);
55 const DWORD level = WLOG_TRACE;
56 if (WLog_IsLevelActive(context->priv->log, level))
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);
63 return context->priv->activeCapSet.version > 0;
75WINPR_ATTR_NODISCARD
static inline size_t rdpgfx_pdu_length(
size_t dataLen)
77 return RDPGFX_HEADER_SIZE + dataLen;
80WINPR_ATTR_NODISCARD
static inline UINT rdpgfx_server_packet_init_header(
wStream* s, UINT16 cmdId,
85 .pduLength = WINPR_ASSERTING_INT_CAST(UINT32, pduLength) };
88 return rdpgfx_write_header(s, &header);
98WINPR_ATTR_NODISCARD
static inline BOOL rdpgfx_server_packet_complete_header(
wStream* s,
101 const size_t current = Stream_GetPosition(s);
102 const size_t cap = Stream_Capacity(s);
103 if (cap < start + RDPGFX_HEADER_SIZE)
105 if ((start > UINT32_MAX) || (current < start))
108 if (!Stream_SetPosition(s, start + RDPGFX_HEADER_SIZE -
sizeof(UINT32)))
110 Stream_Write_UINT32(s, (UINT32)(current - start));
111 return Stream_SetPosition(s, current);
121WINPR_ATTR_NODISCARD
static UINT rdpgfx_server_packet_send(RdpgfxServerContext* context,
wStream* s)
126 BYTE* pSrcData = Stream_Buffer(s);
127 const size_t SrcSize = Stream_GetPosition(s);
128 if (SrcSize > UINT32_MAX)
129 return ERROR_INTERNAL_ERROR;
131 WINPR_ASSERT(context);
132 WINPR_ASSERT(context->priv);
137 wStream* fs = Stream_New(
nullptr, SrcSize + 7 + (SrcSize / ZGFX_SEGMENTED_MAXSIZE + 1) * 4);
141 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
142 error = CHANNEL_RC_NO_MEMORY;
146 if (zgfx_compress_to_stream(context->priv->zgfx, fs, pSrcData, (UINT32)SrcSize, &flags) < 0)
148 WLog_Print(context->priv->log, WLOG_ERROR,
"zgfx_compress_to_stream failed!");
149 error = ERROR_INTERNAL_ERROR;
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))
159 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelWrite failed!");
160 error = ERROR_INTERNAL_ERROR;
165 if (written < Stream_GetPosition(fs))
167 WLog_Print(context->priv->log, WLOG_WARN,
168 "Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
169 Stream_GetPosition(fs));
172 error = CHANNEL_RC_OK;
174 Stream_Free(fs, TRUE);
175 Stream_Free(s, TRUE);
191WINPR_ATTR_MALLOC(Stream_Free, 1)
192static
wStream* rdpgfx_server_single_packet_new(wLog* log, UINT16 cmdId,
size_t dataLen)
195 const size_t pduLength = rdpgfx_pdu_length(dataLen);
196 wStream* s = Stream_New(
nullptr, pduLength);
200 WLog_Print(log, WLOG_ERROR,
"Stream_New failed!");
204 if ((error = rdpgfx_server_packet_init_header(s, cmdId, pduLength)))
206 WLog_Print(log, WLOG_ERROR,
"Failed to init header with error %" PRIu32
"!", error);
212 Stream_Free(s, TRUE);
224WINPR_ATTR_NODISCARD
static inline UINT
225rdpgfx_server_single_packet_send(RdpgfxServerContext* context,
wStream* s)
228 if (!rdpgfx_server_packet_complete_header(s, 0))
229 return ERROR_INTERNAL_ERROR;
230 return rdpgfx_server_packet_send(context, s);
238WINPR_ATTR_NODISCARD
static UINT
239rdpgfx_send_caps_confirm_pdu(RdpgfxServerContext* context,
242 WINPR_ASSERT(context);
243 WINPR_ASSERT(context->priv);
244 WINPR_ASSERT(capsConfirm);
247 WINPR_ASSERT(capsSet);
249 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CAPSCONFIRM,
250 RDPGFX_CAPSET_BASE_SIZE + capsSet->length);
254 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
255 return CHANNEL_RC_NO_MEMORY;
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);
262 Stream_Write_UINT32(s, capsSet->length);
264 if (capsSet->length >= 4)
266 Stream_Write_UINT32(s, capsSet->flags);
267 Stream_Zero(s, capsSet->length - 4);
270 Stream_Zero(s, capsSet->length);
272 context->priv->activeCapSet = *capsSet;
273 return rdpgfx_server_single_packet_send(context, s);
281WINPR_ATTR_NODISCARD
static UINT
284 const size_t RDPGFX_RESET_GRAPHICS_PDU_SIZE = 340;
286 if (!checkCapsAreExchanged(context))
287 return CHANNEL_RC_NOT_INITIALIZED;
290 WINPR_ASSERT(context->priv);
293 if (pdu->monitorCount >= 16)
295 WLog_Print(context->priv->log, WLOG_ERROR,
296 "Monitor count MUST be less than or equal to 16: %" PRIu32
"",
298 return ERROR_INVALID_DATA;
302 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_RESETGRAPHICS,
303 RDPGFX_RESET_GRAPHICS_PDU_SIZE - RDPGFX_HEADER_SIZE);
307 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
308 return CHANNEL_RC_NO_MEMORY;
311 Stream_Write_UINT32(s, pdu->width);
312 Stream_Write_UINT32(s, pdu->height);
313 Stream_Write_UINT32(s, pdu->monitorCount);
315 for (UINT32 index = 0; index < pdu->monitorCount; index++)
317 const MONITOR_DEF* monitor = &(pdu->monitorDefArray[index]);
318 Stream_Write_INT32(s, monitor->left);
319 Stream_Write_INT32(s, monitor->top);
320 Stream_Write_INT32(s, monitor->right);
321 Stream_Write_INT32(s, monitor->bottom);
322 Stream_Write_UINT32(s, monitor->flags);
326 const size_t pos = Stream_GetPosition(s);
327 if (pos > RDPGFX_RESET_GRAPHICS_PDU_SIZE)
329 Stream_Free(s, TRUE);
330 return ERROR_INVALID_DATA;
332 if (!Stream_SafeZero(s, RDPGFX_RESET_GRAPHICS_PDU_SIZE - pos))
334 Stream_Free(s, TRUE);
335 return ERROR_INVALID_DATA;
337 return rdpgfx_server_single_packet_send(context, s);
345WINPR_ATTR_NODISCARD
static UINT
346rdpgfx_send_evict_cache_entry_pdu(RdpgfxServerContext* context,
349 if (!checkCapsAreExchanged(context))
350 return CHANNEL_RC_NOT_INITIALIZED;
353 WINPR_ASSERT(context->priv);
355 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_EVICTCACHEENTRY, 2);
359 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
360 return CHANNEL_RC_NO_MEMORY;
363 Stream_Write_UINT16(s, pdu->cacheSlot);
364 return rdpgfx_server_single_packet_send(context, s);
372WINPR_ATTR_NODISCARD
static UINT
373rdpgfx_send_cache_import_reply_pdu(RdpgfxServerContext* context,
376 if (!checkCapsAreExchanged(context))
377 return CHANNEL_RC_NOT_INITIALIZED;
378 WINPR_ASSERT(context);
379 WINPR_ASSERT(context->priv);
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);
389 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
390 return CHANNEL_RC_NO_MEMORY;
394 Stream_Write_UINT16(s, pdu->importedEntriesCount);
396 for (UINT16 index = 0; index < pdu->importedEntriesCount; index++)
398 Stream_Write_UINT16(s, pdu->cacheSlots[index]);
401 return rdpgfx_server_single_packet_send(context, s);
404WINPR_ATTR_NODISCARD
static UINT
405rdpgfx_process_cache_import_offer_pdu(RdpgfxServerContext* context,
408 if (!checkCapsAreExchanged(context))
409 return CHANNEL_RC_NOT_INITIALIZED;
410 WINPR_ASSERT(context);
411 WINPR_ASSERT(context->priv);
412 WINPR_ASSERT(cacheImportOffer);
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);
426WINPR_ATTR_NODISCARD
static UINT
429 if (!checkCapsAreExchanged(context))
430 return CHANNEL_RC_NOT_INITIALIZED;
432 WINPR_ASSERT(context->priv);
433 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CREATESURFACE, 7);
435 WINPR_ASSERT(context);
437 WINPR_ASSERT((pdu->pixelFormat == GFX_PIXEL_FORMAT_XRGB_8888) ||
438 (pdu->pixelFormat == GFX_PIXEL_FORMAT_ARGB_8888));
442 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
443 return CHANNEL_RC_NO_MEMORY;
446 Stream_Write_UINT16(s, pdu->surfaceId);
447 Stream_Write_UINT16(s, pdu->width);
448 Stream_Write_UINT16(s, pdu->height);
449 Stream_Write_UINT8(s, pdu->pixelFormat);
450 return rdpgfx_server_single_packet_send(context, s);
458WINPR_ATTR_NODISCARD
static UINT
461 if (!checkCapsAreExchanged(context))
462 return CHANNEL_RC_NOT_INITIALIZED;
465 WINPR_ASSERT(context->priv);
466 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETESURFACE, 2);
470 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
471 return CHANNEL_RC_NO_MEMORY;
474 Stream_Write_UINT16(s, pdu->surfaceId);
475 return rdpgfx_server_single_packet_send(context, s);
478WINPR_ATTR_NODISCARD
static inline BOOL
481 if (!Stream_EnsureRemainingCapacity(s, 8))
485 Stream_Write_UINT32(s, pdu->timestamp);
486 Stream_Write_UINT32(s, pdu->frameId);
490WINPR_ATTR_NODISCARD
static inline BOOL rdpgfx_write_end_frame_pdu(
wStream* s,
493 if (!Stream_EnsureRemainingCapacity(s, 4))
496 Stream_Write_UINT32(s, pdu->frameId);
505WINPR_ATTR_NODISCARD
static UINT rdpgfx_send_start_frame_pdu(RdpgfxServerContext* context,
508 if (!checkCapsAreExchanged(context))
509 return CHANNEL_RC_NOT_INITIALIZED;
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);
518 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
519 return CHANNEL_RC_NO_MEMORY;
522 if (!rdpgfx_write_start_frame_pdu(s, pdu))
524 Stream_Free(s, TRUE);
525 return ERROR_INTERNAL_ERROR;
527 return rdpgfx_server_single_packet_send(context, s);
535WINPR_ATTR_NODISCARD
static UINT rdpgfx_send_end_frame_pdu(RdpgfxServerContext* context,
538 if (!checkCapsAreExchanged(context))
539 return CHANNEL_RC_NOT_INITIALIZED;
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);
548 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
549 return CHANNEL_RC_NO_MEMORY;
552 if (!rdpgfx_write_end_frame_pdu(s, pdu))
554 Stream_Free(s, TRUE);
555 return ERROR_INTERNAL_ERROR;
557 return rdpgfx_server_single_packet_send(context, s);
566WINPR_ATTR_NODISCARD
static inline UINT32
569 WINPR_ASSERT(havc420);
571 return sizeof(UINT32)
573 * havc420->meta.numRegionRects +
577#if defined(WITH_GFX_AZURE)
578WINPR_ATTR_NODISCARD
static inline UINT32
583 UINT32 len =
sizeof(UINT32)
585 * hevc->meta.numRegionRects +
587 hevc->cbNalSizes + hevc->length;
588 if (hevc->frameAttributes >= 0)
600WINPR_ATTR_NODISCARD
static inline UINT32
610 switch (cmd->codecId)
612 case RDPGFX_CODECID_CAPROGRESSIVE:
613 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
614 return RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE + cmd->length;
616#if defined(WITH_GFX_AZURE)
617 case RDPGFX_CODECID_HEVC420:
618 case RDPGFX_CODECID_HEVC444:
621 const UINT32 hevcSize = rdpgfx_estimate_hevc(hevc);
622 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + hevcSize;
626#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
627 case RDPGFX_CODECID_AV1:
629 case RDPGFX_CODECID_AVC420:
631 h264Size = rdpgfx_estimate_h264_avc420(havc420);
632 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
634 case RDPGFX_CODECID_AVC444:
636 h264Size =
sizeof(UINT32);
638 havc420 = &(havc444->bitstream[0]);
639 h264Size += rdpgfx_estimate_h264_avc420(havc420);
642 if (havc444->LC == 0)
644 havc420 = &(havc444->bitstream[1]);
645 h264Size += rdpgfx_estimate_h264_avc420(havc420);
648 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
651 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + cmd->length;
662WINPR_ATTR_NODISCARD
static inline UINT16
667 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
668 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
670 return RDPGFX_CMDID_WIRETOSURFACE_2;
673 return RDPGFX_CMDID_WIRETOSURFACE_1;
681WINPR_ATTR_NODISCARD
static UINT rdpgfx_write_h264_metablock(wLog* log,
wStream* s,
685 UINT error = CHANNEL_RC_OK;
688 if (!Stream_EnsureRemainingCapacity(s, 4ull + meta->numRegionRects * 10ull))
689 return ERROR_OUTOFMEMORY;
691 Stream_Write_UINT32(s, meta->numRegionRects);
693 if (meta->numRegionRects > RDPGFX_MAX_META_REGION_COUNT)
695 WLog_Print(log, WLOG_ERROR,
"unusually high number of update rectangles %" PRIu32,
696 meta->numRegionRects);
697 return ERROR_INVALID_PARAMETER;
700 for (UINT32 index = 0; index < meta->numRegionRects; index++)
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);
707 if ((error = rdpgfx_write_rect16(s, regionRect)))
709 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_rect16 failed with error %" PRIu32
"!",
715 for (UINT32 index = 0; index < meta->numRegionRects; index++)
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)));
722 Stream_Write_UINT8(s, quantQualityVal->qualityVal);
734WINPR_ATTR_NODISCARD
static inline UINT
737 WINPR_ASSERT(havc420);
738 const UINT error = rdpgfx_write_h264_metablock(log, s, &(havc420->meta));
740 if (error != CHANNEL_RC_OK)
742 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_metablock failed with error %" PRIu32
"!",
747 if (!Stream_EnsureRemainingCapacity(s, havc420->length))
748 return ERROR_OUTOFMEMORY;
750 Stream_Write(s, havc420->data, havc420->length);
754#if defined(WITH_GFX_AZURE)
755WINPR_ATTR_NODISCARD
static inline UINT rdpgfx_write_hevc(wLog* log,
wStream* s,
760 const UINT error = rdpgfx_write_h264_metablock(log, s, &(hevc->meta));
762 if (error != CHANNEL_RC_OK)
764 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_metablock failed with error %" PRIu32
"!",
769 if (!Stream_EnsureRemainingCapacity(s,
sizeof(UINT32)))
770 return ERROR_OUTOFMEMORY;
771 Stream_Write_UINT32(s, hevc->cbNalSizes);
773 if (!Stream_EnsureRemainingCapacity(s, hevc->cbNalSizes))
774 return ERROR_OUTOFMEMORY;
776 for (
size_t x = 0; x < hevc->cbNalSizes / 4; x++)
777 Stream_Write_UINT32(s, hevc->nalSize[x]);
779 if (activeCaps->version >= RDPGFX_CAPVERSION_115)
781 if (!Stream_EnsureRemainingCapacity(s, 1))
782 return ERROR_OUTOFMEMORY;
784 if (hevc->frameAttributes < 0)
785 Stream_Write_UINT8(s, 1);
787 Stream_Write_UINT8(s, hevc->frameAttributes & 0xff);
790 if (!Stream_EnsureRemainingCapacity(s, hevc->length))
791 return ERROR_OUTOFMEMORY;
793 Stream_Write(s, hevc->data, hevc->length);
805WINPR_ATTR_NODISCARD
static UINT rdpgfx_write_surface_command(wLog* log,
wStream* s,
809 UINT error = CHANNEL_RC_OK;
812 UINT8 pixelFormat = 0;
817 case PIXEL_FORMAT_BGRX32:
818 pixelFormat = GFX_PIXEL_FORMAT_XRGB_8888;
821 case PIXEL_FORMAT_BGRA32:
822 pixelFormat = GFX_PIXEL_FORMAT_ARGB_8888;
826 WLog_Print(log, WLOG_ERROR,
"Format %s not supported!",
827 FreeRDPGetColorFormatName(cmd->format));
828 return ERROR_INVALID_DATA;
831 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
832 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
834 if (!Stream_EnsureRemainingCapacity(s, 13ull + cmd->length))
835 return ERROR_INTERNAL_ERROR;
838 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->surfaceId));
840 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->codecId));
841 Stream_Write_UINT32(s, cmd->contextId);
842 Stream_Write_UINT8(s, pixelFormat);
843 Stream_Write_UINT32(s, cmd->length);
844 Stream_Write(s, cmd->data, cmd->length);
849 if (!Stream_EnsureRemainingCapacity(s, 17))
850 return ERROR_INTERNAL_ERROR;
852 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->surfaceId));
854 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->codecId));
855 Stream_Write_UINT8(s, pixelFormat);
856 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->left));
857 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->top));
858 Stream_Write_UINT16(s,
859 WINPR_ASSERTING_INT_CAST(uint16_t, cmd->right));
860 Stream_Write_UINT16(s,
861 WINPR_ASSERTING_INT_CAST(uint16_t, cmd->bottom));
862 Stream_Write_UINT32(s, cmd->length);
863 const size_t bitmapDataStart = Stream_GetPosition(s);
865 switch (cmd->codecId)
867#if defined(WITH_GFX_AZURE)
868 case RDPGFX_CODECID_HEVC420:
869 case RDPGFX_CODECID_HEVC444:
872 error = rdpgfx_write_hevc(log, s, hevc, activeCaps);
874 if (error != CHANNEL_RC_OK)
876 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
882 WINPR_UNUSED(activeCaps);
884#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
885 case RDPGFX_CODECID_AV1:
887 case RDPGFX_CODECID_AVC420:
890 error = rdpgfx_write_h264_avc420(log, s, havc420);
892 if (error != CHANNEL_RC_OK)
894 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
900 case RDPGFX_CODECID_AVC444:
901 case RDPGFX_CODECID_AVC444v2:
904 havc420 = &(havc444->bitstream[0]);
905 if (!Stream_EnsureRemainingCapacity(s, 4))
906 return ERROR_INTERNAL_ERROR;
907 Stream_Write_UINT32(s, havc444->cbAvc420EncodedBitstream1 |
908 ((uint32_t)havc444->LC << 30UL));
910 error = rdpgfx_write_h264_avc420(log, s, havc420);
912 if (error != CHANNEL_RC_OK)
914 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
919 if (havc444->LC == 0)
921 havc420 = &(havc444->bitstream[1]);
922 error = rdpgfx_write_h264_avc420(log, s, havc420);
924 if (error != CHANNEL_RC_OK)
926 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
933 if (!Stream_EnsureRemainingCapacity(s, cmd->length))
934 return ERROR_INTERNAL_ERROR;
935 Stream_Write(s, cmd->data, cmd->length);
940 const size_t bitmapDataLength = Stream_GetPosition(s) - bitmapDataStart;
941 if (bitmapDataLength > UINT32_MAX)
942 return ERROR_INTERNAL_ERROR;
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);
949 if (!Stream_SafeSeek(s, bitmapDataLength))
950 return ERROR_INTERNAL_ERROR;
963WINPR_ATTR_NODISCARD
static UINT rdpgfx_send_surface_command(RdpgfxServerContext* context,
966 if (!checkCapsAreExchanged(context))
967 return CHANNEL_RC_NOT_INITIALIZED;
968 UINT error = CHANNEL_RC_OK;
970 WINPR_ASSERT(context);
971 WINPR_ASSERT(context->priv);
975 rdpgfx_server_single_packet_new(context->priv->log, rdpgfx_surface_command_cmdid(cmd),
976 rdpgfx_estimate_surface_command(cmd));
980 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
981 return CHANNEL_RC_NO_MEMORY;
984 error = rdpgfx_write_surface_command(context->priv->log, s, cmd, &context->priv->activeCapSet);
986 if (error != CHANNEL_RC_OK)
988 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_write_surface_command failed!");
992 return rdpgfx_server_single_packet_send(context, s);
994 Stream_Free(s, TRUE);
1006WINPR_ATTR_NODISCARD
static UINT
1012 if (!checkCapsAreExchanged(context))
1013 return CHANNEL_RC_NOT_INITIALIZED;
1015 WINPR_ASSERT(context->priv);
1017 WINPR_ASSERT(startFrame);
1018 WINPR_ASSERT(endFrame);
1020 UINT error = CHANNEL_RC_OK;
1021 size_t size = rdpgfx_pdu_length(rdpgfx_estimate_surface_command(cmd));
1025 size += rdpgfx_pdu_length(RDPGFX_START_FRAME_PDU_SIZE);
1030 size += rdpgfx_pdu_length(RDPGFX_END_FRAME_PDU_SIZE);
1033 wStream* s = Stream_New(
nullptr, size);
1037 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
1038 return CHANNEL_RC_NO_MEMORY;
1044 const size_t position = Stream_GetPosition(s);
1045 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_STARTFRAME, 0);
1047 if (error != CHANNEL_RC_OK)
1049 WLog_Print(context->priv->log, WLOG_ERROR,
1050 "Failed to init header with error %" PRIu32
"!", error);
1054 if (!rdpgfx_write_start_frame_pdu(s, startFrame) ||
1055 !rdpgfx_server_packet_complete_header(s, position))
1057 error = ERROR_INTERNAL_ERROR;
1064 const size_t pos = Stream_GetPosition(s);
1065 error = rdpgfx_server_packet_init_header(s, rdpgfx_surface_command_cmdid(cmd),
1068 if (error != CHANNEL_RC_OK)
1070 WLog_Print(context->priv->log, WLOG_ERROR,
1071 "Failed to init header with error %" PRIu32
"!", error);
1076 rdpgfx_write_surface_command(context->priv->log, s, cmd, &context->priv->activeCapSet);
1078 if (error != CHANNEL_RC_OK)
1080 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_write_surface_command failed!");
1084 if (!rdpgfx_server_packet_complete_header(s, pos))
1086 error = ERROR_INTERNAL_ERROR;
1094 const size_t position = Stream_GetPosition(s);
1095 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_ENDFRAME, 0);
1097 if (error != CHANNEL_RC_OK)
1099 WLog_Print(context->priv->log, WLOG_ERROR,
1100 "Failed to init header with error %" PRIu32
"!", error);
1104 if (!rdpgfx_write_end_frame_pdu(s, endFrame) ||
1105 !rdpgfx_server_packet_complete_header(s, position))
1107 error = ERROR_INTERNAL_ERROR;
1112 return rdpgfx_server_packet_send(context, s);
1114 Stream_Free(s, TRUE);
1123WINPR_ATTR_NODISCARD
static UINT
1124rdpgfx_send_delete_encoding_context_pdu(RdpgfxServerContext* context,
1127 if (!checkCapsAreExchanged(context))
1128 return CHANNEL_RC_NOT_INITIALIZED;
1130 WINPR_ASSERT(context->priv);
1133 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETEENCODINGCONTEXT, 6);
1137 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1138 return CHANNEL_RC_NO_MEMORY;
1141 Stream_Write_UINT16(s, pdu->surfaceId);
1142 Stream_Write_UINT32(s, pdu->codecContextId);
1143 return rdpgfx_server_single_packet_send(context, s);
1151WINPR_ATTR_NODISCARD
static UINT rdpgfx_send_solid_fill_pdu(RdpgfxServerContext* context,
1154 if (!checkCapsAreExchanged(context))
1155 return CHANNEL_RC_NOT_INITIALIZED;
1157 WINPR_ASSERT(context->priv);
1160 UINT error = CHANNEL_RC_OK;
1162 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SOLIDFILL,
1163 8 + 8 * pdu->fillRectCount);
1167 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1168 return CHANNEL_RC_NO_MEMORY;
1171 Stream_Write_UINT16(s, pdu->surfaceId);
1174 if ((error = rdpgfx_write_color32(s, &(pdu->fillPixel))))
1176 WLog_Print(context->priv->log, WLOG_ERROR,
1177 "rdpgfx_write_color32 failed with error %" PRIu32
"!", error);
1181 Stream_Write_UINT16(s, pdu->fillRectCount);
1183 for (UINT16 index = 0; index < pdu->fillRectCount; index++)
1185 fillRect = &(pdu->fillRects[index]);
1187 if ((error = rdpgfx_write_rect16(s, fillRect)))
1189 WLog_Print(context->priv->log, WLOG_ERROR,
1190 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1195 return rdpgfx_server_single_packet_send(context, s);
1197 Stream_Free(s, TRUE);
1206WINPR_ATTR_NODISCARD
static UINT
1207rdpgfx_send_surface_to_surface_pdu(RdpgfxServerContext* context,
1210 if (!checkCapsAreExchanged(context))
1211 return CHANNEL_RC_NOT_INITIALIZED;
1214 WINPR_ASSERT(context->priv);
1216 UINT error = CHANNEL_RC_OK;
1218 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOSURFACE,
1219 14 + 4 * pdu->destPtsCount);
1223 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1224 return CHANNEL_RC_NO_MEMORY;
1227 Stream_Write_UINT16(s, pdu->surfaceIdSrc);
1228 Stream_Write_UINT16(s, pdu->surfaceIdDest);
1231 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1233 WLog_Print(context->priv->log, WLOG_ERROR,
1234 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1238 Stream_Write_UINT16(s, pdu->destPtsCount);
1240 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1242 destPt = &(pdu->destPts[index]);
1244 if ((error = rdpgfx_write_point16(s, destPt)))
1246 WLog_Print(context->priv->log, WLOG_ERROR,
1247 "rdpgfx_write_point16 failed with error %" PRIu32
"!", error);
1252 return rdpgfx_server_single_packet_send(context, s);
1254 Stream_Free(s, TRUE);
1263WINPR_ATTR_NODISCARD
static UINT
1264rdpgfx_send_surface_to_cache_pdu(RdpgfxServerContext* context,
1267 if (!checkCapsAreExchanged(context))
1268 return CHANNEL_RC_NOT_INITIALIZED;
1271 WINPR_ASSERT(context->priv);
1273 UINT error = CHANNEL_RC_OK;
1275 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOCACHE, 20);
1279 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1280 return CHANNEL_RC_NO_MEMORY;
1283 Stream_Write_UINT16(s, pdu->surfaceId);
1284 Stream_Write_UINT64(s, pdu->cacheKey);
1285 Stream_Write_UINT16(s, pdu->cacheSlot);
1288 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1290 WLog_Print(context->priv->log, WLOG_ERROR,
1291 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1295 return rdpgfx_server_single_packet_send(context, s);
1297 Stream_Free(s, TRUE);
1306WINPR_ATTR_NODISCARD
static UINT
1307rdpgfx_send_cache_to_surface_pdu(RdpgfxServerContext* context,
1310 if (!checkCapsAreExchanged(context))
1311 return CHANNEL_RC_NOT_INITIALIZED;
1314 WINPR_ASSERT(context->priv);
1316 UINT error = CHANNEL_RC_OK;
1318 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CACHETOSURFACE,
1319 6 + 4 * pdu->destPtsCount);
1323 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1324 return CHANNEL_RC_NO_MEMORY;
1327 Stream_Write_UINT16(s, pdu->cacheSlot);
1328 Stream_Write_UINT16(s, pdu->surfaceId);
1329 Stream_Write_UINT16(s, pdu->destPtsCount);
1331 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1333 destPt = &(pdu->destPts[index]);
1335 if ((error = rdpgfx_write_point16(s, destPt)))
1337 WLog_Print(context->priv->log, WLOG_ERROR,
1338 "rdpgfx_write_point16 failed with error %" PRIu32
"", error);
1343 return rdpgfx_server_single_packet_send(context, s);
1345 Stream_Free(s, TRUE);
1354WINPR_ATTR_NODISCARD
static UINT
1355rdpgfx_send_map_surface_to_output_pdu(RdpgfxServerContext* context,
1358 if (!checkCapsAreExchanged(context))
1359 return CHANNEL_RC_NOT_INITIALIZED;
1362 WINPR_ASSERT(context->priv);
1364 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOOUTPUT, 12);
1368 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1369 return CHANNEL_RC_NO_MEMORY;
1372 Stream_Write_UINT16(s, pdu->surfaceId);
1373 Stream_Write_UINT16(s, 0);
1374 Stream_Write_UINT32(s, pdu->outputOriginX);
1375 Stream_Write_UINT32(s, pdu->outputOriginY);
1376 return rdpgfx_server_single_packet_send(context, s);
1384WINPR_ATTR_NODISCARD
static UINT
1385rdpgfx_send_map_surface_to_window_pdu(RdpgfxServerContext* context,
1388 if (!checkCapsAreExchanged(context))
1389 return CHANNEL_RC_NOT_INITIALIZED;
1392 WINPR_ASSERT(context->priv);
1395 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOWINDOW, 18);
1399 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1400 return CHANNEL_RC_NO_MEMORY;
1403 Stream_Write_UINT16(s, pdu->surfaceId);
1404 Stream_Write_UINT64(s, pdu->windowId);
1405 Stream_Write_UINT32(s, pdu->mappedWidth);
1406 Stream_Write_UINT32(s, pdu->mappedHeight);
1407 return rdpgfx_server_single_packet_send(context, s);
1410WINPR_ATTR_NODISCARD
static UINT
1411rdpgfx_send_map_surface_to_scaled_window_pdu(RdpgfxServerContext* context,
1414 if (!checkCapsAreExchanged(context))
1415 return CHANNEL_RC_NOT_INITIALIZED;
1418 WINPR_ASSERT(context->priv);
1419 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1420 RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW, 26);
1424 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1425 return CHANNEL_RC_NO_MEMORY;
1428 Stream_Write_UINT16(s, pdu->surfaceId);
1429 Stream_Write_UINT64(s, pdu->windowId);
1430 Stream_Write_UINT32(s, pdu->mappedWidth);
1431 Stream_Write_UINT32(s, pdu->mappedHeight);
1432 Stream_Write_UINT32(s, pdu->targetWidth);
1433 Stream_Write_UINT32(s, pdu->targetHeight);
1434 return rdpgfx_server_single_packet_send(context, s);
1442WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_frame_acknowledge_pdu(RdpgfxServerContext* context,
1445 WINPR_ASSERT(context);
1447 if (!checkCapsAreExchanged(context))
1448 return CHANNEL_RC_NOT_INITIALIZED;
1450 WINPR_ASSERT(context->priv);
1453 UINT error = CHANNEL_RC_OK;
1455 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 12))
1456 return ERROR_INVALID_DATA;
1458 Stream_Read_UINT32(s, pdu.queueDepth);
1459 Stream_Read_UINT32(s, pdu.frameId);
1460 Stream_Read_UINT32(s, pdu.totalFramesDecoded);
1462 IFCALLRET(context->FrameAcknowledge, error, context, &pdu);
1465 WLog_Print(context->priv->log, WLOG_ERROR,
1466 "context->FrameAcknowledge failed with error %" PRIu32
"", error);
1476WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_cache_import_offer_pdu(RdpgfxServerContext* context,
1479 WINPR_ASSERT(context);
1480 if (!checkCapsAreExchanged(context))
1481 return CHANNEL_RC_NOT_INITIALIZED;
1483 WINPR_ASSERT(context->priv);
1487 UINT error = CHANNEL_RC_OK;
1489 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1490 return ERROR_INVALID_DATA;
1493 Stream_Read_UINT16(s, pdu.cacheEntriesCount);
1496 if (pdu.cacheEntriesCount >= 5462)
1498 WLog_Print(context->priv->log, WLOG_ERROR,
"Invalid cacheEntriesCount: %" PRIu16
"",
1499 pdu.cacheEntriesCount);
1500 return ERROR_INVALID_DATA;
1503 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, pdu.cacheEntriesCount,
1505 return ERROR_INVALID_DATA;
1507 for (UINT16 index = 0; index < pdu.cacheEntriesCount; index++)
1509 cacheEntry = &(pdu.cacheEntries[index]);
1510 Stream_Read_UINT64(s, cacheEntry->cacheKey);
1511 Stream_Read_UINT32(s, cacheEntry->bitmapLength);
1514 IFCALLRET(context->CacheImportOffer, error, context, &pdu);
1517 WLog_Print(context->priv->log, WLOG_ERROR,
1518 "context->CacheImportOffer failed with error %" PRIu32
"", error);
1528WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_caps_advertise_pdu(RdpgfxServerContext* context,
1533 UINT error = ERROR_INVALID_DATA;
1536 return ERROR_BAD_ARGUMENTS;
1538 WINPR_ASSERT(context->priv);
1539 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1540 return ERROR_INVALID_DATA;
1542 Stream_Read_UINT16(s, pdu.capsSetCount);
1543 if (pdu.capsSetCount > 0)
1545 capsSets = calloc(pdu.capsSetCount, (RDPGFX_CAPSET_BASE_SIZE + 4));
1547 return ERROR_OUTOFMEMORY;
1550 pdu.capsSets = capsSets;
1552 for (UINT16 index = 0; index < pdu.capsSetCount; index++)
1556 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 8))
1559 Stream_Read_UINT32(s, capsSet->version);
1560 Stream_Read_UINT32(s, capsSet->length);
1562 if (capsSet->length >= 4)
1564 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
1567 Stream_Peek_UINT32(s, capsSet->flags);
1570 if (!Stream_SafeSeek(s, capsSet->length))
1574 error = ERROR_BAD_CONFIGURATION;
1575 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
1578 WLog_Print(context->priv->log, WLOG_ERROR,
1579 "context->CapsAdvertise failed with error %" PRIu32
"", error);
1591WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_qoe_frame_acknowledge_pdu(RdpgfxServerContext* context,
1594 WINPR_ASSERT(context);
1596 if (!checkCapsAreExchanged(context))
1597 return CHANNEL_RC_NOT_INITIALIZED;
1600 UINT error = CHANNEL_RC_OK;
1602 WINPR_ASSERT(context->priv);
1604 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 12))
1605 return ERROR_INVALID_DATA;
1607 Stream_Read_UINT32(s, pdu.frameId);
1608 Stream_Read_UINT32(s, pdu.timestamp);
1609 Stream_Read_UINT16(s, pdu.timeDiffSE);
1610 Stream_Read_UINT16(s, pdu.timeDiffEDR);
1612 IFCALLRET(context->QoeFrameAcknowledge, error, context, &pdu);
1615 WLog_Print(context->priv->log, WLOG_ERROR,
1616 "context->QoeFrameAcknowledge failed with error %" PRIu32
"", error);
1621WINPR_ATTR_NODISCARD
static UINT
1622rdpgfx_send_map_surface_to_scaled_output_pdu(RdpgfxServerContext* context,
1625 if (!checkCapsAreExchanged(context))
1626 return CHANNEL_RC_NOT_INITIALIZED;
1629 WINPR_ASSERT(context->priv);
1630 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1631 RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT, 20);
1635 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1636 return CHANNEL_RC_NO_MEMORY;
1639 Stream_Write_UINT16(s, pdu->surfaceId);
1640 Stream_Write_UINT16(s, 0);
1641 Stream_Write_UINT32(s, pdu->outputOriginX);
1642 Stream_Write_UINT32(s, pdu->outputOriginY);
1643 Stream_Write_UINT32(s, pdu->targetWidth);
1644 Stream_Write_UINT32(s, pdu->targetHeight);
1645 return rdpgfx_server_single_packet_send(context, s);
1653WINPR_ATTR_NODISCARD
static UINT rdpgfx_server_receive_pdu(RdpgfxServerContext* context,
1656 size_t packetlen = 0;
1657 wStream sbuffer = WINPR_C_ARRAY_INIT;
1659 UINT error = CHANNEL_RC_OK;
1661 WINPR_ASSERT(context);
1662 WINPR_ASSERT(context->priv);
1664 if ((error = rdpgfx_read_header(context->priv->log, data, &header, &packetlen)))
1666 WLog_Print(context->priv->log, WLOG_ERROR,
1667 "rdpgfx_read_header failed with error %" PRIu32
"!", error);
1672 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
1673 Stream_Seek(data, packetlen);
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,
1680 switch (header.cmdId)
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
"!",
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
"!",
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
"!",
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
"!",
1719 error = CHANNEL_RC_BAD_PROC;
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);
1731 const size_t end = Stream_GetPosition(s);
1733 if (end != packetlen)
1735 WLog_Print(context->priv->log, WLOG_ERROR,
1736 "Unexpected gfx pdu end: Actual: %" PRIuz
", Expected: %" PRIuz
"", end,
1743WINPR_ATTR_NODISCARD
static DWORD WINAPI rdpgfx_server_thread_func(LPVOID arg)
1745 RdpgfxServerContext* context = (RdpgfxServerContext*)arg;
1746 WINPR_ASSERT(context);
1748 RdpgfxServerPrivate* priv = context->priv;
1751 HANDLE events[8] = WINPR_C_ARRAY_INIT;
1752 UINT error = CHANNEL_RC_OK;
1756 if (priv->ownThread)
1758 WINPR_ASSERT(priv->stopEvent);
1759 events[nCount++] = priv->stopEvent;
1762 WINPR_ASSERT(priv->channelEvent);
1763 events[nCount++] = priv->channelEvent;
1768 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1770 if (status == WAIT_FAILED)
1772 error = GetLastError();
1773 WLog_Print(context->priv->log, WLOG_ERROR,
1774 "WaitForMultipleObjects failed with error %" PRIu32
"", error);
1779 if (status == WAIT_OBJECT_0)
1782 if ((error = rdpgfx_server_handle_messages(context)))
1784 WLog_Print(context->priv->log, WLOG_ERROR,
1785 "rdpgfx_server_handle_messages failed with error %" PRIu32
"", error);
1790 if (error && context->rdpcontext)
1791 setChannelError(context->rdpcontext, error,
"rdpgfx_server_thread_func reported an error");
1797WINPR_ATTR_NODISCARD
static BOOL rdpgfx_server_open(RdpgfxServerContext* context)
1799 WINPR_ASSERT(context);
1800 RdpgfxServerPrivate* priv = context->priv;
1801 void* buffer =
nullptr;
1805 if (!priv->isOpened)
1807 PULONG pSessionId =
nullptr;
1808 DWORD BytesReturned = 0;
1809 priv->SessionId = WTS_CURRENT_SESSION;
1810 UINT32 channelId = 0;
1813 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
1814 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
1816 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSQuerySessionInformationA failed!");
1820 priv->SessionId = (DWORD)*pSessionId;
1821 WTSFreeMemory(pSessionId);
1822 priv->rdpgfx_channel = WTSVirtualChannelOpenEx(priv->SessionId, RDPGFX_DVC_CHANNEL_NAME,
1823 WTS_CHANNEL_OPTION_DYNAMIC);
1825 if (!priv->rdpgfx_channel)
1827 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelOpenEx failed!");
1831 channelId = WTSChannelGetIdByHandle(priv->rdpgfx_channel);
1833 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
1836 WLog_Print(context->priv->log, WLOG_ERROR,
"context->ChannelIdAssigned failed!");
1841 if (!WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualEventHandle, &buffer,
1843 (BytesReturned !=
sizeof(HANDLE)))
1845 WLog_Print(context->priv->log, WLOG_ERROR,
1846 "WTSVirtualChannelQuery failed "
1847 "or invalid returned size(%" PRIu32
")",
1851 WTSFreeMemory(buffer);
1856 priv->channelEvent = *(HANDLE*)buffer;
1857 WTSFreeMemory(buffer);
1859 if (!(priv->zgfx = zgfx_context_new(TRUE)))
1861 WLog_Print(context->priv->log, WLOG_ERROR,
"Create zgfx context failed!");
1865 priv->isReady = FALSE;
1867 priv->activeCapSet = empty;
1868 if (priv->ownThread)
1870 if (!(priv->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
1872 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateEvent failed!");
1876 if (!(priv->thread = CreateThread(
nullptr, 0, rdpgfx_server_thread_func, (
void*)context,
1879 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThread failed!");
1884 priv->isOpened = TRUE;
1888 WLog_Print(context->priv->log, WLOG_ERROR,
"RDPGFX channel is already opened!");
1891 (void)rdpgfx_server_close(context);
1895BOOL rdpgfx_server_close(RdpgfxServerContext* context)
1897 WINPR_ASSERT(context);
1899 RdpgfxServerPrivate* priv = context->priv;
1902 if (priv->ownThread && priv->thread)
1904 (void)SetEvent(priv->stopEvent);
1906 if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
1908 WLog_Print(context->priv->log, WLOG_ERROR,
1909 "WaitForSingleObject failed with error %" PRIu32
"", GetLastError());
1913 (void)CloseHandle(priv->thread);
1914 (void)CloseHandle(priv->stopEvent);
1915 priv->thread =
nullptr;
1916 priv->stopEvent =
nullptr;
1919 zgfx_context_free(priv->zgfx);
1920 priv->zgfx =
nullptr;
1922 if (priv->rdpgfx_channel)
1924 (void)WTSVirtualChannelClose(priv->rdpgfx_channel);
1925 priv->rdpgfx_channel =
nullptr;
1928 priv->channelEvent =
nullptr;
1929 priv->isOpened = FALSE;
1930 priv->isReady = FALSE;
1932 priv->activeCapSet = empty;
1936WINPR_ATTR_NODISCARD
static BOOL rdpgfx_server_initialize(RdpgfxServerContext* context,
1937 BOOL externalThread)
1939 WINPR_ASSERT(context);
1940 WINPR_ASSERT(context->priv);
1942 if (context->priv->isOpened)
1944 WLog_Print(context->priv->log, WLOG_WARN,
1945 "Application error: RDPEGFX channel already initialized, "
1946 "calling in this state is not possible!");
1950 context->priv->ownThread = !externalThread;
1954RdpgfxServerContext* rdpgfx_server_context_new(HANDLE vcm)
1956 RdpgfxServerContext* context = (RdpgfxServerContext*)calloc(1,
sizeof(RdpgfxServerContext));
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));
1994 priv->log = WLog_Get(TAG);
1999 priv->input_stream = Stream_New(
nullptr, 4);
2001 if (!priv->input_stream)
2003 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
2007 priv->isOpened = FALSE;
2008 priv->isReady = FALSE;
2009 priv->ownThread = TRUE;
2013 priv->activeCapSet = empty;
2018 WINPR_PRAGMA_DIAG_PUSH
2019 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2020 rdpgfx_server_context_free(context);
2021 WINPR_PRAGMA_DIAG_POP
2025void rdpgfx_server_context_free(RdpgfxServerContext* context)
2030 (void)rdpgfx_server_close(context);
2033 Stream_Free(context->priv->input_stream, TRUE);
2035 free(context->priv);
2039HANDLE rdpgfx_server_get_event_handle(RdpgfxServerContext* context)
2045 return context->priv->channelEvent;
2057UINT rdpgfx_server_handle_messages(RdpgfxServerContext* context)
2059 DWORD BytesReturned = 0;
2060 void* buffer =
nullptr;
2061 UINT ret = CHANNEL_RC_OK;
2063 WINPR_ASSERT(context);
2064 WINPR_ASSERT(context->priv);
2066 RdpgfxServerPrivate* priv = context->priv;
2067 wStream* s = priv->input_stream;
2072 if (WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualChannelReady, &buffer,
2073 &BytesReturned) == FALSE)
2075 if (GetLastError() == ERROR_NO_DATA)
2076 return ERROR_NO_DATA;
2078 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelQuery failed");
2079 return ERROR_INTERNAL_ERROR;
2082 priv->isReady = *((BOOL*)buffer);
2083 WTSFreeMemory(buffer);
2089 Stream_ResetPosition(s);
2091 if (!WTSVirtualChannelRead(priv->rdpgfx_channel, 0,
nullptr, 0, &BytesReturned))
2093 if (GetLastError() == ERROR_NO_DATA)
2094 return ERROR_NO_DATA;
2096 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
2097 return ERROR_INTERNAL_ERROR;
2100 if (BytesReturned < 1)
2101 return CHANNEL_RC_OK;
2103 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
2105 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
2106 return CHANNEL_RC_NO_MEMORY;
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)
2115 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
2116 return ERROR_INTERNAL_ERROR;
2119 if (!Stream_SetLength(s, BytesReturned))
2120 return ERROR_INTERNAL_ERROR;
2122 Stream_ResetPosition(s);
2124 while (Stream_GetPosition(s) < Stream_Length(s))
2126 if ((ret = rdpgfx_server_receive_pdu(context, s)))
2128 WLog_Print(context->priv->log, WLOG_ERROR,
2129 "rdpgfx_server_receive_pdu "
2130 "failed with error %" PRIu32
"!",