Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/gdi/gfx.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * GDI Graphics Pipeline
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016 Thincast Technologies GmbH
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include "../core/update.h"
25
26
#include <winpr/assert.h>
27
#include <winpr/cast.h>
28
29
#include <freerdp/api.h>
30
#include <freerdp/log.h>
31
#include <freerdp/gdi/gfx.h>
32
#include <freerdp/gdi/region.h>
33
#include <freerdp/utils/gfx.h>
34
#include <math.h>
35
36
#define TAG FREERDP_TAG("gdi")
37
38
WINPR_ATTR_NODISCARD
39
static BOOL is_rect_valid(const RECTANGLE_16* rect, size_t width, size_t height)
40
0
{
41
0
  if (!rect)
42
0
    return FALSE;
43
0
  if ((rect->left > rect->right) || (rect->right > width))
44
0
    return FALSE;
45
0
  if ((rect->top > rect->bottom) || (rect->bottom > height))
46
0
    return FALSE;
47
0
  return TRUE;
48
0
}
49
50
WINPR_ATTR_NODISCARD
51
static BOOL is_within_surface(const gdiGfxSurface* surface, const RDPGFX_SURFACE_COMMAND* cmd)
52
0
{
53
0
  RECTANGLE_16 rect;
54
0
  if (!surface || !cmd)
55
0
    return FALSE;
56
0
  rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
57
0
  rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
58
0
  rect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
59
0
  rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
60
0
  if (!is_rect_valid(&rect, surface->width, surface->height))
61
0
  {
62
0
    WLog_ERR(TAG,
63
0
             "Command rect %" PRIu32 "x%" PRIu32 "-%" PRIu32 "x%" PRIu32
64
0
             " not within bounds of %" PRIu32 "x%" PRIu32,
65
0
             rect.left, rect.top, cmd->width, cmd->height, surface->width, surface->height);
66
0
    return FALSE;
67
0
  }
68
0
  if (rect.left > surface->width)
69
0
    return FALSE;
70
0
  if (rect.right > surface->width)
71
0
    return FALSE;
72
0
  if (rect.top > surface->height)
73
0
    return FALSE;
74
0
  if (rect.bottom > surface->height)
75
0
    return FALSE;
76
77
0
  return TRUE;
78
0
}
79
80
WINPR_ATTR_NODISCARD
81
static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
82
0
{
83
0
  const UINT32 align = alignment;
84
0
  const UINT32 pad = align - (widthInBytes % alignment);
85
0
  UINT32 scanline = widthInBytes;
86
87
0
  if (align != pad)
88
0
    scanline += pad;
89
90
0
  return scanline;
91
0
}
92
93
/**
94
 * Function description
95
 *
96
 * @return 0 on success, otherwise a Win32 error code
97
 */
98
WINPR_ATTR_NODISCARD
99
static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
100
                              const RDPGFX_RESET_GRAPHICS_PDU* resetGraphics)
101
0
{
102
0
  UINT rc = ERROR_INTERNAL_ERROR;
103
0
  UINT16 count = 0;
104
0
  UINT16* pSurfaceIds = nullptr;
105
106
0
  WINPR_ASSERT(context);
107
0
  WINPR_ASSERT(resetGraphics);
108
109
0
  rdpGdi* gdi = (rdpGdi*)context->custom;
110
0
  WINPR_ASSERT(gdi);
111
112
0
  rdpUpdate* update = gdi->context->update;
113
0
  WINPR_ASSERT(update);
114
115
0
  rdpSettings* settings = gdi->context->settings;
116
0
  WINPR_ASSERT(settings);
117
0
  EnterCriticalSection(&context->mux);
118
0
  const UINT32 DesktopWidth = resetGraphics->width;
119
0
  const UINT32 DesktopHeight = resetGraphics->height;
120
121
0
  WLog_DBG(TAG, "new size: %" PRIu32 "x%" PRIu32, DesktopWidth, DesktopHeight);
122
123
0
  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, DesktopWidth))
124
0
    goto fail;
125
0
  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, DesktopHeight))
126
0
    goto fail;
127
128
0
  if (update)
129
0
  {
130
0
    WINPR_ASSERT(update->DesktopResize);
131
0
    if (!update->DesktopResize(gdi->context))
132
0
      goto fail;
133
0
  }
134
135
0
  WINPR_ASSERT(context->GetSurfaceIds);
136
0
  const UINT res = context->GetSurfaceIds(context, &pSurfaceIds, &count);
137
0
  if (res != CHANNEL_RC_OK)
138
0
  {
139
0
    rc = res;
140
0
    goto fail;
141
0
  }
142
143
0
  for (UINT32 index = 0; index < count; index++)
144
0
  {
145
0
    WINPR_ASSERT(context->GetSurfaceData);
146
0
    gdiGfxSurface* surface =
147
0
        (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
148
149
0
    if (!surface)
150
0
      continue;
151
152
0
    memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);
153
0
    region16_clear(&surface->invalidRegion);
154
0
  }
155
156
0
  free(pSurfaceIds);
157
158
0
  if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
159
0
  {
160
0
    const UINT32 width = (UINT32)MAX(0, gdi->width);
161
0
    const UINT32 height = (UINT32)MAX(0, gdi->height);
162
163
0
    if (!freerdp_client_codecs_reset(
164
0
            context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))
165
0
    {
166
0
      goto fail;
167
0
    }
168
0
  }
169
170
0
  rc = CHANNEL_RC_OK;
171
0
fail:
172
0
  LeaveCriticalSection(&context->mux);
173
0
  return rc;
174
0
}
175
176
WINPR_ATTR_NODISCARD
177
static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
178
0
{
179
0
  UINT rc = ERROR_INTERNAL_ERROR;
180
0
  UINT32 surfaceX = 0;
181
0
  UINT32 surfaceY = 0;
182
0
  RECTANGLE_16 surfaceRect;
183
0
  const RECTANGLE_16* rects = nullptr;
184
0
  UINT32 nbRects = 0;
185
0
  rdpUpdate* update = nullptr;
186
187
0
  WINPR_ASSERT(gdi);
188
0
  WINPR_ASSERT(gdi->context);
189
0
  WINPR_ASSERT(surface);
190
191
0
  update = gdi->context->update;
192
0
  WINPR_ASSERT(update);
193
194
0
  if (gdi->suppressOutput)
195
0
    return CHANNEL_RC_OK;
196
197
0
  surfaceX = surface->outputOriginX;
198
0
  surfaceY = surface->outputOriginY;
199
0
  surfaceRect.left = 0;
200
0
  surfaceRect.top = 0;
201
0
  surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
202
0
  surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
203
0
  if (!region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion),
204
0
                               &surfaceRect))
205
0
    goto fail;
206
207
0
  const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
208
0
  const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
209
210
0
  rects = region16_rects(&surface->invalidRegion, &nbRects);
211
0
  if (!rects || (nbRects == 0))
212
0
  {
213
0
    rc = CHANNEL_RC_OK;
214
0
    goto fail;
215
0
  }
216
217
0
  if (!update_begin_paint(update))
218
0
    goto fail;
219
220
0
  for (UINT32 i = 0; i < nbRects; i++)
221
0
  {
222
0
    const UINT32 nXSrc = rects[i].left;
223
0
    const UINT32 nYSrc = rects[i].top;
224
0
    const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
225
0
    const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
226
0
    const UINT32 swidth = rects[i].right - rects[i].left;
227
0
    const UINT32 sheight = rects[i].bottom - rects[i].top;
228
0
    const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
229
0
    const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
230
231
0
    if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
232
0
                             dwidth, dheight, surface->data, surface->format, surface->scanline,
233
0
                             nXSrc, nYSrc, swidth, sheight))
234
0
    {
235
0
      rc = CHANNEL_RC_NULL_DATA;
236
0
      goto clear;
237
0
    }
238
239
0
    if (!gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
240
0
                              (INT32)dheight))
241
0
      goto clear;
242
0
  }
243
244
0
  rc = CHANNEL_RC_OK;
245
0
clear:
246
247
0
  if (!update_end_paint(update))
248
0
    rc = ERROR_INTERNAL_ERROR;
249
250
0
fail:
251
0
  region16_clear(&(surface->invalidRegion));
252
0
  return rc;
253
0
}
254
255
WINPR_ATTR_NODISCARD
256
static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
257
0
{
258
0
  WINPR_ASSERT(context);
259
0
  WINPR_ASSERT(surface);
260
0
  return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
261
0
}
262
263
WINPR_ATTR_NODISCARD
264
static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
265
0
{
266
0
  UINT16 count = 0;
267
0
  UINT16* pSurfaceIds = nullptr;
268
269
0
  WINPR_ASSERT(context);
270
271
0
  rdpGdi* gdi = (rdpGdi*)context->custom;
272
0
  WINPR_ASSERT(gdi);
273
274
0
  EnterCriticalSection(&context->mux);
275
276
0
  WINPR_ASSERT(context->GetSurfaceIds);
277
0
  UINT status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
278
0
  if (status != CHANNEL_RC_OK)
279
0
    goto fail;
280
281
0
  for (UINT32 index = 0; index < count; index++)
282
0
  {
283
0
    WINPR_ASSERT(context->GetSurfaceData);
284
0
    gdiGfxSurface* surface =
285
0
        (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
286
287
0
    if (!surface)
288
0
      continue;
289
290
    /* Already handled in UpdateSurfaceArea callbacks */
291
0
    if (context->UpdateSurfaceArea)
292
0
    {
293
0
      if (surface->handleInUpdateSurfaceArea)
294
0
        continue;
295
0
    }
296
297
0
    if (surface->outputMapped)
298
0
      status = gdi_OutputUpdate(gdi, surface);
299
0
    else if (surface->windowMapped)
300
0
      status = gdi_WindowUpdate(context, surface);
301
302
0
    if (status != CHANNEL_RC_OK)
303
0
      break;
304
0
  }
305
306
0
fail:
307
0
  free(pSurfaceIds);
308
0
  LeaveCriticalSection(&context->mux);
309
0
  return status;
310
0
}
311
312
/**
313
 * Function description
314
 *
315
 * @return 0 on success, otherwise a Win32 error code
316
 */
317
WINPR_ATTR_NODISCARD
318
static UINT gdi_StartFrame(RdpgfxClientContext* context, const RDPGFX_START_FRAME_PDU* startFrame)
319
0
{
320
0
  rdpGdi* gdi = nullptr;
321
322
0
  WINPR_ASSERT(context);
323
0
  WINPR_ASSERT(startFrame);
324
325
0
  gdi = (rdpGdi*)context->custom;
326
0
  WINPR_ASSERT(gdi);
327
0
  gdi->inGfxFrame = TRUE;
328
0
  gdi->frameId = startFrame->frameId;
329
0
  return CHANNEL_RC_OK;
330
0
}
331
332
WINPR_ATTR_NODISCARD
333
static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
334
0
{
335
0
  WINPR_ASSERT(context);
336
0
  return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
337
0
}
338
339
/**
340
 * Function description
341
 *
342
 * @return 0 on success, otherwise a Win32 error code
343
 */
344
WINPR_ATTR_NODISCARD
345
static UINT gdi_EndFrame(RdpgfxClientContext* context,
346
                         WINPR_ATTR_UNUSED const RDPGFX_END_FRAME_PDU* endFrame)
347
0
{
348
0
  WINPR_ASSERT(context);
349
0
  WINPR_ASSERT(endFrame);
350
351
0
  rdpGdi* gdi = (rdpGdi*)context->custom;
352
0
  WINPR_ASSERT(gdi);
353
0
  const UINT status = gdi_call_update_surfaces(context);
354
0
  gdi->inGfxFrame = FALSE;
355
0
  return status;
356
0
}
357
358
WINPR_ATTR_NODISCARD
359
static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
360
0
{
361
0
  WINPR_ASSERT(gdi);
362
0
  UINT status = CHANNEL_RC_OK;
363
0
  if (!gdi->inGfxFrame)
364
0
    status = gdi_call_update_surfaces(context);
365
0
  return status;
366
0
}
367
368
/**
369
 * Function description
370
 *
371
 * @return 0 on success, otherwise a Win32 error code
372
 */
373
WINPR_ATTR_NODISCARD
374
static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
375
                                            const RDPGFX_SURFACE_COMMAND* cmd)
376
0
{
377
0
  UINT status = CHANNEL_RC_OK;
378
0
  gdiGfxSurface* surface = nullptr;
379
0
  RECTANGLE_16 invalidRect;
380
0
  DWORD bpp = 0;
381
0
  size_t size = 0;
382
0
  WINPR_ASSERT(gdi);
383
0
  WINPR_ASSERT(context);
384
0
  WINPR_ASSERT(cmd);
385
386
0
  WINPR_ASSERT(context->GetSurfaceData);
387
0
  surface =
388
0
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
389
390
0
  if (!surface)
391
0
  {
392
0
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
393
0
    return ERROR_NOT_FOUND;
394
0
  }
395
396
0
  if (!is_within_surface(surface, cmd))
397
0
    return ERROR_INVALID_DATA;
398
399
0
  bpp = FreeRDPGetBytesPerPixel(cmd->format);
400
0
  size = 1ull * bpp * cmd->width * cmd->height;
401
0
  if (cmd->length < size)
402
0
  {
403
0
    WLog_ERR(TAG, "Not enough data, got %" PRIu32 ", expected %" PRIuz, cmd->length, size);
404
0
    return ERROR_INVALID_DATA;
405
0
  }
406
407
0
  if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
408
0
                                     cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
409
0
                                     0, 0, nullptr, FREERDP_FLIP_NONE))
410
0
    return ERROR_INTERNAL_ERROR;
411
412
0
  invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
413
0
  invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
414
0
  invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
415
0
  invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
416
0
  if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
417
0
    goto fail;
418
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
419
0
                        &invalidRect);
420
421
0
  if (status != CHANNEL_RC_OK)
422
0
    goto fail;
423
424
0
  status = gdi_interFrameUpdate(gdi, context);
425
426
0
fail:
427
0
  return status;
428
0
}
429
430
#if defined(WITH_GFX_AV1)
431
WINPR_ATTR_NODISCARD
432
static UINT gdi_SurfaceCommand_AV1(rdpGdi* gdi, RdpgfxClientContext* context,
433
                                   const RDPGFX_SURFACE_COMMAND* cmd)
434
{
435
  INT32 rc = 0;
436
  UINT status = CHANNEL_RC_OK;
437
  gdiGfxSurface* surface = nullptr;
438
  RDPGFX_H264_METABLOCK* meta = nullptr;
439
  RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
440
  WINPR_ASSERT(gdi);
441
  WINPR_ASSERT(context);
442
  WINPR_ASSERT(cmd);
443
444
  WINPR_ASSERT(context->GetSurfaceData);
445
  surface =
446
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
447
448
  if (!surface)
449
  {
450
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
451
    return ERROR_NOT_FOUND;
452
  }
453
454
  if (!surface->av1)
455
  {
456
    surface->av1 = freerdp_av1_context_new(FALSE);
457
458
    if (!surface->av1)
459
    {
460
      WLog_ERR(TAG, "unable to create av1 context");
461
      return ERROR_NOT_ENOUGH_MEMORY;
462
    }
463
464
    if (!freerdp_av1_context_reset(surface->av1, surface->width, surface->height))
465
      return ERROR_INTERNAL_ERROR;
466
  }
467
468
  if (!surface->av1)
469
    return ERROR_NOT_SUPPORTED;
470
471
  if (!is_within_surface(surface, cmd))
472
    return ERROR_INVALID_DATA;
473
474
  bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
475
476
  if (!bs)
477
    return ERROR_INTERNAL_ERROR;
478
479
  meta = &(bs->meta);
480
  rc = freerdp_av1_decompress(surface->av1, bs->data, bs->length, surface->data, surface->format,
481
                              surface->scanline, surface->width, surface->height,
482
                              meta->regionRects, meta->numRegionRects);
483
484
  if (rc < 0)
485
  {
486
    WLog_WARN(TAG, "freerdp_av1_decompress failure: %" PRId32 ", ignoring update.", rc);
487
    return CHANNEL_RC_OK;
488
  }
489
490
  for (UINT32 i = 0; i < meta->numRegionRects; i++)
491
  {
492
    if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
493
                             &(meta->regionRects[i])))
494
      goto fail;
495
  }
496
497
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
498
                        meta->numRegionRects, meta->regionRects);
499
500
  if (status != CHANNEL_RC_OK)
501
    goto fail;
502
503
  status = gdi_interFrameUpdate(gdi, context);
504
505
fail:
506
  return status;
507
}
508
#endif
509
510
/**
511
 * Function description
512
 *
513
 * @return 0 on success, otherwise a Win32 error code
514
 */
515
WINPR_ATTR_NODISCARD
516
static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
517
                                        const RDPGFX_SURFACE_COMMAND* cmd)
518
0
{
519
0
  UINT status = ERROR_INTERNAL_ERROR;
520
0
  gdiGfxSurface* surface = nullptr;
521
0
  REGION16 invalidRegion;
522
0
  const RECTANGLE_16* rects = nullptr;
523
0
  UINT32 nrRects = 0;
524
0
  WINPR_ASSERT(gdi);
525
0
  WINPR_ASSERT(context);
526
0
  WINPR_ASSERT(cmd);
527
528
0
  WINPR_ASSERT(context->GetSurfaceData);
529
0
  surface =
530
0
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
531
532
0
  if (!surface)
533
0
  {
534
0
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
535
0
    return ERROR_NOT_FOUND;
536
0
  }
537
538
0
  if (!is_within_surface(surface, cmd))
539
0
    return ERROR_INVALID_DATA;
540
541
0
  WINPR_ASSERT(surface->codecs);
542
0
  rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
543
0
  region16_init(&invalidRegion);
544
545
0
  if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
546
0
                           surface->data, surface->format, surface->scanline, surface->height,
547
0
                           &invalidRegion))
548
0
  {
549
0
    WLog_ERR(TAG, "Failed to process RemoteFX message");
550
0
    goto fail;
551
0
  }
552
553
0
  rects = region16_rects(&invalidRegion, &nrRects);
554
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
555
0
                        nrRects, rects);
556
557
0
  if (status != CHANNEL_RC_OK)
558
0
    goto fail;
559
560
0
  for (UINT32 x = 0; x < nrRects; x++)
561
0
  {
562
0
    if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
563
0
      goto fail;
564
0
  }
565
566
0
  status = gdi_interFrameUpdate(gdi, context);
567
568
0
fail:
569
0
  region16_uninit(&invalidRegion);
570
0
  return status;
571
0
}
572
573
/**
574
 * Function description
575
 *
576
 * @return 0 on success, otherwise a Win32 error code
577
 */
578
WINPR_ATTR_NODISCARD
579
static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
580
                                          const RDPGFX_SURFACE_COMMAND* cmd)
581
0
{
582
0
  INT32 rc = 0;
583
0
  UINT status = CHANNEL_RC_OK;
584
0
  gdiGfxSurface* surface = nullptr;
585
0
  RECTANGLE_16 invalidRect;
586
0
  WINPR_ASSERT(gdi);
587
0
  WINPR_ASSERT(context);
588
0
  WINPR_ASSERT(cmd);
589
590
0
  WINPR_ASSERT(context->GetSurfaceData);
591
0
  surface =
592
0
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
593
594
0
  if (!surface)
595
0
  {
596
0
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
597
0
    return ERROR_NOT_FOUND;
598
0
  }
599
600
0
  if (!is_within_surface(surface, cmd))
601
0
    return ERROR_INVALID_DATA;
602
603
0
  WINPR_ASSERT(surface->codecs);
604
0
  rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
605
0
                        surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
606
0
                        surface->width, surface->height, &gdi->palette);
607
608
0
  if (rc < 0)
609
0
  {
610
0
    WLog_ERR(TAG, "clear_decompress failure: %" PRId32 "", rc);
611
0
    return ERROR_INTERNAL_ERROR;
612
0
  }
613
614
0
  invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
615
0
  invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
616
0
  invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
617
0
  invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
618
0
  if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
619
0
    goto fail;
620
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
621
0
                        &invalidRect);
622
623
0
  if (status != CHANNEL_RC_OK)
624
0
    goto fail;
625
626
0
  status = gdi_interFrameUpdate(gdi, context);
627
628
0
fail:
629
0
  return status;
630
0
}
631
632
/**
633
 * Function description
634
 *
635
 * @return 0 on success, otherwise a Win32 error code
636
 */
637
WINPR_ATTR_NODISCARD
638
static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
639
                                      const RDPGFX_SURFACE_COMMAND* cmd)
640
0
{
641
0
  UINT status = CHANNEL_RC_OK;
642
0
  BYTE* DstData = nullptr;
643
0
  gdiGfxSurface* surface = nullptr;
644
0
  RECTANGLE_16 invalidRect;
645
0
  WINPR_ASSERT(gdi);
646
0
  WINPR_ASSERT(context);
647
0
  WINPR_ASSERT(cmd);
648
649
0
  WINPR_ASSERT(context->GetSurfaceData);
650
0
  surface =
651
0
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
652
653
0
  if (!surface)
654
0
  {
655
0
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
656
0
    return ERROR_NOT_FOUND;
657
0
  }
658
659
0
  DstData = surface->data;
660
661
0
  if (!is_within_surface(surface, cmd))
662
0
    return ERROR_INVALID_DATA;
663
664
0
  if (!freerdp_bitmap_decompress_planar(surface->codecs->planar, cmd->data, cmd->length,
665
0
                                        cmd->width, cmd->height, DstData, surface->format,
666
0
                                        surface->scanline, cmd->left, cmd->top, cmd->width,
667
0
                                        cmd->height, FALSE))
668
0
    return ERROR_INTERNAL_ERROR;
669
670
0
  invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
671
0
  invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
672
0
  invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
673
0
  invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
674
0
  if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
675
0
    goto fail;
676
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
677
0
                        &invalidRect);
678
679
0
  if (status != CHANNEL_RC_OK)
680
0
    goto fail;
681
682
0
  status = gdi_interFrameUpdate(gdi, context);
683
684
0
fail:
685
0
  return status;
686
0
}
687
688
/**
689
 * Function description
690
 *
691
 * @return 0 on success, otherwise a Win32 error code
692
 */
693
WINPR_ATTR_NODISCARD
694
static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
695
                                      const RDPGFX_SURFACE_COMMAND* cmd)
696
0
{
697
#ifdef WITH_GFX_H264
698
  INT32 rc = 0;
699
  UINT status = CHANNEL_RC_OK;
700
  gdiGfxSurface* surface = nullptr;
701
  RDPGFX_H264_METABLOCK* meta = nullptr;
702
  RDPGFX_AVC420_BITMAP_STREAM* bs = nullptr;
703
  WINPR_ASSERT(gdi);
704
  WINPR_ASSERT(context);
705
  WINPR_ASSERT(cmd);
706
707
  WINPR_ASSERT(context->GetSurfaceData);
708
  surface =
709
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
710
711
  if (!surface)
712
  {
713
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
714
    return ERROR_NOT_FOUND;
715
  }
716
717
  if (!surface->h264)
718
  {
719
    surface->h264 = h264_context_new(FALSE);
720
721
    if (!surface->h264)
722
    {
723
      WLog_ERR(TAG, "unable to create h264 context");
724
      return ERROR_NOT_ENOUGH_MEMORY;
725
    }
726
727
    if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
728
                                 (UINT32)freerdp_settings_get_bool(gdi->context->settings,
729
                                                                   FreeRDP_SoftwareGdi) == 0))
730
      return ERROR_INTERNAL_ERROR;
731
732
    if (!h264_context_reset(surface->h264, surface->width, surface->height))
733
      return ERROR_INTERNAL_ERROR;
734
  }
735
736
  if (!surface->h264)
737
    return ERROR_NOT_SUPPORTED;
738
739
  if (!is_within_surface(surface, cmd))
740
    return ERROR_INVALID_DATA;
741
742
  bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
743
744
  if (!bs)
745
    return ERROR_INTERNAL_ERROR;
746
747
  meta = &(bs->meta);
748
  rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
749
                         surface->scanline, surface->width, surface->height, meta->regionRects,
750
                         meta->numRegionRects);
751
752
  if (rc < 0)
753
  {
754
    WLog_WARN(TAG, "avc420_decompress failure: %" PRId32 ", ignoring update.", rc);
755
    return CHANNEL_RC_OK;
756
  }
757
758
  for (UINT32 i = 0; i < meta->numRegionRects; i++)
759
  {
760
    if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
761
                             &(meta->regionRects[i])))
762
      goto fail;
763
  }
764
765
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
766
                        meta->numRegionRects, meta->regionRects);
767
768
  if (status != CHANNEL_RC_OK)
769
    goto fail;
770
771
  status = gdi_interFrameUpdate(gdi, context);
772
773
fail:
774
  return status;
775
#else
776
0
  return ERROR_NOT_SUPPORTED;
777
0
#endif
778
0
}
779
780
/**
781
 * Function description
782
 *
783
 * @return 0 on success, otherwise a Win32 error code
784
 */
785
WINPR_ATTR_NODISCARD
786
static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
787
                                      const RDPGFX_SURFACE_COMMAND* cmd)
788
0
{
789
#ifdef WITH_GFX_H264
790
  INT32 rc = 0;
791
  UINT status = CHANNEL_RC_OK;
792
  gdiGfxSurface* surface = nullptr;
793
  RDPGFX_AVC444_BITMAP_STREAM* bs = nullptr;
794
  RDPGFX_AVC420_BITMAP_STREAM* avc1 = nullptr;
795
  RDPGFX_H264_METABLOCK* meta1 = nullptr;
796
  RDPGFX_AVC420_BITMAP_STREAM* avc2 = nullptr;
797
  RDPGFX_H264_METABLOCK* meta2 = nullptr;
798
  WINPR_ASSERT(gdi);
799
  WINPR_ASSERT(context);
800
  WINPR_ASSERT(cmd);
801
802
  WINPR_ASSERT(context->GetSurfaceData);
803
  surface =
804
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
805
806
  if (!surface)
807
  {
808
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
809
    return ERROR_NOT_FOUND;
810
  }
811
812
  if (!surface->h264)
813
  {
814
    surface->h264 = h264_context_new(FALSE);
815
816
    if (!surface->h264)
817
    {
818
      WLog_ERR(TAG, "unable to create h264 context");
819
      return ERROR_NOT_ENOUGH_MEMORY;
820
    }
821
822
    if (!h264_context_set_option(surface->h264, H264_CONTEXT_OPTION_HW_ACCEL,
823
                                 (UINT32)freerdp_settings_get_bool(gdi->context->settings,
824
                                                                   FreeRDP_SoftwareGdi) == 0))
825
      return ERROR_INTERNAL_ERROR;
826
    if (!h264_context_reset(surface->h264, surface->width, surface->height))
827
      return ERROR_INTERNAL_ERROR;
828
  }
829
830
  if (!surface->h264)
831
    return ERROR_NOT_SUPPORTED;
832
833
  if (!is_within_surface(surface, cmd))
834
    return ERROR_INVALID_DATA;
835
836
  bs = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
837
838
  if (!bs)
839
    return ERROR_INTERNAL_ERROR;
840
841
  avc1 = &bs->bitstream[0];
842
  avc2 = &bs->bitstream[1];
843
  meta1 = &avc1->meta;
844
  meta2 = &avc2->meta;
845
  rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
846
                         avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
847
                         avc2->data, avc2->length, surface->data, surface->format,
848
                         surface->scanline, surface->width, surface->height, cmd->codecId);
849
850
  if (rc < 0)
851
  {
852
    WLog_WARN(TAG, "avc444_decompress failure: %" PRId32 ", ignoring update.", rc);
853
    return CHANNEL_RC_OK;
854
  }
855
856
  for (UINT32 i = 0; i < meta1->numRegionRects; i++)
857
  {
858
    if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
859
                             &(meta1->regionRects[i])))
860
      goto fail;
861
  }
862
863
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
864
                        meta1->numRegionRects, meta1->regionRects);
865
866
  if (status != CHANNEL_RC_OK)
867
    goto fail;
868
869
  for (UINT32 i = 0; i < meta2->numRegionRects; i++)
870
  {
871
    if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
872
                             &(meta2->regionRects[i])))
873
      goto fail;
874
  }
875
876
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
877
                        meta2->numRegionRects, meta2->regionRects);
878
879
  if (status != CHANNEL_RC_OK)
880
    goto fail;
881
882
  status = gdi_interFrameUpdate(gdi, context);
883
884
fail:
885
  return status;
886
#else
887
0
  return ERROR_NOT_SUPPORTED;
888
0
#endif
889
0
}
890
891
WINPR_ATTR_NODISCARD
892
static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride, RECTANGLE_16* rect,
893
                            UINT32 startOffsetX, UINT32 count, BYTE a)
894
0
{
895
0
  UINT32 written = 0;
896
0
  BOOL first = TRUE;
897
0
  const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
898
0
  WINPR_ASSERT(rect);
899
900
0
  for (size_t y = rect->top; y < rect->bottom; y++)
901
0
  {
902
0
    BYTE* line = &data[y * stride];
903
904
0
    for (size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
905
0
    {
906
0
      BYTE r = 0;
907
0
      BYTE g = 0;
908
0
      BYTE b = 0;
909
910
0
      if (written == count)
911
0
        return TRUE;
912
913
0
      BYTE* src = &line[x * bpp];
914
0
      UINT32 color = FreeRDPReadColor(src, format);
915
0
      FreeRDPSplitColor(color, format, &r, &g, &b, nullptr, nullptr);
916
0
      color = FreeRDPGetColor(format, r, g, b, a);
917
0
      FreeRDPWriteColor(src, format, color);
918
0
      written++;
919
0
    }
920
921
0
    first = FALSE;
922
0
  }
923
924
0
  return TRUE;
925
0
}
926
/**
927
 * Function description
928
 *
929
 * @return 0 on success, otherwise a Win32 error code
930
 */
931
WINPR_ATTR_NODISCARD
932
static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
933
                                     const RDPGFX_SURFACE_COMMAND* cmd)
934
0
{
935
0
  UINT status = CHANNEL_RC_OK;
936
0
  UINT16 alphaSig = 0;
937
0
  UINT16 compressed = 0;
938
0
  RECTANGLE_16 invalidRect;
939
0
  wStream buffer = WINPR_C_ARRAY_INIT;
940
0
  WINPR_ASSERT(gdi);
941
0
  WINPR_ASSERT(context);
942
0
  WINPR_ASSERT(cmd);
943
944
0
  wStream* s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
945
946
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
947
0
    return ERROR_INVALID_DATA;
948
949
0
  WINPR_ASSERT(context->GetSurfaceData);
950
0
  gdiGfxSurface* surface =
951
0
      (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
952
953
0
  if (!surface)
954
0
  {
955
0
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
956
0
    return ERROR_NOT_FOUND;
957
0
  }
958
959
0
  if (!is_within_surface(surface, cmd))
960
0
    return ERROR_INVALID_DATA;
961
962
0
  Stream_Read_UINT16(s, alphaSig);
963
0
  Stream_Read_UINT16(s, compressed);
964
965
0
  if (alphaSig != 0x414C)
966
0
    return ERROR_INVALID_DATA;
967
968
0
  if (compressed == 0)
969
0
  {
970
0
    if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
971
0
      return ERROR_INVALID_DATA;
972
973
0
    for (size_t y = cmd->top; y < cmd->top + cmd->height; y++)
974
0
    {
975
0
      BYTE* line = &surface->data[y * surface->scanline];
976
977
0
      for (size_t x = cmd->left; x < cmd->left + cmd->width; x++)
978
0
      {
979
0
        UINT32 color = 0;
980
0
        BYTE r = 0;
981
0
        BYTE g = 0;
982
0
        BYTE b = 0;
983
0
        BYTE a = 0;
984
0
        BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
985
0
        Stream_Read_UINT8(s, a);
986
0
        color = FreeRDPReadColor(src, surface->format);
987
0
        FreeRDPSplitColor(color, surface->format, &r, &g, &b, nullptr, nullptr);
988
0
        color = FreeRDPGetColor(surface->format, r, g, b, a);
989
0
        FreeRDPWriteColor(src, surface->format, color);
990
0
      }
991
0
    }
992
0
  }
993
0
  else
994
0
  {
995
0
    UINT32 startOffsetX = 0;
996
0
    RECTANGLE_16 rect = WINPR_C_ARRAY_INIT;
997
0
    rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
998
0
    rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
999
0
    rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);
1000
0
    rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);
1001
1002
0
    while (rect.top < rect.bottom)
1003
0
    {
1004
0
      UINT32 count = 0;
1005
0
      BYTE a = 0;
1006
1007
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1008
0
        return ERROR_INVALID_DATA;
1009
1010
0
      Stream_Read_UINT8(s, a);
1011
0
      Stream_Read_UINT8(s, count);
1012
1013
0
      if (count >= 0xFF)
1014
0
      {
1015
0
        if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1016
0
          return ERROR_INVALID_DATA;
1017
1018
0
        Stream_Read_UINT16(s, count);
1019
1020
0
        if (count >= 0xFFFF)
1021
0
        {
1022
0
          if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1023
0
            return ERROR_INVALID_DATA;
1024
1025
0
          Stream_Read_UINT32(s, count);
1026
0
        }
1027
0
      }
1028
1029
0
      if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
1030
0
                           startOffsetX, count, a))
1031
0
        return ERROR_INTERNAL_ERROR;
1032
1033
0
      startOffsetX += count;
1034
1035
0
      while (startOffsetX >= cmd->width)
1036
0
      {
1037
0
        startOffsetX -= cmd->width;
1038
0
        rect.top++;
1039
0
      }
1040
0
    }
1041
0
  }
1042
1043
0
  invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
1044
0
  invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
1045
0
  invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
1046
0
  invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
1047
0
  if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect))
1048
0
    goto fail;
1049
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
1050
0
                        &invalidRect);
1051
1052
0
  if (status != CHANNEL_RC_OK)
1053
0
    goto fail;
1054
1055
0
  status = gdi_interFrameUpdate(gdi, context);
1056
1057
0
fail:
1058
0
  return status;
1059
0
}
1060
1061
#if defined(WITH_GFX_FRAME_DUMP)
1062
static void dump_cmd(const RDPGFX_SURFACE_COMMAND* cmd, UINT32 frameId)
1063
{
1064
  static UINT64 xxx = 0;
1065
  const char* path = "/tmp/dump/";
1066
  WINPR_ASSERT(cmd);
1067
  char fname[1024] = WINPR_C_ARRAY_INIT;
1068
1069
  if (_snprintf(fname, sizeof(fname), "%s/%08" PRIx64 ".raw", path, xxx++) < 0)
1070
    return;
1071
1072
  FILE* fp = fopen(fname, "w");
1073
  if (!fp)
1074
    return;
1075
  (void)fprintf(fp, "frameid: %" PRIu32 "\n", frameId);
1076
  (void)fprintf(fp, "surfaceId: %" PRIu32 "\n", cmd->surfaceId);
1077
  (void)fprintf(fp, "codecId: %" PRIu32 "\n", cmd->codecId);
1078
  (void)fprintf(fp, "contextId: %" PRIu32 "\n", cmd->contextId);
1079
  (void)fprintf(fp, "format: %" PRIu32 "\n", cmd->format);
1080
  (void)fprintf(fp, "left: %" PRIu32 "\n", cmd->left);
1081
  (void)fprintf(fp, "top: %" PRIu32 "\n", cmd->top);
1082
  (void)fprintf(fp, "right: %" PRIu32 "\n", cmd->right);
1083
  (void)fprintf(fp, "bottom: %" PRIu32 "\n", cmd->bottom);
1084
  (void)fprintf(fp, "width: %" PRIu32 "\n", cmd->width);
1085
  (void)fprintf(fp, "height: %" PRIu32 "\n", cmd->height);
1086
  (void)fprintf(fp, "length: %" PRIu32 "\n", cmd->length);
1087
1088
  char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
1089
  (void)fprintf(fp, "data: %s\n", bdata);
1090
  free(bdata);
1091
  fclose(fp);
1092
}
1093
#endif
1094
1095
/**
1096
 * Function description
1097
 *
1098
 * @return 0 on success, otherwise a Win32 error code
1099
 */
1100
WINPR_ATTR_NODISCARD
1101
static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
1102
                                           const RDPGFX_SURFACE_COMMAND* cmd)
1103
0
{
1104
0
  INT32 rc = 0;
1105
0
  UINT status = ERROR_INTERNAL_ERROR;
1106
0
  gdiGfxSurface* surface = nullptr;
1107
0
  REGION16 invalidRegion;
1108
0
  const RECTANGLE_16* rects = nullptr;
1109
0
  UINT32 nrRects = 0;
1110
  /**
1111
   * Note: Since this comes via a Wire-To-Surface-2 PDU the
1112
   * cmd's top/left/right/bottom/width/height members are always zero!
1113
   * The update region is determined during decompression.
1114
   */
1115
0
  WINPR_ASSERT(gdi);
1116
0
  WINPR_ASSERT(context);
1117
0
  WINPR_ASSERT(cmd);
1118
0
  const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
1119
1120
0
  WINPR_ASSERT(context->GetSurfaceData);
1121
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
1122
1123
0
  if (!surface)
1124
0
  {
1125
0
    WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
1126
0
    return ERROR_NOT_FOUND;
1127
0
  }
1128
1129
0
  if (!is_within_surface(surface, cmd))
1130
0
    return ERROR_INVALID_DATA;
1131
1132
0
  WINPR_ASSERT(surface->codecs);
1133
0
  rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
1134
0
                                          surface->height);
1135
1136
0
  if (rc < 0)
1137
0
  {
1138
0
    WLog_ERR(TAG, "progressive_create_surface_context failure: %" PRId32 "", rc);
1139
0
    return ERROR_INTERNAL_ERROR;
1140
0
  }
1141
1142
0
  region16_init(&invalidRegion);
1143
1144
0
  rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
1145
0
                              surface->format, surface->scanline, cmd->left, cmd->top,
1146
0
                              &invalidRegion, surfaceId, gdi->frameId);
1147
1148
0
  if (rc < 0)
1149
0
  {
1150
0
    WLog_ERR(TAG, "progressive_decompress failure: %" PRId32 "", rc);
1151
0
    goto fail;
1152
0
  }
1153
1154
0
  rects = region16_rects(&invalidRegion, &nrRects);
1155
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1156
0
                        nrRects, rects);
1157
1158
0
  if (status != CHANNEL_RC_OK)
1159
0
    goto fail;
1160
1161
0
  status = ERROR_INTERNAL_ERROR;
1162
0
  for (UINT32 x = 0; x < nrRects; x++)
1163
0
  {
1164
0
    if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]))
1165
0
      goto fail;
1166
0
  }
1167
1168
0
  status = gdi_interFrameUpdate(gdi, context);
1169
1170
0
fail:
1171
1172
0
  region16_uninit(&invalidRegion);
1173
0
  return status;
1174
0
}
1175
1176
/**
1177
 * Function description
1178
 *
1179
 * @return 0 on success, otherwise a Win32 error code
1180
 */
1181
WINPR_ATTR_NODISCARD
1182
static UINT gdi_SurfaceCommand(RdpgfxClientContext* context, const RDPGFX_SURFACE_COMMAND* cmd)
1183
0
{
1184
0
  UINT status = CHANNEL_RC_OK;
1185
0
  rdpGdi* gdi = nullptr;
1186
1187
0
  if (!context || !cmd)
1188
0
    return ERROR_INVALID_PARAMETER;
1189
1190
0
  gdi = (rdpGdi*)context->custom;
1191
1192
0
  EnterCriticalSection(&context->mux);
1193
0
  const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1194
0
  WLog_Print(gdi->log, WLOG_TRACE,
1195
0
             "surfaceId=%" PRIu32 ", codec=%s [%" PRIu32 "], contextId=%" PRIu32 ", format=%s, "
1196
0
             "left=%" PRIu32 ", top=%" PRIu32 ", right=%" PRIu32 ", bottom=%" PRIu32
1197
0
             ", width=%" PRIu32 ", height=%" PRIu32 " "
1198
0
             "length=%" PRIu32 ", data=%p, extra=%p",
1199
0
             cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1200
0
             FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1201
0
             cmd->width, cmd->height, cmd->length, (void*)cmd->data, (void*)cmd->extra);
1202
#if defined(WITH_GFX_FRAME_DUMP)
1203
  dump_cmd(cmd, gdi->frameId);
1204
#endif
1205
1206
0
  switch (codecId)
1207
0
  {
1208
0
    case RDPGFX_CODECID_UNCOMPRESSED:
1209
0
      status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1210
0
      break;
1211
1212
#if defined(WITH_GFX_AV1)
1213
    case RDPGFX_CODECID_AV1:
1214
      status = gdi_SurfaceCommand_AV1(gdi, context, cmd);
1215
      break;
1216
#endif
1217
1218
0
    case RDPGFX_CODECID_CAVIDEO:
1219
0
      status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1220
0
      break;
1221
1222
0
    case RDPGFX_CODECID_CLEARCODEC:
1223
0
      status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1224
0
      break;
1225
1226
0
    case RDPGFX_CODECID_PLANAR:
1227
0
      status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1228
0
      break;
1229
1230
0
    case RDPGFX_CODECID_AVC420:
1231
0
      status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1232
0
      break;
1233
1234
0
    case RDPGFX_CODECID_AVC444v2:
1235
0
    case RDPGFX_CODECID_AVC444:
1236
0
      status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1237
0
      break;
1238
1239
0
    case RDPGFX_CODECID_ALPHA:
1240
0
      status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1241
0
      break;
1242
1243
0
    case RDPGFX_CODECID_CAPROGRESSIVE:
1244
0
      status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1245
0
      break;
1246
1247
0
    case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1248
0
      WLog_WARN(TAG, "SurfaceCommand %s [0x%08" PRIX16 "] not implemented",
1249
0
                rdpgfx_get_codec_id_string(codecId), codecId);
1250
0
      break;
1251
1252
0
    default:
1253
0
      WLog_WARN(TAG, "Invalid SurfaceCommand %s [0x%08" PRIX16 "]",
1254
0
                rdpgfx_get_codec_id_string(codecId), codecId);
1255
0
      break;
1256
0
  }
1257
1258
0
  LeaveCriticalSection(&context->mux);
1259
0
  return status;
1260
0
}
1261
1262
/**
1263
 * Function description
1264
 *
1265
 * @return 0 on success, otherwise a Win32 error code
1266
 */
1267
WINPR_ATTR_NODISCARD
1268
static UINT
1269
gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1270
                          const RDPGFX_DELETE_ENCODING_CONTEXT_PDU* deleteEncodingContext)
1271
0
{
1272
0
  WINPR_ASSERT(context);
1273
0
  WINPR_ASSERT(deleteEncodingContext);
1274
0
  WINPR_UNUSED(context);
1275
0
  WINPR_UNUSED(deleteEncodingContext);
1276
0
  return CHANNEL_RC_OK;
1277
0
}
1278
1279
/**
1280
 * Function description
1281
 *
1282
 * @return 0 on success, otherwise a Win32 error code
1283
 */
1284
WINPR_ATTR_NODISCARD
1285
static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1286
                              const RDPGFX_CREATE_SURFACE_PDU* createSurface)
1287
0
{
1288
0
  UINT rc = ERROR_INTERNAL_ERROR;
1289
0
  gdiGfxSurface* surface = nullptr;
1290
0
  rdpGdi* gdi = nullptr;
1291
0
  WINPR_ASSERT(context);
1292
0
  WINPR_ASSERT(createSurface);
1293
0
  gdi = (rdpGdi*)context->custom;
1294
0
  WINPR_ASSERT(gdi);
1295
0
  WINPR_ASSERT(gdi->context);
1296
0
  EnterCriticalSection(&context->mux);
1297
0
  surface = (gdiGfxSurface*)calloc(1, sizeof(gdiGfxSurface));
1298
1299
0
  if (!surface)
1300
0
    goto fail;
1301
1302
0
  if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
1303
0
  {
1304
0
    WINPR_ASSERT(context->codecs);
1305
0
    surface->codecs = context->codecs;
1306
1307
0
    if (!surface->codecs)
1308
0
    {
1309
0
      free(surface);
1310
0
      goto fail;
1311
0
    }
1312
0
  }
1313
1314
0
  surface->surfaceId = createSurface->surfaceId;
1315
0
  surface->width = gfx_align_scanline(createSurface->width, 16);
1316
0
  surface->height = gfx_align_scanline(createSurface->height, 16);
1317
0
  surface->mappedWidth = createSurface->width;
1318
0
  surface->mappedHeight = createSurface->height;
1319
0
  surface->outputTargetWidth = createSurface->width;
1320
0
  surface->outputTargetHeight = createSurface->height;
1321
1322
0
  const BOOL rails =
1323
0
      freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode);
1324
0
  switch (createSurface->pixelFormat)
1325
0
  {
1326
0
    case GFX_PIXEL_FORMAT_ARGB_8888:
1327
0
      surface->format = PIXEL_FORMAT_BGRA32;
1328
0
      break;
1329
1330
0
    case GFX_PIXEL_FORMAT_XRGB_8888:
1331
0
      surface->format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
1332
0
      break;
1333
1334
0
    default:
1335
0
      free(surface);
1336
0
      goto fail;
1337
0
  }
1338
1339
0
  if (surface->width > UINT32_MAX / 4 - 2 * 16)
1340
0
  {
1341
0
    free(surface);
1342
0
    goto fail;
1343
0
  }
1344
1345
  /* Align to multiples of 16, but also add additional 16 bytes so SIMD implementations do not
1346
   * accidentally read out of bound */
1347
0
  surface->scanline = gfx_align_scanline(surface->width * 4UL, 16) + 16ul;
1348
0
  surface->data = (BYTE*)winpr_aligned_calloc(surface->scanline, surface->height, 16);
1349
1350
0
  if (!surface->data)
1351
0
  {
1352
0
    free(surface);
1353
0
    goto fail;
1354
0
  }
1355
1356
0
  for (UINT64 y = 0; y < surface->height; y++)
1357
0
    memset(&surface->data[surface->scanline * y], 0xFF, surface->scanline);
1358
0
  region16_init(&surface->invalidRegion);
1359
1360
0
  WINPR_ASSERT(context->SetSurfaceData);
1361
0
  rc = context->SetSurfaceData(context, surface->surfaceId, (void*)surface);
1362
0
fail:
1363
0
  LeaveCriticalSection(&context->mux);
1364
0
  return rc;
1365
0
}
1366
1367
/**
1368
 * Function description
1369
 *
1370
 * @return 0 on success, otherwise a Win32 error code
1371
 */
1372
WINPR_ATTR_NODISCARD
1373
static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1374
                              const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
1375
0
{
1376
0
  UINT rc = CHANNEL_RC_OK;
1377
0
  UINT res = ERROR_INTERNAL_ERROR;
1378
0
  rdpCodecs* codecs = nullptr;
1379
0
  gdiGfxSurface* surface = nullptr;
1380
0
  EnterCriticalSection(&context->mux);
1381
1382
0
  WINPR_ASSERT(context->GetSurfaceData);
1383
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1384
1385
0
  if (surface)
1386
0
  {
1387
0
    if (surface->windowMapped)
1388
0
      rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1389
0
                        surface->windowId);
1390
1391
#ifdef WITH_GFX_H264
1392
    h264_context_free(surface->h264);
1393
#endif
1394
#if defined(WITH_GFX_AV1)
1395
    freerdp_av1_context_free(surface->av1);
1396
#endif
1397
0
    region16_uninit(&surface->invalidRegion);
1398
0
    codecs = surface->codecs;
1399
0
    winpr_aligned_free(surface->data);
1400
0
    free(surface);
1401
0
  }
1402
1403
0
  WINPR_ASSERT(context->SetSurfaceData);
1404
0
  res = context->SetSurfaceData(context, deleteSurface->surfaceId, nullptr);
1405
0
  if (res)
1406
0
    rc = res;
1407
1408
0
  if (codecs && codecs->progressive)
1409
0
    progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1410
1411
0
  LeaveCriticalSection(&context->mux);
1412
0
  return rc;
1413
0
}
1414
1415
WINPR_ATTR_NODISCARD
1416
static BOOL intersect_rect(const RECTANGLE_16* rect, const gdiGfxSurface* surface,
1417
                           RECTANGLE_16* prect)
1418
0
{
1419
0
  WINPR_ASSERT(rect);
1420
0
  WINPR_ASSERT(surface);
1421
0
  WINPR_ASSERT(prect);
1422
1423
0
  if (rect->left > rect->right)
1424
0
    return FALSE;
1425
0
  if (rect->left > surface->width)
1426
0
    return FALSE;
1427
0
  if (rect->top > rect->bottom)
1428
0
    return FALSE;
1429
0
  if (rect->top > surface->height)
1430
0
    return FALSE;
1431
0
  prect->left = rect->left;
1432
0
  prect->top = rect->top;
1433
1434
0
  prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1435
0
  prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1436
0
  return TRUE;
1437
0
}
1438
1439
/**
1440
 * Function description
1441
 *
1442
 * @return 0 on success, otherwise a Win32 error code
1443
 */
1444
WINPR_ATTR_NODISCARD
1445
static UINT gdi_SolidFill(RdpgfxClientContext* context, const RDPGFX_SOLID_FILL_PDU* solidFill)
1446
0
{
1447
0
  UINT status = ERROR_INTERNAL_ERROR;
1448
0
  BYTE a = 0xff;
1449
0
  RECTANGLE_16 invalidRect = WINPR_C_ARRAY_INIT;
1450
0
  rdpGdi* gdi = (rdpGdi*)context->custom;
1451
1452
0
  EnterCriticalSection(&context->mux);
1453
1454
0
  WINPR_ASSERT(context->GetSurfaceData);
1455
0
  gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1456
1457
0
  if (!surface)
1458
0
    goto fail;
1459
1460
0
  {
1461
0
    const BYTE b = solidFill->fillPixel.B;
1462
0
    const BYTE g = solidFill->fillPixel.G;
1463
0
    const BYTE r = solidFill->fillPixel.R;
1464
1465
    /* [MS-RDPEGFX] 3.3.5.4 Processing an RDPGFX_SOLIDFILL_PDU message
1466
     * https://learn.microsoft.com/en-us/windows/win32/gdi/binary-raster-operations
1467
     *
1468
     * this sounds like the alpha value is always ignored.
1469
     */
1470
0
    const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1471
0
    for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1472
0
    {
1473
0
      const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1474
1475
0
      if (!intersect_rect(rect, surface, &invalidRect))
1476
0
        goto fail;
1477
1478
0
      const UINT32 nWidth = invalidRect.right - invalidRect.left;
1479
0
      const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1480
1481
0
      if (!freerdp_image_fill(surface->data, surface->format, surface->scanline,
1482
0
                              invalidRect.left, invalidRect.top, nWidth, nHeight, color))
1483
0
        goto fail;
1484
1485
0
      if (!region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
1486
0
                               &invalidRect))
1487
0
        goto fail;
1488
0
    }
1489
0
  }
1490
1491
0
  status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1492
0
                        solidFill->fillRectCount, solidFill->fillRects);
1493
1494
0
  if (status != CHANNEL_RC_OK)
1495
0
    goto fail;
1496
1497
0
  LeaveCriticalSection(&context->mux);
1498
1499
0
  return gdi_interFrameUpdate(gdi, context);
1500
0
fail:
1501
0
  LeaveCriticalSection(&context->mux);
1502
0
  return status;
1503
0
}
1504
1505
/**
1506
 * Function description
1507
 *
1508
 * @return 0 on success, otherwise a Win32 error code
1509
 */
1510
WINPR_ATTR_NODISCARD
1511
static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1512
                                 const RDPGFX_SURFACE_TO_SURFACE_PDU* surfaceToSurface)
1513
0
{
1514
0
  UINT status = ERROR_INTERNAL_ERROR;
1515
0
  BOOL sameSurface = 0;
1516
0
  const RECTANGLE_16* rectSrc = nullptr;
1517
0
  RECTANGLE_16 invalidRect;
1518
0
  gdiGfxSurface* surfaceSrc = nullptr;
1519
0
  gdiGfxSurface* surfaceDst = nullptr;
1520
0
  rdpGdi* gdi = (rdpGdi*)context->custom;
1521
0
  EnterCriticalSection(&context->mux);
1522
0
  rectSrc = &(surfaceToSurface->rectSrc);
1523
1524
0
  WINPR_ASSERT(context->GetSurfaceData);
1525
0
  surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1526
0
  sameSurface = (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest);
1527
1528
0
  if (!sameSurface)
1529
0
    surfaceDst =
1530
0
        (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1531
0
  else
1532
0
    surfaceDst = surfaceSrc;
1533
1534
0
  if (!surfaceSrc || !surfaceDst)
1535
0
    goto fail;
1536
1537
0
  if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1538
0
    goto fail;
1539
1540
0
  {
1541
0
    const UINT32 nWidth = rectSrc->right - rectSrc->left;
1542
0
    const UINT32 nHeight = rectSrc->bottom - rectSrc->top;
1543
1544
0
    for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1545
0
    {
1546
0
      const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1547
0
      const RECTANGLE_16 rect = { destPt->x, destPt->y,
1548
0
                                (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1549
0
                                (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1550
0
      if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1551
0
        goto fail;
1552
1553
0
      const UINT32 rwidth = rect.right - rect.left;
1554
0
      const UINT32 rheight = rect.bottom - rect.top;
1555
0
      if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1556
0
                              destPt->x, destPt->y, rwidth, rheight, surfaceSrc->data,
1557
0
                              surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1558
0
                              rectSrc->top, nullptr, FREERDP_FLIP_NONE))
1559
0
        goto fail;
1560
1561
0
      invalidRect = rect;
1562
0
      if (!region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion,
1563
0
                               &invalidRect))
1564
0
        goto fail;
1565
0
      status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1566
0
                            surfaceDst->surfaceId, 1, &invalidRect);
1567
1568
0
      if (status != CHANNEL_RC_OK)
1569
0
        goto fail;
1570
0
    }
1571
0
  }
1572
1573
0
  LeaveCriticalSection(&context->mux);
1574
1575
0
  return gdi_interFrameUpdate(gdi, context);
1576
0
fail:
1577
0
  LeaveCriticalSection(&context->mux);
1578
0
  return status;
1579
0
}
1580
1581
static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1582
0
{
1583
0
  if (!entry)
1584
0
    return;
1585
0
  free(entry->data);
1586
0
  free(entry);
1587
0
}
1588
1589
WINPR_ATTR_MALLOC(gdi_GfxCacheEntryFree, 1)
1590
static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1591
                                              UINT32 format)
1592
0
{
1593
0
  gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1, sizeof(gdiGfxCacheEntry));
1594
0
  if (!cacheEntry)
1595
0
    goto fail;
1596
1597
0
  cacheEntry->cacheKey = cacheKey;
1598
0
  cacheEntry->width = width;
1599
0
  cacheEntry->height = height;
1600
0
  cacheEntry->format = format;
1601
1602
0
  const UINT32 bpp = MAX(4, FreeRDPGetBytesPerPixel(format));
1603
0
  cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * bpp, 16);
1604
1605
0
  if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1606
0
  {
1607
0
    cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1608
1609
0
    if (!cacheEntry->data)
1610
0
      goto fail;
1611
0
  }
1612
0
  return cacheEntry;
1613
0
fail:
1614
0
  gdi_GfxCacheEntryFree(cacheEntry);
1615
0
  return nullptr;
1616
0
}
1617
1618
/**
1619
 * Function description
1620
 *
1621
 * @return 0 on success, otherwise a Win32 error code
1622
 */
1623
WINPR_ATTR_NODISCARD
1624
static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1625
                               const RDPGFX_SURFACE_TO_CACHE_PDU* surfaceToCache)
1626
0
{
1627
0
  gdiGfxCacheEntry* cacheEntry = nullptr;
1628
0
  UINT rc = ERROR_INTERNAL_ERROR;
1629
0
  EnterCriticalSection(&context->mux);
1630
0
  const RECTANGLE_16* rect = &(surfaceToCache->rectSrc);
1631
1632
0
  WINPR_ASSERT(context->GetSurfaceData);
1633
0
  gdiGfxSurface* surface =
1634
0
      (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1635
1636
0
  if (!surface)
1637
0
    goto fail;
1638
1639
0
  if (!is_rect_valid(rect, surface->width, surface->height))
1640
0
    goto fail;
1641
1642
0
  cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1643
0
                                    (UINT32)(rect->bottom - rect->top), surface->format);
1644
1645
0
  if (!cacheEntry)
1646
0
    goto fail;
1647
1648
0
  if (!cacheEntry->data)
1649
0
    goto fail;
1650
1651
0
  if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1652
0
                                     0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1653
0
                                     surface->format, surface->scanline, rect->left, rect->top,
1654
0
                                     nullptr, FREERDP_FLIP_NONE))
1655
0
    goto fail;
1656
1657
0
  {
1658
0
    RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { surfaceToCache->cacheSlot };
1659
0
    WINPR_ASSERT(context->EvictCacheEntry);
1660
0
    rc = context->EvictCacheEntry(context, &evict);
1661
0
    if (rc != CHANNEL_RC_OK)
1662
0
      goto fail;
1663
0
  }
1664
1665
0
  WINPR_ASSERT(context->SetCacheSlotData);
1666
0
  rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (void*)cacheEntry);
1667
0
fail:
1668
0
  if (rc != CHANNEL_RC_OK)
1669
0
    gdi_GfxCacheEntryFree(cacheEntry);
1670
0
  LeaveCriticalSection(&context->mux);
1671
0
  return rc;
1672
0
}
1673
1674
/**
1675
 * Function description
1676
 *
1677
 * @return 0 on success, otherwise a Win32 error code
1678
 */
1679
WINPR_ATTR_NODISCARD
1680
static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1681
                               const RDPGFX_CACHE_TO_SURFACE_PDU* cacheToSurface)
1682
0
{
1683
0
  UINT status = ERROR_INTERNAL_ERROR;
1684
0
  gdiGfxSurface* surface = nullptr;
1685
0
  gdiGfxCacheEntry* cacheEntry = nullptr;
1686
0
  RECTANGLE_16 invalidRect;
1687
0
  rdpGdi* gdi = (rdpGdi*)context->custom;
1688
1689
0
  EnterCriticalSection(&context->mux);
1690
1691
0
  WINPR_ASSERT(context->GetSurfaceData);
1692
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1693
1694
0
  WINPR_ASSERT(context->GetCacheSlotData);
1695
0
  cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1696
1697
0
  if (!surface || !cacheEntry)
1698
0
    goto fail;
1699
1700
0
  for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1701
0
  {
1702
0
    const RDPGFX_POINT16* destPt = &cacheToSurface->destPts[index];
1703
0
    const RECTANGLE_16 rect = { destPt->x, destPt->y,
1704
0
                              (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1705
0
                              (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1706
1707
0
    if (rectangle_is_empty(&rect))
1708
0
      continue;
1709
1710
0
    if (!is_rect_valid(&rect, surface->width, surface->height))
1711
0
      goto fail;
1712
1713
0
    const UINT32 w = rect.right - rect.left;
1714
0
    const UINT32 h = rect.bottom - rect.top;
1715
0
    if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1716
0
                                       destPt->x, destPt->y, w, h, cacheEntry->data,
1717
0
                                       cacheEntry->format, cacheEntry->scanline, 0, 0, nullptr,
1718
0
                                       FREERDP_FLIP_NONE))
1719
0
      goto fail;
1720
1721
0
    invalidRect = rect;
1722
0
    if (!region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect))
1723
0
      goto fail;
1724
0
    status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1725
0
                          surface->surfaceId, 1, &invalidRect);
1726
1727
0
    if (status != CHANNEL_RC_OK)
1728
0
      goto fail;
1729
0
  }
1730
1731
0
  LeaveCriticalSection(&context->mux);
1732
1733
0
  return gdi_interFrameUpdate(gdi, context);
1734
1735
0
fail:
1736
0
  LeaveCriticalSection(&context->mux);
1737
0
  return status;
1738
0
}
1739
1740
/**
1741
 * Function description
1742
 *
1743
 * @return 0 on success, otherwise a Win32 error code
1744
 */
1745
WINPR_ATTR_NODISCARD
1746
static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
1747
                                 const RDPGFX_CACHE_IMPORT_REPLY_PDU* cacheImportReply)
1748
0
{
1749
0
  UINT16 count = 0;
1750
0
  const UINT16* slots = nullptr;
1751
0
  UINT error = CHANNEL_RC_OK;
1752
1753
0
  slots = cacheImportReply->cacheSlots;
1754
0
  count = cacheImportReply->importedEntriesCount;
1755
1756
0
  for (UINT16 index = 0; index < count; index++)
1757
0
  {
1758
0
    UINT16 cacheSlot = slots[index];
1759
1760
0
    if (cacheSlot == 0)
1761
0
      continue;
1762
1763
0
    WINPR_ASSERT(context->GetCacheSlotData);
1764
0
    gdiGfxCacheEntry* cacheEntry =
1765
0
        (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1766
1767
0
    if (cacheEntry)
1768
0
      continue;
1769
1770
0
    cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
1771
1772
0
    if (!cacheEntry)
1773
0
      return ERROR_INTERNAL_ERROR;
1774
1775
0
    WINPR_ASSERT(context->SetCacheSlotData);
1776
0
    error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
1777
1778
0
    if (error)
1779
0
    {
1780
0
      WLog_ERR(TAG, "CacheImportReply: SetCacheSlotData failed with error %" PRIu32 "",
1781
0
               error);
1782
0
      gdi_GfxCacheEntryFree(cacheEntry);
1783
0
      break;
1784
0
    }
1785
0
  }
1786
1787
0
  return error;
1788
0
}
1789
1790
WINPR_ATTR_NODISCARD
1791
static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1792
                                 const PERSISTENT_CACHE_ENTRY* importCacheEntry)
1793
0
{
1794
0
  UINT error = ERROR_INTERNAL_ERROR;
1795
0
  gdiGfxCacheEntry* cacheEntry = nullptr;
1796
1797
0
  if (cacheSlot == 0)
1798
0
    return CHANNEL_RC_OK;
1799
1800
0
  cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
1801
0
                                    importCacheEntry->height, PIXEL_FORMAT_BGRX32);
1802
1803
0
  if (!cacheEntry)
1804
0
    goto fail;
1805
1806
0
  if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1807
0
                                     0, 0, cacheEntry->width, cacheEntry->height,
1808
0
                                     importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0,
1809
0
                                     nullptr, FREERDP_FLIP_NONE))
1810
0
    goto fail;
1811
1812
0
  {
1813
0
    RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { cacheSlot };
1814
0
    WINPR_ASSERT(context->EvictCacheEntry);
1815
0
    error = context->EvictCacheEntry(context, &evict);
1816
0
  }
1817
0
  if (error != CHANNEL_RC_OK)
1818
0
    goto fail;
1819
1820
0
  WINPR_ASSERT(context->SetCacheSlotData);
1821
0
  error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
1822
1823
0
fail:
1824
0
  if (error)
1825
0
  {
1826
0
    gdi_GfxCacheEntryFree(cacheEntry);
1827
0
    WLog_ERR(TAG, "ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32 "", error);
1828
0
  }
1829
1830
0
  return error;
1831
0
}
1832
1833
WINPR_ATTR_NODISCARD
1834
static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1835
                                 PERSISTENT_CACHE_ENTRY* exportCacheEntry)
1836
0
{
1837
0
  WINPR_ASSERT(context->GetCacheSlotData);
1838
0
  gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1839
1840
0
  if (cacheEntry)
1841
0
  {
1842
0
    exportCacheEntry->key64 = cacheEntry->cacheKey;
1843
0
    exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
1844
0
    exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
1845
0
    const UINT64 size = 4ull * cacheEntry->width * cacheEntry->height;
1846
0
    if (size > UINT32_MAX)
1847
0
      return ERROR_NOT_FOUND;
1848
1849
0
    exportCacheEntry->size = WINPR_ASSERTING_INT_CAST(UINT32, size);
1850
0
    exportCacheEntry->flags = 0;
1851
0
    exportCacheEntry->data = cacheEntry->data;
1852
0
    return CHANNEL_RC_OK;
1853
0
  }
1854
1855
0
  return ERROR_NOT_FOUND;
1856
0
}
1857
1858
/**
1859
 * Function description
1860
 *
1861
 * @return 0 on success, otherwise a Win32 error code
1862
 */
1863
WINPR_ATTR_NODISCARD
1864
static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
1865
                                const RDPGFX_EVICT_CACHE_ENTRY_PDU* evictCacheEntry)
1866
0
{
1867
0
  gdiGfxCacheEntry* cacheEntry = nullptr;
1868
0
  UINT rc = ERROR_NOT_FOUND;
1869
1870
0
  WINPR_ASSERT(context);
1871
0
  WINPR_ASSERT(evictCacheEntry);
1872
1873
0
  EnterCriticalSection(&context->mux);
1874
1875
0
  WINPR_ASSERT(context->GetCacheSlotData);
1876
0
  cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
1877
1878
0
  gdi_GfxCacheEntryFree(cacheEntry);
1879
1880
0
  WINPR_ASSERT(context->SetCacheSlotData);
1881
0
  rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot, nullptr);
1882
0
  LeaveCriticalSection(&context->mux);
1883
0
  return rc;
1884
0
}
1885
1886
/**
1887
 * Function description
1888
 *
1889
 * @return 0 on success, otherwise a Win32 error code
1890
 */
1891
WINPR_ATTR_NODISCARD
1892
static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
1893
                                   const RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU* surfaceToOutput)
1894
0
{
1895
0
  UINT rc = ERROR_INTERNAL_ERROR;
1896
0
  gdiGfxSurface* surface = nullptr;
1897
0
  EnterCriticalSection(&context->mux);
1898
1899
0
  WINPR_ASSERT(context->GetSurfaceData);
1900
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1901
1902
0
  if (!surface)
1903
0
    goto fail;
1904
1905
0
  if (surface->windowMapped)
1906
0
  {
1907
0
    WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
1908
0
    goto fail;
1909
0
  }
1910
1911
0
  surface->outputMapped = TRUE;
1912
0
  surface->outputOriginX = surfaceToOutput->outputOriginX;
1913
0
  surface->outputOriginY = surfaceToOutput->outputOriginY;
1914
0
  surface->outputTargetWidth = surface->mappedWidth;
1915
0
  surface->outputTargetHeight = surface->mappedHeight;
1916
0
  region16_clear(&surface->invalidRegion);
1917
0
  rc = CHANNEL_RC_OK;
1918
0
fail:
1919
0
  LeaveCriticalSection(&context->mux);
1920
0
  return rc;
1921
0
}
1922
1923
WINPR_ATTR_NODISCARD
1924
static UINT
1925
gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
1926
                             const RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU* surfaceToOutput)
1927
0
{
1928
0
  UINT rc = ERROR_INTERNAL_ERROR;
1929
0
  gdiGfxSurface* surface = nullptr;
1930
0
  EnterCriticalSection(&context->mux);
1931
1932
0
  WINPR_ASSERT(context->GetSurfaceData);
1933
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1934
1935
0
  if (!surface)
1936
0
    goto fail;
1937
1938
0
  if (surface->windowMapped)
1939
0
  {
1940
0
    WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
1941
0
    goto fail;
1942
0
  }
1943
1944
0
  surface->outputMapped = TRUE;
1945
0
  surface->outputOriginX = surfaceToOutput->outputOriginX;
1946
0
  surface->outputOriginY = surfaceToOutput->outputOriginY;
1947
0
  surface->outputTargetWidth = surfaceToOutput->targetWidth;
1948
0
  surface->outputTargetHeight = surfaceToOutput->targetHeight;
1949
0
  region16_clear(&surface->invalidRegion);
1950
0
  rc = CHANNEL_RC_OK;
1951
0
fail:
1952
0
  LeaveCriticalSection(&context->mux);
1953
0
  return rc;
1954
0
}
1955
1956
/**
1957
 * Function description
1958
 *
1959
 * @return 0 on success, otherwise a Win32 error code
1960
 */
1961
WINPR_ATTR_NODISCARD
1962
static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
1963
                                   const RDPGFX_MAP_SURFACE_TO_WINDOW_PDU* surfaceToWindow)
1964
0
{
1965
0
  UINT rc = ERROR_INTERNAL_ERROR;
1966
0
  gdiGfxSurface* surface = nullptr;
1967
0
  EnterCriticalSection(&context->mux);
1968
1969
0
  WINPR_ASSERT(context->GetSurfaceData);
1970
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1971
1972
0
  if (!surface)
1973
0
    goto fail;
1974
1975
0
  if (surface->outputMapped)
1976
0
  {
1977
0
    WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
1978
0
    goto fail;
1979
0
  }
1980
1981
0
  if (surface->windowMapped)
1982
0
  {
1983
0
    if (surface->windowId != surfaceToWindow->windowId)
1984
0
    {
1985
0
      WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
1986
0
                surface->windowId, surfaceToWindow->windowId);
1987
0
      goto fail;
1988
0
    }
1989
0
  }
1990
0
  surface->windowMapped = TRUE;
1991
1992
0
  surface->windowId = surfaceToWindow->windowId;
1993
0
  surface->mappedWidth = surfaceToWindow->mappedWidth;
1994
0
  if (surface->mappedWidth > surface->width)
1995
0
  {
1996
0
    WLog_WARN(TAG, "surface mappedWidth[%" PRIu32 "] > width[%" PRIu32 "]",
1997
0
              surface->mappedWidth, surface->width);
1998
0
    goto fail;
1999
0
  }
2000
0
  surface->mappedHeight = surfaceToWindow->mappedHeight;
2001
0
  if (surface->mappedHeight > surface->height)
2002
0
  {
2003
0
    WLog_WARN(TAG, "surface mappedHeight[%" PRIu32 "] > height[%" PRIu32 "]",
2004
0
              surface->mappedHeight, surface->height);
2005
0
    goto fail;
2006
0
  }
2007
0
  surface->outputTargetWidth = surfaceToWindow->mappedWidth;
2008
0
  surface->outputTargetHeight = surfaceToWindow->mappedHeight;
2009
0
  rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2010
0
                    surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2011
0
fail:
2012
0
  LeaveCriticalSection(&context->mux);
2013
0
  return rc;
2014
0
}
2015
2016
WINPR_ATTR_NODISCARD
2017
static UINT
2018
gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
2019
                             const RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU* surfaceToWindow)
2020
0
{
2021
0
  UINT rc = ERROR_INTERNAL_ERROR;
2022
0
  gdiGfxSurface* surface = nullptr;
2023
0
  EnterCriticalSection(&context->mux);
2024
2025
0
  WINPR_ASSERT(context->GetSurfaceData);
2026
0
  surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
2027
2028
0
  if (!surface)
2029
0
    goto fail;
2030
2031
0
  if (surface->outputMapped)
2032
0
  {
2033
0
    WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
2034
0
    goto fail;
2035
0
  }
2036
2037
0
  if (surface->windowMapped)
2038
0
  {
2039
0
    if (surface->windowId != surfaceToWindow->windowId)
2040
0
    {
2041
0
      WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
2042
0
                surface->windowId, surfaceToWindow->windowId);
2043
0
      goto fail;
2044
0
    }
2045
0
  }
2046
0
  surface->windowMapped = TRUE;
2047
2048
0
  surface->windowId = surfaceToWindow->windowId;
2049
0
  surface->mappedWidth = surfaceToWindow->mappedWidth;
2050
0
  if (surface->mappedWidth > surface->width)
2051
0
  {
2052
0
    WLog_WARN(TAG, "surface mappedWidth[%" PRIu32 "] > width[%" PRIu32 "]",
2053
0
              surface->mappedWidth, surface->width);
2054
0
    goto fail;
2055
0
  }
2056
2057
0
  surface->mappedHeight = surfaceToWindow->mappedHeight;
2058
0
  if (surface->mappedHeight > surface->height)
2059
0
  {
2060
0
    WLog_WARN(TAG, "surface mappedHeight[%" PRIu32 "] > height[%" PRIu32 "]",
2061
0
              surface->mappedHeight, surface->height);
2062
0
    goto fail;
2063
0
  }
2064
0
  surface->outputTargetWidth = surfaceToWindow->targetWidth;
2065
0
  surface->outputTargetHeight = surfaceToWindow->targetHeight;
2066
0
  rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
2067
0
                    surfaceToWindow->surfaceId, surfaceToWindow->windowId);
2068
0
fail:
2069
0
  LeaveCriticalSection(&context->mux);
2070
0
  return rc;
2071
0
}
2072
2073
BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
2074
0
{
2075
0
  return gdi_graphics_pipeline_init_ex(gdi, gfx, nullptr, nullptr, nullptr);
2076
0
}
2077
2078
BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
2079
                                   pcRdpgfxMapWindowForSurface map,
2080
                                   pcRdpgfxUnmapWindowForSurface unmap,
2081
                                   pcRdpgfxUpdateSurfaceArea update)
2082
0
{
2083
0
  if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
2084
0
    return FALSE;
2085
2086
0
  rdpContext* context = gdi->context;
2087
0
  rdpSettings* settings = context->settings;
2088
2089
0
  gdi->gfx = gfx;
2090
0
  gfx->custom = (void*)gdi;
2091
0
  gfx->ResetGraphics = gdi_ResetGraphics;
2092
0
  gfx->StartFrame = gdi_StartFrame;
2093
0
  gfx->EndFrame = gdi_EndFrame;
2094
0
  gfx->SurfaceCommand = gdi_SurfaceCommand;
2095
0
  gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
2096
0
  gfx->CreateSurface = gdi_CreateSurface;
2097
0
  gfx->DeleteSurface = gdi_DeleteSurface;
2098
0
  gfx->SolidFill = gdi_SolidFill;
2099
0
  gfx->SurfaceToSurface = gdi_SurfaceToSurface;
2100
0
  gfx->SurfaceToCache = gdi_SurfaceToCache;
2101
0
  gfx->CacheToSurface = gdi_CacheToSurface;
2102
0
  gfx->CacheImportReply = gdi_CacheImportReply;
2103
0
  gfx->ImportCacheEntry = gdi_ImportCacheEntry;
2104
0
  gfx->ExportCacheEntry = gdi_ExportCacheEntry;
2105
0
  gfx->EvictCacheEntry = gdi_EvictCacheEntry;
2106
0
  gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
2107
0
  gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
2108
0
  gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
2109
0
  gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
2110
0
  gfx->UpdateSurfaces = gdi_UpdateSurfaces;
2111
0
  gfx->MapWindowForSurface = map;
2112
0
  gfx->UnmapWindowForSurface = unmap;
2113
0
  gfx->UpdateSurfaceArea = update;
2114
2115
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
2116
0
  {
2117
0
    const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
2118
0
    const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2119
0
    const UINT32 flags = freerdp_settings_get_uint32(settings, FreeRDP_ThreadingFlags);
2120
2121
0
    gfx->codecs = freerdp_client_codecs_new(flags);
2122
0
    if (!gfx->codecs)
2123
0
      return FALSE;
2124
0
    if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
2125
0
      return FALSE;
2126
0
  }
2127
0
  InitializeCriticalSection(&gfx->mux);
2128
0
  PROFILER_CREATE(gfx->SurfaceProfiler, "GFX-PROFILER")
2129
2130
  /**
2131
   * gdi->graphicsReset will be removed in FreeRDP v3 from public headers,
2132
   * since the EGFX Reset Graphics PDU seems to be optional.
2133
   * There are still some clients that expect and check it and therefore
2134
   * we simply initialize it with TRUE here for now.
2135
   */
2136
0
  gdi->graphicsReset = TRUE;
2137
0
  if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
2138
0
  {
2139
0
    gfx->UpdateSurfaceArea = nullptr;
2140
0
    gfx->UpdateSurfaces = nullptr;
2141
0
    gfx->SurfaceCommand = nullptr;
2142
0
  }
2143
2144
0
  return TRUE;
2145
0
}
2146
2147
void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
2148
0
{
2149
0
  if (gdi)
2150
0
    gdi->gfx = nullptr;
2151
2152
0
  if (!gfx)
2153
0
    return;
2154
2155
0
  gfx->custom = nullptr;
2156
0
  freerdp_client_codecs_free(gfx->codecs);
2157
0
  gfx->codecs = nullptr;
2158
0
  DeleteCriticalSection(&gfx->mux);
2159
0
  PROFILER_PRINT_HEADER
2160
0
  PROFILER_PRINT(gfx->SurfaceProfiler)
2161
0
  PROFILER_PRINT_FOOTER
2162
0
  PROFILER_FREE(gfx->SurfaceProfiler)
2163
0
}