Coverage Report

Created: 2026-03-04 06:17

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