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 for (UINT32 x = 0; x < nbRects; x++)
79 {
80 const RECTANGLE_16* rect = &rects[x];
81 const UINT32 nXSrc = rect->left;
82 const UINT32 nYSrc = rect->top;
83 const UINT32 swidth = rect->right - nXSrc;
84 const UINT32 sheight = rect->bottom - nYSrc;
85 const UINT32 nXDst = (UINT32)lround(1.0 * surfaceX + nXSrc * sx);
86 const UINT32 nYDst = (UINT32)lround(1.0 * surfaceY + nYSrc * sy);
87 const UINT32 dwidth = (UINT32)lround(1.0 * swidth * sx);
88 const UINT32 dheight = (UINT32)lround(1.0 * sheight * sy);
89
90 if (surface->stage)
91 {
92 if (!freerdp_image_scale(surface->stage, gdi->dstFormat, surface->stageScanline, nXSrc,
93 nYSrc, dwidth, dheight, surface->gdi.data, surface->gdi.format,
94 surface->gdi.scanline, nXSrc, nYSrc, swidth, sheight))
95 goto fail;
96 }
97
98 if (xfc->remote_app)
99 {
100 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, surface->image,
101 WINPR_ASSERTING_INT_CAST(int, nXSrc),
102 WINPR_ASSERTING_INT_CAST(int, nYSrc),
103 WINPR_ASSERTING_INT_CAST(int, nXDst),
104 WINPR_ASSERTING_INT_CAST(int, nYDst), dwidth, dheight);
105 xf_lock_x11(xfc);
106 xf_rail_paint_surface(xfc, surface->gdi.windowId, rect);
107 xf_unlock_x11(xfc);
108 }
109 else
110#ifdef WITH_XRENDER
111 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
112 freerdp_settings_get_bool(settings, FreeRDP_MultiTouchGestures))
113 {
114 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, surface->image,
115 WINPR_ASSERTING_INT_CAST(int, nXSrc),
116 WINPR_ASSERTING_INT_CAST(int, nYSrc),
117 WINPR_ASSERTING_INT_CAST(int, nXDst),
118 WINPR_ASSERTING_INT_CAST(int, nYDst), dwidth, dheight);
119 xf_draw_screen(xfc, WINPR_ASSERTING_INT_CAST(int32_t, nXDst),
120 WINPR_ASSERTING_INT_CAST(int32_t, nYDst),
121 WINPR_ASSERTING_INT_CAST(int32_t, dwidth),
122 WINPR_ASSERTING_INT_CAST(int32_t, dheight));
123 }
124 else
125#endif
126 {
127 LogDynAndXPutImage(xfc->log, xfc->display, xfc->drawable, xfc->gc, surface->image,
128 WINPR_ASSERTING_INT_CAST(int, nXSrc),
129 WINPR_ASSERTING_INT_CAST(int, nYSrc),
130 WINPR_ASSERTING_INT_CAST(int, nXDst),
131 WINPR_ASSERTING_INT_CAST(int, nYDst), dwidth, dheight);
132 }
133 }
134
135 rc = CHANNEL_RC_OK;
136fail:
137 region16_clear(&surface->gdi.invalidRegion);
138 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
139 LogDynAndXSync(xfc->log, xfc->display, False);
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
296static UINT xf_CreateSurface(RdpgfxClientContext* context,
297 const RDPGFX_CREATE_SURFACE_PDU* createSurface)
298{
299 UINT ret = CHANNEL_RC_NO_MEMORY;
300 size_t size = 0;
301 xfGfxSurface* surface = nullptr;
302 rdpGdi* gdi = (rdpGdi*)context->custom;
303 xfContext* xfc = (xfContext*)gdi->context;
304 surface = (xfGfxSurface*)calloc(1, sizeof(xfGfxSurface));
305
306 if (!surface)
307 return CHANNEL_RC_NO_MEMORY;
308
309 surface->gdi.codecs = context->codecs;
310
311 if (!surface->gdi.codecs)
312 {
313 WLog_ERR(TAG, "global GDI codecs aren't set");
314 goto out_free;
315 }
316
317 surface->gdi.surfaceId = createSurface->surfaceId;
318 surface->gdi.width = x11_pad_scanline(createSurface->width, 0);
319 surface->gdi.height = x11_pad_scanline(createSurface->height, 0);
320 surface->gdi.mappedWidth = createSurface->width;
321 surface->gdi.mappedHeight = createSurface->height;
322 surface->gdi.outputTargetWidth = createSurface->width;
323 surface->gdi.outputTargetHeight = createSurface->height;
324
325 switch (createSurface->pixelFormat)
326 {
327 case GFX_PIXEL_FORMAT_ARGB_8888:
328 surface->gdi.format = PIXEL_FORMAT_BGRA32;
329 break;
330
331 case GFX_PIXEL_FORMAT_XRGB_8888:
332 surface->gdi.format = PIXEL_FORMAT_BGRX32;
333 break;
334
335 default:
336 WLog_ERR(TAG, "unknown pixelFormat 0x%" PRIx32 "", createSurface->pixelFormat);
337 ret = ERROR_INTERNAL_ERROR;
338 goto out_free;
339 }
340
341 surface->gdi.scanline = surface->gdi.width * FreeRDPGetBytesPerPixel(surface->gdi.format);
342 surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline,
343 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
344 size = 1ull * surface->gdi.scanline * surface->gdi.height;
345 surface->gdi.data = (BYTE*)winpr_aligned_malloc(size, 16);
346
347 if (!surface->gdi.data)
348 {
349 WLog_ERR(TAG, "unable to allocate GDI data");
350 goto out_free;
351 }
352
353 ZeroMemory(surface->gdi.data, size);
354
355 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
356 {
357 WINPR_ASSERT(xfc->depth != 0);
358 surface->image = LogDynAndXCreateImage(
359 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
360 ZPixmap, 0, (char*)surface->gdi.data, surface->gdi.mappedWidth,
361 surface->gdi.mappedHeight, xfc->scanline_pad,
362 WINPR_ASSERTING_INT_CAST(int, surface->gdi.scanline));
363 }
364 else
365 {
366 UINT32 width = surface->gdi.width;
367 UINT32 bytes = FreeRDPGetBytesPerPixel(gdi->dstFormat);
368 surface->stageScanline = width * bytes;
369 surface->stageScanline = x11_pad_scanline(
370 surface->stageScanline, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
371 size = 1ull * surface->stageScanline * surface->gdi.height;
372 surface->stage = (BYTE*)winpr_aligned_malloc(size, 16);
373
374 if (!surface->stage)
375 {
376 WLog_ERR(TAG, "unable to allocate stage buffer");
377 goto out_free_gdidata;
378 }
379
380 ZeroMemory(surface->stage, size);
381 WINPR_ASSERT(xfc->depth != 0);
382 surface->image = LogDynAndXCreateImage(
383 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
384 ZPixmap, 0, (char*)surface->stage, surface->gdi.mappedWidth, surface->gdi.mappedHeight,
385 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(int, surface->stageScanline));
386 }
387
388 if (!surface->image)
389 {
390 WLog_ERR(TAG, "an error occurred when creating the XImage");
391 goto error_surface_image;
392 }
393
394 surface->image->byte_order = LSBFirst;
395 surface->image->bitmap_bit_order = LSBFirst;
396
397 region16_init(&surface->gdi.invalidRegion);
398
399 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (void*)surface) != CHANNEL_RC_OK)
400 {
401 WLog_ERR(TAG, "an error occurred during SetSurfaceData");
402 goto error_set_surface_data;
403 }
404
405 return CHANNEL_RC_OK;
406error_set_surface_data:
407 surface->image->data = nullptr;
408 XDestroyImage(surface->image);
409error_surface_image:
410 winpr_aligned_free(surface->stage);
411out_free_gdidata:
412 winpr_aligned_free(surface->gdi.data);
413out_free:
414 free(surface);
415 return ret;
416}
417
423static UINT xf_DeleteSurface(RdpgfxClientContext* context,
424 const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
425{
426 rdpCodecs* codecs = nullptr;
427
428 UINT status = 0;
429 EnterCriticalSection(&context->mux);
430 xfGfxSurface* surface =
431 (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
432
433 if (surface)
434 {
435 if (surface->gdi.windowMapped)
436 {
437 status = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
438 surface->gdi.windowId);
439 if (status != CHANNEL_RC_OK)
440 goto fail;
441 }
442
443#ifdef WITH_GFX_H264
444 h264_context_free(surface->gdi.h264);
445#endif
446 surface->image->data = nullptr;
447 XDestroyImage(surface->image);
448 winpr_aligned_free(surface->gdi.data);
449 winpr_aligned_free(surface->stage);
450 region16_uninit(&surface->gdi.invalidRegion);
451 codecs = surface->gdi.codecs;
452 free(surface);
453 }
454
455 status = context->SetSurfaceData(context, deleteSurface->surfaceId, nullptr);
456
457 if (codecs && codecs->progressive)
458 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
459
460fail:
461 LeaveCriticalSection(&context->mux);
462 return status;
463}
464
465static UINT xf_UnmapWindowForSurface(RdpgfxClientContext* context, UINT64 windowID)
466{
467 WINPR_ASSERT(context);
468 rdpGdi* gdi = (rdpGdi*)context->custom;
469 WINPR_ASSERT(gdi);
470
471 xfContext* xfc = (xfContext*)gdi->context;
472 WINPR_ASSERT(gdi->context);
473
474 if (freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode))
475 {
476 xfAppWindow* appWindow = xf_rail_get_window(xfc, windowID);
477 if (appWindow)
478 xf_AppWindowDestroyImage(appWindow);
479 xf_rail_return_window(appWindow);
480 }
481
482 WLog_WARN(TAG, "function not implemented");
483 return CHANNEL_RC_OK;
484}
485
486static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
487{
488 WINPR_ASSERT(context);
489 WINPR_ASSERT(surface);
490
491 rdpGdi* gdi = (rdpGdi*)context->custom;
492 WINPR_ASSERT(gdi);
493
494 xfContext* xfc = (xfContext*)gdi->context;
495 WINPR_ASSERT(gdi->context);
496
497 if (freerdp_settings_get_bool(gdi->context->settings, FreeRDP_RemoteApplicationMode))
498 return xf_AppUpdateWindowFromSurface(xfc, surface);
499
500 WLog_WARN(TAG, "function not implemented");
501 return CHANNEL_RC_OK;
502}
503
504void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
505{
506 rdpGdi* gdi = nullptr;
507 const rdpSettings* settings = nullptr;
508 WINPR_ASSERT(xfc);
509 WINPR_ASSERT(gfx);
510
511 settings = xfc->common.context.settings;
512 WINPR_ASSERT(settings);
513
514 gdi = xfc->common.context.gdi;
515
516 gdi_graphics_pipeline_init(gdi, gfx);
517
518 if (!freerdp_settings_get_bool(settings, FreeRDP_SoftwareGdi))
519 {
520 gfx->UpdateSurfaces = xf_UpdateSurfaces;
521 gfx->CreateSurface = xf_CreateSurface;
522 gfx->DeleteSurface = xf_DeleteSurface;
523 }
524
525 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
526 gfx->UnmapWindowForSurface = xf_UnmapWindowForSurface;
527}
528
529void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
530{
531 rdpGdi* gdi = nullptr;
532
533 WINPR_ASSERT(xfc);
534
535 gdi = xfc->common.context.gdi;
536 gdi_graphics_pipeline_uninit(gdi, gfx);
537}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.