22#include <freerdp/config.h>
26#include <winpr/assert.h>
27#include <winpr/cast.h>
29#include <freerdp/log.h>
37#define TAG CLIENT_TAG("x11")
39static UINT xf_OutputUpdate(xfContext* xfc, xfGfxSurface* surface)
41 UINT rc = ERROR_INTERNAL_ERROR;
49 WINPR_ASSERT(surface);
51 rdpGdi* gdi = xfc->common.context.gdi;
54 rdpSettings* settings = xfc->common.context.settings;
55 WINPR_ASSERT(settings);
57 surfaceX = surface->gdi.outputOriginX;
58 surfaceY = surface->gdi.outputOriginY;
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),
68 return ERROR_INTERNAL_ERROR;
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;
75 if (!(rects = region16_rects(&surface->gdi.invalidRegion, &nbRects)))
79 for (UINT32 x = 0; x < nbRects; 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);
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))
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 if (!xf_rail_paint_surface(xfc, surface->gdi.windowId, rect))
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));
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);
137 region16_clear(&surface->gdi.invalidRegion);
138 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
139 LogDynAndXSync(xfc->log, xfc->display, False);
144static UINT xf_WindowUpdate(RdpgfxClientContext* context, xfGfxSurface* surface)
146 WINPR_ASSERT(context);
147 WINPR_ASSERT(surface);
148 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, &surface->gdi);
151static UINT xf_UpdateSurfaces(RdpgfxClientContext* context)
154 UINT status = CHANNEL_RC_OK;
155 UINT16* pSurfaceIds =
nullptr;
156 rdpGdi* gdi = (rdpGdi*)context->custom;
157 xfContext* xfc =
nullptr;
162 if (gdi->suppressOutput)
163 return CHANNEL_RC_OK;
165 xfc = (xfContext*)gdi->context;
166 EnterCriticalSection(&context->mux);
167 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
168 if (status != CHANNEL_RC_OK)
171 for (UINT32 index = 0; index < count; index++)
173 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
179 if (context->UpdateSurfaceArea)
181 if (surface->gdi.handleInUpdateSurfaceArea)
185 if (surface->gdi.outputMapped)
186 status = xf_OutputUpdate(xfc, surface);
187 else if (surface->gdi.windowMapped)
188 status = xf_WindowUpdate(context, surface);
190 if (status != CHANNEL_RC_OK)
196 LeaveCriticalSection(&context->mux);
200UINT xf_OutputExpose(xfContext* xfc, UINT32 x, UINT32 y, UINT32 width, UINT32 height)
203 UINT status = ERROR_INTERNAL_ERROR;
206 UINT16* pSurfaceIds =
nullptr;
207 RdpgfxClientContext* context =
nullptr;
210 WINPR_ASSERT(xfc->common.context.gdi);
212 context = xfc->common.context.gdi->gfx;
213 WINPR_ASSERT(context);
215 invalidRect.left = WINPR_ASSERTING_INT_CAST(UINT16, x);
216 invalidRect.top = WINPR_ASSERTING_INT_CAST(UINT16, y);
217 invalidRect.right = WINPR_ASSERTING_INT_CAST(UINT16, x + width);
218 invalidRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, y + height);
219 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
221 if (status != CHANNEL_RC_OK)
224 if (!TryEnterCriticalSection(&context->mux))
227 return CHANNEL_RC_OK;
229 for (UINT32 index = 0; index < count; index++)
232 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
234 if (!surface || (!surface->gdi.outputMapped && !surface->gdi.windowMapped))
237 surfaceRect.left = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX);
238 surfaceRect.top = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY);
239 surfaceRect.right = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX +
240 surface->gdi.outputTargetWidth);
241 surfaceRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY +
242 surface->gdi.outputTargetHeight);
244 if (rectangles_intersection(&invalidRect, &surfaceRect, &intersection))
247 intersection.left -= surfaceRect.left;
248 intersection.top -= surfaceRect.top;
249 intersection.right -= surfaceRect.left;
250 intersection.bottom -= surfaceRect.top;
251 if (!region16_union_rect(&surface->gdi.invalidRegion, &surface->gdi.invalidRegion,
255 LeaveCriticalSection(&context->mux);
263 LeaveCriticalSection(&context->mux);
264 IFCALLRET(context->UpdateSurfaces, status, context);
266 if (status != CHANNEL_RC_OK)
273static UINT32 x11_pad_scanline(UINT32 scanline, UINT32 inPad)
278 const UINT32 align = inPad / 8;
279 const UINT32 pad = align - scanline % align;
287 scanline += 16 - scanline % 16;
297static UINT xf_CreateSurface(RdpgfxClientContext* context,
300 UINT ret = CHANNEL_RC_NO_MEMORY;
302 xfGfxSurface* surface =
nullptr;
303 rdpGdi* gdi = (rdpGdi*)context->custom;
304 xfContext* xfc = (xfContext*)gdi->context;
305 surface = (xfGfxSurface*)calloc(1,
sizeof(xfGfxSurface));
308 return CHANNEL_RC_NO_MEMORY;
310 surface->gdi.codecs = context->codecs;
312 if (!surface->gdi.codecs)
314 WLog_ERR(TAG,
"global GDI codecs aren't set");
318 surface->gdi.surfaceId = createSurface->surfaceId;
319 surface->gdi.width = x11_pad_scanline(createSurface->width, 0);
320 surface->gdi.height = x11_pad_scanline(createSurface->height, 0);
321 surface->gdi.mappedWidth = createSurface->width;
322 surface->gdi.mappedHeight = createSurface->height;
323 surface->gdi.outputTargetWidth = createSurface->width;
324 surface->gdi.outputTargetHeight = createSurface->height;
326 switch (createSurface->pixelFormat)
328 case GFX_PIXEL_FORMAT_ARGB_8888:
329 surface->gdi.format = PIXEL_FORMAT_BGRA32;
332 case GFX_PIXEL_FORMAT_XRGB_8888:
333 surface->gdi.format = PIXEL_FORMAT_BGRX32;
337 WLog_ERR(TAG,
"unknown pixelFormat 0x%" PRIx32
"", createSurface->pixelFormat);
338 ret = ERROR_INTERNAL_ERROR;
342 surface->gdi.scanline = surface->gdi.width * FreeRDPGetBytesPerPixel(surface->gdi.format);
343 surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline,
344 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
345 size = 1ull * surface->gdi.scanline * surface->gdi.height;
346 surface->gdi.data = (BYTE*)winpr_aligned_malloc(size, 16);
348 if (!surface->gdi.data)
350 WLog_ERR(TAG,
"unable to allocate GDI data");
354 ZeroMemory(surface->gdi.data, size);
356 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
358 WINPR_ASSERT(xfc->depth != 0);
359 surface->image = LogDynAndXCreateImage(
360 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
361 ZPixmap, 0, (
char*)surface->gdi.data, surface->gdi.mappedWidth,
362 surface->gdi.mappedHeight, xfc->scanline_pad,
363 WINPR_ASSERTING_INT_CAST(
int, surface->gdi.scanline));
367 UINT32 width = surface->gdi.width;
368 UINT32 bytes = FreeRDPGetBytesPerPixel(gdi->dstFormat);
369 surface->stageScanline = width * bytes;
370 surface->stageScanline = x11_pad_scanline(
371 surface->stageScanline, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
372 size = 1ull * surface->stageScanline * surface->gdi.height;
373 surface->stage = (BYTE*)winpr_aligned_malloc(size, 16);
377 WLog_ERR(TAG,
"unable to allocate stage buffer");
378 goto out_free_gdidata;
381 ZeroMemory(surface->stage, size);
382 WINPR_ASSERT(xfc->depth != 0);
383 surface->image = LogDynAndXCreateImage(
384 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
385 ZPixmap, 0, (
char*)surface->stage, surface->gdi.mappedWidth, surface->gdi.mappedHeight,
386 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(
int, surface->stageScanline));
391 WLog_ERR(TAG,
"an error occurred when creating the XImage");
392 goto error_surface_image;
395 surface->image->byte_order = LSBFirst;
396 surface->image->bitmap_bit_order = LSBFirst;
398 region16_init(&surface->gdi.invalidRegion);
400 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (
void*)surface) != CHANNEL_RC_OK)
402 WLog_ERR(TAG,
"an error occurred during SetSurfaceData");
403 goto error_set_surface_data;
406 return CHANNEL_RC_OK;
407error_set_surface_data:
408 surface->image->data =
nullptr;
409 XDestroyImage(surface->image);
411 winpr_aligned_free(surface->stage);
413 winpr_aligned_free(surface->gdi.data);
424static UINT xf_DeleteSurface(RdpgfxClientContext* context,
427 rdpCodecs* codecs =
nullptr;
430 EnterCriticalSection(&context->mux);
431 xfGfxSurface* surface =
432 (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
436 if (surface->gdi.windowMapped)
438 status = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
439 surface->gdi.windowId);
440 if (status != CHANNEL_RC_OK)
445 h264_context_free(surface->gdi.h264);
447#if defined(WITH_GFX_AV1)
448 freerdp_av1_context_free(surface->gdi.av1);
450 surface->image->data =
nullptr;
451 XDestroyImage(surface->image);
452 winpr_aligned_free(surface->gdi.data);
453 winpr_aligned_free(surface->stage);
454 region16_uninit(&surface->gdi.invalidRegion);
455 codecs = surface->gdi.codecs;
459 status = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
461 if (codecs && codecs->progressive)
462 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
465 LeaveCriticalSection(&context->mux);
469static UINT xf_UnmapWindowForSurface(RdpgfxClientContext* context, UINT64 windowID)
471 WINPR_ASSERT(context);
472 rdpGdi* gdi = (rdpGdi*)context->custom;
475 xfContext* xfc = (xfContext*)gdi->context;
476 WINPR_ASSERT(gdi->context);
480 xfAppWindow* appWindow = xf_rail_get_window(xfc, windowID, FALSE);
482 xf_AppWindowDestroyImage(appWindow);
483 xf_rail_return_window(appWindow, FALSE);
486 WLog_WARN(TAG,
"function not implemented");
487 return CHANNEL_RC_OK;
490static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
492 WINPR_ASSERT(context);
493 WINPR_ASSERT(surface);
495 rdpGdi* gdi = (rdpGdi*)context->custom;
498 xfContext* xfc = (xfContext*)gdi->context;
499 WINPR_ASSERT(gdi->context);
502 return xf_AppUpdateWindowFromSurface(xfc, surface);
504 WLog_WARN(TAG,
"function not implemented");
505 return CHANNEL_RC_OK;
508void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
510 rdpGdi* gdi =
nullptr;
511 const rdpSettings* settings =
nullptr;
515 settings = xfc->common.context.settings;
516 WINPR_ASSERT(settings);
518 gdi = xfc->common.context.gdi;
520 gdi_graphics_pipeline_init(gdi, gfx);
524 gfx->UpdateSurfaces = xf_UpdateSurfaces;
525 gfx->CreateSurface = xf_CreateSurface;
526 gfx->DeleteSurface = xf_DeleteSurface;
529 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
530 gfx->UnmapWindowForSurface = xf_UnmapWindowForSurface;
533void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
537 rdpGdi* gdi = xfc->common.context.gdi;
538 gdi_graphics_pipeline_uninit(gdi, gfx);
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.