Coverage Report

Created: 2025-07-01 06:46

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