Coverage Report

Created: 2026-03-07 07:07

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