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 +
583WINPR_ATTR_NODISCARD
static inline UINT32
593 switch (cmd->codecId)
595 case RDPGFX_CODECID_CAPROGRESSIVE:
596 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
597 return RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE + cmd->length;
599 case RDPGFX_CODECID_AVC420:
601 h264Size = rdpgfx_estimate_h264_avc420(havc420);
602 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
604 case RDPGFX_CODECID_AVC444:
606 h264Size =
sizeof(UINT32);
608 havc420 = &(havc444->bitstream[0]);
609 h264Size += rdpgfx_estimate_h264_avc420(havc420);
612 if (havc444->LC == 0)
614 havc420 = &(havc444->bitstream[1]);
615 h264Size += rdpgfx_estimate_h264_avc420(havc420);
618 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
621 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + cmd->length;
632WINPR_ATTR_NODISCARD
static inline UINT16
637 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
638 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
640 return RDPGFX_CMDID_WIRETOSURFACE_2;
643 return RDPGFX_CMDID_WIRETOSURFACE_1;
651WINPR_ATTR_NODISCARD
static UINT rdpgfx_write_h264_metablock(wLog* log,
wStream* s,
656 UINT error = CHANNEL_RC_OK;
659 if (!Stream_EnsureRemainingCapacity(s, 4ull + meta->numRegionRects * 10ull))
660 return ERROR_OUTOFMEMORY;
662 Stream_Write_UINT32(s, meta->numRegionRects);
664 for (UINT32 index = 0; index < meta->numRegionRects; index++)
666 regionRect = &(meta->regionRects[index]);
668 if ((error = rdpgfx_write_rect16(s, regionRect)))
670 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_rect16 failed with error %" PRIu32
"!",
676 for (UINT32 index = 0; index < meta->numRegionRects; index++)
678 quantQualityVal = &(meta->quantQualityVals[index]);
679 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(
680 uint8_t, quantQualityVal->qp | (quantQualityVal->r << 6) |
681 (quantQualityVal->p << 7)));
683 Stream_Write_UINT8(s, quantQualityVal->qualityVal);
695WINPR_ATTR_NODISCARD
static inline UINT
698 WINPR_ASSERT(havc420);
699 const UINT error = rdpgfx_write_h264_metablock(log, s, &(havc420->meta));
701 if (error != CHANNEL_RC_OK)
703 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_metablock failed with error %" PRIu32
"!",
708 if (!Stream_EnsureRemainingCapacity(s, havc420->length))
709 return ERROR_OUTOFMEMORY;
711 Stream_Write(s, havc420->data, havc420->length);
722WINPR_ATTR_NODISCARD
static UINT rdpgfx_write_surface_command(wLog* log,
wStream* s,
725 UINT error = CHANNEL_RC_OK;
728 UINT8 pixelFormat = 0;
733 case PIXEL_FORMAT_BGRX32:
734 pixelFormat = GFX_PIXEL_FORMAT_XRGB_8888;
737 case PIXEL_FORMAT_BGRA32:
738 pixelFormat = GFX_PIXEL_FORMAT_ARGB_8888;
742 WLog_Print(log, WLOG_ERROR,
"Format %s not supported!",
743 FreeRDPGetColorFormatName(cmd->format));
744 return ERROR_INVALID_DATA;
747 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
748 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
750 if (!Stream_EnsureRemainingCapacity(s, 13ull + cmd->length))
751 return ERROR_INTERNAL_ERROR;
754 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->surfaceId));
756 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->codecId));
757 Stream_Write_UINT32(s, cmd->contextId);
758 Stream_Write_UINT8(s, pixelFormat);
759 Stream_Write_UINT32(s, cmd->length);
760 Stream_Write(s, cmd->data, cmd->length);
765 if (!Stream_EnsureRemainingCapacity(s, 17))
766 return ERROR_INTERNAL_ERROR;
768 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->surfaceId));
770 s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->codecId));
771 Stream_Write_UINT8(s, pixelFormat);
772 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->left));
773 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, cmd->top));
774 Stream_Write_UINT16(s,
775 WINPR_ASSERTING_INT_CAST(uint16_t, cmd->right));
776 Stream_Write_UINT16(s,
777 WINPR_ASSERTING_INT_CAST(uint16_t, cmd->bottom));
778 Stream_Write_UINT32(s, cmd->length);
779 const size_t bitmapDataStart = Stream_GetPosition(s);
781 switch (cmd->codecId)
783#if defined(WITH_GFX_AV1)
784 case RDPGFX_CODECID_AV1:
786 case RDPGFX_CODECID_AVC420:
789 error = rdpgfx_write_h264_avc420(log, s, havc420);
791 if (error != CHANNEL_RC_OK)
793 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
799 case RDPGFX_CODECID_AVC444:
800 case RDPGFX_CODECID_AVC444v2:
803 havc420 = &(havc444->bitstream[0]);
804 if (!Stream_EnsureRemainingCapacity(s, 4))
805 return ERROR_INTERNAL_ERROR;
806 Stream_Write_UINT32(s, havc444->cbAvc420EncodedBitstream1 |
807 ((uint32_t)havc444->LC << 30UL));
809 error = rdpgfx_write_h264_avc420(log, s, havc420);
811 if (error != CHANNEL_RC_OK)
813 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
818 if (havc444->LC == 0)
820 havc420 = &(havc444->bitstream[1]);
821 error = rdpgfx_write_h264_avc420(log, s, havc420);
823 if (error != CHANNEL_RC_OK)
825 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
832 if (!Stream_EnsureRemainingCapacity(s, cmd->length))
833 return ERROR_INTERNAL_ERROR;
834 Stream_Write(s, cmd->data, cmd->length);
839 const size_t bitmapDataLength = Stream_GetPosition(s) - bitmapDataStart;
840 if (bitmapDataLength > UINT32_MAX)
841 return ERROR_INTERNAL_ERROR;
843 if (!Stream_SetPosition(s, bitmapDataStart -
sizeof(UINT32)))
844 return ERROR_INVALID_DATA;
845 if (!Stream_EnsureRemainingCapacity(s, 4))
846 return ERROR_INTERNAL_ERROR;
847 Stream_Write_UINT32(s, (UINT32)bitmapDataLength);
848 if (!Stream_SafeSeek(s, bitmapDataLength))
849 return ERROR_INTERNAL_ERROR;
862WINPR_ATTR_NODISCARD
static UINT rdpgfx_send_surface_command(RdpgfxServerContext* context,
865 if (!checkCapsAreExchanged(context))
866 return CHANNEL_RC_NOT_INITIALIZED;
867 UINT error = CHANNEL_RC_OK;
869 WINPR_ASSERT(context);
870 WINPR_ASSERT(context->priv);
874 rdpgfx_server_single_packet_new(context->priv->log, rdpgfx_surface_command_cmdid(cmd),
875 rdpgfx_estimate_surface_command(cmd));
879 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
880 return CHANNEL_RC_NO_MEMORY;
883 error = rdpgfx_write_surface_command(context->priv->log, s, cmd);
885 if (error != CHANNEL_RC_OK)
887 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_write_surface_command failed!");
891 return rdpgfx_server_single_packet_send(context, s);
893 Stream_Free(s, TRUE);
905WINPR_ATTR_NODISCARD
static UINT
911 if (!checkCapsAreExchanged(context))
912 return CHANNEL_RC_NOT_INITIALIZED;
914 WINPR_ASSERT(context->priv);
916 WINPR_ASSERT(startFrame);
917 WINPR_ASSERT(endFrame);
919 UINT error = CHANNEL_RC_OK;
920 size_t size = rdpgfx_pdu_length(rdpgfx_estimate_surface_command(cmd));
924 size += rdpgfx_pdu_length(RDPGFX_START_FRAME_PDU_SIZE);
929 size += rdpgfx_pdu_length(RDPGFX_END_FRAME_PDU_SIZE);
932 wStream* s = Stream_New(
nullptr, size);
936 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
937 return CHANNEL_RC_NO_MEMORY;
943 const size_t position = Stream_GetPosition(s);
944 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_STARTFRAME, 0);
946 if (error != CHANNEL_RC_OK)
948 WLog_Print(context->priv->log, WLOG_ERROR,
949 "Failed to init header with error %" PRIu32
"!", error);
953 if (!rdpgfx_write_start_frame_pdu(s, startFrame) ||
954 !rdpgfx_server_packet_complete_header(s, position))
956 error = ERROR_INTERNAL_ERROR;
963 const size_t pos = Stream_GetPosition(s);
964 error = rdpgfx_server_packet_init_header(s, rdpgfx_surface_command_cmdid(cmd),
967 if (error != CHANNEL_RC_OK)
969 WLog_Print(context->priv->log, WLOG_ERROR,
970 "Failed to init header with error %" PRIu32
"!", error);
974 error = rdpgfx_write_surface_command(context->priv->log, s, cmd);
976 if (error != CHANNEL_RC_OK)
978 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_write_surface_command failed!");
982 if (!rdpgfx_server_packet_complete_header(s, pos))
984 error = ERROR_INTERNAL_ERROR;
992 const size_t position = Stream_GetPosition(s);
993 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_ENDFRAME, 0);
995 if (error != CHANNEL_RC_OK)
997 WLog_Print(context->priv->log, WLOG_ERROR,
998 "Failed to init header with error %" PRIu32
"!", error);
1002 if (!rdpgfx_write_end_frame_pdu(s, endFrame) ||
1003 !rdpgfx_server_packet_complete_header(s, position))
1005 error = ERROR_INTERNAL_ERROR;
1010 return rdpgfx_server_packet_send(context, s);
1012 Stream_Free(s, TRUE);
1021WINPR_ATTR_NODISCARD
static UINT
1022rdpgfx_send_delete_encoding_context_pdu(RdpgfxServerContext* context,
1025 if (!checkCapsAreExchanged(context))
1026 return CHANNEL_RC_NOT_INITIALIZED;
1028 WINPR_ASSERT(context->priv);
1031 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETEENCODINGCONTEXT, 6);
1035 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1036 return CHANNEL_RC_NO_MEMORY;
1039 Stream_Write_UINT16(s, pdu->surfaceId);
1040 Stream_Write_UINT32(s, pdu->codecContextId);
1041 return rdpgfx_server_single_packet_send(context, s);
1049WINPR_ATTR_NODISCARD
static UINT rdpgfx_send_solid_fill_pdu(RdpgfxServerContext* context,
1052 if (!checkCapsAreExchanged(context))
1053 return CHANNEL_RC_NOT_INITIALIZED;
1055 WINPR_ASSERT(context->priv);
1058 UINT error = CHANNEL_RC_OK;
1060 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SOLIDFILL,
1061 8 + 8 * pdu->fillRectCount);
1065 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1066 return CHANNEL_RC_NO_MEMORY;
1069 Stream_Write_UINT16(s, pdu->surfaceId);
1072 if ((error = rdpgfx_write_color32(s, &(pdu->fillPixel))))
1074 WLog_Print(context->priv->log, WLOG_ERROR,
1075 "rdpgfx_write_color32 failed with error %" PRIu32
"!", error);
1079 Stream_Write_UINT16(s, pdu->fillRectCount);
1081 for (UINT16 index = 0; index < pdu->fillRectCount; index++)
1083 fillRect = &(pdu->fillRects[index]);
1085 if ((error = rdpgfx_write_rect16(s, fillRect)))
1087 WLog_Print(context->priv->log, WLOG_ERROR,
1088 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1093 return rdpgfx_server_single_packet_send(context, s);
1095 Stream_Free(s, TRUE);
1104WINPR_ATTR_NODISCARD
static UINT
1105rdpgfx_send_surface_to_surface_pdu(RdpgfxServerContext* context,
1108 if (!checkCapsAreExchanged(context))
1109 return CHANNEL_RC_NOT_INITIALIZED;
1112 WINPR_ASSERT(context->priv);
1114 UINT error = CHANNEL_RC_OK;
1116 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOSURFACE,
1117 14 + 4 * pdu->destPtsCount);
1121 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1122 return CHANNEL_RC_NO_MEMORY;
1125 Stream_Write_UINT16(s, pdu->surfaceIdSrc);
1126 Stream_Write_UINT16(s, pdu->surfaceIdDest);
1129 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1131 WLog_Print(context->priv->log, WLOG_ERROR,
1132 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1136 Stream_Write_UINT16(s, pdu->destPtsCount);
1138 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1140 destPt = &(pdu->destPts[index]);
1142 if ((error = rdpgfx_write_point16(s, destPt)))
1144 WLog_Print(context->priv->log, WLOG_ERROR,
1145 "rdpgfx_write_point16 failed with error %" PRIu32
"!", error);
1150 return rdpgfx_server_single_packet_send(context, s);
1152 Stream_Free(s, TRUE);
1161WINPR_ATTR_NODISCARD
static UINT
1162rdpgfx_send_surface_to_cache_pdu(RdpgfxServerContext* context,
1165 if (!checkCapsAreExchanged(context))
1166 return CHANNEL_RC_NOT_INITIALIZED;
1169 WINPR_ASSERT(context->priv);
1171 UINT error = CHANNEL_RC_OK;
1173 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOCACHE, 20);
1177 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1178 return CHANNEL_RC_NO_MEMORY;
1181 Stream_Write_UINT16(s, pdu->surfaceId);
1182 Stream_Write_UINT64(s, pdu->cacheKey);
1183 Stream_Write_UINT16(s, pdu->cacheSlot);
1186 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1188 WLog_Print(context->priv->log, WLOG_ERROR,
1189 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1193 return rdpgfx_server_single_packet_send(context, s);
1195 Stream_Free(s, TRUE);
1204WINPR_ATTR_NODISCARD
static UINT
1205rdpgfx_send_cache_to_surface_pdu(RdpgfxServerContext* context,
1208 if (!checkCapsAreExchanged(context))
1209 return CHANNEL_RC_NOT_INITIALIZED;
1212 WINPR_ASSERT(context->priv);
1214 UINT error = CHANNEL_RC_OK;
1216 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CACHETOSURFACE,
1217 6 + 4 * pdu->destPtsCount);
1221 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1222 return CHANNEL_RC_NO_MEMORY;
1225 Stream_Write_UINT16(s, pdu->cacheSlot);
1226 Stream_Write_UINT16(s, pdu->surfaceId);
1227 Stream_Write_UINT16(s, pdu->destPtsCount);
1229 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1231 destPt = &(pdu->destPts[index]);
1233 if ((error = rdpgfx_write_point16(s, destPt)))
1235 WLog_Print(context->priv->log, WLOG_ERROR,
1236 "rdpgfx_write_point16 failed with error %" PRIu32
"", error);
1241 return rdpgfx_server_single_packet_send(context, s);
1243 Stream_Free(s, TRUE);
1252WINPR_ATTR_NODISCARD
static UINT
1253rdpgfx_send_map_surface_to_output_pdu(RdpgfxServerContext* context,
1256 if (!checkCapsAreExchanged(context))
1257 return CHANNEL_RC_NOT_INITIALIZED;
1260 WINPR_ASSERT(context->priv);
1262 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOOUTPUT, 12);
1266 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1267 return CHANNEL_RC_NO_MEMORY;
1270 Stream_Write_UINT16(s, pdu->surfaceId);
1271 Stream_Write_UINT16(s, 0);
1272 Stream_Write_UINT32(s, pdu->outputOriginX);
1273 Stream_Write_UINT32(s, pdu->outputOriginY);
1274 return rdpgfx_server_single_packet_send(context, s);
1282WINPR_ATTR_NODISCARD
static UINT
1283rdpgfx_send_map_surface_to_window_pdu(RdpgfxServerContext* context,
1286 if (!checkCapsAreExchanged(context))
1287 return CHANNEL_RC_NOT_INITIALIZED;
1290 WINPR_ASSERT(context->priv);
1293 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOWINDOW, 18);
1297 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1298 return CHANNEL_RC_NO_MEMORY;
1301 Stream_Write_UINT16(s, pdu->surfaceId);
1302 Stream_Write_UINT64(s, pdu->windowId);
1303 Stream_Write_UINT32(s, pdu->mappedWidth);
1304 Stream_Write_UINT32(s, pdu->mappedHeight);
1305 return rdpgfx_server_single_packet_send(context, s);
1308WINPR_ATTR_NODISCARD
static UINT
1309rdpgfx_send_map_surface_to_scaled_window_pdu(RdpgfxServerContext* context,
1312 if (!checkCapsAreExchanged(context))
1313 return CHANNEL_RC_NOT_INITIALIZED;
1316 WINPR_ASSERT(context->priv);
1317 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1318 RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW, 26);
1322 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1323 return CHANNEL_RC_NO_MEMORY;
1326 Stream_Write_UINT16(s, pdu->surfaceId);
1327 Stream_Write_UINT64(s, pdu->windowId);
1328 Stream_Write_UINT32(s, pdu->mappedWidth);
1329 Stream_Write_UINT32(s, pdu->mappedHeight);
1330 Stream_Write_UINT32(s, pdu->targetWidth);
1331 Stream_Write_UINT32(s, pdu->targetHeight);
1332 return rdpgfx_server_single_packet_send(context, s);
1340WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_frame_acknowledge_pdu(RdpgfxServerContext* context,
1343 WINPR_ASSERT(context);
1345 if (!checkCapsAreExchanged(context))
1346 return CHANNEL_RC_NOT_INITIALIZED;
1348 WINPR_ASSERT(context->priv);
1351 UINT error = CHANNEL_RC_OK;
1353 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 12))
1354 return ERROR_INVALID_DATA;
1356 Stream_Read_UINT32(s, pdu.queueDepth);
1357 Stream_Read_UINT32(s, pdu.frameId);
1358 Stream_Read_UINT32(s, pdu.totalFramesDecoded);
1360 IFCALLRET(context->FrameAcknowledge, error, context, &pdu);
1363 WLog_Print(context->priv->log, WLOG_ERROR,
1364 "context->FrameAcknowledge failed with error %" PRIu32
"", error);
1374WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_cache_import_offer_pdu(RdpgfxServerContext* context,
1377 WINPR_ASSERT(context);
1378 if (!checkCapsAreExchanged(context))
1379 return CHANNEL_RC_NOT_INITIALIZED;
1381 WINPR_ASSERT(context->priv);
1385 UINT error = CHANNEL_RC_OK;
1387 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1388 return ERROR_INVALID_DATA;
1391 Stream_Read_UINT16(s, pdu.cacheEntriesCount);
1394 if (pdu.cacheEntriesCount >= 5462)
1396 WLog_Print(context->priv->log, WLOG_ERROR,
"Invalid cacheEntriesCount: %" PRIu16
"",
1397 pdu.cacheEntriesCount);
1398 return ERROR_INVALID_DATA;
1401 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, pdu.cacheEntriesCount,
1403 return ERROR_INVALID_DATA;
1405 for (UINT16 index = 0; index < pdu.cacheEntriesCount; index++)
1407 cacheEntry = &(pdu.cacheEntries[index]);
1408 Stream_Read_UINT64(s, cacheEntry->cacheKey);
1409 Stream_Read_UINT32(s, cacheEntry->bitmapLength);
1412 IFCALLRET(context->CacheImportOffer, error, context, &pdu);
1415 WLog_Print(context->priv->log, WLOG_ERROR,
1416 "context->CacheImportOffer failed with error %" PRIu32
"", error);
1426WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_caps_advertise_pdu(RdpgfxServerContext* context,
1431 UINT error = ERROR_INVALID_DATA;
1434 return ERROR_BAD_ARGUMENTS;
1436 WINPR_ASSERT(context->priv);
1437 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1438 return ERROR_INVALID_DATA;
1440 Stream_Read_UINT16(s, pdu.capsSetCount);
1441 if (pdu.capsSetCount > 0)
1443 capsSets = calloc(pdu.capsSetCount, (RDPGFX_CAPSET_BASE_SIZE + 4));
1445 return ERROR_OUTOFMEMORY;
1448 pdu.capsSets = capsSets;
1450 for (UINT16 index = 0; index < pdu.capsSetCount; index++)
1454 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 8))
1457 Stream_Read_UINT32(s, capsSet->version);
1458 Stream_Read_UINT32(s, capsSet->length);
1460 if (capsSet->length >= 4)
1462 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
1465 Stream_Peek_UINT32(s, capsSet->flags);
1468 if (!Stream_SafeSeek(s, capsSet->length))
1472 error = ERROR_BAD_CONFIGURATION;
1473 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
1476 WLog_Print(context->priv->log, WLOG_ERROR,
1477 "context->CapsAdvertise failed with error %" PRIu32
"", error);
1489WINPR_ATTR_NODISCARD
static UINT rdpgfx_recv_qoe_frame_acknowledge_pdu(RdpgfxServerContext* context,
1492 WINPR_ASSERT(context);
1494 if (!checkCapsAreExchanged(context))
1495 return CHANNEL_RC_NOT_INITIALIZED;
1498 UINT error = CHANNEL_RC_OK;
1500 WINPR_ASSERT(context->priv);
1502 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 12))
1503 return ERROR_INVALID_DATA;
1505 Stream_Read_UINT32(s, pdu.frameId);
1506 Stream_Read_UINT32(s, pdu.timestamp);
1507 Stream_Read_UINT16(s, pdu.timeDiffSE);
1508 Stream_Read_UINT16(s, pdu.timeDiffEDR);
1510 IFCALLRET(context->QoeFrameAcknowledge, error, context, &pdu);
1513 WLog_Print(context->priv->log, WLOG_ERROR,
1514 "context->QoeFrameAcknowledge failed with error %" PRIu32
"", error);
1519WINPR_ATTR_NODISCARD
static UINT
1520rdpgfx_send_map_surface_to_scaled_output_pdu(RdpgfxServerContext* context,
1523 if (!checkCapsAreExchanged(context))
1524 return CHANNEL_RC_NOT_INITIALIZED;
1527 WINPR_ASSERT(context->priv);
1528 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1529 RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT, 20);
1533 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1534 return CHANNEL_RC_NO_MEMORY;
1537 Stream_Write_UINT16(s, pdu->surfaceId);
1538 Stream_Write_UINT16(s, 0);
1539 Stream_Write_UINT32(s, pdu->outputOriginX);
1540 Stream_Write_UINT32(s, pdu->outputOriginY);
1541 Stream_Write_UINT32(s, pdu->targetWidth);
1542 Stream_Write_UINT32(s, pdu->targetHeight);
1543 return rdpgfx_server_single_packet_send(context, s);
1551WINPR_ATTR_NODISCARD
static UINT rdpgfx_server_receive_pdu(RdpgfxServerContext* context,
1554 size_t packetlen = 0;
1555 wStream sbuffer = WINPR_C_ARRAY_INIT;
1557 UINT error = CHANNEL_RC_OK;
1559 WINPR_ASSERT(context);
1560 WINPR_ASSERT(context->priv);
1562 if ((error = rdpgfx_read_header(context->priv->log, data, &header, &packetlen)))
1564 WLog_Print(context->priv->log, WLOG_ERROR,
1565 "rdpgfx_read_header failed with error %" PRIu32
"!", error);
1570 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
1571 Stream_Seek(data, packetlen);
1573 WLog_Print(context->priv->log, WLOG_TRACE,
1574 "cmdId: %s (0x%04" PRIX16
") flags: 0x%04" PRIX16
" pduLength: %" PRIu32
"",
1575 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
1578 switch (header.cmdId)
1580 case RDPGFX_CMDID_FRAMEACKNOWLEDGE:
1581 if ((error = rdpgfx_recv_frame_acknowledge_pdu(context, s)))
1582 WLog_Print(context->priv->log, WLOG_ERROR,
1583 "rdpgfx_recv_frame_acknowledge_pdu "
1584 "failed with error %" PRIu32
"!",
1589 case RDPGFX_CMDID_CACHEIMPORTOFFER:
1590 if ((error = rdpgfx_recv_cache_import_offer_pdu(context, s)))
1591 WLog_Print(context->priv->log, WLOG_ERROR,
1592 "rdpgfx_recv_cache_import_offer_pdu "
1593 "failed with error %" PRIu32
"!",
1598 case RDPGFX_CMDID_CAPSADVERTISE:
1599 if ((error = rdpgfx_recv_caps_advertise_pdu(context, s)))
1600 WLog_Print(context->priv->log, WLOG_ERROR,
1601 "rdpgfx_recv_caps_advertise_pdu "
1602 "failed with error %" PRIu32
"!",
1607 case RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE:
1608 if ((error = rdpgfx_recv_qoe_frame_acknowledge_pdu(context, s)))
1609 WLog_Print(context->priv->log, WLOG_ERROR,
1610 "rdpgfx_recv_qoe_frame_acknowledge_pdu "
1611 "failed with error %" PRIu32
"!",
1617 error = CHANNEL_RC_BAD_PROC;
1623 WLog_Print(context->priv->log, WLOG_ERROR,
1624 "Error while parsing GFX cmdId: %s (0x%04" PRIX16
")",
1625 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
1629 const size_t end = Stream_GetPosition(s);
1631 if (end != packetlen)
1633 WLog_Print(context->priv->log, WLOG_ERROR,
1634 "Unexpected gfx pdu end: Actual: %" PRIuz
", Expected: %" PRIuz
"", end,
1641WINPR_ATTR_NODISCARD
static DWORD WINAPI rdpgfx_server_thread_func(LPVOID arg)
1643 RdpgfxServerContext* context = (RdpgfxServerContext*)arg;
1644 WINPR_ASSERT(context);
1646 RdpgfxServerPrivate* priv = context->priv;
1649 HANDLE events[8] = WINPR_C_ARRAY_INIT;
1650 UINT error = CHANNEL_RC_OK;
1654 if (priv->ownThread)
1656 WINPR_ASSERT(priv->stopEvent);
1657 events[nCount++] = priv->stopEvent;
1660 WINPR_ASSERT(priv->channelEvent);
1661 events[nCount++] = priv->channelEvent;
1666 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1668 if (status == WAIT_FAILED)
1670 error = GetLastError();
1671 WLog_Print(context->priv->log, WLOG_ERROR,
1672 "WaitForMultipleObjects failed with error %" PRIu32
"", error);
1677 if (status == WAIT_OBJECT_0)
1680 if ((error = rdpgfx_server_handle_messages(context)))
1682 WLog_Print(context->priv->log, WLOG_ERROR,
1683 "rdpgfx_server_handle_messages failed with error %" PRIu32
"", error);
1688 if (error && context->rdpcontext)
1689 setChannelError(context->rdpcontext, error,
"rdpgfx_server_thread_func reported an error");
1695WINPR_ATTR_NODISCARD
static BOOL rdpgfx_server_open(RdpgfxServerContext* context)
1697 WINPR_ASSERT(context);
1698 RdpgfxServerPrivate* priv = context->priv;
1699 void* buffer =
nullptr;
1703 if (!priv->isOpened)
1705 PULONG pSessionId =
nullptr;
1706 DWORD BytesReturned = 0;
1707 priv->SessionId = WTS_CURRENT_SESSION;
1708 UINT32 channelId = 0;
1711 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
1712 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
1714 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSQuerySessionInformationA failed!");
1718 priv->SessionId = (DWORD)*pSessionId;
1719 WTSFreeMemory(pSessionId);
1720 priv->rdpgfx_channel = WTSVirtualChannelOpenEx(priv->SessionId, RDPGFX_DVC_CHANNEL_NAME,
1721 WTS_CHANNEL_OPTION_DYNAMIC);
1723 if (!priv->rdpgfx_channel)
1725 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelOpenEx failed!");
1729 channelId = WTSChannelGetIdByHandle(priv->rdpgfx_channel);
1731 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
1734 WLog_Print(context->priv->log, WLOG_ERROR,
"context->ChannelIdAssigned failed!");
1739 if (!WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualEventHandle, &buffer,
1741 (BytesReturned !=
sizeof(HANDLE)))
1743 WLog_Print(context->priv->log, WLOG_ERROR,
1744 "WTSVirtualChannelQuery failed "
1745 "or invalid returned size(%" PRIu32
")",
1749 WTSFreeMemory(buffer);
1754 priv->channelEvent = *(HANDLE*)buffer;
1755 WTSFreeMemory(buffer);
1757 if (!(priv->zgfx = zgfx_context_new(TRUE)))
1759 WLog_Print(context->priv->log, WLOG_ERROR,
"Create zgfx context failed!");
1763 priv->isReady = FALSE;
1765 priv->activeCapSet = empty;
1766 if (priv->ownThread)
1768 if (!(priv->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
1770 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateEvent failed!");
1774 if (!(priv->thread = CreateThread(
nullptr, 0, rdpgfx_server_thread_func, (
void*)context,
1777 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThread failed!");
1782 priv->isOpened = TRUE;
1786 WLog_Print(context->priv->log, WLOG_ERROR,
"RDPGFX channel is already opened!");
1789 (void)rdpgfx_server_close(context);
1793BOOL rdpgfx_server_close(RdpgfxServerContext* context)
1795 WINPR_ASSERT(context);
1797 RdpgfxServerPrivate* priv = context->priv;
1800 if (priv->ownThread && priv->thread)
1802 (void)SetEvent(priv->stopEvent);
1804 if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
1806 WLog_Print(context->priv->log, WLOG_ERROR,
1807 "WaitForSingleObject failed with error %" PRIu32
"", GetLastError());
1811 (void)CloseHandle(priv->thread);
1812 (void)CloseHandle(priv->stopEvent);
1813 priv->thread =
nullptr;
1814 priv->stopEvent =
nullptr;
1817 zgfx_context_free(priv->zgfx);
1818 priv->zgfx =
nullptr;
1820 if (priv->rdpgfx_channel)
1822 (void)WTSVirtualChannelClose(priv->rdpgfx_channel);
1823 priv->rdpgfx_channel =
nullptr;
1826 priv->channelEvent =
nullptr;
1827 priv->isOpened = FALSE;
1828 priv->isReady = FALSE;
1830 priv->activeCapSet = empty;
1834WINPR_ATTR_NODISCARD
static BOOL rdpgfx_server_initialize(RdpgfxServerContext* context,
1835 BOOL externalThread)
1837 WINPR_ASSERT(context);
1838 WINPR_ASSERT(context->priv);
1840 if (context->priv->isOpened)
1842 WLog_Print(context->priv->log, WLOG_WARN,
1843 "Application error: RDPEGFX channel already initialized, "
1844 "calling in this state is not possible!");
1848 context->priv->ownThread = !externalThread;
1852RdpgfxServerContext* rdpgfx_server_context_new(HANDLE vcm)
1854 RdpgfxServerContext* context = (RdpgfxServerContext*)calloc(1,
sizeof(RdpgfxServerContext));
1860 context->Initialize = rdpgfx_server_initialize;
1861 context->Open = rdpgfx_server_open;
1862 context->Close = rdpgfx_server_close;
1863 context->ResetGraphics = rdpgfx_send_reset_graphics_pdu;
1864 context->StartFrame = rdpgfx_send_start_frame_pdu;
1865 context->EndFrame = rdpgfx_send_end_frame_pdu;
1866 context->SurfaceCommand = rdpgfx_send_surface_command;
1867 context->SurfaceFrameCommand = rdpgfx_send_surface_frame_command;
1868 context->DeleteEncodingContext = rdpgfx_send_delete_encoding_context_pdu;
1869 context->CreateSurface = rdpgfx_send_create_surface_pdu;
1870 context->DeleteSurface = rdpgfx_send_delete_surface_pdu;
1871 context->SolidFill = rdpgfx_send_solid_fill_pdu;
1872 context->SurfaceToSurface = rdpgfx_send_surface_to_surface_pdu;
1873 context->SurfaceToCache = rdpgfx_send_surface_to_cache_pdu;
1874 context->CacheToSurface = rdpgfx_send_cache_to_surface_pdu;
1875 context->CacheImportOffer = rdpgfx_process_cache_import_offer_pdu;
1876 context->CacheImportReply = rdpgfx_send_cache_import_reply_pdu;
1877 context->EvictCacheEntry = rdpgfx_send_evict_cache_entry_pdu;
1878 context->MapSurfaceToOutput = rdpgfx_send_map_surface_to_output_pdu;
1879 context->MapSurfaceToWindow = rdpgfx_send_map_surface_to_window_pdu;
1880 context->MapSurfaceToScaledOutput = rdpgfx_send_map_surface_to_scaled_output_pdu;
1881 context->MapSurfaceToScaledWindow = rdpgfx_send_map_surface_to_scaled_window_pdu;
1882 context->CapsAdvertise =
nullptr;
1883 context->CapsConfirm = rdpgfx_send_caps_confirm_pdu;
1884 context->FrameAcknowledge =
nullptr;
1885 context->QoeFrameAcknowledge =
nullptr;
1886 RdpgfxServerPrivate* priv = context->priv =
1887 (RdpgfxServerPrivate*)calloc(1,
sizeof(RdpgfxServerPrivate));
1892 priv->log = WLog_Get(TAG);
1897 priv->input_stream = Stream_New(
nullptr, 4);
1899 if (!priv->input_stream)
1901 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
1905 priv->isOpened = FALSE;
1906 priv->isReady = FALSE;
1907 priv->ownThread = TRUE;
1911 priv->activeCapSet = empty;
1916 WINPR_PRAGMA_DIAG_PUSH
1917 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1918 rdpgfx_server_context_free(context);
1919 WINPR_PRAGMA_DIAG_POP
1923void rdpgfx_server_context_free(RdpgfxServerContext* context)
1928 (void)rdpgfx_server_close(context);
1931 Stream_Free(context->priv->input_stream, TRUE);
1933 free(context->priv);
1937HANDLE rdpgfx_server_get_event_handle(RdpgfxServerContext* context)
1943 return context->priv->channelEvent;
1955UINT rdpgfx_server_handle_messages(RdpgfxServerContext* context)
1957 DWORD BytesReturned = 0;
1958 void* buffer =
nullptr;
1959 UINT ret = CHANNEL_RC_OK;
1961 WINPR_ASSERT(context);
1962 WINPR_ASSERT(context->priv);
1964 RdpgfxServerPrivate* priv = context->priv;
1965 wStream* s = priv->input_stream;
1970 if (WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualChannelReady, &buffer,
1971 &BytesReturned) == FALSE)
1973 if (GetLastError() == ERROR_NO_DATA)
1974 return ERROR_NO_DATA;
1976 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelQuery failed");
1977 return ERROR_INTERNAL_ERROR;
1980 priv->isReady = *((BOOL*)buffer);
1981 WTSFreeMemory(buffer);
1987 Stream_ResetPosition(s);
1989 if (!WTSVirtualChannelRead(priv->rdpgfx_channel, 0,
nullptr, 0, &BytesReturned))
1991 if (GetLastError() == ERROR_NO_DATA)
1992 return ERROR_NO_DATA;
1994 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
1995 return ERROR_INTERNAL_ERROR;
1998 if (BytesReturned < 1)
1999 return CHANNEL_RC_OK;
2001 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
2003 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
2004 return CHANNEL_RC_NO_MEMORY;
2007 const size_t len = Stream_Capacity(s);
2008 if (len > UINT32_MAX)
2009 return ERROR_INTERNAL_ERROR;
2010 if (WTSVirtualChannelRead(priv->rdpgfx_channel, 0, Stream_BufferAs(s,
char), (UINT32)len,
2011 &BytesReturned) == FALSE)
2013 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
2014 return ERROR_INTERNAL_ERROR;
2017 if (!Stream_SetLength(s, BytesReturned))
2018 return ERROR_INTERNAL_ERROR;
2020 Stream_ResetPosition(s);
2022 while (Stream_GetPosition(s) < Stream_Length(s))
2024 if ((ret = rdpgfx_server_receive_pdu(context, s)))
2026 WLog_Print(context->priv->log, WLOG_ERROR,
2027 "rdpgfx_server_receive_pdu "
2028 "failed with error %" PRIu32
"!",