Coverage Report

Created: 2024-05-20 06:11

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