Coverage Report

Created: 2024-09-08 06:20

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