FreeRDP
Loading...
Searching...
No Matches
xf_gfx.c
1
22#include <freerdp/config.h>
23
24#include <math.h>
25
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28
29#include <freerdp/log.h>
30#include "xf_gfx.h"
31#include "xf_rail.h"
32#include "xf_utils.h"
33#include "xf_window.h"
34
35#include <X11/Xutil.h>
36
37#define TAG CLIENT_TAG("x11")
38
39static UINT xf_OutputUpdate(xfContext* xfc, xfGfxSurface* surface)
40{
41 UINT rc = ERROR_INTERNAL_ERROR;
42 UINT32 surfaceX = 0;
43 UINT32 surfaceY = 0;
44 RECTANGLE_16 surfaceRect = WINPR_C_ARRAY_INIT;
45 UINT32 nbRects = 0;
46 const RECTANGLE_16* rects = nullptr;
47
48 WINPR_ASSERT(xfc);
49 WINPR_ASSERT(surface);
50
51 rdpGdi* gdi = xfc->common.context.gdi;
52 WINPR_ASSERT(gdi);
53
54 rdpSettings* settings = xfc->common.context.settings;
55 WINPR_ASSERT(settings);
56
57 surfaceX = surface->gdi.outputOriginX;
58 surfaceY = surface->gdi.outputOriginY;
59 surfaceRect.left = 0;
60 surfaceRect.top = 0;
61 surfaceRect.right = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.mappedWidth);
62 surfaceRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.mappedHeight);
63 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
64 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
65 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
66 if (!region16_intersect_rect(&(surface->gdi.invalidRegion), &(surface->gdi.invalidRegion),
67 &surfaceRect))
68 return ERROR_INTERNAL_ERROR;
69
70 WINPR_ASSERT(surface->gdi.mappedWidth);
71 WINPR_ASSERT(surface->gdi.mappedHeight);
72 const double sx = 1.0 * surface->gdi.outputTargetWidth / (double)surface->gdi.mappedWidth;
73 const double sy = 1.0 * surface->gdi.outputTargetHeight / (double)surface->gdi.mappedHeight;
74
75 if (!(rects = region16_rects(&surface->gdi.invalidRegion, &nbRects)))
76 return CHANNEL_RC_OK;
77
78 xf_lock_x11(xfc);
79 for (UINT32 x = 0; x < nbRects; x++)
80 {
81 const RECTANGLE_16* rect = &rects[x];
82 const UINT32 nXSrc = rect->left;
83 const UINT32 nYSrc = rect->top;
84 const UINT32 swidth = rect->right - nXSrc;
85 const UINT32 sheight = rect->bottom - nYSrc;
86 const UINT32 nXDst = (UINT32)lround(1.0 * surfaceX + nXSrc * sx);
87 const UINT32 nYDst = (UINT32)lround(1.0 * surfaceY + nYSrc * sy);
88 const UINT32 dwidth = (UINT32)lround(1.0 * swidth * sx);
89 const UINT32 dheight = (UINT32)lround(1.0 * sheight * sy);
90
91 if (surface->stage)
92 {
93 if (!freerdp_image_scale(surface->stage, gdi->dstFormat, surface->stageScanline, nXSrc,
94 nYSrc, dwidth, dheight, surface->gdi.data, surface->gdi.format,
95 surface->gdi.scanline, nXSrc, nYSrc, swidth, sheight))
96 goto fail;
97 }
98
99 if (xfc->remote_app)
100 {
101 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, surface->image,
102 WINPR_ASSERTING_INT_CAST(int, nXSrc),
103 WINPR_ASSERTING_INT_CAST(int, nYSrc),
104 WINPR_ASSERTING_INT_CAST(int, nXDst),
105 WINPR_ASSERTING_INT_CAST(int, nYDst), dwidth, dheight);
106 (void)xf_rail_paint_surface(xfc, surface->gdi.windowId, rect);
107 }
108 else
109#ifdef WITH_XRENDER
110 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
111 freerdp_settings_get_bool(settings, FreeRDP_MultiTouchGestures))
112 {
113 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, surface->image,
114 WINPR_ASSERTING_INT_CAST(int, nXSrc),
115 WINPR_ASSERTING_INT_CAST(int, nYSrc),
116 WINPR_ASSERTING_INT_CAST(int, nXDst),
117 WINPR_ASSERTING_INT_CAST(int, nYDst), dwidth, dheight);
118 xf_draw_screen(xfc, WINPR_ASSERTING_INT_CAST(int32_t, nXDst),
119 WINPR_ASSERTING_INT_CAST(int32_t, nYDst),
120 WINPR_ASSERTING_INT_CAST(int32_t, dwidth),
121 WINPR_ASSERTING_INT_CAST(int32_t, dheight));
122 }
123 else
124#endif
125 {
126 LogDynAndXPutImage(xfc->log, xfc->display, xfc->drawable, xfc->gc, surface->image,
127 WINPR_ASSERTING_INT_CAST(int, nXSrc),
128 WINPR_ASSERTING_INT_CAST(int, nYSrc),
129 WINPR_ASSERTING_INT_CAST(int, nXDst),
130 WINPR_ASSERTING_INT_CAST(int, nYDst), dwidth, dheight);
131 }
132 }
133
134 rc = CHANNEL_RC_OK;
135fail:
136 region16_clear(&surface->gdi.invalidRegion);
137 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
138 LogDynAndXSync(xfc->log, xfc->display, False);
139 xf_unlock_x11(xfc);
140 return rc;
141}
142
143static UINT xf_WindowUpdate(RdpgfxClientContext* context, xfGfxSurface* surface)
144{
145 WINPR_ASSERT(context);
146 WINPR_ASSERT(surface);
147 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, &surface->gdi);
148}
149
150static UINT xf_UpdateSurfaces(RdpgfxClientContext* context)
151{
152 UINT16 count = 0;
153 UINT status = CHANNEL_RC_OK;
154 UINT16* pSurfaceIds = nullptr;
155 rdpGdi* gdi = (rdpGdi*)context->custom;
156 xfContext* xfc = nullptr;
157
158 if (!gdi)
159 return status;
160
161 if (gdi->suppressOutput)
162 return CHANNEL_RC_OK;
163
164 xfc = (xfContext*)gdi->context;
165 EnterCriticalSection(&context->mux);
166 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
167 if (status != CHANNEL_RC_OK)
168 goto fail;
169
170 for (UINT32 index = 0; index < count; index++)
171 {
172 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
173
174 if (!surface)
175 continue;
176
177 /* If UpdateSurfaceArea callback is available, the output has already been updated. */
178 if (context->UpdateSurfaceArea)
179 {
180 if (surface->gdi.handleInUpdateSurfaceArea)
181 continue;
182 }
183
184 if (surface->gdi.outputMapped)
185 status = xf_OutputUpdate(xfc, surface);
186 else if (surface->gdi.windowMapped)
187 status = xf_WindowUpdate(context, surface);
188
189 if (status != CHANNEL_RC_OK)
190 break;
191 }
192
193fail:
194 free(pSurfaceIds);
195 LeaveCriticalSection(&context->mux);
196 return status;
197}
198
199UINT xf_OutputExpose(xfContext* xfc, UINT32 x, UINT32 y, UINT32 width, UINT32 height)
200{
201 UINT16 count = 0;
202 UINT status = ERROR_INTERNAL_ERROR;
203 RECTANGLE_16 invalidRect = WINPR_C_ARRAY_INIT;
204 RECTANGLE_16 intersection = WINPR_C_ARRAY_INIT;
205 UINT16* pSurfaceIds = nullptr;
206 RdpgfxClientContext* context = nullptr;
207
208 WINPR_ASSERT(xfc);
209 WINPR_ASSERT(xfc->common.context.gdi);
210
211 context = xfc->common.context.gdi->gfx;
212 WINPR_ASSERT(context);
213
214 invalidRect.left = WINPR_ASSERTING_INT_CAST(UINT16, x);
215 invalidRect.top = WINPR_ASSERTING_INT_CAST(UINT16, y);
216 invalidRect.right = WINPR_ASSERTING_INT_CAST(UINT16, x + width);
217 invalidRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, y + height);
218 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
219
220 if (status != CHANNEL_RC_OK)
221 goto fail;
222
223 if (!TryEnterCriticalSection(&context->mux))
224 {
225 free(pSurfaceIds);
226 return CHANNEL_RC_OK;
227 }
228 for (UINT32 index = 0; index < count; index++)
229 {
230 RECTANGLE_16 surfaceRect = WINPR_C_ARRAY_INIT;
231 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
232
233 if (!surface || (!surface->gdi.outputMapped && !surface->gdi.windowMapped))
234 continue;
235
236 surfaceRect.left = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX);
237 surfaceRect.top = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY);
238 surfaceRect.right = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX +
239 surface->gdi.outputTargetWidth);
240 surfaceRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY +
241 surface->gdi.outputTargetHeight);
242
243 if (rectangles_intersection(&invalidRect, &surfaceRect, &intersection))
244 {
245 /* Invalid rects are specified relative to surface origin */
246 intersection.left -= surfaceRect.left;
247 intersection.top -= surfaceRect.top;
248 intersection.right -= surfaceRect.left;
249 intersection.bottom -= surfaceRect.top;
250 if (!region16_union_rect(&surface->gdi.invalidRegion, &surface->gdi.invalidRegion,
251 &intersection))
252 {
253 free(pSurfaceIds);
254 LeaveCriticalSection(&context->mux);
255
256 goto fail;
257 }
258 }
259 }
260
261 free(pSurfaceIds);
262 LeaveCriticalSection(&context->mux);
263 IFCALLRET(context->UpdateSurfaces, status, context);
264
265 if (status != CHANNEL_RC_OK)
266 goto fail;
267
268fail:
269 return status;
270}
271
272static UINT32 x11_pad_scanline(UINT32 scanline, UINT32 inPad)
273{
274 /* Ensure X11 alignment is met */
275 if (inPad > 0)
276 {
277 const UINT32 align = inPad / 8;
278 const UINT32 pad = align - scanline % align;
279
280 if (align != pad)
281 scanline += pad;
282 }
283
284 /* 16 byte alignment is required for ASM optimized code */
285 if (scanline % 16)
286 scanline += 16 - scanline % 16;
287
288 return scanline;
289}
290
291static void DestroySurface(xfGfxSurface* surface)
292{
293 if (!surface)
294 return;
295
296#ifdef WITH_GFX_H264
297 h264_context_free(surface->gdi.h264);
298#endif
299#if defined(WITH_GFX_AV1)
300 freerdp_av1_context_free(surface->gdi.av1);
301#endif
302 if (surface->image)
303 {
304 surface->image->data = nullptr;
305 XDestroyImage(surface->image);
306 }
307
308 winpr_aligned_free(surface->gdi.data);
309 winpr_aligned_free(surface->stage);
310 region16_uninit(&surface->gdi.invalidRegion);
311 free(surface);
312}
313
319static UINT xf_CreateSurface(RdpgfxClientContext* context,
320 const RDPGFX_CREATE_SURFACE_PDU* createSurface)
321{
322 UINT ret = CHANNEL_RC_NO_MEMORY;
323 WINPR_ASSERT(context);
324 WINPR_ASSERT(createSurface);
325
326 rdpGdi* gdi = (rdpGdi*)context->custom;
327 xfContext* xfc = (xfContext*)gdi->context;
328
329 xfGfxSurface* surface = (xfGfxSurface*)calloc(1, sizeof(xfGfxSurface));
330
331 if (!surface)
332 return CHANNEL_RC_NO_MEMORY;
333
334 surface->gdi.codecs = context->codecs;
335
336 if (!surface->gdi.codecs)
337 {
338 WLog_ERR(TAG, "global GDI codecs aren't set");
339 goto fail;
340 }
341
342 surface->gdi.surfaceId = createSurface->surfaceId;
343 surface->gdi.width = x11_pad_scanline(createSurface->width, 16);
344 surface->gdi.height = x11_pad_scanline(createSurface->height, 16);
345 surface->gdi.mappedWidth = createSurface->width;
346 surface->gdi.mappedHeight = createSurface->height;
347 surface->gdi.outputTargetWidth = createSurface->width;
348 surface->gdi.outputTargetHeight = createSurface->height;
349
350 const BOOL rails =
351 freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode);
352 switch (createSurface->pixelFormat)
353 {
354 case GFX_PIXEL_FORMAT_ARGB_8888:
355 surface->gdi.format = PIXEL_FORMAT_BGRA32;
356 break;
357
358 case GFX_PIXEL_FORMAT_XRGB_8888:
359 surface->gdi.format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
360 break;
361
362 default:
363 WLog_ERR(TAG, "unknown pixelFormat 0x%" PRIx32 "", createSurface->pixelFormat);
364 ret = ERROR_INTERNAL_ERROR;
365 goto fail;
366 }
367
368 const UINT32 pad = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad);
369 if (surface->gdi.width > UINT32_MAX / FreeRDPGetBytesPerPixel(surface->gdi.format) - 16 - pad)
370 goto fail;
371
372 /* Align to multiples of 16, but also add additional 16 bytes so SIMD implementations do not
373 * accidentally read out of bound */
374 surface->gdi.scanline = surface->gdi.width * FreeRDPGetBytesPerPixel(surface->gdi.format);
375 surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline, pad) + 16ul;
376
377 surface->gdi.data = (BYTE*)winpr_aligned_calloc(surface->gdi.scanline, surface->gdi.height, 16);
378
379 if (!surface->gdi.data)
380 {
381 WLog_ERR(TAG, "unable to allocate GDI data");
382 goto fail;
383 }
384
385 for (UINT64 y = 0; y < surface->gdi.height; y++)
386 memset(&surface->gdi.data[surface->gdi.scanline * y], 0xff, surface->gdi.scanline);
387
388 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
389 {
390 WINPR_ASSERT(xfc->depth != 0);
391 surface->image = LogDynAndXCreateImage(
392 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
393 ZPixmap, 0, (char*)surface->gdi.data, surface->gdi.mappedWidth,
394 surface->gdi.mappedHeight, xfc->scanline_pad,
395 WINPR_ASSERTING_INT_CAST(int, surface->gdi.scanline));
396 }
397 else
398 {
399 UINT32 width = surface->gdi.width;
400 UINT32 bytes = FreeRDPGetBytesPerPixel(gdi->dstFormat);
401 if (width > UINT32_MAX / bytes - 16 - pad)
402 goto fail;
403
404 /* Align to multiples of scanline_pad, but also add additional 16 bytes so SIMD
405 * implementations do not accidentally read out of bound */
406 surface->stageScanline = width * bytes;
407 surface->stageScanline = x11_pad_scanline(surface->stageScanline, pad) + 16ul;
408
409 surface->stage =
410 (BYTE*)winpr_aligned_calloc(surface->stageScanline, surface->gdi.height, 16);
411
412 if (!surface->stage)
413 {
414 WLog_ERR(TAG, "unable to allocate stage buffer");
415 goto fail;
416 }
417
418 for (UINT64 y = 0; y < surface->gdi.height; y++)
419 memset(&surface->stage[surface->stageScanline * y], 0xff, surface->stageScanline);
420
421 WINPR_ASSERT(xfc->depth != 0);
422 surface->image = LogDynAndXCreateImage(
423 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
424 ZPixmap, 0, (char*)surface->stage, surface->gdi.mappedWidth, surface->gdi.mappedHeight,
425 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(int, surface->stageScanline));
426 }
427
428 if (!surface->image)
429 {
430 WLog_ERR(TAG, "an error occurred when creating the XImage");
431 goto fail;
432 }
433
434 surface->image->byte_order = LSBFirst;
435 surface->image->bitmap_bit_order = LSBFirst;
436
437 region16_init(&surface->gdi.invalidRegion);
438
439 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (void*)surface) != CHANNEL_RC_OK)
440 {
441 WLog_ERR(TAG, "an error occurred during SetSurfaceData");
442 goto fail;
443 }
444
445 return CHANNEL_RC_OK;
446fail:
447 DestroySurface(surface);
448 return ret;
449}
450
456static UINT xf_DeleteSurface(RdpgfxClientContext* context,
457 const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
458{
459 rdpCodecs* codecs = nullptr;
460
461 UINT status = 0;
462 EnterCriticalSection(&context->mux);
463 xfGfxSurface* surface =
464 (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
465
466 if (surface)
467 {
468 if (surface->gdi.windowMapped)
469 {
470 status = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
471 surface->gdi.windowId);
472 if (status != CHANNEL_RC_OK)
473 goto fail;
474 }
475
476 codecs = surface->gdi.codecs;
477 DestroySurface(surface);
478 }
479
480 status = context->SetSurfaceData(context, deleteSurface->surfaceId, nullptr);
481
482 if (codecs && codecs->progressive)
483 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
484
485fail:
486 LeaveCriticalSection(&context->mux);
487 return status;
488}
489
490static UINT xf_UnmapWindowForSurface(RdpgfxClientContext* context, UINT64 windowID)
491{
492 WINPR_ASSERT(context);
493 rdpGdi* gdi = (rdpGdi*)context->custom;
494 WINPR_ASSERT(gdi);
495
496 xfContext* xfc = (xfContext*)gdi->context;
497 WINPR_ASSERT(gdi->context);
498
499 if (freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode))
500 {
501 xfAppWindow* appWindow = xf_rail_get_window(xfc, windowID, FALSE);
502 if (appWindow)
503 xf_AppWindowDestroyImage(appWindow);
504 xf_rail_return_window(appWindow, FALSE);
505 }
506
507 WLog_WARN(TAG, "function not implemented");
508 return CHANNEL_RC_OK;
509}
510
511static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
512{
513 WINPR_ASSERT(context);
514 WINPR_ASSERT(surface);
515
516 rdpGdi* gdi = (rdpGdi*)context->custom;
517 WINPR_ASSERT(gdi);
518
519 xfContext* xfc = (xfContext*)gdi->context;
520 WINPR_ASSERT(gdi->context);
521
522 if (freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode))
523 return xf_AppUpdateWindowFromSurface(xfc, surface);
524
525 WLog_WARN(TAG, "function not implemented");
526 return CHANNEL_RC_OK;
527}
528
529void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
530{
531 rdpGdi* gdi = nullptr;
532 const rdpSettings* settings = nullptr;
533 WINPR_ASSERT(xfc);
534 WINPR_ASSERT(gfx);
535
536 settings = xfc->common.context.settings;
537 WINPR_ASSERT(settings);
538
539 gdi = xfc->common.context.gdi;
540
541 gdi_graphics_pipeline_init(gdi, gfx);
542
543 if (!freerdp_settings_get_bool(settings, FreeRDP_SoftwareGdi))
544 {
545 gfx->UpdateSurfaces = xf_UpdateSurfaces;
546 gfx->CreateSurface = xf_CreateSurface;
547 gfx->DeleteSurface = xf_DeleteSurface;
548 }
549
550 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
551 gfx->UnmapWindowForSurface = xf_UnmapWindowForSurface;
552}
553
554void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
555{
556 WINPR_ASSERT(xfc);
557
558 rdpGdi* gdi = xfc->common.context.gdi;
559 gdi_graphics_pipeline_uninit(gdi, gfx);
560}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.