Coverage Report

Created: 2026-11-10 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/rdpgfx/client/rdpgfx_main.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Graphics Pipeline Extension
4
 *
5
 * Copyright 2013-2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 * Copyright 2016 Thincast Technologies GmbH
9
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
#include <freerdp/config.h>
25
26
#include <winpr/assert.h>
27
#include <winpr/cast.h>
28
29
#include <winpr/crt.h>
30
#include <winpr/wlog.h>
31
#include <winpr/print.h>
32
#include <winpr/synch.h>
33
#include <winpr/thread.h>
34
#include <winpr/stream.h>
35
#include <winpr/sysinfo.h>
36
#include <winpr/cmdline.h>
37
#include <winpr/collections.h>
38
39
#include <freerdp/addin.h>
40
#include <freerdp/channels/log.h>
41
42
#include "rdpgfx_common.h"
43
#include "rdpgfx_codec.h"
44
45
#include "rdpgfx_main.h"
46
47
0
#define GFXTAG CHANNELS_TAG("rdpgfx.client")
48
49
#define RDPGFX_NUMBER_CAPSETS 0x100
50
51
typedef struct
52
{
53
  uint64_t cntGfxCodecID[RDPGFX_CODECID_MAX];
54
  uint64_t cntGfxCommandID[RDPGFX_CMDID_MAX + 1];
55
  uint64_t bytesGfxCodecID[RDPGFX_CODECID_MAX];
56
  uint64_t framesDecoded;
57
  uint64_t framesUnacknowledged;
58
} RdpgfxClientContextStats;
59
60
typedef struct
61
{
62
  RdpgfxClientContext common;
63
  RdpgfxClientContextStats stats;
64
} RdpgfxClientContextInt;
65
66
/* The name of a byte counter is the name of its codec ID with _BYTES added */
67
static char bytesGfxCodecNames[RDPGFX_CODECID_MAX][64];
68
static INIT_ONCE bytesGfxCodecNamesOnce = INIT_ONCE_STATIC_INIT;
69
70
static BOOL CALLBACK rdpgfx_stats_init_names(WINPR_ATTR_UNUSED PINIT_ONCE once,
71
                                             WINPR_ATTR_UNUSED PVOID param,
72
                                             WINPR_ATTR_UNUSED PVOID* context)
73
0
{
74
0
  for (UINT16 x = 0; x < RDPGFX_CODECID_MAX; x++)
75
0
    (void)_snprintf(bytesGfxCodecNames[x], sizeof(bytesGfxCodecNames[x]), "%s_BYTES",
76
0
                    rdpgfx_get_codec_id_string(x));
77
0
  return TRUE;
78
0
}
79
80
static const UINT32 MAX_SURFACE_SIZE = 32766;
81
static const UINT32 MAX_MONITOR_COUNT = 16;
82
83
size_t rdpgfx_stats_max_index(void)
84
0
{
85
0
  return sizeof(RdpgfxClientContextStats) / sizeof(uint64_t);
86
0
}
87
88
const char* rdpgfx_stats_name_for_index(size_t index)
89
0
{
90
0
  if (index < RDPGFX_CODECID_MAX)
91
0
    return rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
92
93
0
  index -= RDPGFX_CODECID_MAX;
94
0
  if (index < RDPGFX_CMDID_MAX)
95
0
    return rdpgfx_get_cmd_id_string(WINPR_ASSERTING_INT_CAST(UINT16, index));
96
0
  if (index == RDPGFX_CMDID_MAX)
97
0
    return "RDPGFX_CMDID_INVALID";
98
99
0
  index -= RDPGFX_CMDID_MAX + 1;
100
0
  if (index < RDPGFX_CODECID_MAX)
101
0
  {
102
0
    if (!InitOnceExecuteOnce(&bytesGfxCodecNamesOnce, rdpgfx_stats_init_names, nullptr,
103
0
                             nullptr))
104
0
      return "RDPGFX_STATS_UNUSED";
105
0
    return bytesGfxCodecNames[index];
106
0
  }
107
108
0
  index -= RDPGFX_CODECID_MAX;
109
0
  if (index == 0)
110
0
    return "RDPGFX_FRAMES_DECODED";
111
0
  if (index == 1)
112
0
    return "RDPGFX_FRAMES_UNACKNOWLEDGED";
113
0
  return "RDPGFX_STATS_UNUSED";
114
0
}
115
116
uint64_t rdpgfx_stats_value_for_index(RdpgfxClientContext* context, size_t index)
117
0
{
118
0
  WINPR_ASSERT(context);
119
120
0
  RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
121
122
0
  if (index < RDPGFX_CODECID_MAX)
123
0
    return intCtx->stats.cntGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
124
125
0
  index -= RDPGFX_CODECID_MAX;
126
0
  if (index <= RDPGFX_CMDID_MAX)
127
0
    return intCtx->stats.cntGfxCommandID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
128
129
0
  index -= RDPGFX_CMDID_MAX + 1;
130
0
  if (index < RDPGFX_CODECID_MAX)
131
0
    return intCtx->stats.bytesGfxCodecID[WINPR_ASSERTING_INT_CAST(UINT16, index)];
132
133
0
  index -= RDPGFX_CODECID_MAX;
134
0
  if (index == 0)
135
0
    return intCtx->stats.framesDecoded;
136
0
  if (index == 1)
137
0
    return intCtx->stats.framesUnacknowledged;
138
0
  return 0;
139
0
}
140
141
static void rdpgfx_codecid_event(RdpgfxClientContext* context, uint32_t index, uint32_t length)
142
0
{
143
0
  if (!context)
144
0
    return;
145
0
  RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
146
0
  WINPR_ASSERT(intCtx);
147
0
  if (index < RDPGFX_CODECID_MAX)
148
0
  {
149
0
    intCtx->stats.cntGfxCodecID[index]++;
150
0
    intCtx->stats.bytesGfxCodecID[index] += length;
151
0
  }
152
0
  else
153
0
  {
154
0
    RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
155
0
    WINPR_ASSERT(gfx);
156
0
    WLog_Print(gfx->base.log, WLOG_WARN, "Invalid event index %" PRIu32, index);
157
0
  }
158
0
}
159
160
static void rdpgfx_stats_cmdid_event(RdpgfxClientContext* context, uint32_t index)
161
0
{
162
0
  if (!context)
163
0
    return;
164
0
  RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
165
0
  WINPR_ASSERT(intCtx);
166
167
0
  if (index >= RDPGFX_CMDID_MAX)
168
0
  {
169
0
    RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
170
0
    WINPR_ASSERT(gfx);
171
0
    WLog_Print(gfx->base.log, WLOG_WARN, "Invalid codecID %" PRIu32, index);
172
0
    index = RDPGFX_CMDID_MAX;
173
0
  }
174
0
  intCtx->stats.cntGfxCommandID[index]++;
175
0
}
176
177
static void rdpgfx_stats_frame_decoded(RdpgfxClientContext* context)
178
0
{
179
0
  if (!context)
180
0
    return;
181
0
  RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
182
0
  intCtx->stats.framesDecoded++;
183
0
}
184
185
/* A copy: the plugin's own counter is reset before the stats are dumped on close */
186
static void rdpgfx_stats_frames_unacknowledged(RdpgfxClientContext* context, UINT32 frames)
187
0
{
188
0
  if (!context)
189
0
    return;
190
0
  RdpgfxClientContextInt* intCtx = (RdpgfxClientContextInt*)context;
191
0
  intCtx->stats.framesUnacknowledged = frames;
192
0
}
193
194
static BOOL delete_surface(const void* key, void* value, void* arg)
195
0
{
196
0
  const ULONG_PTR id = (ULONG_PTR)(key);
197
0
  if ((id < 1) || (id - 1 > UINT16_MAX))
198
0
    return FALSE;
199
0
  const UINT16 uid = WINPR_ASSERTING_INT_CAST(UINT16, id - 1);
200
201
0
  RdpgfxClientContext* context = arg;
202
0
  const RDPGFX_DELETE_SURFACE_PDU pdu = { .surfaceId = uid };
203
204
0
  WINPR_UNUSED(value);
205
206
0
  if (context)
207
0
  {
208
0
    UINT error = CHANNEL_RC_OK;
209
0
    IFCALLRET(context->DeleteSurface, error, context, &pdu);
210
211
0
    if (error)
212
0
    {
213
0
      RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
214
0
      WINPR_ASSERT(gfx);
215
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
216
0
                 "context->DeleteSurface failed with error %" PRIu32 "", error);
217
0
    }
218
0
  }
219
0
  return TRUE;
220
0
}
221
222
static void free_surfaces(RdpgfxClientContext* context, wHashTable* SurfaceTable)
223
0
{
224
0
  WINPR_ASSERT(context);
225
0
  if (!HashTable_Foreach(SurfaceTable, delete_surface, context))
226
0
  {
227
0
    RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
228
0
    WINPR_ASSERT(gfx);
229
0
    WLog_Print(gfx->base.log, WLOG_WARN, "delete_surface failed");
230
0
  }
231
0
}
232
233
static UINT evict_cache_slots(RdpgfxClientContext* context, UINT16 MaxCacheSlots, void** CacheSlots)
234
0
{
235
0
  UINT error = CHANNEL_RC_OK;
236
237
0
  WINPR_ASSERT(CacheSlots);
238
0
  for (UINT16 index = 0; index < MaxCacheSlots; index++)
239
0
  {
240
0
    if (CacheSlots[index])
241
0
    {
242
0
      const RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = { .cacheSlot = index + 1 };
243
244
0
      if (context && context->EvictCacheEntry)
245
0
      {
246
0
        const UINT rc = context->EvictCacheEntry(context, &pdu);
247
0
        if (rc != CHANNEL_RC_OK)
248
0
          error = rc;
249
0
      }
250
251
0
      CacheSlots[index] = nullptr;
252
0
    }
253
0
  }
254
0
  return error;
255
0
}
256
257
/**
258
 * Function description
259
 *
260
 * @return 0 on success, otherwise a Win32 error code
261
 */
262
static UINT rdpgfx_send_caps_advertise_pdu(RdpgfxClientContext* context,
263
                                           const RDPGFX_CAPS_ADVERTISE_PDU* pdu)
264
0
{
265
0
  UINT error = CHANNEL_RC_OK;
266
267
0
  WINPR_ASSERT(pdu);
268
0
  WINPR_ASSERT(context);
269
270
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
271
272
0
  if (!gfx || !gfx->base.listener_callback)
273
0
    return ERROR_BAD_ARGUMENTS;
274
275
0
  GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
276
277
0
  RDPGFX_HEADER header = { .flags = 0,
278
0
                         .cmdId = RDPGFX_CMDID_CAPSADVERTISE,
279
0
                         .pduLength = RDPGFX_HEADER_SIZE + 2 };
280
281
0
  for (UINT16 index = 0; index < pdu->capsSetCount; index++)
282
0
  {
283
0
    const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
284
0
    header.pduLength += RDPGFX_CAPSET_BASE_SIZE + capsSet->length;
285
0
  }
286
287
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCapsAdvertisePdu %" PRIu16 "", pdu->capsSetCount);
288
0
  wStream* s = Stream_New(nullptr, header.pduLength);
289
290
0
  if (!s)
291
0
  {
292
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
293
0
    return CHANNEL_RC_NO_MEMORY;
294
0
  }
295
296
0
  if ((error = rdpgfx_write_header(s, &header)))
297
0
    goto fail;
298
299
  /* RDPGFX_CAPS_ADVERTISE_PDU */
300
0
  Stream_Write_UINT16(s, pdu->capsSetCount); /* capsSetCount (2 bytes) */
301
302
0
  for (UINT16 index = 0; index < pdu->capsSetCount; index++)
303
0
  {
304
0
    const RDPGFX_CAPSET* capsSet = &(pdu->capsSets[index]);
305
306
0
    WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending %s [0x%08" PRIx32 "] flags=0x%08" PRIx32,
307
0
               rdpgfx_caps_version_str(capsSet->version), capsSet->version, capsSet->flags);
308
309
0
    Stream_Write_UINT32(s, capsSet->version); /* version (4 bytes) */
310
0
    Stream_Write_UINT32(s, capsSet->length);  /* capsDataLength (4 bytes) */
311
0
    Stream_Write_UINT32(s, capsSet->flags);   /* capsData (4 bytes) */
312
0
    if (capsSet->length < 4)
313
0
      goto fail;
314
0
    Stream_Zero(s, capsSet->length - 4);
315
0
  }
316
317
0
  Stream_SealLength(s);
318
0
  error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
319
0
                                   nullptr);
320
0
fail:
321
0
  Stream_Free(s, TRUE);
322
0
  return error;
323
0
}
324
325
static BOOL rdpgfx_is_capability_filtered(RDPGFX_PLUGIN* gfx, UINT32 caps)
326
0
{
327
0
  WINPR_ASSERT(gfx);
328
0
  const UINT32 filter =
329
0
      freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCapsFilter);
330
0
  const UINT32 capList[] = {
331
#if defined(WITH_GFX_AV1)
332
    RDPGFX_CAPVERSION_FRDP_1,
333
#endif
334
0
    RDPGFX_CAPVERSION_8,
335
0
    RDPGFX_CAPVERSION_81,
336
0
    RDPGFX_CAPVERSION_10,
337
0
    RDPGFX_CAPVERSION_101,
338
0
    RDPGFX_CAPVERSION_102,
339
0
    RDPGFX_CAPVERSION_103,
340
0
    RDPGFX_CAPVERSION_104,
341
0
    RDPGFX_CAPVERSION_105,
342
0
    RDPGFX_CAPVERSION_106,
343
0
    RDPGFX_CAPVERSION_106_ERR,
344
0
    RDPGFX_CAPVERSION_107
345
#if defined(WITH_GFX_AZURE)
346
    ,
347
    RDPGFX_CAPVERSION_111,
348
    RDPGFX_CAPVERSION_112,
349
    RDPGFX_CAPVERSION_113,
350
    RDPGFX_CAPVERSION_115
351
#endif
352
0
  };
353
354
0
  for (size_t x = 0; x < ARRAYSIZE(capList); x++)
355
0
  {
356
0
    if (caps == capList[x])
357
0
      return (filter & (1u << x)) != 0;
358
0
  }
359
360
0
  return TRUE;
361
0
}
362
363
WINPR_ATTR_NODISCARD
364
static RDPGFX_CAPSET* nextCapset(RDPGFX_CAPS_ADVERTISE_PDU* pdu, size_t count)
365
0
{
366
0
  WINPR_ASSERT(pdu);
367
0
  WINPR_ASSERT(pdu->capsSets);
368
0
  WINPR_ASSERT(count > 0);
369
0
  WINPR_ASSERT(pdu->capsSetCount < count);
370
0
  if (pdu->capsSetCount >= count)
371
0
    return nullptr;
372
0
  return &pdu->capsSets[pdu->capsSetCount++];
373
0
}
374
375
/**
376
 * Function description
377
 *
378
 * @return 0 on success, otherwise a Win32 error code
379
 */
380
static UINT rdpgfx_send_supported_caps(GENERIC_CHANNEL_CALLBACK* callback)
381
0
{
382
0
  RDPGFX_CAPSET capsSets[RDPGFX_NUMBER_CAPSETS] = WINPR_C_ARRAY_INIT;
383
384
0
  if (!callback)
385
0
    return ERROR_BAD_ARGUMENTS;
386
387
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
388
389
0
  if (!gfx)
390
0
    return ERROR_BAD_CONFIGURATION;
391
392
0
  RdpgfxClientContext* context = gfx->context;
393
394
0
  if (!context)
395
0
    return ERROR_BAD_CONFIGURATION;
396
397
0
  RDPGFX_CAPS_ADVERTISE_PDU pdu = { .capsSetCount = 0, .capsSets = capsSets };
398
399
0
  if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_8))
400
0
  {
401
0
    RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
402
0
    if (!capsSet)
403
0
      return ERROR_BAD_CONFIGURATION;
404
0
    capsSet->version = RDPGFX_CAPVERSION_8;
405
0
    capsSet->length = 4;
406
0
    capsSet->flags = 0;
407
408
0
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
409
0
      capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
410
411
    /* in CAPVERSION_8 the spec says that we should not have both
412
     * thinclient and smallcache (and thinclient implies a small cache)
413
     */
414
0
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) &&
415
0
        !freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
416
0
      capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
417
0
  }
418
419
0
  if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_81))
420
0
  {
421
0
    RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
422
0
    if (!capsSet)
423
0
      return ERROR_BAD_CONFIGURATION;
424
0
    capsSet->version = RDPGFX_CAPVERSION_81;
425
0
    capsSet->length = 4;
426
0
    capsSet->flags = 0;
427
428
0
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
429
0
      capsSet->flags |= RDPGFX_CAPS_FLAG_THINCLIENT;
430
431
0
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
432
0
      capsSet->flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
433
434
#ifdef WITH_GFX_H264
435
436
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264))
437
      capsSet->flags |= RDPGFX_CAPS_FLAG_AVC420_ENABLED;
438
439
#endif
440
0
  }
441
442
0
  UINT32 caps10Flags = 0;
443
0
  if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxH264) ||
444
0
      freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444))
445
0
  {
446
0
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache))
447
0
      caps10Flags |= RDPGFX_CAPS_FLAG_SMALL_CACHE;
448
449
#ifdef WITH_GFX_H264
450
451
    if (!freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxAVC444))
452
      caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
453
454
#else
455
0
    caps10Flags |= RDPGFX_CAPS_FLAG_AVC_DISABLED;
456
0
#endif
457
458
0
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_10))
459
0
    {
460
0
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
461
0
      if (!capsSet)
462
0
        return ERROR_BAD_CONFIGURATION;
463
0
      capsSet->version = RDPGFX_CAPVERSION_10;
464
0
      capsSet->length = 4;
465
0
      capsSet->flags = caps10Flags;
466
0
    }
467
468
0
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_101))
469
0
    {
470
0
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
471
0
      if (!capsSet)
472
0
        return ERROR_BAD_CONFIGURATION;
473
0
      capsSet->version = RDPGFX_CAPVERSION_101;
474
0
      capsSet->length = 0x10;
475
0
      capsSet->flags = 0;
476
0
    }
477
478
0
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_102))
479
0
    {
480
0
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
481
0
      if (!capsSet)
482
0
        return ERROR_BAD_CONFIGURATION;
483
0
      capsSet->version = RDPGFX_CAPVERSION_102;
484
0
      capsSet->length = 0x4;
485
0
      capsSet->flags = caps10Flags;
486
0
    }
487
488
0
    if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxThinClient))
489
0
    {
490
0
      if ((caps10Flags & RDPGFX_CAPS_FLAG_AVC_DISABLED) == 0)
491
0
        caps10Flags |= RDPGFX_CAPS_FLAG_AVC_THINCLIENT;
492
0
    }
493
494
0
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_103))
495
0
    {
496
0
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
497
0
      if (!capsSet)
498
0
        return ERROR_BAD_CONFIGURATION;
499
0
      capsSet->version = RDPGFX_CAPVERSION_103;
500
0
      capsSet->length = 0x4;
501
0
      capsSet->flags = caps10Flags & ~RDPGFX_CAPS_FLAG_SMALL_CACHE;
502
0
    }
503
504
0
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_104))
505
0
    {
506
0
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
507
0
      if (!capsSet)
508
0
        return ERROR_BAD_CONFIGURATION;
509
0
      capsSet->version = RDPGFX_CAPVERSION_104;
510
0
      capsSet->length = 0x4;
511
0
      capsSet->flags = caps10Flags;
512
0
    }
513
514
    /* The following capabilities expect support for image scaling.
515
     * Disable these for builds that do not have support for that.
516
     */
517
#if defined(WITH_CAIRO) || defined(WITH_SWSCALE)
518
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_105))
519
    {
520
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
521
      if (!capsSet)
522
        return ERROR_BAD_CONFIGURATION;
523
      capsSet->version = RDPGFX_CAPVERSION_105;
524
      capsSet->length = 0x4;
525
      capsSet->flags = caps10Flags;
526
    }
527
528
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106))
529
    {
530
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
531
      if (!capsSet)
532
        return ERROR_BAD_CONFIGURATION;
533
      capsSet->version = RDPGFX_CAPVERSION_106;
534
      capsSet->length = 0x4;
535
      capsSet->flags = caps10Flags;
536
    }
537
538
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_106_ERR))
539
    {
540
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
541
      if (!capsSet)
542
        return ERROR_BAD_CONFIGURATION;
543
      capsSet->version = RDPGFX_CAPVERSION_106_ERR;
544
      capsSet->length = 0x4;
545
      capsSet->flags = caps10Flags;
546
    }
547
#endif
548
549
0
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_107))
550
0
    {
551
0
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
552
0
      if (!capsSet)
553
0
        return ERROR_BAD_CONFIGURATION;
554
0
      capsSet->version = RDPGFX_CAPVERSION_107;
555
0
      capsSet->length = 0x4;
556
0
      capsSet->flags = caps10Flags;
557
0
#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
558
0
      capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
559
0
#endif
560
0
    }
561
562
#if defined(WITH_GFX_AZURE)
563
    const UINT32 caps[] = { RDPGFX_CAPVERSION_111, RDPGFX_CAPVERSION_112, RDPGFX_CAPVERSION_113,
564
                          RDPGFX_CAPVERSION_115 };
565
566
    for (size_t x = 0; x < ARRAYSIZE(caps); x++)
567
    {
568
      const UINT32 cap = caps[x];
569
      if (!rdpgfx_is_capability_filtered(gfx, cap))
570
      {
571
        RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
572
        if (!capsSet)
573
          return ERROR_BAD_CONFIGURATION;
574
        capsSet->version = cap;
575
        capsSet->length = 0x4;
576
        capsSet->flags = caps10Flags;
577
#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
578
      capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
579
#endif
580
      }
581
    }
582
#endif
583
0
  }
584
585
#if defined(WITH_GFX_AV1)
586
  if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1))
587
  {
588
    if (!rdpgfx_is_capability_filtered(gfx, RDPGFX_CAPVERSION_FRDP_1))
589
    {
590
      RDPGFX_CAPSET* capsSet = nextCapset(&pdu, ARRAYSIZE(capsSets));
591
      if (!capsSet)
592
        return ERROR_BAD_CONFIGURATION;
593
      capsSet->version = RDPGFX_CAPVERSION_FRDP_1;
594
      capsSet->length = 0x4;
595
596
      capsSet->flags = caps10Flags | RDPGFX_CAPS_FLAG_AV1_I444_SUPPORTED;
597
      const UINT32 profile =
598
          freerdp_settings_get_uint32(gfx->rdpcontext->settings, FreeRDP_GfxCodecAV1Profile);
599
      if (profile == 0)
600
        capsSet->flags |= RDPGFX_CAPS_FLAG_AV1_I444_DISABLED;
601
602
#if !defined(WITH_CAIRO) && !defined(WITH_SWSCALE)
603
      capsSet->flags |= RDPGFX_CAPS_FLAG_SCALEDMAP_DISABLE;
604
#endif
605
    }
606
  }
607
#endif
608
609
0
  return IFCALLRESULT(ERROR_BAD_CONFIGURATION, context->CapsAdvertise, context, &pdu);
610
0
}
611
612
/**
613
 * Function description
614
 *
615
 * @return 0 on success, otherwise a Win32 error code
616
 */
617
static UINT rdpgfx_recv_caps_confirm_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
618
0
{
619
0
  WINPR_ASSERT(callback);
620
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
621
622
0
  WINPR_ASSERT(gfx);
623
0
  RdpgfxClientContext* context = gfx->context;
624
625
0
  RDPGFX_CAPSET capsSet = WINPR_C_ARRAY_INIT;
626
0
  RDPGFX_CAPS_CONFIRM_PDU pdu = { .capsSet = &capsSet };
627
628
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
629
0
    return ERROR_INVALID_DATA;
630
631
0
  Stream_Read_UINT32(s, capsSet.version); /* version (4 bytes) */
632
0
  Stream_Read_UINT32(s, capsSet.length);  /* capsDataLength (4 bytes) */
633
0
  Stream_Read_UINT32(s, capsSet.flags);   /* capsData (4 bytes) */
634
0
  gfx->TotalDecodedFrames = 0;
635
0
  gfx->ConnectionCaps = capsSet;
636
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
637
0
             "RecvCapsConfirmPdu: version: %s [0x%08" PRIX32 "] flags: 0x%08" PRIX32 "",
638
0
             rdpgfx_caps_version_str(capsSet.version), capsSet.version, capsSet.flags);
639
640
0
  if (!context)
641
0
    return ERROR_BAD_CONFIGURATION;
642
643
0
  return IFCALLRESULT(CHANNEL_RC_OK, context->CapsConfirm, context, &pdu);
644
0
}
645
646
/**
647
 * Function description
648
 *
649
 * @return 0 on success, otherwise a Win32 error code
650
 */
651
static UINT rdpgfx_send_frame_acknowledge_pdu(RdpgfxClientContext* context,
652
                                              const RDPGFX_FRAME_ACKNOWLEDGE_PDU* pdu)
653
0
{
654
0
  UINT error = 0;
655
656
0
  if (!context || !pdu)
657
0
    return ERROR_BAD_ARGUMENTS;
658
659
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
660
661
0
  if (!gfx || !gfx->base.listener_callback)
662
0
    return ERROR_BAD_CONFIGURATION;
663
664
0
  GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
665
666
0
  if (!callback)
667
0
    return ERROR_BAD_CONFIGURATION;
668
669
0
  const RDPGFX_HEADER header = { .flags = 0,
670
0
                               .cmdId = RDPGFX_CMDID_FRAMEACKNOWLEDGE,
671
0
                               .pduLength = RDPGFX_HEADER_SIZE + 12 };
672
673
0
  WLog_Print(gfx->base.log, WLOG_TRACE, "SendFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
674
675
0
  wStream* s = Stream_New(nullptr, header.pduLength);
676
677
0
  if (!s)
678
0
  {
679
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
680
0
    return CHANNEL_RC_NO_MEMORY;
681
0
  }
682
683
0
  if ((error = rdpgfx_write_header(s, &header)))
684
0
    goto fail;
685
686
  /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
687
0
  Stream_Write_UINT32(s, pdu->queueDepth);         /* queueDepth (4 bytes) */
688
0
  Stream_Write_UINT32(s, pdu->frameId);            /* frameId (4 bytes) */
689
0
  Stream_Write_UINT32(s, pdu->totalFramesDecoded); /* totalFramesDecoded (4 bytes) */
690
0
  error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
691
0
                                   nullptr);
692
693
0
  if (error == CHANNEL_RC_OK) /* frame successfully acked */
694
0
  {
695
0
    gfx->UnacknowledgedFrames--;
696
0
    rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
697
0
  }
698
699
0
fail:
700
0
  Stream_Free(s, TRUE);
701
0
  return error;
702
0
}
703
704
static UINT rdpgfx_send_qoe_frame_acknowledge_pdu(RdpgfxClientContext* context,
705
                                                  const RDPGFX_QOE_FRAME_ACKNOWLEDGE_PDU* pdu)
706
0
{
707
0
  UINT error = CHANNEL_RC_OK;
708
0
  const RDPGFX_HEADER header = { .flags = 0,
709
0
                               .cmdId = RDPGFX_CMDID_QOEFRAMEACKNOWLEDGE,
710
0
                               .pduLength = RDPGFX_HEADER_SIZE + 12 };
711
712
0
  if (!context || !pdu)
713
0
    return ERROR_BAD_ARGUMENTS;
714
715
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
716
717
0
  if (!gfx || !gfx->base.listener_callback)
718
0
    return ERROR_BAD_CONFIGURATION;
719
720
0
  GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
721
722
0
  if (!callback)
723
0
    return ERROR_BAD_CONFIGURATION;
724
725
0
  WLog_Print(gfx->base.log, WLOG_TRACE, "SendQoeFrameAcknowledgePdu: %" PRIu32 "", pdu->frameId);
726
0
  wStream* s = Stream_New(nullptr, header.pduLength);
727
728
0
  if (!s)
729
0
  {
730
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
731
0
    return CHANNEL_RC_NO_MEMORY;
732
0
  }
733
734
0
  if ((error = rdpgfx_write_header(s, &header)))
735
0
    goto fail;
736
737
  /* RDPGFX_FRAME_ACKNOWLEDGE_PDU */
738
0
  Stream_Write_UINT32(s, pdu->frameId);
739
0
  Stream_Write_UINT32(s, pdu->timestamp);
740
0
  Stream_Write_UINT16(s, pdu->timeDiffSE);
741
0
  Stream_Write_UINT16(s, pdu->timeDiffEDR);
742
0
  error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
743
0
                                   nullptr);
744
0
fail:
745
0
  Stream_Free(s, TRUE);
746
0
  return error;
747
0
}
748
749
/**
750
 * Function description
751
 *
752
 * @return 0 on success, otherwise a Win32 error code
753
 */
754
static UINT rdpgfx_recv_reset_graphics_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
755
0
{
756
0
  RDPGFX_RESET_GRAPHICS_PDU pdu = WINPR_C_ARRAY_INIT;
757
0
  WINPR_ASSERT(callback);
758
759
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
760
761
0
  WINPR_ASSERT(gfx);
762
763
0
  RdpgfxClientContext* context = gfx->context;
764
0
  UINT error = CHANNEL_RC_OK;
765
0
  GraphicsResetEventArgs graphicsReset = WINPR_C_ARRAY_INIT;
766
767
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
768
0
    return ERROR_INVALID_DATA;
769
770
0
  Stream_Read_UINT32(s, pdu.width);        /* width (4 bytes) */
771
0
  Stream_Read_UINT32(s, pdu.height);       /* height (4 bytes) */
772
0
  Stream_Read_UINT32(s, pdu.monitorCount); /* monitorCount (4 bytes) */
773
774
0
  if ((pdu.width == 0) || (pdu.height == 0) || (pdu.monitorCount == 0))
775
0
  {
776
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
777
0
               "context->ResetGraphics Width[%" PRIu32 "], Height[%" PRIu32
778
0
               "] or monitorCount[%" PRIu32 " is 0",
779
0
               pdu.width, pdu.height, pdu.monitorCount);
780
0
    return ERROR_INVALID_DATA;
781
0
  }
782
0
  if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE) ||
783
0
      (pdu.monitorCount > MAX_MONITOR_COUNT))
784
0
  {
785
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
786
0
               "context->ResetGraphics Width[%" PRIu32 "], Height[%" PRIu32 "] > %" PRIu32
787
0
               " or monitorCount[%" PRIu32 " > %" PRIu32,
788
0
               pdu.width, pdu.height, MAX_SURFACE_SIZE, pdu.monitorCount, MAX_MONITOR_COUNT);
789
0
    return ERROR_INVALID_DATA;
790
0
  }
791
792
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.monitorCount, 20ull))
793
0
    return ERROR_INVALID_DATA;
794
795
0
  pdu.monitorDefArray = (MONITOR_DEF*)calloc(pdu.monitorCount, sizeof(MONITOR_DEF));
796
797
0
  if (!pdu.monitorDefArray)
798
0
  {
799
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
800
0
    return CHANNEL_RC_NO_MEMORY;
801
0
  }
802
803
0
  for (UINT32 index = 0; index < pdu.monitorCount; index++)
804
0
  {
805
0
    MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
806
0
    Stream_Read_INT32(s, monitor->left);   /* left (4 bytes) */
807
0
    Stream_Read_INT32(s, monitor->top);    /* top (4 bytes) */
808
0
    Stream_Read_INT32(s, monitor->right);  /* right (4 bytes) */
809
0
    Stream_Read_INT32(s, monitor->bottom); /* bottom (4 bytes) */
810
0
    Stream_Read_UINT32(s, monitor->flags); /* flags (4 bytes) */
811
0
  }
812
813
0
  const size_t size = (RDPGFX_HEADER_SIZE + 12ULL + (pdu.monitorCount * 20ULL));
814
0
  if (size > 340)
815
0
  {
816
0
    free(pdu.monitorDefArray);
817
0
    return CHANNEL_RC_NULL_DATA;
818
0
  }
819
0
  const size_t pad = 340ULL - size;
820
821
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, (size_t)pad))
822
0
  {
823
0
    free(pdu.monitorDefArray);
824
0
    return CHANNEL_RC_NO_MEMORY;
825
0
  }
826
827
0
  Stream_Seek(s, pad); /* pad (total size is 340 bytes) */
828
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
829
0
             "RecvResetGraphicsPdu: width: %" PRIu32 " height: %" PRIu32 " count: %" PRIu32 "",
830
0
             pdu.width, pdu.height, pdu.monitorCount);
831
832
0
  for (UINT32 index = 0; index < pdu.monitorCount; index++)
833
0
  {
834
0
    MONITOR_DEF* monitor = &(pdu.monitorDefArray[index]);
835
0
    WLog_Print(gfx->base.log, WLOG_TRACE,
836
0
               "RecvResetGraphicsPdu: monitor left:%" PRIi32 " top:%" PRIi32 " right:%" PRIi32
837
0
               " bottom:%" PRIi32 " flags:0x%" PRIx32 "",
838
0
               monitor->left, monitor->top, monitor->right, monitor->bottom, monitor->flags);
839
0
  }
840
841
0
  if (context)
842
0
  {
843
0
    IFCALLRET(context->ResetGraphics, error, context, &pdu);
844
845
0
    if (error)
846
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
847
0
                 "context->ResetGraphics failed with error %" PRIu32 "", error);
848
0
  }
849
850
0
  free(pdu.monitorDefArray);
851
852
  /* some listeners may be interested (namely the display channel) */
853
0
  if (error == CHANNEL_RC_OK)
854
0
  {
855
0
    EventArgsInit(&graphicsReset, "libfreerdp");
856
0
    graphicsReset.width = pdu.width;
857
0
    graphicsReset.height = pdu.height;
858
0
    if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
859
0
      return ERROR_INTERNAL_ERROR;
860
0
  }
861
0
  return error;
862
0
}
863
864
/**
865
 * Function description
866
 *
867
 * @return 0 on success, otherwise a Win32 error code
868
 */
869
static UINT rdpgfx_recv_evict_cache_entry_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
870
0
{
871
0
  RDPGFX_EVICT_CACHE_ENTRY_PDU pdu = WINPR_C_ARRAY_INIT;
872
0
  WINPR_ASSERT(callback);
873
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
874
0
  WINPR_ASSERT(gfx);
875
0
  RdpgfxClientContext* context = gfx->context;
876
0
  UINT error = CHANNEL_RC_OK;
877
878
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
879
0
    return ERROR_INVALID_DATA;
880
881
0
  Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
882
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvEvictCacheEntryPdu: cacheSlot: %" PRIu16 "",
883
0
             pdu.cacheSlot);
884
885
0
  if (context)
886
0
  {
887
0
    IFCALLRET(context->EvictCacheEntry, error, context, &pdu);
888
889
0
    if (error)
890
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
891
0
                 "context->EvictCacheEntry failed with error %" PRIu32 "", error);
892
0
  }
893
894
0
  return error;
895
0
}
896
897
/**
898
 * Function description
899
 *
900
 * @return 0 on success, otherwise a Win32 error code
901
 */
902
static UINT rdpgfx_save_persistent_cache(RDPGFX_PLUGIN* gfx)
903
0
{
904
0
  UINT error = CHANNEL_RC_OK;
905
906
0
  WINPR_ASSERT(gfx);
907
0
  WINPR_ASSERT(gfx->rdpcontext);
908
0
  rdpSettings* settings = gfx->rdpcontext->settings;
909
0
  RdpgfxClientContext* context = gfx->context;
910
911
0
  WINPR_ASSERT(context);
912
0
  WINPR_ASSERT(settings);
913
914
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
915
0
    return CHANNEL_RC_OK;
916
917
0
  const char* BitmapCachePersistFile =
918
0
      freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
919
0
  if (!BitmapCachePersistFile)
920
0
    return CHANNEL_RC_OK;
921
922
0
  if (!context->ExportCacheEntry)
923
0
    return CHANNEL_RC_INITIALIZATION_ERROR;
924
925
0
  rdpPersistentCache* persistent = persistent_cache_new();
926
927
0
  if (!persistent)
928
0
    return CHANNEL_RC_NO_MEMORY;
929
930
0
  if (persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, 3) < 1)
931
0
  {
932
0
    error = CHANNEL_RC_INITIALIZATION_ERROR;
933
0
    goto fail;
934
0
  }
935
936
0
  for (UINT16 idx = 0; idx < gfx->MaxCacheSlots; idx++)
937
0
  {
938
0
    if (gfx->CacheSlots[idx])
939
0
    {
940
0
      const UINT16 cacheSlot = idx;
941
0
      PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
942
943
0
      if (context->ExportCacheEntry(context, cacheSlot, &cacheEntry) != CHANNEL_RC_OK)
944
0
        continue;
945
946
0
      if (persistent_cache_write_entry(persistent, &cacheEntry) < 0)
947
0
        goto fail;
948
0
    }
949
0
  }
950
951
0
  persistent_cache_free(persistent);
952
953
0
  return error;
954
0
fail:
955
0
  persistent_cache_free(persistent);
956
0
  return error;
957
0
}
958
959
/**
960
 * Function description
961
 *
962
 * @return 0 on success, otherwise a Win32 error code
963
 */
964
static UINT rdpgfx_send_cache_import_offer_pdu(RdpgfxClientContext* context,
965
                                               const RDPGFX_CACHE_IMPORT_OFFER_PDU* pdu)
966
0
{
967
0
  UINT error = CHANNEL_RC_OK;
968
969
0
  if (!context || !pdu)
970
0
    return ERROR_BAD_ARGUMENTS;
971
972
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
973
974
0
  if (!gfx || !gfx->base.listener_callback)
975
0
    return ERROR_BAD_CONFIGURATION;
976
977
0
  GENERIC_CHANNEL_CALLBACK* callback = gfx->base.listener_callback->channel_callback;
978
979
0
  if (!callback)
980
0
    return ERROR_BAD_CONFIGURATION;
981
982
0
  const RDPGFX_HEADER header = { .flags = 0,
983
0
                               .cmdId = RDPGFX_CMDID_CACHEIMPORTOFFER,
984
0
                               .pduLength =
985
0
                                   RDPGFX_HEADER_SIZE + 2ul + pdu->cacheEntriesCount * 12ul };
986
987
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "SendCacheImportOfferPdu: cacheEntriesCount: %" PRIu16 "",
988
0
             pdu->cacheEntriesCount);
989
0
  wStream* s = Stream_New(nullptr, header.pduLength);
990
991
0
  if (!s)
992
0
  {
993
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
994
0
    return CHANNEL_RC_NO_MEMORY;
995
0
  }
996
997
0
  if ((error = rdpgfx_write_header(s, &header)))
998
0
    goto fail;
999
1000
0
  if (pdu->cacheEntriesCount <= 0)
1001
0
  {
1002
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Invalid cacheEntriesCount: %" PRIu16 "",
1003
0
               pdu->cacheEntriesCount);
1004
0
    error = ERROR_INVALID_DATA;
1005
0
    goto fail;
1006
0
  }
1007
1008
  /* cacheEntriesCount (2 bytes) */
1009
0
  Stream_Write_UINT16(s, pdu->cacheEntriesCount);
1010
1011
0
  for (UINT16 index = 0; index < pdu->cacheEntriesCount; index++)
1012
0
  {
1013
0
    const RDPGFX_CACHE_ENTRY_METADATA* cacheEntry = &(pdu->cacheEntries[index]);
1014
0
    Stream_Write_UINT64(s, cacheEntry->cacheKey);     /* cacheKey (8 bytes) */
1015
0
    Stream_Write_UINT32(s, cacheEntry->bitmapLength); /* bitmapLength (4 bytes) */
1016
0
  }
1017
1018
0
  error = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
1019
0
                                   nullptr);
1020
1021
0
fail:
1022
0
  Stream_Free(s, TRUE);
1023
0
  return error;
1024
0
}
1025
1026
/**
1027
 * Function description
1028
 *
1029
 * @return 0 on success, otherwise a Win32 error code
1030
 */
1031
static UINT rdpgfx_send_cache_offer(RDPGFX_PLUGIN* gfx)
1032
0
{
1033
0
  int count = 0;
1034
0
  UINT error = CHANNEL_RC_OK;
1035
0
  PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1036
0
  RDPGFX_CACHE_IMPORT_OFFER_PDU* offer = nullptr;
1037
1038
0
  WINPR_ASSERT(gfx);
1039
0
  WINPR_ASSERT(gfx->rdpcontext);
1040
1041
0
  RdpgfxClientContext* context = gfx->context;
1042
0
  rdpSettings* settings = gfx->rdpcontext->settings;
1043
1044
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1045
0
    return CHANNEL_RC_OK;
1046
1047
0
  const char* BitmapCachePersistFile =
1048
0
      freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1049
0
  if (!BitmapCachePersistFile)
1050
0
    return CHANNEL_RC_OK;
1051
1052
0
  rdpPersistentCache* persistent = persistent_cache_new();
1053
1054
0
  if (!persistent)
1055
0
    return CHANNEL_RC_NO_MEMORY;
1056
1057
0
  if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1058
0
  {
1059
0
    error = CHANNEL_RC_INITIALIZATION_ERROR;
1060
0
    goto fail;
1061
0
  }
1062
1063
0
  if (persistent_cache_get_version(persistent) != 3)
1064
0
  {
1065
0
    error = ERROR_INVALID_DATA;
1066
0
    goto fail;
1067
0
  }
1068
1069
0
  count = persistent_cache_get_count(persistent);
1070
0
  if (count < 0)
1071
0
  {
1072
0
    error = ERROR_INVALID_DATA;
1073
0
    goto fail;
1074
0
  }
1075
1076
0
  if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT)
1077
0
    count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1;
1078
1079
0
  if (count > gfx->MaxCacheSlots)
1080
0
    count = gfx->MaxCacheSlots;
1081
1082
0
  offer = (RDPGFX_CACHE_IMPORT_OFFER_PDU*)calloc(1, sizeof(RDPGFX_CACHE_IMPORT_OFFER_PDU));
1083
0
  if (!offer)
1084
0
  {
1085
0
    error = CHANNEL_RC_NO_MEMORY;
1086
0
    goto fail;
1087
0
  }
1088
1089
0
  WINPR_ASSERT(count <= UINT16_MAX);
1090
0
  offer->cacheEntriesCount = (UINT16)count;
1091
1092
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "Sending Cache Import Offer: %d", count);
1093
1094
0
  for (int idx = 0; idx < count; idx++)
1095
0
  {
1096
0
    if (persistent_cache_read_entry(persistent, &entry) < 1)
1097
0
    {
1098
0
      error = ERROR_INVALID_DATA;
1099
0
      goto fail;
1100
0
    }
1101
1102
0
    offer->cacheEntries[idx].cacheKey = entry.key64;
1103
0
    offer->cacheEntries[idx].bitmapLength = entry.size;
1104
0
  }
1105
1106
0
  if (offer->cacheEntriesCount > 0)
1107
0
  {
1108
0
    error = rdpgfx_send_cache_import_offer_pdu(context, offer);
1109
0
    if (error != CHANNEL_RC_OK)
1110
0
    {
1111
0
      WLog_Print(gfx->base.log, WLOG_ERROR, "Failed to send cache import offer PDU");
1112
0
      goto fail;
1113
0
    }
1114
0
  }
1115
1116
0
fail:
1117
0
  persistent_cache_free(persistent);
1118
0
  free(offer);
1119
0
  return error;
1120
0
}
1121
1122
/**
1123
 * Function description
1124
 *
1125
 * @return 0 on success, otherwise a Win32 error code
1126
 */
1127
static UINT rdpgfx_load_cache_import_reply(RDPGFX_PLUGIN* gfx,
1128
                                           const RDPGFX_CACHE_IMPORT_REPLY_PDU* reply)
1129
0
{
1130
0
  UINT error = CHANNEL_RC_OK;
1131
0
  rdpPersistentCache* persistent = nullptr;
1132
0
  WINPR_ASSERT(gfx);
1133
0
  WINPR_ASSERT(gfx->rdpcontext);
1134
0
  rdpSettings* settings = gfx->rdpcontext->settings;
1135
0
  RdpgfxClientContext* context = gfx->context;
1136
1137
0
  WINPR_ASSERT(settings);
1138
0
  WINPR_ASSERT(reply);
1139
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
1140
0
    return CHANNEL_RC_OK;
1141
1142
0
  const char* BitmapCachePersistFile =
1143
0
      freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
1144
0
  if (!BitmapCachePersistFile)
1145
0
    return CHANNEL_RC_OK;
1146
1147
0
  persistent = persistent_cache_new();
1148
1149
0
  if (!persistent)
1150
0
    return CHANNEL_RC_NO_MEMORY;
1151
1152
0
  if (persistent_cache_open(persistent, BitmapCachePersistFile, FALSE, 3) < 1)
1153
0
  {
1154
0
    error = CHANNEL_RC_INITIALIZATION_ERROR;
1155
0
    goto fail;
1156
0
  }
1157
1158
0
  if (persistent_cache_get_version(persistent) != 3)
1159
0
  {
1160
0
    error = ERROR_INVALID_DATA;
1161
0
    goto fail;
1162
0
  }
1163
1164
0
  int count = persistent_cache_get_count(persistent);
1165
1166
0
  count = (count < reply->importedEntriesCount) ? count : reply->importedEntriesCount;
1167
1168
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "Receiving Cache Import Reply: %d", count);
1169
1170
0
  for (int idx = 0; idx < count; idx++)
1171
0
  {
1172
0
    PERSISTENT_CACHE_ENTRY entry = WINPR_C_ARRAY_INIT;
1173
0
    if (persistent_cache_read_entry(persistent, &entry) < 1)
1174
0
    {
1175
0
      error = ERROR_INVALID_DATA;
1176
0
      goto fail;
1177
0
    }
1178
1179
0
    const UINT16 cacheSlot = reply->cacheSlots[idx];
1180
0
    if (context && context->ImportCacheEntry)
1181
0
    {
1182
0
      error = context->ImportCacheEntry(context, cacheSlot, &entry);
1183
0
      if (error != CHANNEL_RC_OK)
1184
0
        break;
1185
0
    }
1186
0
  }
1187
1188
0
  persistent_cache_free(persistent);
1189
1190
0
  return error;
1191
0
fail:
1192
0
  persistent_cache_free(persistent);
1193
0
  return error;
1194
0
}
1195
1196
/**
1197
 * Function description
1198
 *
1199
 * @return 0 on success, otherwise a Win32 error code
1200
 */
1201
static UINT rdpgfx_recv_cache_import_reply_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1202
0
{
1203
0
  RDPGFX_CACHE_IMPORT_REPLY_PDU pdu = WINPR_C_ARRAY_INIT;
1204
0
  WINPR_ASSERT(callback);
1205
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1206
0
  WINPR_ASSERT(gfx);
1207
0
  RdpgfxClientContext* context = gfx->context;
1208
0
  UINT error = CHANNEL_RC_OK;
1209
1210
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1211
0
    return ERROR_INVALID_DATA;
1212
1213
0
  Stream_Read_UINT16(s, pdu.importedEntriesCount); /* cacheSlot (2 bytes) */
1214
1215
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.importedEntriesCount,
1216
0
                                                  2ull))
1217
0
    return ERROR_INVALID_DATA;
1218
1219
0
  if (pdu.importedEntriesCount > RDPGFX_CACHE_ENTRY_MAX_COUNT)
1220
0
    return ERROR_INVALID_DATA;
1221
1222
0
  for (UINT16 idx = 0; idx < pdu.importedEntriesCount; idx++)
1223
0
  {
1224
0
    Stream_Read_UINT16(s, pdu.cacheSlots[idx]); /* cacheSlot (2 bytes) */
1225
0
  }
1226
1227
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1228
0
             "RecvCacheImportReplyPdu: importedEntriesCount: %" PRIu16 "",
1229
0
             pdu.importedEntriesCount);
1230
1231
0
  error = rdpgfx_load_cache_import_reply(gfx, &pdu);
1232
1233
0
  if (error)
1234
0
  {
1235
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
1236
0
               "rdpgfx_load_cache_import_reply failed with error %" PRIu32 "", error);
1237
0
    return error;
1238
0
  }
1239
1240
0
  if (context)
1241
0
  {
1242
0
    IFCALLRET(context->CacheImportReply, error, context, &pdu);
1243
1244
0
    if (error)
1245
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1246
0
                 "context->CacheImportReply failed with error %" PRIu32 "", error);
1247
0
  }
1248
1249
0
  return error;
1250
0
}
1251
1252
/**
1253
 * Function description
1254
 *
1255
 * @return 0 on success, otherwise a Win32 error code
1256
 */
1257
static UINT rdpgfx_recv_create_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1258
0
{
1259
0
  RDPGFX_CREATE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1260
0
  WINPR_ASSERT(callback);
1261
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1262
0
  WINPR_ASSERT(gfx);
1263
0
  RdpgfxClientContext* context = gfx->context;
1264
0
  UINT error = CHANNEL_RC_OK;
1265
1266
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 7))
1267
0
    return ERROR_INVALID_DATA;
1268
1269
0
  Stream_Read_UINT16(s, pdu.surfaceId);  /* surfaceId (2 bytes) */
1270
0
  Stream_Read_UINT16(s, pdu.width);      /* width (2 bytes) */
1271
0
  Stream_Read_UINT16(s, pdu.height);     /* height (2 bytes) */
1272
0
  Stream_Read_UINT8(s, pdu.pixelFormat); /* RDPGFX_PIXELFORMAT (1 byte) */
1273
1274
0
  if ((pdu.width == 0) || (pdu.height == 0))
1275
0
  {
1276
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
1277
0
               "context->RecvCreateSurfacePdu Width[%" PRIu32 "], Height[%" PRIu32 "] is 0",
1278
0
               pdu.width, pdu.height);
1279
0
    return ERROR_INVALID_DATA;
1280
0
  }
1281
0
  if ((pdu.width > MAX_SURFACE_SIZE) || (pdu.height > MAX_SURFACE_SIZE))
1282
0
  {
1283
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
1284
0
               "context->RecvCreateSurfacePdu Width[%" PRIu32 "], Height[%" PRIu32
1285
0
               "] > %" PRIu32,
1286
0
               pdu.width, pdu.height, MAX_SURFACE_SIZE);
1287
0
    return ERROR_INVALID_DATA;
1288
0
  }
1289
1290
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
1291
0
             "RecvCreateSurfacePdu: surfaceId: %" PRIu16 " width: %" PRIu16 " height: %" PRIu16
1292
0
             " pixelFormat: 0x%02" PRIX8 "",
1293
0
             pdu.surfaceId, pdu.width, pdu.height, pdu.pixelFormat);
1294
1295
0
  if (context)
1296
0
  {
1297
    /* create surface PDU sometimes happens for surface ID that are already in use and have not
1298
     * been removed yet. Ensure that there is no surface with the new ID by trying to remove it
1299
     * manually.
1300
     */
1301
0
    RDPGFX_DELETE_SURFACE_PDU deletePdu = { pdu.surfaceId };
1302
0
    const UINT drc = IFCALLRESULT(CHANNEL_RC_OK, context->DeleteSurface, context, &deletePdu);
1303
0
    if (drc != CHANNEL_RC_OK)
1304
0
      WLog_Print(gfx->base.log, WLOG_WARN,
1305
0
                 "context->DeleteSurface failed with error %" PRIu32 ", ignoring", drc);
1306
1307
0
    IFCALLRET(context->CreateSurface, error, context, &pdu);
1308
1309
0
    if (error)
1310
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1311
0
                 "context->CreateSurface failed with error %" PRIu32 "", error);
1312
0
  }
1313
1314
0
  return error;
1315
0
}
1316
1317
/**
1318
 * Function description
1319
 *
1320
 * @return 0 on success, otherwise a Win32 error code
1321
 */
1322
static UINT rdpgfx_recv_delete_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1323
0
{
1324
0
  RDPGFX_DELETE_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1325
0
  WINPR_ASSERT(callback);
1326
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1327
0
  WINPR_ASSERT(gfx);
1328
0
  RdpgfxClientContext* context = gfx->context;
1329
0
  UINT error = CHANNEL_RC_OK;
1330
1331
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 2))
1332
0
    return ERROR_INVALID_DATA;
1333
1334
0
  Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1335
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "RecvDeleteSurfacePdu: surfaceId: %" PRIu16 "",
1336
0
             pdu.surfaceId);
1337
1338
0
  if (context)
1339
0
  {
1340
0
    IFCALLRET(context->DeleteSurface, error, context, &pdu);
1341
1342
0
    if (error)
1343
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1344
0
                 "context->DeleteSurface failed with error %" PRIu32 "", error);
1345
0
  }
1346
1347
0
  return error;
1348
0
}
1349
1350
/**
1351
 * Function description
1352
 *
1353
 * @return 0 on success, otherwise a Win32 error code
1354
 */
1355
static UINT rdpgfx_recv_start_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1356
0
{
1357
0
  RDPGFX_START_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1358
0
  WINPR_ASSERT(callback);
1359
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1360
0
  WINPR_ASSERT(gfx);
1361
0
  RdpgfxClientContext* context = gfx->context;
1362
0
  UINT error = CHANNEL_RC_OK;
1363
1364
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_START_FRAME_PDU_SIZE))
1365
0
    return ERROR_INVALID_DATA;
1366
1367
0
  Stream_Read_UINT32(s, pdu.timestamp); /* timestamp (4 bytes) */
1368
0
  Stream_Read_UINT32(s, pdu.frameId);   /* frameId (4 bytes) */
1369
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1370
0
             "RecvStartFramePdu: frameId: %" PRIu32 " timestamp: 0x%08" PRIX32 "", pdu.frameId,
1371
0
             pdu.timestamp);
1372
0
  gfx->StartDecodingTime = GetTickCount64();
1373
1374
0
  if (context)
1375
0
  {
1376
0
    IFCALLRET(context->StartFrame, error, context, &pdu);
1377
1378
0
    if (error)
1379
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1380
0
                 "context->StartFrame failed with error %" PRIu32 "", error);
1381
0
  }
1382
1383
0
  gfx->UnacknowledgedFrames++;
1384
0
  rdpgfx_stats_frames_unacknowledged(context, gfx->UnacknowledgedFrames);
1385
0
  return error;
1386
0
}
1387
1388
/**
1389
 * Function description
1390
 *
1391
 * @return 0 on success, otherwise a Win32 error code
1392
 */
1393
static UINT rdpgfx_recv_end_frame_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1394
0
{
1395
0
  RDPGFX_END_FRAME_PDU pdu = WINPR_C_ARRAY_INIT;
1396
0
  RDPGFX_FRAME_ACKNOWLEDGE_PDU ack = WINPR_C_ARRAY_INIT;
1397
0
  WINPR_ASSERT(callback);
1398
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1399
0
  WINPR_ASSERT(gfx);
1400
0
  RdpgfxClientContext* context = gfx->context;
1401
0
  UINT error = CHANNEL_RC_OK;
1402
1403
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_END_FRAME_PDU_SIZE))
1404
0
    return ERROR_INVALID_DATA;
1405
1406
0
  Stream_Read_UINT32(s, pdu.frameId); /* frameId (4 bytes) */
1407
0
  WLog_Print(gfx->base.log, WLOG_TRACE, "RecvEndFramePdu: frameId: %" PRIu32 "", pdu.frameId);
1408
1409
0
  const UINT64 start = GetTickCount64();
1410
0
  if (context)
1411
0
  {
1412
0
    IFCALLRET(context->EndFrame, error, context, &pdu);
1413
1414
0
    if (error)
1415
0
    {
1416
0
      WLog_Print(gfx->base.log, WLOG_ERROR, "context->EndFrame failed with error %" PRIu32 "",
1417
0
                 error);
1418
0
      return error;
1419
0
    }
1420
0
  }
1421
0
  const UINT64 end = GetTickCount64();
1422
0
  const UINT64 EndFrameTime = end - start;
1423
0
  gfx->TotalDecodedFrames++;
1424
0
  rdpgfx_stats_frame_decoded(context);
1425
1426
0
  if (!gfx->sendFrameAcks)
1427
0
    return error;
1428
1429
0
  ack.frameId = pdu.frameId;
1430
0
  ack.totalFramesDecoded = gfx->TotalDecodedFrames;
1431
1432
0
  if (gfx->suspendFrameAcks)
1433
0
  {
1434
0
    ack.queueDepth = SUSPEND_FRAME_ACKNOWLEDGEMENT;
1435
1436
0
    if (gfx->TotalDecodedFrames == 1)
1437
0
      if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1438
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
1439
0
                   "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32 "",
1440
0
                   error);
1441
0
  }
1442
0
  else
1443
0
  {
1444
0
    ack.queueDepth = QUEUE_DEPTH_UNAVAILABLE;
1445
1446
0
    if ((error = rdpgfx_send_frame_acknowledge_pdu(context, &ack)))
1447
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1448
0
                 "rdpgfx_send_frame_acknowledge_pdu failed with error %" PRIu32 "", error);
1449
0
  }
1450
1451
0
  switch (gfx->ConnectionCaps.version)
1452
0
  {
1453
#if defined(WITH_GFX_AV1)
1454
    case RDPGFX_CAPVERSION_FRDP_1:
1455
#endif
1456
0
    case RDPGFX_CAPVERSION_10:
1457
0
    case RDPGFX_CAPVERSION_102:
1458
0
    case RDPGFX_CAPVERSION_103:
1459
0
    case RDPGFX_CAPVERSION_104:
1460
0
    case RDPGFX_CAPVERSION_105:
1461
0
    case RDPGFX_CAPVERSION_106:
1462
0
    case RDPGFX_CAPVERSION_106_ERR:
1463
0
    case RDPGFX_CAPVERSION_107:
1464
#if defined(WITH_GFX_AZURE)
1465
    case RDPGFX_CAPVERSION_111:
1466
    case RDPGFX_CAPVERSION_112:
1467
    case RDPGFX_CAPVERSION_113:
1468
    case RDPGFX_CAPVERSION_115:
1469
#endif
1470
0
      if (freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSendQoeAck))
1471
0
      {
1472
0
        RDPGFX_QOE_FRAME_ACKNOWLEDGE_PDU qoe = WINPR_C_ARRAY_INIT;
1473
0
        UINT64 diff = (GetTickCount64() - gfx->StartDecodingTime);
1474
1475
0
        if (diff > 65000)
1476
0
          diff = 0;
1477
1478
0
        qoe.frameId = pdu.frameId;
1479
0
        qoe.timestamp = gfx->StartDecodingTime % UINT32_MAX;
1480
0
        qoe.timeDiffSE = WINPR_ASSERTING_INT_CAST(UINT16, diff);
1481
0
        qoe.timeDiffEDR = WINPR_ASSERTING_INT_CAST(UINT16, EndFrameTime);
1482
1483
0
        if ((error = rdpgfx_send_qoe_frame_acknowledge_pdu(context, &qoe)))
1484
0
          WLog_Print(gfx->base.log, WLOG_ERROR,
1485
0
                     "rdpgfx_send_qoe_frame_acknowledge_pdu failed with error %" PRIu32
1486
0
                     "",
1487
0
                     error);
1488
0
      }
1489
1490
0
      break;
1491
1492
0
    default:
1493
0
      break;
1494
0
  }
1495
1496
0
  return error;
1497
0
}
1498
1499
/**
1500
 * Function description
1501
 *
1502
 * @return 0 on success, otherwise a Win32 error code
1503
 */
1504
static UINT rdpgfx_recv_wire_to_surface_1_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1505
0
{
1506
0
  RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1507
0
  RDPGFX_WIRE_TO_SURFACE_PDU_1 pdu = WINPR_C_ARRAY_INIT;
1508
0
  WINPR_ASSERT(callback);
1509
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1510
0
  UINT error = 0;
1511
1512
0
  WINPR_ASSERT(gfx);
1513
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_1_SIZE))
1514
0
    return ERROR_INVALID_DATA;
1515
1516
0
  Stream_Read_UINT16(s, pdu.surfaceId);  /* surfaceId (2 bytes) */
1517
0
  Stream_Read_UINT16(s, pdu.codecId);    /* codecId (2 bytes) */
1518
0
  Stream_Read_UINT8(s, pdu.pixelFormat); /* pixelFormat (1 byte) */
1519
1520
0
  if ((error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.destRect)))) /* destRect (8 bytes) */
1521
0
  {
1522
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "",
1523
0
               error);
1524
0
    return error;
1525
0
  }
1526
1527
0
  Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1528
1529
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, pdu.bitmapDataLength))
1530
0
    return ERROR_INVALID_DATA;
1531
1532
0
  pdu.bitmapData = Stream_Pointer(s);
1533
0
  Stream_Seek(s, pdu.bitmapDataLength);
1534
1535
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1536
0
             "RecvWireToSurface1Pdu: surfaceId: %" PRIu16 " codecId: %s (0x%04" PRIX16
1537
0
             ") pixelFormat: 0x%02" PRIX8 " "
1538
0
             "destRect: left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16
1539
0
             " bitmapDataLength: %" PRIu32 "",
1540
0
             pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId, pdu.pixelFormat,
1541
0
             pdu.destRect.left, pdu.destRect.top, pdu.destRect.right, pdu.destRect.bottom,
1542
0
             pdu.bitmapDataLength);
1543
0
  cmd.surfaceId = pdu.surfaceId;
1544
0
  cmd.codecId = pdu.codecId;
1545
0
  cmd.contextId = 0;
1546
1547
0
  switch (pdu.pixelFormat)
1548
0
  {
1549
0
    case GFX_PIXEL_FORMAT_XRGB_8888:
1550
0
      cmd.format = PIXEL_FORMAT_BGRX32;
1551
0
      break;
1552
1553
0
    case GFX_PIXEL_FORMAT_ARGB_8888:
1554
0
      cmd.format = PIXEL_FORMAT_BGRA32;
1555
0
      break;
1556
1557
0
    default:
1558
0
      return ERROR_INVALID_DATA;
1559
0
  }
1560
1561
0
  cmd.left = pdu.destRect.left;
1562
0
  cmd.top = pdu.destRect.top;
1563
0
  cmd.right = pdu.destRect.right;
1564
0
  cmd.bottom = pdu.destRect.bottom;
1565
0
  cmd.width = cmd.right - cmd.left;
1566
0
  cmd.height = cmd.bottom - cmd.top;
1567
0
  cmd.length = pdu.bitmapDataLength;
1568
0
  cmd.data = pdu.bitmapData;
1569
0
  cmd.extra = nullptr;
1570
1571
0
  if (cmd.right < cmd.left)
1572
0
  {
1573
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
1574
0
               "RecvWireToSurface1Pdu right=%" PRIu32 " < left=%" PRIu32, cmd.right, cmd.left);
1575
0
    return ERROR_INVALID_DATA;
1576
0
  }
1577
0
  if (cmd.bottom < cmd.top)
1578
0
  {
1579
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
1580
0
               "RecvWireToSurface1Pdu bottom=%" PRIu32 " < top=%" PRIu32, cmd.bottom, cmd.top);
1581
0
    return ERROR_INVALID_DATA;
1582
0
  }
1583
1584
0
  if ((error = rdpgfx_decode(gfx, &cmd)))
1585
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_decode failed with error %" PRIu32 "!",
1586
0
               error);
1587
1588
0
  return error;
1589
0
}
1590
1591
WINPR_ATTR_NODISCARD
1592
static BOOL checkSurfaceCommand(const RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1593
0
{
1594
0
  switch (cmd->codecId)
1595
0
  {
1596
0
    case RDPGFX_CODECID_UNCOMPRESSED:
1597
0
    case RDPGFX_CODECID_CAVIDEO:
1598
0
    case RDPGFX_CODECID_CLEARCODEC:
1599
0
    case RDPGFX_CODECID_PLANAR:
1600
0
    case RDPGFX_CODECID_AVC420:
1601
0
    case RDPGFX_CODECID_ALPHA:
1602
0
    case RDPGFX_CODECID_AVC444:
1603
0
    case RDPGFX_CODECID_AVC444v2:
1604
0
    case RDPGFX_CODECID_CAPROGRESSIVE:
1605
0
    case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1606
0
      return TRUE;
1607
#if defined(WITH_GFX_AV1)
1608
    case RDPGFX_CODECID_AV1:
1609
      if (gfx->ConnectionCaps.version != RDPGFX_CAPVERSION_FRDP_1)
1610
      {
1611
        WLog_Print(gfx->base.log, WLOG_ERROR,
1612
                   "RDPGFX_SURFACE_COMMAND::codecId %" PRIu32
1613
                   " only supported with %s but connection uses %s [0x%08" PRIx32 "]",
1614
                   cmd->codecId, rdpgfx_caps_version_str(RDPGFX_CAPVERSION_FRDP_1),
1615
                   rdpgfx_caps_version_str(gfx->ConnectionCaps.version),
1616
                   gfx->ConnectionCaps.version);
1617
        return FALSE;
1618
      }
1619
      return TRUE;
1620
#endif
1621
0
    default:
1622
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1623
0
                 "Unknown RDPGFX_SURFACE_COMMAND::codecId %" PRIu32, cmd->codecId);
1624
0
      return FALSE;
1625
0
  }
1626
0
}
1627
1628
UINT logSurfaceCommand(RDPGFX_PLUGIN* gfx, const RDPGFX_SURFACE_COMMAND* cmd)
1629
0
{
1630
0
  WINPR_ASSERT(gfx);
1631
1632
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "Got GFX %s",
1633
0
             rdpgfx_get_codec_id_string(WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId)));
1634
1635
0
  RdpgfxClientContext* context = gfx->context;
1636
0
  if (!context)
1637
0
    return CHANNEL_RC_OK;
1638
1639
0
  if (!checkSurfaceCommand(gfx, cmd))
1640
0
    return CHANNEL_RC_NULL_DATA;
1641
1642
0
  const UINT error = IFCALLRESULT(CHANNEL_RC_OK, context->SurfaceCommand, context, cmd);
1643
1644
0
  if (error)
1645
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
1646
0
               "context->SurfaceCommand failed with error %" PRIu32 "", error);
1647
1648
0
  rdpgfx_codecid_event(context, cmd->codecId, cmd->length);
1649
0
  return error;
1650
0
}
1651
1652
/**
1653
 * Function description
1654
 *
1655
 * @return 0 on success, otherwise a Win32 error code
1656
 */
1657
static UINT rdpgfx_recv_wire_to_surface_2_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1658
0
{
1659
0
  RDPGFX_SURFACE_COMMAND cmd = WINPR_C_ARRAY_INIT;
1660
0
  RDPGFX_WIRE_TO_SURFACE_PDU_2 pdu = WINPR_C_ARRAY_INIT;
1661
1662
0
  WINPR_ASSERT(callback);
1663
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1664
0
  WINPR_ASSERT(gfx);
1665
1666
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, RDPGFX_WIRE_TO_SURFACE_PDU_2_SIZE))
1667
0
    return ERROR_INVALID_DATA;
1668
1669
0
  Stream_Read_UINT16(s, pdu.surfaceId);        /* surfaceId (2 bytes) */
1670
0
  Stream_Read_UINT16(s, pdu.codecId);          /* codecId (2 bytes) */
1671
0
  Stream_Read_UINT32(s, pdu.codecContextId);   /* codecContextId (4 bytes) */
1672
0
  Stream_Read_UINT8(s, pdu.pixelFormat);       /* pixelFormat (1 byte) */
1673
0
  Stream_Read_UINT32(s, pdu.bitmapDataLength); /* bitmapDataLength (4 bytes) */
1674
0
  pdu.bitmapData = Stream_Pointer(s);
1675
0
  if (!Stream_SafeSeek(s, pdu.bitmapDataLength))
1676
0
    return ERROR_INVALID_DATA;
1677
1678
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1679
0
             "RecvWireToSurface2Pdu: surfaceId: %" PRIu16 " codecId: %s (0x%04" PRIX16 ") "
1680
0
             "codecContextId: %" PRIu32 " pixelFormat: 0x%02" PRIX8 " bitmapDataLength: %" PRIu32
1681
0
             "",
1682
0
             pdu.surfaceId, rdpgfx_get_codec_id_string(pdu.codecId), pdu.codecId,
1683
0
             pdu.codecContextId, pdu.pixelFormat, pdu.bitmapDataLength);
1684
1685
0
  cmd.surfaceId = pdu.surfaceId;
1686
0
  cmd.codecId = pdu.codecId;
1687
0
  cmd.contextId = pdu.codecContextId;
1688
1689
0
  switch (pdu.pixelFormat)
1690
0
  {
1691
0
    case GFX_PIXEL_FORMAT_XRGB_8888:
1692
0
      cmd.format = PIXEL_FORMAT_BGRX32;
1693
0
      break;
1694
1695
0
    case GFX_PIXEL_FORMAT_ARGB_8888:
1696
0
      cmd.format = PIXEL_FORMAT_BGRA32;
1697
0
      break;
1698
1699
0
    default:
1700
0
      return ERROR_INVALID_DATA;
1701
0
  }
1702
1703
0
  cmd.length = pdu.bitmapDataLength;
1704
0
  cmd.data = pdu.bitmapData;
1705
0
  cmd.extra = nullptr;
1706
1707
0
  if (cmd.codecId != RDPGFX_CODECID_CAPROGRESSIVE)
1708
0
    return ERROR_INVALID_DATA;
1709
1710
0
  return logSurfaceCommand(gfx, &cmd);
1711
0
}
1712
1713
/**
1714
 * Function description
1715
 *
1716
 * @return 0 on success, otherwise a Win32 error code
1717
 */
1718
static UINT rdpgfx_recv_delete_encoding_context_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1719
0
{
1720
0
  RDPGFX_DELETE_ENCODING_CONTEXT_PDU pdu = WINPR_C_ARRAY_INIT;
1721
0
  WINPR_ASSERT(callback);
1722
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1723
0
  WINPR_ASSERT(gfx);
1724
0
  RdpgfxClientContext* context = gfx->context;
1725
0
  UINT error = CHANNEL_RC_OK;
1726
1727
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1728
0
    return ERROR_INVALID_DATA;
1729
1730
0
  Stream_Read_UINT16(s, pdu.surfaceId);      /* surfaceId (2 bytes) */
1731
0
  Stream_Read_UINT32(s, pdu.codecContextId); /* codecContextId (4 bytes) */
1732
1733
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
1734
0
             "RecvDeleteEncodingContextPdu: surfaceId: %" PRIu16 " codecContextId: %" PRIu32 "",
1735
0
             pdu.surfaceId, pdu.codecContextId);
1736
1737
0
  if (context)
1738
0
  {
1739
0
    IFCALLRET(context->DeleteEncodingContext, error, context, &pdu);
1740
1741
0
    if (error)
1742
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1743
0
                 "context->DeleteEncodingContext failed with error %" PRIu32 "", error);
1744
0
  }
1745
1746
0
  return error;
1747
0
}
1748
1749
/**
1750
 * Function description
1751
 *
1752
 * @return 0 on success, otherwise a Win32 error code
1753
 */
1754
static UINT rdpgfx_recv_solid_fill_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1755
0
{
1756
0
  RDPGFX_SOLID_FILL_PDU pdu = WINPR_C_ARRAY_INIT;
1757
0
  WINPR_ASSERT(callback);
1758
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1759
0
  WINPR_ASSERT(gfx);
1760
0
  RdpgfxClientContext* context = gfx->context;
1761
1762
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 8))
1763
0
    return ERROR_INVALID_DATA;
1764
1765
0
  Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1766
1767
0
  UINT error = rdpgfx_read_color32(gfx->base.log, s, &(pdu.fillPixel));
1768
0
  if (error != CHANNEL_RC_OK) /* fillPixel (4 bytes) */
1769
0
  {
1770
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_color32 failed with error %" PRIu32 "!",
1771
0
               error);
1772
0
    return error;
1773
0
  }
1774
1775
0
  Stream_Read_UINT16(s, pdu.fillRectCount); /* fillRectCount (2 bytes) */
1776
1777
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.fillRectCount, 8ull))
1778
0
    return ERROR_INVALID_DATA;
1779
1780
0
  pdu.fillRects = (RECTANGLE_16*)calloc(pdu.fillRectCount, sizeof(RECTANGLE_16));
1781
1782
0
  if (!pdu.fillRects)
1783
0
  {
1784
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
1785
0
    return CHANNEL_RC_NO_MEMORY;
1786
0
  }
1787
1788
0
  for (UINT16 index = 0; index < pdu.fillRectCount; index++)
1789
0
  {
1790
0
    RECTANGLE_16* fillRect = &(pdu.fillRects[index]);
1791
1792
0
    if ((error = rdpgfx_read_rect16(gfx->base.log, s, fillRect)))
1793
0
    {
1794
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1795
0
                 "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error);
1796
0
      free(pdu.fillRects);
1797
0
      return error;
1798
0
    }
1799
0
  }
1800
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1801
0
             "RecvSolidFillPdu: surfaceId: %" PRIu16 " fillRectCount: %" PRIu16 "", pdu.surfaceId,
1802
0
             pdu.fillRectCount);
1803
1804
0
  if (context)
1805
0
  {
1806
0
    IFCALLRET(context->SolidFill, error, context, &pdu);
1807
1808
0
    if (error)
1809
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1810
0
                 "context->SolidFill failed with error %" PRIu32 "", error);
1811
0
  }
1812
1813
0
  free(pdu.fillRects);
1814
0
  return error;
1815
0
}
1816
1817
/**
1818
 * Function description
1819
 *
1820
 * @return 0 on success, otherwise a Win32 error code
1821
 */
1822
static UINT rdpgfx_recv_surface_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1823
0
{
1824
0
  RDPGFX_SURFACE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1825
0
  WINPR_ASSERT(callback);
1826
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1827
0
  WINPR_ASSERT(gfx);
1828
0
  RdpgfxClientContext* context = gfx->context;
1829
0
  UINT error = 0;
1830
1831
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 14))
1832
0
    return ERROR_INVALID_DATA;
1833
1834
0
  Stream_Read_UINT16(s, pdu.surfaceIdSrc);  /* surfaceIdSrc (2 bytes) */
1835
0
  Stream_Read_UINT16(s, pdu.surfaceIdDest); /* surfaceIdDest (2 bytes) */
1836
1837
0
  if ((error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc)))) /* rectSrc (8 bytes ) */
1838
0
  {
1839
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
1840
0
               error);
1841
0
    return error;
1842
0
  }
1843
1844
0
  Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
1845
1846
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
1847
0
    return ERROR_INVALID_DATA;
1848
1849
0
  pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
1850
1851
0
  if (!pdu.destPts)
1852
0
  {
1853
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
1854
0
    return CHANNEL_RC_NO_MEMORY;
1855
0
  }
1856
1857
0
  for (UINT16 index = 0; index < pdu.destPtsCount; index++)
1858
0
  {
1859
0
    RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
1860
1861
0
    if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
1862
0
    {
1863
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1864
0
                 "rdpgfx_read_point16 failed with error %" PRIu32 "!", error);
1865
0
      free(pdu.destPts);
1866
0
      return error;
1867
0
    }
1868
0
  }
1869
1870
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1871
0
             "RecvSurfaceToSurfacePdu: surfaceIdSrc: %" PRIu16 " surfaceIdDest: %" PRIu16 " "
1872
0
             "left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16
1873
0
             " destPtsCount: %" PRIu16 "",
1874
0
             pdu.surfaceIdSrc, pdu.surfaceIdDest, pdu.rectSrc.left, pdu.rectSrc.top,
1875
0
             pdu.rectSrc.right, pdu.rectSrc.bottom, pdu.destPtsCount);
1876
1877
0
  if (context)
1878
0
  {
1879
0
    IFCALLRET(context->SurfaceToSurface, error, context, &pdu);
1880
1881
0
    if (error)
1882
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1883
0
                 "context->SurfaceToSurface failed with error %" PRIu32 "", error);
1884
0
  }
1885
1886
0
  free(pdu.destPts);
1887
0
  return error;
1888
0
}
1889
1890
/**
1891
 * Function description
1892
 *
1893
 * @return 0 on success, otherwise a Win32 error code
1894
 */
1895
static UINT rdpgfx_recv_surface_to_cache_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1896
0
{
1897
0
  RDPGFX_SURFACE_TO_CACHE_PDU pdu = WINPR_C_ARRAY_INIT;
1898
0
  WINPR_ASSERT(callback);
1899
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1900
1901
0
  WINPR_ASSERT(gfx);
1902
0
  RdpgfxClientContext* context = gfx->context;
1903
1904
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
1905
0
    return ERROR_INVALID_DATA;
1906
1907
0
  Stream_Read_UINT16(s, pdu.surfaceId); /* surfaceId (2 bytes) */
1908
0
  Stream_Read_UINT64(s, pdu.cacheKey);  /* cacheKey (8 bytes) */
1909
0
  Stream_Read_UINT16(s, pdu.cacheSlot); /* cacheSlot (2 bytes) */
1910
1911
0
  UINT error = rdpgfx_read_rect16(gfx->base.log, s, &(pdu.rectSrc));
1912
0
  if (error != CHANNEL_RC_OK) /* rectSrc (8 bytes ) */
1913
0
  {
1914
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_rect16 failed with error %" PRIu32 "!",
1915
0
               error);
1916
0
    return error;
1917
0
  }
1918
1919
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1920
0
             "RecvSurfaceToCachePdu: surfaceId: %" PRIu16 " cacheKey: 0x%016" PRIX64
1921
0
             " cacheSlot: %" PRIu16 " "
1922
0
             "left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 " bottom: %" PRIu16 "",
1923
0
             pdu.surfaceId, pdu.cacheKey, pdu.cacheSlot, pdu.rectSrc.left, pdu.rectSrc.top,
1924
0
             pdu.rectSrc.right, pdu.rectSrc.bottom);
1925
1926
0
  if (context)
1927
0
  {
1928
0
    IFCALLRET(context->SurfaceToCache, error, context, &pdu);
1929
1930
0
    if (error)
1931
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1932
0
                 "context->SurfaceToCache failed with error %" PRIu32 "", error);
1933
0
  }
1934
1935
0
  return error;
1936
0
}
1937
1938
/**
1939
 * Function description
1940
 *
1941
 * @return 0 on success, otherwise a Win32 error code
1942
 */
1943
static UINT rdpgfx_recv_cache_to_surface_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
1944
0
{
1945
0
  RDPGFX_CACHE_TO_SURFACE_PDU pdu = WINPR_C_ARRAY_INIT;
1946
0
  WINPR_ASSERT(callback);
1947
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
1948
1949
0
  WINPR_ASSERT(gfx);
1950
0
  RdpgfxClientContext* context = gfx->context;
1951
0
  UINT error = CHANNEL_RC_OK;
1952
1953
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 6))
1954
0
    return ERROR_INVALID_DATA;
1955
1956
0
  Stream_Read_UINT16(s, pdu.cacheSlot);    /* cacheSlot (2 bytes) */
1957
0
  Stream_Read_UINT16(s, pdu.surfaceId);    /* surfaceId (2 bytes) */
1958
0
  Stream_Read_UINT16(s, pdu.destPtsCount); /* destPtsCount (2 bytes) */
1959
1960
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, pdu.destPtsCount, 4ull))
1961
0
    return ERROR_INVALID_DATA;
1962
1963
0
  pdu.destPts = (RDPGFX_POINT16*)calloc(pdu.destPtsCount, sizeof(RDPGFX_POINT16));
1964
1965
0
  if (!pdu.destPts)
1966
0
  {
1967
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
1968
0
    return CHANNEL_RC_NO_MEMORY;
1969
0
  }
1970
1971
0
  for (UINT16 index = 0; index < pdu.destPtsCount; index++)
1972
0
  {
1973
0
    RDPGFX_POINT16* destPt = &(pdu.destPts[index]);
1974
1975
0
    if ((error = rdpgfx_read_point16(gfx->base.log, s, destPt)))
1976
0
    {
1977
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1978
0
                 "rdpgfx_read_point16 failed with error %" PRIu32 "", error);
1979
0
      free(pdu.destPts);
1980
0
      return error;
1981
0
    }
1982
0
  }
1983
1984
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
1985
0
             "RdpGfxRecvCacheToSurfacePdu: cacheSlot: %" PRIu16 " surfaceId: %" PRIu16
1986
0
             " destPtsCount: %" PRIu16 "",
1987
0
             pdu.cacheSlot, pdu.surfaceId, pdu.destPtsCount);
1988
1989
0
  if (context)
1990
0
  {
1991
0
    IFCALLRET(context->CacheToSurface, error, context, &pdu);
1992
1993
0
    if (error)
1994
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
1995
0
                 "context->CacheToSurface failed with error %" PRIu32 "", error);
1996
0
  }
1997
1998
0
  free(pdu.destPts);
1999
0
  return error;
2000
0
}
2001
2002
/**
2003
 * Function description
2004
 *
2005
 * @return 0 on success, otherwise a Win32 error code
2006
 */
2007
static UINT rdpgfx_recv_map_surface_to_output_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2008
0
{
2009
0
  RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
2010
0
  WINPR_ASSERT(callback);
2011
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2012
2013
0
  WINPR_ASSERT(gfx);
2014
0
  RdpgfxClientContext* context = gfx->context;
2015
0
  UINT error = CHANNEL_RC_OK;
2016
2017
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 12))
2018
0
    return ERROR_INVALID_DATA;
2019
2020
0
  Stream_Read_UINT16(s, pdu.surfaceId);     /* surfaceId (2 bytes) */
2021
0
  Stream_Read_UINT16(s, pdu.reserved);      /* reserved (2 bytes) */
2022
0
  Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
2023
0
  Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
2024
2025
0
  if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2026
0
  {
2027
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2028
0
               "context->MapSurfaceToOutput outputOriginX[%" PRIu32 "], outputOriginY[%" PRIu32
2029
0
               "] > %" PRIu32,
2030
0
               pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2031
0
    return ERROR_INVALID_DATA;
2032
0
  }
2033
2034
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
2035
0
             "RecvMapSurfaceToOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
2036
0
             " outputOriginY: %" PRIu32 "",
2037
0
             pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY);
2038
2039
0
  if (context)
2040
0
  {
2041
0
    IFCALLRET(context->MapSurfaceToOutput, error, context, &pdu);
2042
2043
0
    if (error)
2044
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
2045
0
                 "context->MapSurfaceToOutput failed with error %" PRIu32 "", error);
2046
0
  }
2047
2048
0
  return error;
2049
0
}
2050
2051
static UINT rdpgfx_recv_map_surface_to_scaled_output_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2052
                                                         wStream* s)
2053
0
{
2054
0
  RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU pdu = WINPR_C_ARRAY_INIT;
2055
0
  WINPR_ASSERT(callback);
2056
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2057
2058
0
  WINPR_ASSERT(gfx);
2059
0
  RdpgfxClientContext* context = gfx->context;
2060
0
  UINT error = CHANNEL_RC_OK;
2061
2062
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 20))
2063
0
    return ERROR_INVALID_DATA;
2064
2065
0
  Stream_Read_UINT16(s, pdu.surfaceId);     /* surfaceId (2 bytes) */
2066
0
  Stream_Read_UINT16(s, pdu.reserved);      /* reserved (2 bytes) */
2067
0
  Stream_Read_UINT32(s, pdu.outputOriginX); /* outputOriginX (4 bytes) */
2068
0
  Stream_Read_UINT32(s, pdu.outputOriginY); /* outputOriginY (4 bytes) */
2069
0
  Stream_Read_UINT32(s, pdu.targetWidth);   /* targetWidth (4 bytes) */
2070
0
  Stream_Read_UINT32(s, pdu.targetHeight);  /* targetHeight (4 bytes) */
2071
2072
0
  if ((pdu.outputOriginX > MAX_SURFACE_SIZE) || (pdu.outputOriginY > MAX_SURFACE_SIZE))
2073
0
  {
2074
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2075
0
               "context->MapSurfaceToScaledOutput outputOriginX[%" PRIu32
2076
0
               "], outputOriginY[%" PRIu32 "] > %" PRIu32,
2077
0
               pdu.outputOriginX, pdu.outputOriginY, MAX_SURFACE_SIZE);
2078
0
    return ERROR_INVALID_DATA;
2079
0
  }
2080
0
  if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2081
0
  {
2082
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2083
0
               "context->MapSurfaceToScaledOutput targetWidth[%" PRIu32
2084
0
               "], targetHeight[%" PRIu32 "] > %" PRIu32,
2085
0
               pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2086
0
    return ERROR_INVALID_DATA;
2087
0
  }
2088
2089
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
2090
0
             "RecvMapSurfaceToScaledOutputPdu: surfaceId: %" PRIu16 " outputOriginX: %" PRIu32
2091
0
             " outputOriginY: %" PRIu32 " targetWidth: %" PRIu32 " targetHeight: %" PRIu32,
2092
0
             pdu.surfaceId, pdu.outputOriginX, pdu.outputOriginY, pdu.targetWidth,
2093
0
             pdu.targetHeight);
2094
2095
0
  if (context)
2096
0
  {
2097
0
    IFCALLRET(context->MapSurfaceToScaledOutput, error, context, &pdu);
2098
2099
0
    if (error)
2100
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
2101
0
                 "context->MapSurfaceToScaledOutput failed with error %" PRIu32 "", error);
2102
0
  }
2103
2104
0
  return error;
2105
0
}
2106
2107
/**
2108
 * Function description
2109
 *
2110
 * @return 0 on success, otherwise a Win32 error code
2111
 */
2112
static UINT rdpgfx_recv_map_surface_to_window_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
2113
0
{
2114
0
  RDPGFX_MAP_SURFACE_TO_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2115
0
  WINPR_ASSERT(callback);
2116
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2117
2118
0
  WINPR_ASSERT(gfx);
2119
0
  RdpgfxClientContext* context = gfx->context;
2120
0
  UINT error = CHANNEL_RC_OK;
2121
2122
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 18))
2123
0
    return ERROR_INVALID_DATA;
2124
2125
0
  Stream_Read_UINT16(s, pdu.surfaceId);    /* surfaceId (2 bytes) */
2126
0
  Stream_Read_UINT64(s, pdu.windowId);     /* windowId (8 bytes) */
2127
0
  Stream_Read_UINT32(s, pdu.mappedWidth);  /* mappedWidth (4 bytes) */
2128
0
  Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2129
2130
0
  if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2131
0
  {
2132
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2133
0
               "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2134
0
               "] is 0",
2135
0
               pdu.mappedWidth, pdu.mappedHeight);
2136
0
    return ERROR_INVALID_DATA;
2137
0
  }
2138
0
  if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2139
0
  {
2140
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2141
0
               "context->MapSurfaceToWindow mappedWidth[%" PRIu32 "], mappedHeight[%" PRIu32
2142
0
               "] > %" PRIu32,
2143
0
               pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2144
0
    return ERROR_INVALID_DATA;
2145
0
  }
2146
2147
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
2148
0
             "RecvMapSurfaceToWindowPdu: surfaceId: %" PRIu16 " windowId: 0x%016" PRIX64
2149
0
             " mappedWidth: %" PRIu32 " mappedHeight: %" PRIu32 "",
2150
0
             pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight);
2151
2152
0
  if (context && context->MapSurfaceToWindow)
2153
0
  {
2154
0
    IFCALLRET(context->MapSurfaceToWindow, error, context, &pdu);
2155
2156
0
    if (error)
2157
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
2158
0
                 "context->MapSurfaceToWindow failed with error %" PRIu32 "", error);
2159
0
  }
2160
2161
0
  return error;
2162
0
}
2163
2164
static UINT rdpgfx_recv_map_surface_to_scaled_window_pdu(GENERIC_CHANNEL_CALLBACK* callback,
2165
                                                         wStream* s)
2166
0
{
2167
0
  RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU pdu = WINPR_C_ARRAY_INIT;
2168
0
  WINPR_ASSERT(callback);
2169
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2170
0
  WINPR_ASSERT(gfx);
2171
0
  RdpgfxClientContext* context = gfx->context;
2172
0
  UINT error = CHANNEL_RC_OK;
2173
2174
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 26))
2175
0
    return ERROR_INVALID_DATA;
2176
2177
0
  Stream_Read_UINT16(s, pdu.surfaceId);    /* surfaceId (2 bytes) */
2178
0
  Stream_Read_UINT64(s, pdu.windowId);     /* windowId (8 bytes) */
2179
0
  Stream_Read_UINT32(s, pdu.mappedWidth);  /* mappedWidth (4 bytes) */
2180
0
  Stream_Read_UINT32(s, pdu.mappedHeight); /* mappedHeight (4 bytes) */
2181
0
  Stream_Read_UINT32(s, pdu.targetWidth);  /* targetWidth (4 bytes) */
2182
0
  Stream_Read_UINT32(s, pdu.targetHeight); /* targetHeight (4 bytes) */
2183
2184
0
  if ((pdu.mappedWidth == 0) || (pdu.mappedHeight == 0))
2185
0
  {
2186
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2187
0
               "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2188
0
               "], mappedHeight[%" PRIu32 "] is 0",
2189
0
               pdu.mappedWidth, pdu.mappedHeight);
2190
0
    return ERROR_INVALID_DATA;
2191
0
  }
2192
0
  if ((pdu.targetWidth == 0) || (pdu.targetHeight == 0))
2193
0
  {
2194
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2195
0
               "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2196
0
               "], targetHeight[%" PRIu32 "] is 0",
2197
0
               pdu.targetWidth, pdu.targetHeight);
2198
0
    return ERROR_INVALID_DATA;
2199
0
  }
2200
2201
0
  if ((pdu.mappedWidth > MAX_SURFACE_SIZE) || (pdu.mappedHeight > MAX_SURFACE_SIZE))
2202
0
  {
2203
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2204
0
               "context->RecvMapSurfaceToScaledWindowPdu mappedWidth[%" PRIu32
2205
0
               "], mappedHeight[%" PRIu32 "] > %" PRIu32,
2206
0
               pdu.mappedWidth, pdu.mappedHeight, MAX_SURFACE_SIZE);
2207
0
    return ERROR_INVALID_DATA;
2208
0
  }
2209
0
  if ((pdu.targetWidth > MAX_SURFACE_SIZE) || (pdu.targetHeight > MAX_SURFACE_SIZE))
2210
0
  {
2211
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2212
0
               "context->RecvMapSurfaceToScaledWindowPdu targetWidth[%" PRIu32
2213
0
               "], targetHeight[%" PRIu32 "] > %" PRIu32,
2214
0
               pdu.targetWidth, pdu.targetHeight, MAX_SURFACE_SIZE);
2215
0
    return ERROR_INVALID_DATA;
2216
0
  }
2217
2218
0
  WLog_Print(gfx->base.log, WLOG_DEBUG,
2219
0
             "RecvMapSurfaceToScaledWindowPdu: surfaceId: %" PRIu16 " windowId: 0x%016" PRIX64
2220
0
             " mappedWidth: %" PRIu32 " mappedHeight: %" PRIu32 " targetWidth: %" PRIu32
2221
0
             " targetHeight: %" PRIu32 "",
2222
0
             pdu.surfaceId, pdu.windowId, pdu.mappedWidth, pdu.mappedHeight, pdu.targetWidth,
2223
0
             pdu.targetHeight);
2224
2225
0
  if (context && context->MapSurfaceToScaledWindow)
2226
0
  {
2227
0
    IFCALLRET(context->MapSurfaceToScaledWindow, error, context, &pdu);
2228
2229
0
    if (error)
2230
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
2231
0
                 "context->MapSurfaceToScaledWindow failed with error %" PRIu32 "", error);
2232
0
  }
2233
2234
0
  return error;
2235
0
}
2236
2237
/**
2238
 * Function description
2239
 *
2240
 * @return 0 on success, otherwise a Win32 error code
2241
 */
2242
static UINT rdpgfx_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
2243
0
{
2244
0
  RDPGFX_HEADER header = WINPR_C_ARRAY_INIT;
2245
0
  wStream sbuffer = WINPR_C_ARRAY_INIT;
2246
2247
0
  WINPR_ASSERT(callback);
2248
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2249
2250
0
  WINPR_ASSERT(gfx);
2251
2252
0
  size_t packetlen = 0;
2253
0
  UINT error = rdpgfx_read_header(gfx->base.log, data, &header, &packetlen);
2254
0
  if (error != CHANNEL_RC_OK)
2255
0
  {
2256
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_read_header failed with error %" PRIu32 "!",
2257
0
               error);
2258
0
    return error;
2259
0
  }
2260
2261
  /* Limit the PDU parsers to the data of this PDU, the stream might contain more of them */
2262
0
  wStream* s = Stream_StaticConstInit(&sbuffer, Stream_ConstPointer(data), packetlen);
2263
0
  Stream_Seek(data, packetlen);
2264
2265
0
  WLog_Print(gfx->base.log, WLOG_TRACE,
2266
0
             "cmdId: %s (0x%04" PRIX16 ") flags: 0x%04" PRIX16 " pduLength: %" PRIu32 "",
2267
0
             rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId, header.flags,
2268
0
             header.pduLength);
2269
2270
0
  rdpgfx_stats_cmdid_event(gfx->context, header.cmdId);
2271
0
  switch (header.cmdId)
2272
0
  {
2273
0
    case RDPGFX_CMDID_WIRETOSURFACE_1:
2274
0
      if ((error = rdpgfx_recv_wire_to_surface_1_pdu(callback, s)))
2275
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2276
0
                   "rdpgfx_recv_wire_to_surface_1_pdu failed with error %" PRIu32 "!",
2277
0
                   error);
2278
2279
0
      break;
2280
2281
0
    case RDPGFX_CMDID_WIRETOSURFACE_2:
2282
0
      if ((error = rdpgfx_recv_wire_to_surface_2_pdu(callback, s)))
2283
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2284
0
                   "rdpgfx_recv_wire_to_surface_2_pdu failed with error %" PRIu32 "!",
2285
0
                   error);
2286
2287
0
      break;
2288
2289
0
    case RDPGFX_CMDID_DELETEENCODINGCONTEXT:
2290
0
      if ((error = rdpgfx_recv_delete_encoding_context_pdu(callback, s)))
2291
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2292
0
                   "rdpgfx_recv_delete_encoding_context_pdu failed with error %" PRIu32 "!",
2293
0
                   error);
2294
2295
0
      break;
2296
2297
0
    case RDPGFX_CMDID_SOLIDFILL:
2298
0
      if ((error = rdpgfx_recv_solid_fill_pdu(callback, s)))
2299
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2300
0
                   "rdpgfx_recv_solid_fill_pdu failed with error %" PRIu32 "!", error);
2301
2302
0
      break;
2303
2304
0
    case RDPGFX_CMDID_SURFACETOSURFACE:
2305
0
      if ((error = rdpgfx_recv_surface_to_surface_pdu(callback, s)))
2306
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2307
0
                   "rdpgfx_recv_surface_to_surface_pdu failed with error %" PRIu32 "!",
2308
0
                   error);
2309
2310
0
      break;
2311
2312
0
    case RDPGFX_CMDID_SURFACETOCACHE:
2313
0
      if ((error = rdpgfx_recv_surface_to_cache_pdu(callback, s)))
2314
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2315
0
                   "rdpgfx_recv_surface_to_cache_pdu failed with error %" PRIu32 "!",
2316
0
                   error);
2317
2318
0
      break;
2319
2320
0
    case RDPGFX_CMDID_CACHETOSURFACE:
2321
0
      if ((error = rdpgfx_recv_cache_to_surface_pdu(callback, s)))
2322
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2323
0
                   "rdpgfx_recv_cache_to_surface_pdu failed with error %" PRIu32 "!",
2324
0
                   error);
2325
2326
0
      break;
2327
2328
0
    case RDPGFX_CMDID_EVICTCACHEENTRY:
2329
0
      if ((error = rdpgfx_recv_evict_cache_entry_pdu(callback, s)))
2330
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2331
0
                   "rdpgfx_recv_evict_cache_entry_pdu failed with error %" PRIu32 "!",
2332
0
                   error);
2333
2334
0
      break;
2335
2336
0
    case RDPGFX_CMDID_CREATESURFACE:
2337
0
      if ((error = rdpgfx_recv_create_surface_pdu(callback, s)))
2338
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2339
0
                   "rdpgfx_recv_create_surface_pdu failed with error %" PRIu32 "!", error);
2340
2341
0
      break;
2342
2343
0
    case RDPGFX_CMDID_DELETESURFACE:
2344
0
      if ((error = rdpgfx_recv_delete_surface_pdu(callback, s)))
2345
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2346
0
                   "rdpgfx_recv_delete_surface_pdu failed with error %" PRIu32 "!", error);
2347
2348
0
      break;
2349
2350
0
    case RDPGFX_CMDID_STARTFRAME:
2351
0
      if ((error = rdpgfx_recv_start_frame_pdu(callback, s)))
2352
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2353
0
                   "rdpgfx_recv_start_frame_pdu failed with error %" PRIu32 "!", error);
2354
2355
0
      break;
2356
2357
0
    case RDPGFX_CMDID_ENDFRAME:
2358
0
      if ((error = rdpgfx_recv_end_frame_pdu(callback, s)))
2359
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2360
0
                   "rdpgfx_recv_end_frame_pdu failed with error %" PRIu32 "!", error);
2361
2362
0
      break;
2363
2364
0
    case RDPGFX_CMDID_RESETGRAPHICS:
2365
0
      if ((error = rdpgfx_recv_reset_graphics_pdu(callback, s)))
2366
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2367
0
                   "rdpgfx_recv_reset_graphics_pdu failed with error %" PRIu32 "!", error);
2368
2369
0
      break;
2370
2371
0
    case RDPGFX_CMDID_MAPSURFACETOOUTPUT:
2372
0
      if ((error = rdpgfx_recv_map_surface_to_output_pdu(callback, s)))
2373
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2374
0
                   "rdpgfx_recv_map_surface_to_output_pdu failed with error %" PRIu32 "!",
2375
0
                   error);
2376
2377
0
      break;
2378
2379
0
    case RDPGFX_CMDID_CACHEIMPORTREPLY:
2380
0
      if ((error = rdpgfx_recv_cache_import_reply_pdu(callback, s)))
2381
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2382
0
                   "rdpgfx_recv_cache_import_reply_pdu failed with error %" PRIu32 "!",
2383
0
                   error);
2384
2385
0
      break;
2386
2387
0
    case RDPGFX_CMDID_CAPSCONFIRM:
2388
0
      if ((error = rdpgfx_recv_caps_confirm_pdu(callback, s)))
2389
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2390
0
                   "rdpgfx_recv_caps_confirm_pdu failed with error %" PRIu32 "!", error);
2391
2392
0
      if ((error = rdpgfx_send_cache_offer(gfx)))
2393
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2394
0
                   "rdpgfx_send_cache_offer failed with error %" PRIu32 "!", error);
2395
2396
0
      break;
2397
2398
0
    case RDPGFX_CMDID_MAPSURFACETOWINDOW:
2399
0
      if ((error = rdpgfx_recv_map_surface_to_window_pdu(callback, s)))
2400
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2401
0
                   "rdpgfx_recv_map_surface_to_window_pdu failed with error %" PRIu32 "!",
2402
0
                   error);
2403
2404
0
      break;
2405
2406
0
    case RDPGFX_CMDID_MAPSURFACETOSCALEDWINDOW:
2407
0
      if ((error = rdpgfx_recv_map_surface_to_scaled_window_pdu(callback, s)))
2408
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2409
0
                   "rdpgfx_recv_map_surface_to_scaled_window_pdu failed with error %" PRIu32
2410
0
                   "!",
2411
0
                   error);
2412
2413
0
      break;
2414
2415
0
    case RDPGFX_CMDID_MAPSURFACETOSCALEDOUTPUT:
2416
0
      if ((error = rdpgfx_recv_map_surface_to_scaled_output_pdu(callback, s)))
2417
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
2418
0
                   "rdpgfx_recv_map_surface_to_scaled_output_pdu failed with error %" PRIu32
2419
0
                   "!",
2420
0
                   error);
2421
2422
0
      break;
2423
2424
0
    case RDPGFX_CMDID_PROTECT_SURFACE:
2425
0
    case RDPGFX_CMDID_WATERMARK:
2426
0
      WLog_Print(gfx->base.log, WLOG_WARN,
2427
0
                 "Command %s not implemented, skipping %" PRIu32 " bytes",
2428
0
                 rdpgfx_get_cmd_id_string(header.cmdId), header.pduLength);
2429
0
      if (!Stream_SafeSeek(s, packetlen))
2430
0
        error = ERROR_INVALID_DATA;
2431
0
      break;
2432
0
    default:
2433
0
      error = CHANNEL_RC_BAD_PROC;
2434
0
      break;
2435
0
  }
2436
2437
0
  if (error)
2438
0
  {
2439
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2440
0
               "Error while processing GFX cmdId: %s (0x%04" PRIX16 ")",
2441
0
               rdpgfx_get_cmd_id_string(header.cmdId), header.cmdId);
2442
0
    return error;
2443
0
  }
2444
2445
0
  const size_t end = Stream_GetPosition(s);
2446
2447
0
  if (end != packetlen)
2448
0
  {
2449
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2450
0
               "Unexpected gfx pdu end: Actual: %" PRIuz ", Expected: %" PRIuz, end, packetlen);
2451
0
  }
2452
2453
0
  return error;
2454
0
}
2455
2456
/**
2457
 * Function description
2458
 *
2459
 * @return 0 on success, otherwise a Win32 error code
2460
 */
2461
static UINT rdpgfx_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
2462
0
{
2463
0
  UINT32 DstSize = 0;
2464
0
  BYTE* pDstData = nullptr;
2465
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2466
0
  WINPR_ASSERT(callback);
2467
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2468
0
  UINT error = CHANNEL_RC_OK;
2469
2470
0
  WINPR_ASSERT(gfx);
2471
0
  int status = zgfx_decompress(gfx->zgfx, Stream_ConstPointer(data),
2472
0
                               (UINT32)Stream_GetRemainingLength(data), &pDstData, &DstSize, 0);
2473
2474
0
  if ((status < 0) || (DstSize == 0))
2475
0
  {
2476
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2477
0
               "zgfx_decompress failure! status: %d, DstSize=%" PRIu32, status, DstSize);
2478
0
    free(pDstData);
2479
0
    return ERROR_INTERNAL_ERROR;
2480
0
  }
2481
2482
0
  wStream sbuffer = WINPR_C_ARRAY_INIT;
2483
0
  wStream* s = Stream_StaticConstInit(&sbuffer, pDstData, DstSize);
2484
2485
0
  if (!s)
2486
0
  {
2487
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2488
0
    free(pDstData);
2489
0
    return CHANNEL_RC_NO_MEMORY;
2490
0
  }
2491
2492
0
  while (Stream_GetPosition(s) < Stream_Length(s))
2493
0
  {
2494
0
    if ((error = rdpgfx_recv_pdu(callback, s)))
2495
0
    {
2496
0
      WLog_Print(gfx->base.log, WLOG_ERROR, "rdpgfx_recv_pdu failed with error %" PRIu32 "!",
2497
0
                 error);
2498
0
      break;
2499
0
    }
2500
0
  }
2501
2502
0
  free(pDstData);
2503
0
  return error;
2504
0
}
2505
2506
/**
2507
 * Function description
2508
 *
2509
 * @return 0 on success, otherwise a Win32 error code
2510
 */
2511
static UINT rdpgfx_on_open(IWTSVirtualChannelCallback* pChannelCallback)
2512
0
{
2513
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2514
0
  WINPR_ASSERT(callback);
2515
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2516
0
  WINPR_ASSERT(gfx);
2517
0
  RdpgfxClientContext* context = gfx->context;
2518
0
  UINT error = CHANNEL_RC_OK;
2519
0
  BOOL do_caps_advertise = TRUE;
2520
2521
0
  gfx->sendFrameAcks = TRUE;
2522
2523
0
  if (context)
2524
0
  {
2525
0
    IFCALLRET(context->OnOpen, error, context, &do_caps_advertise, &gfx->sendFrameAcks);
2526
2527
0
    if (error)
2528
0
      WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnOpen failed with error %" PRIu32 "",
2529
0
                 error);
2530
2531
0
    RdpgfxClientContextInt* ctx = (RdpgfxClientContextInt*)context;
2532
0
    const RdpgfxClientContextStats empty = WINPR_C_ARRAY_INIT;
2533
0
    ctx->stats = empty;
2534
0
  }
2535
2536
0
  if (do_caps_advertise)
2537
0
    error = rdpgfx_send_supported_caps(callback);
2538
2539
0
  return error;
2540
0
}
2541
2542
static void rdpgfx_dump_stats(RdpgfxClientContext* context)
2543
0
{
2544
0
  WINPR_ASSERT(context);
2545
2546
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2547
0
  WINPR_ASSERT(gfx);
2548
2549
0
  wLog* log = gfx->base.log;
2550
0
  const DWORD level = WLOG_TRACE;
2551
0
  if (!WLog_IsLevelActive(log, level))
2552
0
    return;
2553
2554
0
  WLog_Print(log, level, "RdpgfxClientContextStats");
2555
0
  for (size_t x = 0; x < rdpgfx_stats_max_index(); x++)
2556
0
  {
2557
0
    const char* name = rdpgfx_stats_name_for_index(x);
2558
0
    const uint64_t val = rdpgfx_stats_value_for_index(context, x);
2559
0
    WLog_Print(log, level, "%s: %" PRIu64, name, val);
2560
0
  }
2561
0
}
2562
2563
/**
2564
 * Function description
2565
 *
2566
 * @return 0 on success, otherwise a Win32 error code
2567
 */
2568
static UINT rdpgfx_on_close(IWTSVirtualChannelCallback* pChannelCallback)
2569
0
{
2570
0
  UINT error = CHANNEL_RC_OK;
2571
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
2572
0
  WINPR_ASSERT(callback);
2573
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)callback->plugin;
2574
2575
0
  if (!gfx)
2576
0
    goto fail;
2577
2578
0
  RdpgfxClientContext* context = gfx->context;
2579
2580
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "OnClose");
2581
0
  error = rdpgfx_save_persistent_cache(gfx);
2582
2583
0
  if (error)
2584
0
  {
2585
    // print error, but don't consider this a hard failure
2586
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2587
0
               "rdpgfx_save_persistent_cache failed with error %" PRIu32 "", error);
2588
0
  }
2589
2590
0
  free_surfaces(context, gfx->SurfaceTable);
2591
0
  error = evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2592
0
  if (error)
2593
0
  {
2594
    // print error, but don't consider this a hard failure
2595
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "evict_cache_slots failed with error %" PRIu32 "",
2596
0
               error);
2597
0
  }
2598
2599
0
  free(callback);
2600
0
  gfx->UnacknowledgedFrames = 0;
2601
0
  gfx->TotalDecodedFrames = 0;
2602
2603
0
  if (context)
2604
0
  {
2605
0
    error = IFCALLRESULT(CHANNEL_RC_OK, context->OnClose, context);
2606
0
    if (error)
2607
0
    {
2608
      // print error, but don't consider this a hard failure
2609
0
      WLog_Print(gfx->base.log, WLOG_ERROR, "context->OnClose failed with error %" PRIu32 "",
2610
0
                 error);
2611
0
    }
2612
0
    rdpgfx_dump_stats(context);
2613
0
  }
2614
2615
0
fail:
2616
0
  return CHANNEL_RC_OK;
2617
0
}
2618
2619
static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
2620
0
{
2621
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2622
0
  WINPR_ASSERT(gfx);
2623
0
  RdpgfxClientContext* context = gfx->context;
2624
2625
0
  WLog_Print(gfx->base.log, WLOG_DEBUG, "Terminated");
2626
0
  rdpgfx_client_context_free(context);
2627
0
}
2628
2629
/**
2630
 * Function description
2631
 *
2632
 * @return 0 on success, otherwise a Win32 error code
2633
 */
2634
static UINT rdpgfx_set_surface_data(RdpgfxClientContext* context, UINT16 surfaceId, void* pData)
2635
0
{
2636
0
  WINPR_ASSERT(context);
2637
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2638
0
  WINPR_ASSERT(gfx);
2639
0
  ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2640
2641
0
  if (pData)
2642
0
  {
2643
0
    if (!HashTable_Insert(gfx->SurfaceTable, (void*)key, pData))
2644
0
      return ERROR_BAD_ARGUMENTS;
2645
0
  }
2646
0
  else
2647
0
    HashTable_Remove(gfx->SurfaceTable, (void*)key);
2648
2649
0
  return CHANNEL_RC_OK;
2650
0
}
2651
2652
/**
2653
 * Function description
2654
 *
2655
 * @return 0 on success, otherwise a Win32 error code
2656
 */
2657
static UINT rdpgfx_get_surface_ids(RdpgfxClientContext* context, UINT16** ppSurfaceIds,
2658
                                   UINT16* count_out)
2659
0
{
2660
0
  ULONG_PTR* pKeys = nullptr;
2661
0
  WINPR_ASSERT(context);
2662
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2663
0
  WINPR_ASSERT(gfx);
2664
0
  size_t count = HashTable_GetKeys(gfx->SurfaceTable, &pKeys);
2665
2666
0
  WINPR_ASSERT(ppSurfaceIds);
2667
0
  WINPR_ASSERT(count_out);
2668
0
  if (count < 1)
2669
0
  {
2670
0
    *count_out = 0;
2671
0
    return CHANNEL_RC_OK;
2672
0
  }
2673
2674
0
  UINT16* pSurfaceIds = (UINT16*)calloc(count, sizeof(UINT16));
2675
2676
0
  if (!pSurfaceIds)
2677
0
  {
2678
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "calloc failed!");
2679
0
    free(pKeys);
2680
0
    return CHANNEL_RC_NO_MEMORY;
2681
0
  }
2682
2683
0
  for (size_t index = 0; index < count; index++)
2684
0
  {
2685
0
    pSurfaceIds[index] = (UINT16)(pKeys[index] - 1);
2686
0
  }
2687
2688
0
  free(pKeys);
2689
0
  *ppSurfaceIds = pSurfaceIds;
2690
0
  *count_out = (UINT16)count;
2691
0
  return CHANNEL_RC_OK;
2692
0
}
2693
2694
static void* rdpgfx_get_surface_data(RdpgfxClientContext* context, UINT16 surfaceId)
2695
0
{
2696
0
  WINPR_ASSERT(context);
2697
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2698
0
  WINPR_ASSERT(gfx);
2699
0
  ULONG_PTR key = ((ULONG_PTR)surfaceId) + 1;
2700
0
  return HashTable_GetItemValue(gfx->SurfaceTable, (void*)key);
2701
0
}
2702
2703
/**
2704
 * Function description
2705
 *
2706
 * @return 0 on success, otherwise a Win32 error code
2707
 */
2708
static UINT rdpgfx_set_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot, void* pData)
2709
0
{
2710
0
  WINPR_ASSERT(context);
2711
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2712
2713
0
  WINPR_ASSERT(gfx);
2714
  /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2715
0
  if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2716
0
  {
2717
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2718
0
               "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2719
0
               gfx->MaxCacheSlots);
2720
0
    return ERROR_INVALID_INDEX;
2721
0
  }
2722
2723
0
  gfx->CacheSlots[cacheSlot - 1] = pData;
2724
0
  return CHANNEL_RC_OK;
2725
0
}
2726
2727
static void* rdpgfx_get_cache_slot_data(RdpgfxClientContext* context, UINT16 cacheSlot)
2728
0
{
2729
0
  WINPR_ASSERT(context);
2730
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2731
0
  WINPR_ASSERT(gfx);
2732
  /* Microsoft uses 1-based indexing for the egfx bitmap cache ! */
2733
0
  if (cacheSlot == 0 || cacheSlot > gfx->MaxCacheSlots)
2734
0
  {
2735
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
2736
0
               "invalid cache slot %" PRIu16 ", must be between 1 and %" PRIu16 "", cacheSlot,
2737
0
               gfx->MaxCacheSlots);
2738
0
    return nullptr;
2739
0
  }
2740
2741
0
  return gfx->CacheSlots[cacheSlot - 1];
2742
0
}
2743
2744
static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext,
2745
                           WINPR_ATTR_UNUSED rdpSettings* settings)
2746
0
{
2747
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)base;
2748
2749
0
  WINPR_ASSERT(base);
2750
0
  gfx->rdpcontext = rcontext;
2751
2752
0
  gfx->SurfaceTable = HashTable_New(TRUE);
2753
0
  if (!gfx->SurfaceTable)
2754
0
  {
2755
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "HashTable_New for surfaces failed !");
2756
0
    return CHANNEL_RC_NO_MEMORY;
2757
0
  }
2758
2759
0
  gfx->suspendFrameAcks =
2760
0
      freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSuspendFrameAck);
2761
0
  gfx->MaxCacheSlots =
2762
0
      freerdp_settings_get_bool(gfx->rdpcontext->settings, FreeRDP_GfxSmallCache) ? 4096 : 25600;
2763
2764
0
  RdpgfxClientContextInt* context =
2765
0
      (RdpgfxClientContextInt*)calloc(1, sizeof(RdpgfxClientContextInt));
2766
0
  if (!context)
2767
0
  {
2768
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "context calloc failed!");
2769
0
    HashTable_Free(gfx->SurfaceTable);
2770
0
    gfx->SurfaceTable = nullptr;
2771
0
    return CHANNEL_RC_NO_MEMORY;
2772
0
  }
2773
2774
0
  gfx->zgfx = zgfx_context_new(FALSE);
2775
0
  if (!gfx->zgfx)
2776
0
  {
2777
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "zgfx_context_new failed!");
2778
0
    HashTable_Free(gfx->SurfaceTable);
2779
0
    gfx->SurfaceTable = nullptr;
2780
0
    free(context);
2781
0
    return CHANNEL_RC_NO_MEMORY;
2782
0
  }
2783
2784
0
  context->common.handle = (void*)gfx;
2785
0
  context->common.GetSurfaceIds = rdpgfx_get_surface_ids;
2786
0
  context->common.SetSurfaceData = rdpgfx_set_surface_data;
2787
0
  context->common.GetSurfaceData = rdpgfx_get_surface_data;
2788
0
  context->common.SetCacheSlotData = rdpgfx_set_cache_slot_data;
2789
0
  context->common.GetCacheSlotData = rdpgfx_get_cache_slot_data;
2790
0
  context->common.CapsAdvertise = rdpgfx_send_caps_advertise_pdu;
2791
0
  context->common.FrameAcknowledge = rdpgfx_send_frame_acknowledge_pdu;
2792
0
  context->common.CacheImportOffer = rdpgfx_send_cache_import_offer_pdu;
2793
0
  context->common.QoeFrameAcknowledge = rdpgfx_send_qoe_frame_acknowledge_pdu;
2794
2795
0
  gfx->base.iface.pInterface = (void*)context;
2796
0
  gfx->context = &context->common;
2797
0
  return CHANNEL_RC_OK;
2798
0
}
2799
2800
void rdpgfx_client_context_free(RdpgfxClientContext* context)
2801
0
{
2802
0
  if (!context)
2803
0
    return;
2804
2805
0
  RDPGFX_PLUGIN* gfx = (RDPGFX_PLUGIN*)context->handle;
2806
2807
0
  free_surfaces(context, gfx->SurfaceTable);
2808
0
  evict_cache_slots(context, gfx->MaxCacheSlots, gfx->CacheSlots);
2809
2810
0
  if (gfx->zgfx)
2811
0
  {
2812
0
    zgfx_context_free(gfx->zgfx);
2813
0
    gfx->zgfx = nullptr;
2814
0
  }
2815
2816
0
  HashTable_Free(gfx->SurfaceTable);
2817
0
  free(context);
2818
0
}
2819
2820
static const IWTSVirtualChannelCallback rdpgfx_callbacks = { rdpgfx_on_data_received,
2821
                                                           rdpgfx_on_open, rdpgfx_on_close,
2822
                                                           nullptr };
2823
2824
/**
2825
 * Function description
2826
 *
2827
 * @return 0 on success, otherwise a Win32 error code
2828
 */
2829
FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpgfx_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
2830
0
{
2831
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, GFXTAG, RDPGFX_DVC_CHANNEL_NAME,
2832
0
                                        sizeof(RDPGFX_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
2833
0
                                        &rdpgfx_callbacks, init_plugin_cb, terminate_plugin_cb);
2834
0
}