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 region16_intersect_rect(&(surface->gdi.invalidRegion), &(surface->gdi.invalidRegion),
69 WINPR_ASSERT(surface->gdi.mappedWidth);
70 WINPR_ASSERT(surface->gdi.mappedHeight);
71 const double sx = 1.0 * surface->gdi.outputTargetWidth / (double)surface->gdi.mappedWidth;
72 const double sy = 1.0 * surface->gdi.outputTargetHeight / (double)surface->gdi.mappedHeight;
74 if (!(rects = region16_rects(&surface->gdi.invalidRegion, &nbRects)))
77 for (UINT32 x = 0; x < nbRects; x++)
80 const UINT32 nXSrc = rect->left;
81 const UINT32 nYSrc = rect->top;
82 const UINT32 swidth = rect->right - nXSrc;
83 const UINT32 sheight = rect->bottom - nYSrc;
84 const UINT32 nXDst = (UINT32)lround(1.0 * surfaceX + nXSrc * sx);
85 const UINT32 nYDst = (UINT32)lround(1.0 * surfaceY + nYSrc * sy);
86 const UINT32 dwidth = (UINT32)lround(1.0 * swidth * sx);
87 const UINT32 dheight = (UINT32)lround(1.0 * sheight * sy);
91 if (!freerdp_image_scale(surface->stage, gdi->dstFormat, surface->stageScanline, nXSrc,
92 nYSrc, dwidth, dheight, surface->gdi.data, surface->gdi.format,
93 surface->gdi.scanline, nXSrc, nYSrc, swidth, sheight))
99 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, surface->image,
100 WINPR_ASSERTING_INT_CAST(
int, nXSrc),
101 WINPR_ASSERTING_INT_CAST(
int, nYSrc),
102 WINPR_ASSERTING_INT_CAST(
int, nXDst),
103 WINPR_ASSERTING_INT_CAST(
int, nYDst), dwidth, dheight);
105 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);
142static UINT xf_WindowUpdate(RdpgfxClientContext* context, xfGfxSurface* surface)
144 WINPR_ASSERT(context);
145 WINPR_ASSERT(surface);
146 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, &surface->gdi);
149static UINT xf_UpdateSurfaces(RdpgfxClientContext* context)
152 UINT status = CHANNEL_RC_OK;
153 UINT16* pSurfaceIds = NULL;
154 rdpGdi* gdi = (rdpGdi*)context->custom;
155 xfContext* xfc = NULL;
160 if (gdi->suppressOutput)
161 return CHANNEL_RC_OK;
163 xfc = (xfContext*)gdi->context;
164 EnterCriticalSection(&context->mux);
165 context->GetSurfaceIds(context, &pSurfaceIds, &count);
167 for (UINT32 index = 0; index < count; index++)
169 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
175 if (context->UpdateSurfaceArea)
177 if (surface->gdi.handleInUpdateSurfaceArea)
181 if (surface->gdi.outputMapped)
182 status = xf_OutputUpdate(xfc, surface);
183 else if (surface->gdi.windowMapped)
184 status = xf_WindowUpdate(context, surface);
186 if (status != CHANNEL_RC_OK)
191 LeaveCriticalSection(&context->mux);
195UINT xf_OutputExpose(xfContext* xfc, UINT32 x, UINT32 y, UINT32 width, UINT32 height)
198 UINT status = ERROR_INTERNAL_ERROR;
201 UINT16* pSurfaceIds = NULL;
202 RdpgfxClientContext* context = NULL;
205 WINPR_ASSERT(xfc->common.context.gdi);
207 context = xfc->common.context.gdi->gfx;
208 WINPR_ASSERT(context);
210 invalidRect.left = WINPR_ASSERTING_INT_CAST(UINT16, x);
211 invalidRect.top = WINPR_ASSERTING_INT_CAST(UINT16, y);
212 invalidRect.right = WINPR_ASSERTING_INT_CAST(UINT16, x + width);
213 invalidRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, y + height);
214 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
216 if (status != CHANNEL_RC_OK)
219 if (!TryEnterCriticalSection(&context->mux))
222 return CHANNEL_RC_OK;
224 for (UINT32 index = 0; index < count; index++)
227 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
229 if (!surface || (!surface->gdi.outputMapped && !surface->gdi.windowMapped))
232 surfaceRect.left = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX);
233 surfaceRect.top = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY);
234 surfaceRect.right = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX +
235 surface->gdi.outputTargetWidth);
236 surfaceRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY +
237 surface->gdi.outputTargetHeight);
239 if (rectangles_intersection(&invalidRect, &surfaceRect, &intersection))
242 intersection.left -= surfaceRect.left;
243 intersection.top -= surfaceRect.top;
244 intersection.right -= surfaceRect.left;
245 intersection.bottom -= surfaceRect.top;
246 region16_union_rect(&surface->gdi.invalidRegion, &surface->gdi.invalidRegion,
252 LeaveCriticalSection(&context->mux);
253 IFCALLRET(context->UpdateSurfaces, status, context);
255 if (status != CHANNEL_RC_OK)
262static UINT32 x11_pad_scanline(UINT32 scanline, UINT32 inPad)
267 const UINT32 align = inPad / 8;
268 const UINT32 pad = align - scanline % align;
276 scanline += 16 - scanline % 16;
286static UINT xf_CreateSurface(RdpgfxClientContext* context,
289 UINT ret = CHANNEL_RC_NO_MEMORY;
291 xfGfxSurface* surface = NULL;
292 rdpGdi* gdi = (rdpGdi*)context->custom;
293 xfContext* xfc = (xfContext*)gdi->context;
294 surface = (xfGfxSurface*)calloc(1,
sizeof(xfGfxSurface));
297 return CHANNEL_RC_NO_MEMORY;
299 surface->gdi.codecs = context->codecs;
301 if (!surface->gdi.codecs)
303 WLog_ERR(TAG,
"global GDI codecs aren't set");
307 surface->gdi.surfaceId = createSurface->surfaceId;
308 surface->gdi.width = x11_pad_scanline(createSurface->width, 0);
309 surface->gdi.height = x11_pad_scanline(createSurface->height, 0);
310 surface->gdi.mappedWidth = createSurface->width;
311 surface->gdi.mappedHeight = createSurface->height;
312 surface->gdi.outputTargetWidth = createSurface->width;
313 surface->gdi.outputTargetHeight = createSurface->height;
315 switch (createSurface->pixelFormat)
317 case GFX_PIXEL_FORMAT_ARGB_8888:
318 surface->gdi.format = PIXEL_FORMAT_BGRA32;
321 case GFX_PIXEL_FORMAT_XRGB_8888:
322 surface->gdi.format = PIXEL_FORMAT_BGRX32;
326 WLog_ERR(TAG,
"unknown pixelFormat 0x%" PRIx32
"", createSurface->pixelFormat);
327 ret = ERROR_INTERNAL_ERROR;
331 surface->gdi.scanline = surface->gdi.width * FreeRDPGetBytesPerPixel(surface->gdi.format);
332 surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline,
333 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
334 size = 1ull * surface->gdi.scanline * surface->gdi.height;
335 surface->gdi.data = (BYTE*)winpr_aligned_malloc(size, 16);
337 if (!surface->gdi.data)
339 WLog_ERR(TAG,
"unable to allocate GDI data");
343 ZeroMemory(surface->gdi.data, size);
345 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
347 WINPR_ASSERT(xfc->depth != 0);
348 surface->image = LogDynAndXCreateImage(
349 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
350 ZPixmap, 0, (
char*)surface->gdi.data, surface->gdi.mappedWidth,
351 surface->gdi.mappedHeight, xfc->scanline_pad,
352 WINPR_ASSERTING_INT_CAST(
int, surface->gdi.scanline));
356 UINT32 width = surface->gdi.width;
357 UINT32 bytes = FreeRDPGetBytesPerPixel(gdi->dstFormat);
358 surface->stageScanline = width * bytes;
359 surface->stageScanline = x11_pad_scanline(
360 surface->stageScanline, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
361 size = 1ull * surface->stageScanline * surface->gdi.height;
362 surface->stage = (BYTE*)winpr_aligned_malloc(size, 16);
366 WLog_ERR(TAG,
"unable to allocate stage buffer");
367 goto out_free_gdidata;
370 ZeroMemory(surface->stage, size);
371 WINPR_ASSERT(xfc->depth != 0);
372 surface->image = LogDynAndXCreateImage(
373 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
374 ZPixmap, 0, (
char*)surface->stage, surface->gdi.mappedWidth, surface->gdi.mappedHeight,
375 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(
int, surface->stageScanline));
380 WLog_ERR(TAG,
"an error occurred when creating the XImage");
381 goto error_surface_image;
384 surface->image->byte_order = LSBFirst;
385 surface->image->bitmap_bit_order = LSBFirst;
387 region16_init(&surface->gdi.invalidRegion);
389 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (
void*)surface) != CHANNEL_RC_OK)
391 WLog_ERR(TAG,
"an error occurred during SetSurfaceData");
392 goto error_set_surface_data;
395 return CHANNEL_RC_OK;
396error_set_surface_data:
397 surface->image->data = NULL;
398 XDestroyImage(surface->image);
400 winpr_aligned_free(surface->stage);
402 winpr_aligned_free(surface->gdi.data);
413static UINT xf_DeleteSurface(RdpgfxClientContext* context,
416 rdpCodecs* codecs = NULL;
417 xfGfxSurface* surface = NULL;
419 EnterCriticalSection(&context->mux);
420 surface = (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
424 if (surface->gdi.windowMapped)
425 IFCALL(context->UnmapWindowForSurface, context, surface->gdi.windowId);
428 h264_context_free(surface->gdi.h264);
430 surface->image->data = NULL;
431 XDestroyImage(surface->image);
432 winpr_aligned_free(surface->gdi.data);
433 winpr_aligned_free(surface->stage);
434 region16_uninit(&surface->gdi.invalidRegion);
435 codecs = surface->gdi.codecs;
439 status = context->SetSurfaceData(context, deleteSurface->surfaceId, NULL);
441 if (codecs && codecs->progressive)
442 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
444 LeaveCriticalSection(&context->mux);
448static UINT xf_UnmapWindowForSurface(RdpgfxClientContext* context, UINT64 windowID)
450 WINPR_ASSERT(context);
451 rdpGdi* gdi = (rdpGdi*)context->custom;
454 xfContext* xfc = (xfContext*)gdi->context;
455 WINPR_ASSERT(gdi->context);
459 xfAppWindow* appWindow = xf_rail_get_window(xfc, windowID);
461 xf_AppWindowDestroyImage(appWindow);
462 xf_rail_return_window(appWindow);
465 WLog_WARN(TAG,
"function not implemented");
466 return CHANNEL_RC_OK;
469static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
471 WINPR_ASSERT(context);
472 WINPR_ASSERT(surface);
474 rdpGdi* gdi = (rdpGdi*)context->custom;
477 xfContext* xfc = (xfContext*)gdi->context;
478 WINPR_ASSERT(gdi->context);
481 return xf_AppUpdateWindowFromSurface(xfc, surface);
483 WLog_WARN(TAG,
"function not implemented");
484 return CHANNEL_RC_OK;
487void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
490 const rdpSettings* settings = NULL;
494 settings = xfc->common.context.settings;
495 WINPR_ASSERT(settings);
497 gdi = xfc->common.context.gdi;
499 gdi_graphics_pipeline_init(gdi, gfx);
503 gfx->UpdateSurfaces = xf_UpdateSurfaces;
504 gfx->CreateSurface = xf_CreateSurface;
505 gfx->DeleteSurface = xf_DeleteSurface;
508 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
509 gfx->UnmapWindowForSurface = xf_UnmapWindowForSurface;
512void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
518 gdi = xfc->common.context.gdi;
519 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.