Coverage Report

Created: 2026-04-12 07:03

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