Coverage Report

Created: 2026-10-28 06:49

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