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)))
78 for (UINT32 x = 0; x < nbRects; 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);
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))
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);
106 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);
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;
296static UINT xf_CreateSurface(RdpgfxClientContext* context,
299 UINT ret = CHANNEL_RC_NO_MEMORY;
301 xfGfxSurface* surface =
nullptr;
302 rdpGdi* gdi = (rdpGdi*)context->custom;
303 xfContext* xfc = (xfContext*)gdi->context;
304 surface = (xfGfxSurface*)calloc(1,
sizeof(xfGfxSurface));
307 return CHANNEL_RC_NO_MEMORY;
309 surface->gdi.codecs = context->codecs;
311 if (!surface->gdi.codecs)
313 WLog_ERR(TAG,
"global GDI codecs aren't set");
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;
325 switch (createSurface->pixelFormat)
327 case GFX_PIXEL_FORMAT_ARGB_8888:
328 surface->gdi.format = PIXEL_FORMAT_BGRA32;
331 case GFX_PIXEL_FORMAT_XRGB_8888:
332 surface->gdi.format = PIXEL_FORMAT_BGRX32;
336 WLog_ERR(TAG,
"unknown pixelFormat 0x%" PRIx32
"", createSurface->pixelFormat);
337 ret = ERROR_INTERNAL_ERROR;
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);
347 if (!surface->gdi.data)
349 WLog_ERR(TAG,
"unable to allocate GDI data");
353 ZeroMemory(surface->gdi.data, size);
355 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
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));
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);
376 WLog_ERR(TAG,
"unable to allocate stage buffer");
377 goto out_free_gdidata;
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));
390 WLog_ERR(TAG,
"an error occurred when creating the XImage");
391 goto error_surface_image;
394 surface->image->byte_order = LSBFirst;
395 surface->image->bitmap_bit_order = LSBFirst;
397 region16_init(&surface->gdi.invalidRegion);
399 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (
void*)surface) != CHANNEL_RC_OK)
401 WLog_ERR(TAG,
"an error occurred during SetSurfaceData");
402 goto error_set_surface_data;
405 return CHANNEL_RC_OK;
406error_set_surface_data:
407 surface->image->data =
nullptr;
408 XDestroyImage(surface->image);
410 winpr_aligned_free(surface->stage);
412 winpr_aligned_free(surface->gdi.data);
423static UINT xf_DeleteSurface(RdpgfxClientContext* context,
426 rdpCodecs* codecs =
nullptr;
429 EnterCriticalSection(&context->mux);
430 xfGfxSurface* surface =
431 (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
435 if (surface->gdi.windowMapped)
437 status = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
438 surface->gdi.windowId);
439 if (status != CHANNEL_RC_OK)
444 h264_context_free(surface->gdi.h264);
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;
455 status = context->SetSurfaceData(context, deleteSurface->surfaceId,
nullptr);
457 if (codecs && codecs->progressive)
458 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
461 LeaveCriticalSection(&context->mux);
465static UINT xf_UnmapWindowForSurface(RdpgfxClientContext* context, UINT64 windowID)
467 WINPR_ASSERT(context);
468 rdpGdi* gdi = (rdpGdi*)context->custom;
471 xfContext* xfc = (xfContext*)gdi->context;
472 WINPR_ASSERT(gdi->context);
476 xfAppWindow* appWindow = xf_rail_get_window(xfc, windowID);
478 xf_AppWindowDestroyImage(appWindow);
479 xf_rail_return_window(appWindow);
482 WLog_WARN(TAG,
"function not implemented");
483 return CHANNEL_RC_OK;
486static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
488 WINPR_ASSERT(context);
489 WINPR_ASSERT(surface);
491 rdpGdi* gdi = (rdpGdi*)context->custom;
494 xfContext* xfc = (xfContext*)gdi->context;
495 WINPR_ASSERT(gdi->context);
498 return xf_AppUpdateWindowFromSurface(xfc, surface);
500 WLog_WARN(TAG,
"function not implemented");
501 return CHANNEL_RC_OK;
504void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
506 rdpGdi* gdi =
nullptr;
507 const rdpSettings* settings =
nullptr;
511 settings = xfc->common.context.settings;
512 WINPR_ASSERT(settings);
514 gdi = xfc->common.context.gdi;
516 gdi_graphics_pipeline_init(gdi, gfx);
520 gfx->UpdateSurfaces = xf_UpdateSurfaces;
521 gfx->CreateSurface = xf_CreateSurface;
522 gfx->DeleteSurface = xf_DeleteSurface;
525 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
526 gfx->UnmapWindowForSurface = xf_UnmapWindowForSurface;
529void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
531 rdpGdi* gdi =
nullptr;
535 gdi = xfc->common.context.gdi;
536 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.