Coverage Report

Created: 2026-11-10 06:40

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