Coverage Report

Created: 2026-02-26 06:54

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