Coverage Report

Created: 2025-10-10 06:50

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