Coverage Report

Created: 2026-11-10 06:44

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