Coverage Report

Created: 2026-05-30 06:46

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