Coverage Report

Created: 2026-04-12 07:03

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