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