Coverage Report

Created: 2025-07-01 06:46

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