20 #include <freerdp/config.h>
22 #include <winpr/assert.h>
27 #include <winpr/crt.h>
28 #include <winpr/synch.h>
29 #include <winpr/thread.h>
30 #include <winpr/stream.h>
32 #include <freerdp/freerdp.h>
33 #include <freerdp/codec/color.h>
35 #include <freerdp/channels/wtsvc.h>
36 #include <freerdp/channels/log.h>
38 #include "rdpgfx_common.h"
39 #include "rdpgfx_main.h"
41 #define TAG CHANNELS_TAG("rdpgfx.server")
42 #define RDPGFX_RESET_GRAPHICS_PDU_SIZE 340
44 #define checkCapsAreExchanged(context) \
45 checkCapsAreExchangedInt(context, __FILE__, __func__, __LINE__)
46 static BOOL checkCapsAreExchangedInt(RdpgfxServerContext* context,
const char* file,
47 const char* fkt,
size_t line)
49 WINPR_ASSERT(context);
50 WINPR_ASSERT(context->priv);
52 const DWORD level = WLOG_TRACE;
53 if (WLog_IsLevelActive(context->priv->log, level))
55 WLog_PrintMessage(context->priv->log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
56 "activeCapSet{Version=0x%08" PRIx32
", flags=0x%08" PRIx32
"}",
57 context->priv->activeCapSet.version, context->priv->activeCapSet.flags);
59 return context->priv->activeCapSet.version > 0;
71 static INLINE UINT32 rdpgfx_pdu_length(UINT32 dataLen)
73 return RDPGFX_HEADER_SIZE + dataLen;
76 static INLINE UINT rdpgfx_server_packet_init_header(
wStream* s, UINT16 cmdId, UINT32 pduLength)
81 header.pduLength = pduLength;
84 return rdpgfx_write_header(s, &header);
94 static INLINE BOOL rdpgfx_server_packet_complete_header(
wStream* s,
size_t start)
96 const size_t current = Stream_GetPosition(s);
97 const size_t cap = Stream_Capacity(s);
98 if (cap < start + RDPGFX_HEADER_SIZE)
100 if ((start > UINT32_MAX) || (current > start))
103 Stream_SetPosition(s, start + RDPGFX_HEADER_SIZE -
sizeof(UINT32));
104 Stream_Write_UINT32(s, (UINT32)(current - start));
105 Stream_SetPosition(s, current);
116 static UINT rdpgfx_server_packet_send(RdpgfxServerContext* context,
wStream* s)
121 BYTE* pSrcData = Stream_Buffer(s);
122 const size_t SrcSize = Stream_GetPosition(s);
123 if (SrcSize > UINT32_MAX)
124 return ERROR_INTERNAL_ERROR;
130 fs = Stream_New(NULL, SrcSize + 7 + (SrcSize / ZGFX_SEGMENTED_MAXSIZE + 1) * 4);
134 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
135 error = CHANNEL_RC_NO_MEMORY;
139 if (zgfx_compress_to_stream(context->priv->zgfx, fs, pSrcData, (UINT32)SrcSize, &flags) < 0)
141 WLog_Print(context->priv->log, WLOG_ERROR,
"zgfx_compress_to_stream failed!");
142 error = ERROR_INTERNAL_ERROR;
146 const size_t pos = Stream_GetPosition(fs);
148 WINPR_ASSERT(pos <= UINT32_MAX);
149 if (!WTSVirtualChannelWrite(context->priv->rdpgfx_channel, Stream_BufferAs(fs,
char),
150 (UINT32)pos, &written))
152 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelWrite failed!");
153 error = ERROR_INTERNAL_ERROR;
157 if (written < Stream_GetPosition(fs))
159 WLog_Print(context->priv->log, WLOG_WARN,
160 "Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
161 Stream_GetPosition(fs));
164 error = CHANNEL_RC_OK;
166 Stream_Free(fs, TRUE);
167 Stream_Free(s, TRUE);
183 static wStream* rdpgfx_server_single_packet_new(wLog* log, UINT16 cmdId, UINT32 dataLen)
187 UINT32 pduLength = rdpgfx_pdu_length(dataLen);
188 s = Stream_New(NULL, pduLength);
192 WLog_Print(log, WLOG_ERROR,
"Stream_New failed!");
196 if ((error = rdpgfx_server_packet_init_header(s, cmdId, pduLength)))
198 WLog_Print(log, WLOG_ERROR,
"Failed to init header with error %" PRIu32
"!", error);
204 Stream_Free(s, TRUE);
216 static INLINE UINT rdpgfx_server_single_packet_send(RdpgfxServerContext* context,
wStream* s)
219 rdpgfx_server_packet_complete_header(s, 0);
220 return rdpgfx_server_packet_send(context, s);
228 static UINT rdpgfx_send_caps_confirm_pdu(RdpgfxServerContext* context,
234 WINPR_ASSERT(context);
235 WINPR_ASSERT(capsConfirm);
237 capsSet = capsConfirm->capsSet;
238 WINPR_ASSERT(capsSet);
240 s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CAPSCONFIRM,
241 RDPGFX_CAPSET_BASE_SIZE + capsSet->length);
245 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
246 return CHANNEL_RC_NO_MEMORY;
249 WLog_DBG(TAG,
"CAPS version=0x%04" PRIx32
", flags=0x%04" PRIx32
", length=%" PRIu32,
250 capsSet->version, capsSet->flags, capsSet->length);
251 Stream_Write_UINT32(s, capsSet->version);
252 Stream_Write_UINT32(s, capsSet->length);
254 if (capsSet->length >= 4)
256 Stream_Write_UINT32(s, capsSet->flags);
257 Stream_Zero(s, capsSet->length - 4);
260 Stream_Zero(s, capsSet->length);
262 context->priv->activeCapSet = *capsSet;
263 return rdpgfx_server_single_packet_send(context, s);
271 static UINT rdpgfx_send_reset_graphics_pdu(RdpgfxServerContext* context,
274 if (!checkCapsAreExchanged(context))
275 return CHANNEL_RC_NOT_INITIALIZED;
280 if (pdu->monitorCount >= 16)
282 WLog_Print(context->priv->log, WLOG_ERROR,
283 "Monitor count MUST be less than or equal to 16: %" PRIu32
"",
285 return ERROR_INVALID_DATA;
288 s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_RESETGRAPHICS,
289 RDPGFX_RESET_GRAPHICS_PDU_SIZE - RDPGFX_HEADER_SIZE);
293 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
294 return CHANNEL_RC_NO_MEMORY;
297 Stream_Write_UINT32(s, pdu->width);
298 Stream_Write_UINT32(s, pdu->height);
299 Stream_Write_UINT32(s, pdu->monitorCount);
301 for (UINT32 index = 0; index < pdu->monitorCount; index++)
303 const MONITOR_DEF* monitor = &(pdu->monitorDefArray[index]);
304 Stream_Write_INT32(s, monitor->left);
305 Stream_Write_INT32(s, monitor->top);
306 Stream_Write_INT32(s, monitor->right);
307 Stream_Write_INT32(s, monitor->bottom);
308 Stream_Write_UINT32(s, monitor->flags);
312 Stream_SetPosition(s, RDPGFX_RESET_GRAPHICS_PDU_SIZE);
313 return rdpgfx_server_single_packet_send(context, s);
321 static UINT rdpgfx_send_evict_cache_entry_pdu(RdpgfxServerContext* context,
324 if (!checkCapsAreExchanged(context))
325 return CHANNEL_RC_NOT_INITIALIZED;
327 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_EVICTCACHEENTRY, 2);
331 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
332 return CHANNEL_RC_NO_MEMORY;
335 Stream_Write_UINT16(s, pdu->cacheSlot);
336 return rdpgfx_server_single_packet_send(context, s);
344 static UINT rdpgfx_send_cache_import_reply_pdu(RdpgfxServerContext* context,
347 if (!checkCapsAreExchanged(context))
348 return CHANNEL_RC_NOT_INITIALIZED;
349 WINPR_ASSERT(context);
352 WLog_DBG(TAG,
"reply with %" PRIu16
" entries", pdu->importedEntriesCount);
353 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CACHEIMPORTREPLY,
354 2 + 2 * pdu->importedEntriesCount);
358 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
359 return CHANNEL_RC_NO_MEMORY;
363 Stream_Write_UINT16(s, pdu->importedEntriesCount);
365 for (UINT16 index = 0; index < pdu->importedEntriesCount; index++)
367 Stream_Write_UINT16(s, pdu->cacheSlots[index]);
370 return rdpgfx_server_single_packet_send(context, s);
374 rdpgfx_process_cache_import_offer_pdu(RdpgfxServerContext* context,
377 if (!checkCapsAreExchanged(context))
378 return CHANNEL_RC_NOT_INITIALIZED;
379 WINPR_ASSERT(context);
380 WINPR_ASSERT(cacheImportOffer);
383 WLog_DBG(TAG,
"received %" PRIu16
" entries, reply with %" PRIu16
" entries",
384 cacheImportOffer->cacheEntriesCount, reply.importedEntriesCount);
385 return IFCALLRESULT(CHANNEL_RC_OK, context->CacheImportReply, context, &reply);
393 static UINT rdpgfx_send_create_surface_pdu(RdpgfxServerContext* context,
396 if (!checkCapsAreExchanged(context))
397 return CHANNEL_RC_NOT_INITIALIZED;
398 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CREATESURFACE, 7);
400 WINPR_ASSERT(context);
402 WINPR_ASSERT((pdu->pixelFormat == GFX_PIXEL_FORMAT_XRGB_8888) ||
403 (pdu->pixelFormat == GFX_PIXEL_FORMAT_ARGB_8888));
407 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
408 return CHANNEL_RC_NO_MEMORY;
411 Stream_Write_UINT16(s, pdu->surfaceId);
412 Stream_Write_UINT16(s, pdu->width);
413 Stream_Write_UINT16(s, pdu->height);
414 Stream_Write_UINT8(s, pdu->pixelFormat);
415 return rdpgfx_server_single_packet_send(context, s);
423 static UINT rdpgfx_send_delete_surface_pdu(RdpgfxServerContext* context,
426 if (!checkCapsAreExchanged(context))
427 return CHANNEL_RC_NOT_INITIALIZED;
428 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETESURFACE, 2);
432 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
433 return CHANNEL_RC_NO_MEMORY;
436 Stream_Write_UINT16(s, pdu->surfaceId);
437 return rdpgfx_server_single_packet_send(context, s);
442 if (!Stream_EnsureRemainingCapacity(s, 8))
444 Stream_Write_UINT32(s, pdu->timestamp);
445 Stream_Write_UINT32(s, pdu->frameId);
451 if (!Stream_EnsureRemainingCapacity(s, 4))
453 Stream_Write_UINT32(s, pdu->frameId);
462 static UINT rdpgfx_send_start_frame_pdu(RdpgfxServerContext* context,
465 if (!checkCapsAreExchanged(context))
466 return CHANNEL_RC_NOT_INITIALIZED;
467 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_STARTFRAME,
468 RDPGFX_START_FRAME_PDU_SIZE);
472 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
473 return CHANNEL_RC_NO_MEMORY;
476 rdpgfx_write_start_frame_pdu(s, pdu);
477 return rdpgfx_server_single_packet_send(context, s);
485 static UINT rdpgfx_send_end_frame_pdu(RdpgfxServerContext* context,
const RDPGFX_END_FRAME_PDU* pdu)
487 if (!checkCapsAreExchanged(context))
488 return CHANNEL_RC_NOT_INITIALIZED;
489 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_ENDFRAME,
490 RDPGFX_END_FRAME_PDU_SIZE);
494 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
495 return CHANNEL_RC_NO_MEMORY;
498 rdpgfx_write_end_frame_pdu(s, pdu);
499 return rdpgfx_server_single_packet_send(context, s);
511 return sizeof(UINT32)
513 * havc420->meta.numRegionRects +
530 switch (cmd->codecId)
532 case RDPGFX_CODECID_CAPROGRESSIVE:
533 case RDPGFX_CODECID_CAPROGRESSIVE_V2:
534 return RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE + cmd->length;
536 case RDPGFX_CODECID_AVC420:
538 h264Size = rdpgfx_estimate_h264_avc420(havc420);
539 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
541 case RDPGFX_CODECID_AVC444:
543 h264Size =
sizeof(UINT32);
545 havc420 = &(havc444->bitstream[0]);
546 h264Size += rdpgfx_estimate_h264_avc420(havc420);
549 if (havc444->LC == 0)
551 havc420 = &(havc444->bitstream[1]);
552 h264Size += rdpgfx_estimate_h264_avc420(havc420);
555 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + h264Size;
558 return RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE + cmd->length;
571 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
572 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
574 return RDPGFX_CMDID_WIRETOSURFACE_2;
577 return RDPGFX_CMDID_WIRETOSURFACE_1;
589 UINT error = CHANNEL_RC_OK;
591 if (!Stream_EnsureRemainingCapacity(s, 4 + meta->numRegionRects * 10))
592 return ERROR_OUTOFMEMORY;
594 Stream_Write_UINT32(s, meta->numRegionRects);
596 for (UINT32 index = 0; index < meta->numRegionRects; index++)
598 regionRect = &(meta->regionRects[index]);
600 if ((error = rdpgfx_write_rect16(s, regionRect)))
602 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_rect16 failed with error %" PRIu32
"!",
608 for (UINT32 index = 0; index < meta->numRegionRects; index++)
610 quantQualityVal = &(meta->quantQualityVals[index]);
611 Stream_Write_UINT8(s, quantQualityVal->qp | (quantQualityVal->r << 6) |
612 (quantQualityVal->p << 7));
614 Stream_Write_UINT8(s, quantQualityVal->qualityVal);
626 static INLINE UINT rdpgfx_write_h264_avc420(wLog* log,
wStream* s,
629 UINT error = CHANNEL_RC_OK;
631 if ((error = rdpgfx_write_h264_metablock(log, s, &(havc420->meta))))
633 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_metablock failed with error %" PRIu32
"!",
638 if (!Stream_EnsureRemainingCapacity(s, havc420->length))
639 return ERROR_OUTOFMEMORY;
641 Stream_Write(s, havc420->data, havc420->length);
654 UINT error = CHANNEL_RC_OK;
657 UINT8 pixelFormat = 0;
661 case PIXEL_FORMAT_BGRX32:
662 pixelFormat = GFX_PIXEL_FORMAT_XRGB_8888;
665 case PIXEL_FORMAT_BGRA32:
666 pixelFormat = GFX_PIXEL_FORMAT_ARGB_8888;
670 WLog_Print(log, WLOG_ERROR,
"Format %s not supported!",
671 FreeRDPGetColorFormatName(cmd->format));
672 return ERROR_INVALID_DATA;
675 if (cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE ||
676 cmd->codecId == RDPGFX_CODECID_CAPROGRESSIVE_V2)
678 if (!Stream_EnsureRemainingCapacity(s, 13 + cmd->length))
679 return ERROR_INTERNAL_ERROR;
681 Stream_Write_UINT16(s, cmd->surfaceId);
682 Stream_Write_UINT16(s, cmd->codecId);
683 Stream_Write_UINT32(s, cmd->contextId);
684 Stream_Write_UINT8(s, pixelFormat);
685 Stream_Write_UINT32(s, cmd->length);
686 Stream_Write(s, cmd->data, cmd->length);
691 if (!Stream_EnsureRemainingCapacity(s, 17))
692 return ERROR_INTERNAL_ERROR;
693 Stream_Write_UINT16(s, cmd->surfaceId);
694 Stream_Write_UINT16(s, cmd->codecId);
695 Stream_Write_UINT8(s, pixelFormat);
696 Stream_Write_UINT16(s, cmd->left);
697 Stream_Write_UINT16(s, cmd->top);
698 Stream_Write_UINT16(s, cmd->right);
699 Stream_Write_UINT16(s, cmd->bottom);
700 Stream_Write_UINT32(s, cmd->length);
701 const size_t bitmapDataStart = Stream_GetPosition(s);
703 if (cmd->codecId == RDPGFX_CODECID_AVC420)
706 error = rdpgfx_write_h264_avc420(log, s, havc420);
708 if (error != CHANNEL_RC_OK)
710 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
714 else if ((cmd->codecId == RDPGFX_CODECID_AVC444) ||
715 (cmd->codecId == RDPGFX_CODECID_AVC444v2))
718 havc420 = &(havc444->bitstream[0]);
719 if (!Stream_EnsureRemainingCapacity(s, 4))
720 return ERROR_INTERNAL_ERROR;
721 Stream_Write_UINT32(s, havc444->cbAvc420EncodedBitstream1 | (havc444->LC << 30UL));
723 error = rdpgfx_write_h264_avc420(log, s, havc420);
725 if (error != CHANNEL_RC_OK)
727 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
732 if (havc444->LC == 0)
734 havc420 = &(havc444->bitstream[1]);
735 error = rdpgfx_write_h264_avc420(log, s, havc420);
737 if (error != CHANNEL_RC_OK)
739 WLog_Print(log, WLOG_ERROR,
"rdpgfx_write_h264_avc420 failed!");
746 if (!Stream_EnsureRemainingCapacity(s, cmd->length))
747 return ERROR_INTERNAL_ERROR;
748 Stream_Write(s, cmd->data, cmd->length);
752 const size_t bitmapDataLength = Stream_GetPosition(s) - bitmapDataStart;
753 if (bitmapDataLength > UINT32_MAX)
754 return ERROR_INTERNAL_ERROR;
756 Stream_SetPosition(s, bitmapDataStart -
sizeof(UINT32));
757 if (!Stream_EnsureRemainingCapacity(s, 4))
758 return ERROR_INTERNAL_ERROR;
759 Stream_Write_UINT32(s, (UINT32)bitmapDataLength);
760 if (!Stream_SafeSeek(s, bitmapDataLength))
761 return ERROR_INTERNAL_ERROR;
774 static UINT rdpgfx_send_surface_command(RdpgfxServerContext* context,
777 if (!checkCapsAreExchanged(context))
778 return CHANNEL_RC_NOT_INITIALIZED;
779 UINT error = CHANNEL_RC_OK;
781 s = rdpgfx_server_single_packet_new(context->priv->log, rdpgfx_surface_command_cmdid(cmd),
782 rdpgfx_estimate_surface_command(cmd));
786 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
787 return CHANNEL_RC_NO_MEMORY;
790 error = rdpgfx_write_surface_command(context->priv->log, s, cmd);
792 if (error != CHANNEL_RC_OK)
794 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_write_surface_command failed!");
798 return rdpgfx_server_single_packet_send(context, s);
800 Stream_Free(s, TRUE);
812 static UINT rdpgfx_send_surface_frame_command(RdpgfxServerContext* context,
818 if (!checkCapsAreExchanged(context))
819 return CHANNEL_RC_NOT_INITIALIZED;
820 UINT error = CHANNEL_RC_OK;
821 UINT32 size = rdpgfx_pdu_length(rdpgfx_estimate_surface_command(cmd));
825 size += rdpgfx_pdu_length(RDPGFX_START_FRAME_PDU_SIZE);
830 size += rdpgfx_pdu_length(RDPGFX_END_FRAME_PDU_SIZE);
833 wStream* s = Stream_New(NULL, size);
837 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
838 return CHANNEL_RC_NO_MEMORY;
844 const size_t position = Stream_GetPosition(s);
845 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_STARTFRAME, 0);
847 if (error != CHANNEL_RC_OK)
849 WLog_Print(context->priv->log, WLOG_ERROR,
850 "Failed to init header with error %" PRIu32
"!", error);
854 if (!rdpgfx_write_start_frame_pdu(s, startFrame) ||
855 !rdpgfx_server_packet_complete_header(s, position))
860 const size_t pos = Stream_GetPosition(s);
861 error = rdpgfx_server_packet_init_header(s, rdpgfx_surface_command_cmdid(cmd),
864 if (error != CHANNEL_RC_OK)
866 WLog_Print(context->priv->log, WLOG_ERROR,
"Failed to init header with error %" PRIu32
"!",
871 error = rdpgfx_write_surface_command(context->priv->log, s, cmd);
873 if (error != CHANNEL_RC_OK)
875 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_write_surface_command failed!");
879 if (!rdpgfx_server_packet_complete_header(s, pos))
885 const size_t position = Stream_GetPosition(s);
886 error = rdpgfx_server_packet_init_header(s, RDPGFX_CMDID_ENDFRAME, 0);
888 if (error != CHANNEL_RC_OK)
890 WLog_Print(context->priv->log, WLOG_ERROR,
891 "Failed to init header with error %" PRIu32
"!", error);
895 if (!rdpgfx_write_end_frame_pdu(s, endFrame) ||
896 !rdpgfx_server_packet_complete_header(s, position))
900 return rdpgfx_server_packet_send(context, s);
902 Stream_Free(s, TRUE);
911 static UINT rdpgfx_send_delete_encoding_context_pdu(RdpgfxServerContext* context,
914 if (!checkCapsAreExchanged(context))
915 return CHANNEL_RC_NOT_INITIALIZED;
917 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_DELETEENCODINGCONTEXT, 6);
921 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
922 return CHANNEL_RC_NO_MEMORY;
925 Stream_Write_UINT16(s, pdu->surfaceId);
926 Stream_Write_UINT32(s, pdu->codecContextId);
927 return rdpgfx_server_single_packet_send(context, s);
935 static UINT rdpgfx_send_solid_fill_pdu(RdpgfxServerContext* context,
938 if (!checkCapsAreExchanged(context))
939 return CHANNEL_RC_NOT_INITIALIZED;
940 UINT error = CHANNEL_RC_OK;
942 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SOLIDFILL,
943 8 + 8 * pdu->fillRectCount);
947 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
948 return CHANNEL_RC_NO_MEMORY;
951 Stream_Write_UINT16(s, pdu->surfaceId);
954 if ((error = rdpgfx_write_color32(s, &(pdu->fillPixel))))
956 WLog_Print(context->priv->log, WLOG_ERROR,
957 "rdpgfx_write_color32 failed with error %" PRIu32
"!", error);
961 Stream_Write_UINT16(s, pdu->fillRectCount);
963 for (UINT16 index = 0; index < pdu->fillRectCount; index++)
965 fillRect = &(pdu->fillRects[index]);
967 if ((error = rdpgfx_write_rect16(s, fillRect)))
969 WLog_Print(context->priv->log, WLOG_ERROR,
970 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
975 return rdpgfx_server_single_packet_send(context, s);
977 Stream_Free(s, TRUE);
986 static UINT rdpgfx_send_surface_to_surface_pdu(RdpgfxServerContext* context,
989 if (!checkCapsAreExchanged(context))
990 return CHANNEL_RC_NOT_INITIALIZED;
991 UINT error = CHANNEL_RC_OK;
993 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOSURFACE,
994 14 + 4 * pdu->destPtsCount);
998 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
999 return CHANNEL_RC_NO_MEMORY;
1002 Stream_Write_UINT16(s, pdu->surfaceIdSrc);
1003 Stream_Write_UINT16(s, pdu->surfaceIdDest);
1006 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1008 WLog_Print(context->priv->log, WLOG_ERROR,
1009 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1013 Stream_Write_UINT16(s, pdu->destPtsCount);
1015 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1017 destPt = &(pdu->destPts[index]);
1019 if ((error = rdpgfx_write_point16(s, destPt)))
1021 WLog_Print(context->priv->log, WLOG_ERROR,
1022 "rdpgfx_write_point16 failed with error %" PRIu32
"!", error);
1027 return rdpgfx_server_single_packet_send(context, s);
1029 Stream_Free(s, TRUE);
1038 static UINT rdpgfx_send_surface_to_cache_pdu(RdpgfxServerContext* context,
1041 if (!checkCapsAreExchanged(context))
1042 return CHANNEL_RC_NOT_INITIALIZED;
1043 UINT error = CHANNEL_RC_OK;
1045 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_SURFACETOCACHE, 20);
1049 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1050 return CHANNEL_RC_NO_MEMORY;
1053 Stream_Write_UINT16(s, pdu->surfaceId);
1054 Stream_Write_UINT64(s, pdu->cacheKey);
1055 Stream_Write_UINT16(s, pdu->cacheSlot);
1058 if ((error = rdpgfx_write_rect16(s, &(pdu->rectSrc))))
1060 WLog_Print(context->priv->log, WLOG_ERROR,
1061 "rdpgfx_write_rect16 failed with error %" PRIu32
"!", error);
1065 return rdpgfx_server_single_packet_send(context, s);
1067 Stream_Free(s, TRUE);
1076 static UINT rdpgfx_send_cache_to_surface_pdu(RdpgfxServerContext* context,
1079 if (!checkCapsAreExchanged(context))
1080 return CHANNEL_RC_NOT_INITIALIZED;
1081 UINT error = CHANNEL_RC_OK;
1083 wStream* s = rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_CACHETOSURFACE,
1084 6 + 4 * pdu->destPtsCount);
1088 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1089 return CHANNEL_RC_NO_MEMORY;
1092 Stream_Write_UINT16(s, pdu->cacheSlot);
1093 Stream_Write_UINT16(s, pdu->surfaceId);
1094 Stream_Write_UINT16(s, pdu->destPtsCount);
1096 for (UINT16 index = 0; index < pdu->destPtsCount; index++)
1098 destPt = &(pdu->destPts[index]);
1100 if ((error = rdpgfx_write_point16(s, destPt)))
1102 WLog_Print(context->priv->log, WLOG_ERROR,
1103 "rdpgfx_write_point16 failed with error %" PRIu32
"", error);
1108 return rdpgfx_server_single_packet_send(context, s);
1110 Stream_Free(s, TRUE);
1119 static UINT rdpgfx_send_map_surface_to_output_pdu(RdpgfxServerContext* context,
1122 if (!checkCapsAreExchanged(context))
1123 return CHANNEL_RC_NOT_INITIALIZED;
1125 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOOUTPUT, 12);
1129 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1130 return CHANNEL_RC_NO_MEMORY;
1133 Stream_Write_UINT16(s, pdu->surfaceId);
1134 Stream_Write_UINT16(s, 0);
1135 Stream_Write_UINT32(s, pdu->outputOriginX);
1136 Stream_Write_UINT32(s, pdu->outputOriginY);
1137 return rdpgfx_server_single_packet_send(context, s);
1145 static UINT rdpgfx_send_map_surface_to_window_pdu(RdpgfxServerContext* context,
1148 if (!checkCapsAreExchanged(context))
1149 return CHANNEL_RC_NOT_INITIALIZED;
1151 rdpgfx_server_single_packet_new(context->priv->log, RDPGFX_CMDID_MAPSURFACETOWINDOW, 18);
1155 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1156 return CHANNEL_RC_NO_MEMORY;
1159 Stream_Write_UINT16(s, pdu->surfaceId);
1160 Stream_Write_UINT64(s, pdu->windowId);
1161 Stream_Write_UINT32(s, pdu->mappedWidth);
1162 Stream_Write_UINT32(s, pdu->mappedHeight);
1163 return rdpgfx_server_single_packet_send(context, s);
1167 rdpgfx_send_map_surface_to_scaled_window_pdu(RdpgfxServerContext* context,
1170 if (!checkCapsAreExchanged(context))
1171 return CHANNEL_RC_NOT_INITIALIZED;
1172 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1173 RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW, 26);
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->windowId);
1183 Stream_Write_UINT32(s, pdu->mappedWidth);
1184 Stream_Write_UINT32(s, pdu->mappedHeight);
1185 Stream_Write_UINT32(s, pdu->targetWidth);
1186 Stream_Write_UINT32(s, pdu->targetHeight);
1187 return rdpgfx_server_single_packet_send(context, s);
1195 static UINT rdpgfx_recv_frame_acknowledge_pdu(RdpgfxServerContext* context,
wStream* s)
1197 if (!checkCapsAreExchanged(context))
1198 return CHANNEL_RC_NOT_INITIALIZED;
1200 UINT error = CHANNEL_RC_OK;
1202 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1203 return ERROR_INVALID_DATA;
1205 Stream_Read_UINT32(s, pdu.queueDepth);
1206 Stream_Read_UINT32(s, pdu.frameId);
1207 Stream_Read_UINT32(s, pdu.totalFramesDecoded);
1211 IFCALLRET(context->FrameAcknowledge, error, context, &pdu);
1214 WLog_Print(context->priv->log, WLOG_ERROR,
1215 "context->FrameAcknowledge failed with error %" PRIu32
"", error);
1226 static UINT rdpgfx_recv_cache_import_offer_pdu(RdpgfxServerContext* context,
wStream* s)
1228 if (!checkCapsAreExchanged(context))
1229 return CHANNEL_RC_NOT_INITIALIZED;
1233 UINT error = CHANNEL_RC_OK;
1235 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1236 return ERROR_INVALID_DATA;
1239 Stream_Read_UINT16(s, pdu.cacheEntriesCount);
1242 if (pdu.cacheEntriesCount >= 5462)
1244 WLog_Print(context->priv->log, WLOG_ERROR,
"Invalid cacheEntriesCount: %" PRIu16
"",
1245 pdu.cacheEntriesCount);
1246 return ERROR_INVALID_DATA;
1249 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, pdu.cacheEntriesCount, 12ull))
1250 return ERROR_INVALID_DATA;
1252 for (UINT16 index = 0; index < pdu.cacheEntriesCount; index++)
1254 cacheEntry = &(pdu.cacheEntries[index]);
1255 Stream_Read_UINT64(s, cacheEntry->cacheKey);
1256 Stream_Read_UINT32(s, cacheEntry->bitmapLength);
1261 IFCALLRET(context->CacheImportOffer, error, context, &pdu);
1264 WLog_Print(context->priv->log, WLOG_ERROR,
1265 "context->CacheImportOffer failed with error %" PRIu32
"", error);
1276 static UINT rdpgfx_recv_caps_advertise_pdu(RdpgfxServerContext* context,
wStream* s)
1280 UINT error = ERROR_INVALID_DATA;
1283 return ERROR_BAD_ARGUMENTS;
1285 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1286 return ERROR_INVALID_DATA;
1288 Stream_Read_UINT16(s, pdu.capsSetCount);
1289 if (pdu.capsSetCount > 0)
1291 capsSets = calloc(pdu.capsSetCount, (RDPGFX_CAPSET_BASE_SIZE + 4));
1293 return ERROR_OUTOFMEMORY;
1296 pdu.capsSets = capsSets;
1298 for (UINT16 index = 0; index < pdu.capsSetCount; index++)
1302 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1305 Stream_Read_UINT32(s, capsSet->version);
1306 Stream_Read_UINT32(s, capsSet->length);
1308 if (capsSet->length >= 4)
1310 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1313 Stream_Peek_UINT32(s, capsSet->flags);
1316 if (!Stream_SafeSeek(s, capsSet->length))
1320 error = ERROR_BAD_CONFIGURATION;
1321 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
1324 WLog_Print(context->priv->log, WLOG_ERROR,
1325 "context->CapsAdvertise failed with error %" PRIu32
"", error);
1337 static UINT rdpgfx_recv_qoe_frame_acknowledge_pdu(RdpgfxServerContext* context,
wStream* s)
1339 if (!checkCapsAreExchanged(context))
1340 return CHANNEL_RC_NOT_INITIALIZED;
1342 UINT error = CHANNEL_RC_OK;
1344 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1345 return ERROR_INVALID_DATA;
1347 Stream_Read_UINT32(s, pdu.frameId);
1348 Stream_Read_UINT32(s, pdu.timestamp);
1349 Stream_Read_UINT16(s, pdu.timeDiffSE);
1350 Stream_Read_UINT16(s, pdu.timeDiffEDR);
1354 IFCALLRET(context->QoeFrameAcknowledge, error, context, &pdu);
1357 WLog_Print(context->priv->log, WLOG_ERROR,
1358 "context->QoeFrameAcknowledge failed with error %" PRIu32
"", error);
1365 rdpgfx_send_map_surface_to_scaled_output_pdu(RdpgfxServerContext* context,
1368 if (!checkCapsAreExchanged(context))
1369 return CHANNEL_RC_NOT_INITIALIZED;
1370 wStream* s = rdpgfx_server_single_packet_new(context->priv->log,
1371 RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT, 20);
1375 WLog_Print(context->priv->log, WLOG_ERROR,
"rdpgfx_server_single_packet_new failed!");
1376 return CHANNEL_RC_NO_MEMORY;
1379 Stream_Write_UINT16(s, pdu->surfaceId);
1380 Stream_Write_UINT16(s, 0);
1381 Stream_Write_UINT32(s, pdu->outputOriginX);
1382 Stream_Write_UINT32(s, pdu->outputOriginY);
1383 Stream_Write_UINT32(s, pdu->targetWidth);
1384 Stream_Write_UINT32(s, pdu->targetHeight);
1385 return rdpgfx_server_single_packet_send(context, s);
1393 static UINT rdpgfx_server_receive_pdu(RdpgfxServerContext* context,
wStream* s)
1398 UINT error = CHANNEL_RC_OK;
1399 beg = Stream_GetPosition(s);
1401 if ((error = rdpgfx_read_header(s, &header)))
1403 WLog_Print(context->priv->log, WLOG_ERROR,
1404 "rdpgfx_read_header failed with error %" PRIu32
"!", error);
1408 #ifdef WITH_DEBUG_RDPGFX
1409 WLog_DBG(TAG,
"cmdId: %s (0x%04" PRIX16
") flags: 0x%04" PRIX16
" pduLength: %" PRIu32
"",
1410 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags, header.pduLength);
1413 switch (header.cmdId)
1415 case RDPGFX_CMDID_FRAMEACKNOWLEDGE:
1416 if ((error = rdpgfx_recv_frame_acknowledge_pdu(context, s)))
1417 WLog_Print(context->priv->log, WLOG_ERROR,
1418 "rdpgfx_recv_frame_acknowledge_pdu "
1419 "failed with error %" PRIu32
"!",
1424 case RDPGFX_CMDID_CACHEIMPORTOFFER:
1425 if ((error = rdpgfx_recv_cache_import_offer_pdu(context, s)))
1426 WLog_Print(context->priv->log, WLOG_ERROR,
1427 "rdpgfx_recv_cache_import_offer_pdu "
1428 "failed with error %" PRIu32
"!",
1433 case RDPGFX_CMDID_CAPSADVERTISE:
1434 if ((error = rdpgfx_recv_caps_advertise_pdu(context, s)))
1435 WLog_Print(context->priv->log, WLOG_ERROR,
1436 "rdpgfx_recv_caps_advertise_pdu "
1437 "failed with error %" PRIu32
"!",
1442 case RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE:
1443 if ((error = rdpgfx_recv_qoe_frame_acknowledge_pdu(context, s)))
1444 WLog_Print(context->priv->log, WLOG_ERROR,
1445 "rdpgfx_recv_qoe_frame_acknowledge_pdu "
1446 "failed with error %" PRIu32
"!",
1452 error = CHANNEL_RC_BAD_PROC;
1458 WLog_Print(context->priv->log, WLOG_ERROR,
1459 "Error while parsing GFX cmdId: %s (0x%04" PRIX16
")",
1460 rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
1464 end = Stream_GetPosition(s);
1466 if (end != (beg + header.pduLength))
1468 WLog_Print(context->priv->log, WLOG_ERROR,
1469 "Unexpected gfx pdu end: Actual: %" PRIuz
", Expected: %" PRIuz
"", end,
1470 (beg + header.pduLength));
1471 Stream_SetPosition(s, (beg + header.pduLength));
1477 static BOOL rdpgfx_server_close(RdpgfxServerContext* context);
1479 static DWORD WINAPI rdpgfx_server_thread_func(LPVOID arg)
1481 RdpgfxServerContext* context = (RdpgfxServerContext*)arg;
1482 WINPR_ASSERT(context);
1484 RdpgfxServerPrivate* priv = context->priv;
1487 HANDLE events[8] = { 0 };
1488 UINT error = CHANNEL_RC_OK;
1492 if (priv->ownThread)
1494 WINPR_ASSERT(priv->stopEvent);
1495 events[nCount++] = priv->stopEvent;
1498 WINPR_ASSERT(priv->channelEvent);
1499 events[nCount++] = priv->channelEvent;
1504 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1506 if (status == WAIT_FAILED)
1508 error = GetLastError();
1509 WLog_Print(context->priv->log, WLOG_ERROR,
1510 "WaitForMultipleObjects failed with error %" PRIu32
"", error);
1515 if (status == WAIT_OBJECT_0)
1518 if ((error = rdpgfx_server_handle_messages(context)))
1520 WLog_Print(context->priv->log, WLOG_ERROR,
1521 "rdpgfx_server_handle_messages failed with error %" PRIu32
"", error);
1526 if (error && context->rdpcontext)
1527 setChannelError(context->rdpcontext, error,
"rdpgfx_server_thread_func reported an error");
1533 static BOOL rdpgfx_server_open(RdpgfxServerContext* context)
1535 WINPR_ASSERT(context);
1536 RdpgfxServerPrivate* priv = context->priv;
1537 void* buffer = NULL;
1541 if (!priv->isOpened)
1543 PULONG pSessionId = NULL;
1544 DWORD BytesReturned = 0;
1545 priv->SessionId = WTS_CURRENT_SESSION;
1546 UINT32 channelId = 0;
1549 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
1550 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
1552 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSQuerySessionInformationA failed!");
1556 priv->SessionId = (DWORD)*pSessionId;
1557 WTSFreeMemory(pSessionId);
1558 priv->rdpgfx_channel = WTSVirtualChannelOpenEx(priv->SessionId, RDPGFX_DVC_CHANNEL_NAME,
1559 WTS_CHANNEL_OPTION_DYNAMIC);
1561 if (!priv->rdpgfx_channel)
1563 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelOpenEx failed!");
1567 channelId = WTSChannelGetIdByHandle(priv->rdpgfx_channel);
1569 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
1572 WLog_Print(context->priv->log, WLOG_ERROR,
"context->ChannelIdAssigned failed!");
1577 if (!WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualEventHandle, &buffer,
1579 (BytesReturned !=
sizeof(HANDLE)))
1581 WLog_Print(context->priv->log, WLOG_ERROR,
1582 "WTSVirtualChannelQuery failed "
1583 "or invalid returned size(%" PRIu32
")",
1587 WTSFreeMemory(buffer);
1592 CopyMemory(&priv->channelEvent, buffer,
sizeof(HANDLE));
1593 WTSFreeMemory(buffer);
1595 if (!(priv->zgfx = zgfx_context_new(TRUE)))
1597 WLog_Print(context->priv->log, WLOG_ERROR,
"Create zgfx context failed!");
1601 priv->isReady = FALSE;
1603 priv->activeCapSet = empty;
1604 if (priv->ownThread)
1606 if (!(priv->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
1608 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateEvent failed!");
1612 if (!(priv->thread =
1613 CreateThread(NULL, 0, rdpgfx_server_thread_func, (
void*)context, 0, NULL)))
1615 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThread failed!");
1620 priv->isOpened = TRUE;
1624 WLog_Print(context->priv->log, WLOG_ERROR,
"RDPGFX channel is already opened!");
1627 rdpgfx_server_close(context);
1631 BOOL rdpgfx_server_close(RdpgfxServerContext* context)
1633 WINPR_ASSERT(context);
1635 RdpgfxServerPrivate* priv = context->priv;
1638 if (priv->ownThread && priv->thread)
1640 (void)SetEvent(priv->stopEvent);
1642 if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
1644 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", GetLastError());
1648 (void)CloseHandle(priv->thread);
1649 (void)CloseHandle(priv->stopEvent);
1650 priv->thread = NULL;
1651 priv->stopEvent = NULL;
1654 zgfx_context_free(priv->zgfx);
1657 if (priv->rdpgfx_channel)
1659 (void)WTSVirtualChannelClose(priv->rdpgfx_channel);
1660 priv->rdpgfx_channel = NULL;
1663 priv->channelEvent = NULL;
1664 priv->isOpened = FALSE;
1665 priv->isReady = FALSE;
1667 priv->activeCapSet = empty;
1671 static BOOL rdpgfx_server_initialize(RdpgfxServerContext* context, BOOL externalThread)
1673 WINPR_ASSERT(context);
1674 WINPR_ASSERT(context->priv);
1676 if (context->priv->isOpened)
1678 WLog_Print(context->priv->log, WLOG_WARN,
1679 "Application error: RDPEGFX channel already initialized, "
1680 "calling in this state is not possible!");
1684 context->priv->ownThread = !externalThread;
1688 RdpgfxServerContext* rdpgfx_server_context_new(HANDLE vcm)
1690 RdpgfxServerContext* context = (RdpgfxServerContext*)calloc(1,
sizeof(RdpgfxServerContext));
1694 WLog_ERR(TAG,
"calloc failed!");
1699 context->Initialize = rdpgfx_server_initialize;
1700 context->Open = rdpgfx_server_open;
1701 context->Close = rdpgfx_server_close;
1702 context->ResetGraphics = rdpgfx_send_reset_graphics_pdu;
1703 context->StartFrame = rdpgfx_send_start_frame_pdu;
1704 context->EndFrame = rdpgfx_send_end_frame_pdu;
1705 context->SurfaceCommand = rdpgfx_send_surface_command;
1706 context->SurfaceFrameCommand = rdpgfx_send_surface_frame_command;
1707 context->DeleteEncodingContext = rdpgfx_send_delete_encoding_context_pdu;
1708 context->CreateSurface = rdpgfx_send_create_surface_pdu;
1709 context->DeleteSurface = rdpgfx_send_delete_surface_pdu;
1710 context->SolidFill = rdpgfx_send_solid_fill_pdu;
1711 context->SurfaceToSurface = rdpgfx_send_surface_to_surface_pdu;
1712 context->SurfaceToCache = rdpgfx_send_surface_to_cache_pdu;
1713 context->CacheToSurface = rdpgfx_send_cache_to_surface_pdu;
1714 context->CacheImportOffer = rdpgfx_process_cache_import_offer_pdu;
1715 context->CacheImportReply = rdpgfx_send_cache_import_reply_pdu;
1716 context->EvictCacheEntry = rdpgfx_send_evict_cache_entry_pdu;
1717 context->MapSurfaceToOutput = rdpgfx_send_map_surface_to_output_pdu;
1718 context->MapSurfaceToWindow = rdpgfx_send_map_surface_to_window_pdu;
1719 context->MapSurfaceToScaledOutput = rdpgfx_send_map_surface_to_scaled_output_pdu;
1720 context->MapSurfaceToScaledWindow = rdpgfx_send_map_surface_to_scaled_window_pdu;
1721 context->CapsAdvertise = NULL;
1722 context->CapsConfirm = rdpgfx_send_caps_confirm_pdu;
1723 context->FrameAcknowledge = NULL;
1724 context->QoeFrameAcknowledge = NULL;
1725 RdpgfxServerPrivate* priv = context->priv =
1726 (RdpgfxServerPrivate*)calloc(1,
sizeof(RdpgfxServerPrivate));
1730 WLog_ERR(TAG,
"calloc failed!");
1734 priv->log = WLog_Get(TAG);
1739 priv->input_stream = Stream_New(NULL, 4);
1741 if (!priv->input_stream)
1743 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_New failed!");
1747 priv->isOpened = FALSE;
1748 priv->isReady = FALSE;
1749 priv->ownThread = TRUE;
1752 priv->activeCapSet = empty;
1755 WINPR_PRAGMA_DIAG_PUSH
1756 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1757 rdpgfx_server_context_free(context);
1758 WINPR_PRAGMA_DIAG_POP
1762 void rdpgfx_server_context_free(RdpgfxServerContext* context)
1767 rdpgfx_server_close(context);
1770 Stream_Free(context->priv->input_stream, TRUE);
1772 free(context->priv);
1776 HANDLE rdpgfx_server_get_event_handle(RdpgfxServerContext* context)
1782 return context->priv->channelEvent;
1794 UINT rdpgfx_server_handle_messages(RdpgfxServerContext* context)
1796 DWORD BytesReturned = 0;
1797 void* buffer = NULL;
1798 UINT ret = CHANNEL_RC_OK;
1800 WINPR_ASSERT(context);
1801 WINPR_ASSERT(context->priv);
1803 RdpgfxServerPrivate* priv = context->priv;
1804 wStream* s = priv->input_stream;
1809 if (WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualChannelReady, &buffer,
1810 &BytesReturned) == FALSE)
1812 if (GetLastError() == ERROR_NO_DATA)
1813 return ERROR_NO_DATA;
1815 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelQuery failed");
1816 return ERROR_INTERNAL_ERROR;
1819 priv->isReady = *((BOOL*)buffer);
1820 WTSFreeMemory(buffer);
1826 Stream_SetPosition(s, 0);
1828 if (!WTSVirtualChannelRead(priv->rdpgfx_channel, 0, NULL, 0, &BytesReturned))
1830 if (GetLastError() == ERROR_NO_DATA)
1831 return ERROR_NO_DATA;
1833 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
1834 return ERROR_INTERNAL_ERROR;
1837 if (BytesReturned < 1)
1838 return CHANNEL_RC_OK;
1840 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
1842 WLog_Print(context->priv->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
1843 return CHANNEL_RC_NO_MEMORY;
1846 const size_t len = Stream_Capacity(s);
1847 if (len > UINT32_MAX)
1848 return ERROR_INTERNAL_ERROR;
1849 if (WTSVirtualChannelRead(priv->rdpgfx_channel, 0, Stream_BufferAs(s,
char), (UINT32)len,
1850 &BytesReturned) == FALSE)
1852 WLog_Print(context->priv->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
1853 return ERROR_INTERNAL_ERROR;
1856 Stream_SetLength(s, BytesReturned);
1857 Stream_SetPosition(s, 0);
1859 while (Stream_GetPosition(s) < Stream_Length(s))
1861 if ((ret = rdpgfx_server_receive_pdu(context, s)))
1863 WLog_Print(context->priv->log, WLOG_ERROR,
1864 "rdpgfx_server_receive_pdu "
1865 "failed with error %" PRIu32
"!",