Coverage Report

Created: 2026-06-15 06:57

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