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 (void)xf_rail_paint_surface(xfc, surface->gdi.windowId, rect);
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));
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);
136 region16_clear(&surface->gdi.invalidRegion);
137 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
138 LogDynAndXSync(xfc->log, xfc->display, False);
143static UINT xf_WindowUpdate(RdpgfxClientContext* context, xfGfxSurface* surface)
145 WINPR_ASSERT(context);
146 WINPR_ASSERT(surface);
147 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, &surface->gdi);
150static UINT xf_UpdateSurfaces(RdpgfxClientContext* context)
153 UINT status = CHANNEL_RC_OK;
154 UINT16* pSurfaceIds =
nullptr;
155 rdpGdi* gdi = (rdpGdi*)context->custom;
156 xfContext* xfc =
nullptr;
161 if (gdi->suppressOutput)
162 return CHANNEL_RC_OK;
164 xfc = (xfContext*)gdi->context;
165 EnterCriticalSection(&context->mux);
166 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
167 if (status != CHANNEL_RC_OK)
170 for (UINT32 index = 0; index < count; index++)
172 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
178 if (context->UpdateSurfaceArea)
180 if (surface->gdi.handleInUpdateSurfaceArea)
184 if (surface->gdi.outputMapped)
185 status = xf_OutputUpdate(xfc, surface);
186 else if (surface->gdi.windowMapped)
187 status = xf_WindowUpdate(context, surface);
189 if (status != CHANNEL_RC_OK)
195 LeaveCriticalSection(&context->mux);
199UINT xf_OutputExpose(xfContext* xfc, UINT32 x, UINT32 y, UINT32 width, UINT32 height)
202 UINT status = ERROR_INTERNAL_ERROR;
205 UINT16* pSurfaceIds =
nullptr;
206 RdpgfxClientContext* context =
nullptr;
209 WINPR_ASSERT(xfc->common.context.gdi);
211 context = xfc->common.context.gdi->gfx;
212 WINPR_ASSERT(context);
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);
220 if (status != CHANNEL_RC_OK)
223 if (!TryEnterCriticalSection(&context->mux))
226 return CHANNEL_RC_OK;
228 for (UINT32 index = 0; index < count; index++)
231 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
233 if (!surface || (!surface->gdi.outputMapped && !surface->gdi.windowMapped))
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);
243 if (rectangles_intersection(&invalidRect, &surfaceRect, &intersection))
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,
254 LeaveCriticalSection(&context->mux);
262 LeaveCriticalSection(&context->mux);
263 IFCALLRET(context->UpdateSurfaces, status, context);
265 if (status != CHANNEL_RC_OK)
272static UINT32 x11_pad_scanline(UINT32 scanline, UINT32 inPad)
277 const UINT32 align = inPad / 8;
278 const UINT32 pad = align - scanline % align;
286 scanline += 16 - scanline % 16;
291static void DestroySurface(xfGfxSurface* surface)
297 h264_context_free(surface->gdi.h264);
299#if defined(WITH_GFX_AV1)
300 freerdp_av1_context_free(surface->gdi.av1);
304 surface->image->data =
nullptr;
305 XDestroyImage(surface->image);
308 winpr_aligned_free(surface->gdi.data);
309 winpr_aligned_free(surface->stage);
310 region16_uninit(&surface->gdi.invalidRegion);
319static UINT xf_CreateSurface(RdpgfxClientContext* context,
322 UINT ret = CHANNEL_RC_NO_MEMORY;
323 WINPR_ASSERT(context);
324 WINPR_ASSERT(createSurface);
326 rdpGdi* gdi = (rdpGdi*)context->custom;
327 xfContext* xfc = (xfContext*)gdi->context;
329 xfGfxSurface* surface = (xfGfxSurface*)calloc(1,
sizeof(xfGfxSurface));
332 return CHANNEL_RC_NO_MEMORY;
334 surface->gdi.codecs = context->codecs;
336 if (!surface->gdi.codecs)
338 WLog_ERR(TAG,
"global GDI codecs aren't set");
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;
352 switch (createSurface->pixelFormat)
354 case GFX_PIXEL_FORMAT_ARGB_8888:
355 surface->gdi.format = PIXEL_FORMAT_BGRA32;
358 case GFX_PIXEL_FORMAT_XRGB_8888:
359 surface->gdi.format = rails ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGRX32;
363 WLog_ERR(TAG,
"unknown pixelFormat 0x%" PRIx32
"", createSurface->pixelFormat);
364 ret = ERROR_INTERNAL_ERROR;
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)
374 surface->gdi.scanline = surface->gdi.width * FreeRDPGetBytesPerPixel(surface->gdi.format);
375 surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline, pad) + 16ul;
377 surface->gdi.data = (BYTE*)winpr_aligned_calloc(surface->gdi.scanline, surface->gdi.height, 16);
379 if (!surface->gdi.data)
381 WLog_ERR(TAG,
"unable to allocate GDI data");
385 for (UINT64 y = 0; y < surface->gdi.height; y++)
386 memset(&surface->gdi.data[surface->gdi.scanline * y], 0xff, surface->gdi.scanline);
388 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
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));
399 UINT32 width = surface->gdi.width;
400 UINT32 bytes = FreeRDPGetBytesPerPixel(gdi->dstFormat);
401 if (width > UINT32_MAX / bytes - 16 - pad)
406 surface->stageScanline = width * bytes;
407 surface->stageScanline = x11_pad_scanline(surface->stageScanline, pad) + 16ul;
410 (BYTE*)winpr_aligned_calloc(surface->stageScanline, surface->gdi.height, 16);
414 WLog_ERR(TAG,
"unable to allocate stage buffer");
418 for (UINT64 y = 0; y < surface->gdi.height; y++)
419 memset(&surface->stage[surface->stageScanline * y], 0xff, surface->stageScanline);
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));
430 WLog_ERR(TAG,
"an error occurred when creating the XImage");
434 surface->image->byte_order = LSBFirst;
435 surface->image->bitmap_bit_order = LSBFirst;
437 region16_init(&surface->gdi.invalidRegion);
439 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (
void*)surface) != CHANNEL_RC_OK)
441 WLog_ERR(TAG,
"an error occurred during SetSurfaceData");
445 return CHANNEL_RC_OK;
447 DestroySurface(surface);
456static UINT xf_DeleteSurface(RdpgfxClientContext* context,
459 rdpCodecs* codecs =
nullptr;
462 EnterCriticalSection(&context->mux);
463 xfGfxSurface* surface =
464 (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
468 if (surface->gdi.windowMapped)
470 status = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
471 surface->gdi.windowId);
472 if (status != CHANNEL_RC_OK)
476 codecs = surface->gdi.codecs;
477 DestroySurface(surface);
480 status = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
482 if (codecs && codecs->progressive)
483 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
486 LeaveCriticalSection(&context->mux);
490static UINT xf_UnmapWindowForSurface(RdpgfxClientContext* context, UINT64 windowID)
492 WINPR_ASSERT(context);
493 rdpGdi* gdi = (rdpGdi*)context->custom;
496 xfContext* xfc = (xfContext*)gdi->context;
497 WINPR_ASSERT(gdi->context);
501 xfAppWindow* appWindow = xf_rail_get_window(xfc, windowID, FALSE);
503 xf_AppWindowDestroyImage(appWindow);
504 xf_rail_return_window(appWindow, FALSE);
507 WLog_WARN(TAG,
"function not implemented");
508 return CHANNEL_RC_OK;
511static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
513 WINPR_ASSERT(context);
514 WINPR_ASSERT(surface);
516 rdpGdi* gdi = (rdpGdi*)context->custom;
519 xfContext* xfc = (xfContext*)gdi->context;
520 WINPR_ASSERT(gdi->context);
523 return xf_AppUpdateWindowFromSurface(xfc, surface);
525 WLog_WARN(TAG,
"function not implemented");
526 return CHANNEL_RC_OK;
529void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
531 rdpGdi* gdi =
nullptr;
532 const rdpSettings* settings =
nullptr;
536 settings = xfc->common.context.settings;
537 WINPR_ASSERT(settings);
539 gdi = xfc->common.context.gdi;
541 gdi_graphics_pipeline_init(gdi, gfx);
545 gfx->UpdateSurfaces = xf_UpdateSurfaces;
546 gfx->CreateSurface = xf_CreateSurface;
547 gfx->DeleteSurface = xf_DeleteSurface;
550 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
551 gfx->UnmapWindowForSurface = xf_UnmapWindowForSurface;
554void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
558 rdpGdi* gdi = xfc->common.context.gdi;
559 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.