Coverage Report

Created: 2026-03-04 06:17

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