Coverage Report

Created: 2026-10-28 06:50

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