Coverage Report

Created: 2026-10-15 06:27

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