Coverage Report

Created: 2026-10-15 06:27

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