Coverage Report

Created: 2026-02-26 06:54

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