Coverage Report

Created: 2026-08-31 06:29

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