22 #include <freerdp/config.h>
26 #include <winpr/assert.h>
27 #include <winpr/cast.h>
29 #include <freerdp/log.h>
33 #include <X11/Xutil.h>
35 #define TAG CLIENT_TAG("x11")
37 static UINT xf_OutputUpdate(xfContext* xfc, xfGfxSurface* surface)
39 UINT rc = ERROR_INTERNAL_ERROR;
47 WINPR_ASSERT(surface);
49 rdpGdi* gdi = xfc->common.context.gdi;
52 rdpSettings* settings = xfc->common.context.settings;
53 WINPR_ASSERT(settings);
55 surfaceX = surface->gdi.outputOriginX;
56 surfaceY = surface->gdi.outputOriginY;
59 surfaceRect.right = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.mappedWidth);
60 surfaceRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.mappedHeight);
61 XSetClipMask(xfc->display, xfc->gc, None);
62 XSetFunction(xfc->display, xfc->gc, GXcopy);
63 XSetFillStyle(xfc->display, xfc->gc, FillSolid);
64 region16_intersect_rect(&(surface->gdi.invalidRegion), &(surface->gdi.invalidRegion),
67 WINPR_ASSERT(surface->gdi.mappedWidth);
68 WINPR_ASSERT(surface->gdi.mappedHeight);
69 const double sx = 1.0 * surface->gdi.outputTargetWidth / (double)surface->gdi.mappedWidth;
70 const double sy = 1.0 * surface->gdi.outputTargetHeight / (
double)surface->gdi.mappedHeight;
72 if (!(rects = region16_rects(&surface->gdi.invalidRegion, &nbRects)))
75 for (UINT32 x = 0; x < nbRects; x++)
78 const UINT32 nXSrc = rect->left;
79 const UINT32 nYSrc = rect->top;
80 const UINT32 swidth = rect->right - nXSrc;
81 const UINT32 sheight = rect->bottom - nYSrc;
82 const UINT32 nXDst = (UINT32)lround(1.0 * surfaceX + nXSrc * sx);
83 const UINT32 nYDst = (UINT32)lround(1.0 * surfaceY + nYSrc * sy);
84 const UINT32 dwidth = (UINT32)lround(1.0 * swidth * sx);
85 const UINT32 dheight = (UINT32)lround(1.0 * sheight * sy);
89 if (!freerdp_image_scale(surface->stage, gdi->dstFormat, surface->stageScanline, nXSrc,
90 nYSrc, dwidth, dheight, surface->gdi.data, surface->gdi.format,
91 surface->gdi.scanline, nXSrc, nYSrc, swidth, sheight))
97 XPutImage(xfc->display, xfc->primary, xfc->gc, surface->image,
98 WINPR_ASSERTING_INT_CAST(
int, nXSrc), WINPR_ASSERTING_INT_CAST(
int, nYSrc),
99 WINPR_ASSERTING_INT_CAST(
int, nXDst), WINPR_ASSERTING_INT_CAST(
int, nYDst),
102 xf_rail_paint_surface(xfc, surface->gdi.windowId, rect);
110 XPutImage(xfc->display, xfc->primary, xfc->gc, surface->image,
111 WINPR_ASSERTING_INT_CAST(
int, nXSrc), WINPR_ASSERTING_INT_CAST(
int, nYSrc),
112 WINPR_ASSERTING_INT_CAST(
int, nXDst), WINPR_ASSERTING_INT_CAST(
int, nYDst),
114 xf_draw_screen(xfc, WINPR_ASSERTING_INT_CAST(int32_t, nXDst),
115 WINPR_ASSERTING_INT_CAST(int32_t, nYDst),
116 WINPR_ASSERTING_INT_CAST(int32_t, dwidth),
117 WINPR_ASSERTING_INT_CAST(int32_t, dheight));
122 XPutImage(xfc->display, xfc->drawable, xfc->gc, surface->image,
123 WINPR_ASSERTING_INT_CAST(
int, nXSrc), WINPR_ASSERTING_INT_CAST(
int, nYSrc),
124 WINPR_ASSERTING_INT_CAST(
int, nXDst), WINPR_ASSERTING_INT_CAST(
int, nYDst),
131 region16_clear(&surface->gdi.invalidRegion);
132 XSetClipMask(xfc->display, xfc->gc, None);
133 XSync(xfc->display, False);
137 static UINT xf_WindowUpdate(RdpgfxClientContext* context, xfGfxSurface* surface)
139 WINPR_ASSERT(context);
140 WINPR_ASSERT(surface);
141 return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, &surface->gdi);
144 static UINT xf_UpdateSurfaces(RdpgfxClientContext* context)
147 UINT status = CHANNEL_RC_OK;
148 UINT16* pSurfaceIds = NULL;
149 rdpGdi* gdi = (rdpGdi*)context->custom;
150 xfContext* xfc = NULL;
155 if (gdi->suppressOutput)
156 return CHANNEL_RC_OK;
158 xfc = (xfContext*)gdi->context;
159 EnterCriticalSection(&context->mux);
160 context->GetSurfaceIds(context, &pSurfaceIds, &count);
162 for (UINT32 index = 0; index < count; index++)
164 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
170 if (context->UpdateSurfaceArea)
172 if (surface->gdi.handleInUpdateSurfaceArea)
176 if (surface->gdi.outputMapped)
177 status = xf_OutputUpdate(xfc, surface);
178 else if (surface->gdi.windowMapped)
179 status = xf_WindowUpdate(context, surface);
181 if (status != CHANNEL_RC_OK)
186 LeaveCriticalSection(&context->mux);
190 UINT xf_OutputExpose(xfContext* xfc, UINT32 x, UINT32 y, UINT32 width, UINT32 height)
193 UINT status = ERROR_INTERNAL_ERROR;
196 UINT16* pSurfaceIds = NULL;
197 RdpgfxClientContext* context = NULL;
200 WINPR_ASSERT(xfc->common.context.gdi);
202 context = xfc->common.context.gdi->gfx;
203 WINPR_ASSERT(context);
205 invalidRect.left = WINPR_ASSERTING_INT_CAST(UINT16, x);
206 invalidRect.top = WINPR_ASSERTING_INT_CAST(UINT16, y);
207 invalidRect.right = WINPR_ASSERTING_INT_CAST(UINT16, x + width);
208 invalidRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, y + height);
209 status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
211 if (status != CHANNEL_RC_OK)
214 if (!TryEnterCriticalSection(&context->mux))
217 return CHANNEL_RC_OK;
219 for (UINT32 index = 0; index < count; index++)
222 xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
224 if (!surface || (!surface->gdi.outputMapped && !surface->gdi.windowMapped))
227 surfaceRect.left = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX);
228 surfaceRect.top = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY);
229 surfaceRect.right = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginX +
230 surface->gdi.outputTargetWidth);
231 surfaceRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, surface->gdi.outputOriginY +
232 surface->gdi.outputTargetHeight);
234 if (rectangles_intersection(&invalidRect, &surfaceRect, &intersection))
237 intersection.left -= surfaceRect.left;
238 intersection.top -= surfaceRect.top;
239 intersection.right -= surfaceRect.left;
240 intersection.bottom -= surfaceRect.top;
241 region16_union_rect(&surface->gdi.invalidRegion, &surface->gdi.invalidRegion,
247 LeaveCriticalSection(&context->mux);
248 IFCALLRET(context->UpdateSurfaces, status, context);
250 if (status != CHANNEL_RC_OK)
257 static UINT32 x11_pad_scanline(UINT32 scanline, UINT32 inPad)
262 const UINT32 align = inPad / 8;
263 const UINT32 pad = align - scanline % align;
271 scanline += 16 - scanline % 16;
281 static UINT xf_CreateSurface(RdpgfxClientContext* context,
284 UINT ret = CHANNEL_RC_NO_MEMORY;
286 xfGfxSurface* surface = NULL;
287 rdpGdi* gdi = (rdpGdi*)context->custom;
288 xfContext* xfc = (xfContext*)gdi->context;
289 surface = (xfGfxSurface*)calloc(1,
sizeof(xfGfxSurface));
292 return CHANNEL_RC_NO_MEMORY;
294 surface->gdi.codecs = context->codecs;
296 if (!surface->gdi.codecs)
298 WLog_ERR(TAG,
"global GDI codecs aren't set");
302 surface->gdi.surfaceId = createSurface->surfaceId;
303 surface->gdi.width = x11_pad_scanline(createSurface->width, 0);
304 surface->gdi.height = x11_pad_scanline(createSurface->height, 0);
305 surface->gdi.mappedWidth = createSurface->width;
306 surface->gdi.mappedHeight = createSurface->height;
307 surface->gdi.outputTargetWidth = createSurface->width;
308 surface->gdi.outputTargetHeight = createSurface->height;
310 switch (createSurface->pixelFormat)
312 case GFX_PIXEL_FORMAT_ARGB_8888:
313 surface->gdi.format = PIXEL_FORMAT_BGRA32;
316 case GFX_PIXEL_FORMAT_XRGB_8888:
317 surface->gdi.format = PIXEL_FORMAT_BGRX32;
321 WLog_ERR(TAG,
"unknown pixelFormat 0x%" PRIx32
"", createSurface->pixelFormat);
322 ret = ERROR_INTERNAL_ERROR;
326 surface->gdi.scanline = surface->gdi.width * FreeRDPGetBytesPerPixel(surface->gdi.format);
327 surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline,
328 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
329 size = 1ull * surface->gdi.scanline * surface->gdi.height;
330 surface->gdi.data = (BYTE*)winpr_aligned_malloc(size, 16);
332 if (!surface->gdi.data)
334 WLog_ERR(TAG,
"unable to allocate GDI data");
338 ZeroMemory(surface->gdi.data, size);
340 if (FreeRDPAreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
342 WINPR_ASSERT(xfc->depth != 0);
343 surface->image = XCreateImage(
344 xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth), ZPixmap, 0,
345 (
char*)surface->gdi.data, surface->gdi.mappedWidth, surface->gdi.mappedHeight,
346 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(
int, surface->gdi.scanline));
350 UINT32 width = surface->gdi.width;
351 UINT32 bytes = FreeRDPGetBytesPerPixel(gdi->dstFormat);
352 surface->stageScanline = width * bytes;
353 surface->stageScanline = x11_pad_scanline(
354 surface->stageScanline, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->scanline_pad));
355 size = 1ull * surface->stageScanline * surface->gdi.height;
356 surface->stage = (BYTE*)winpr_aligned_malloc(size, 16);
360 WLog_ERR(TAG,
"unable to allocate stage buffer");
361 goto out_free_gdidata;
364 ZeroMemory(surface->stage, size);
365 WINPR_ASSERT(xfc->depth != 0);
366 surface->image = XCreateImage(
367 xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth), ZPixmap, 0,
368 (
char*)surface->stage, surface->gdi.mappedWidth, surface->gdi.mappedHeight,
369 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(
int, surface->stageScanline));
374 WLog_ERR(TAG,
"an error occurred when creating the XImage");
375 goto error_surface_image;
378 surface->image->byte_order = LSBFirst;
379 surface->image->bitmap_bit_order = LSBFirst;
381 region16_init(&surface->gdi.invalidRegion);
383 if (context->SetSurfaceData(context, surface->gdi.surfaceId, (
void*)surface) != CHANNEL_RC_OK)
385 WLog_ERR(TAG,
"an error occurred during SetSurfaceData");
386 goto error_set_surface_data;
389 return CHANNEL_RC_OK;
390 error_set_surface_data:
391 surface->image->data = NULL;
392 XDestroyImage(surface->image);
394 winpr_aligned_free(surface->stage);
396 winpr_aligned_free(surface->gdi.data);
407 static UINT xf_DeleteSurface(RdpgfxClientContext* context,
410 rdpCodecs* codecs = NULL;
411 xfGfxSurface* surface = NULL;
413 EnterCriticalSection(&context->mux);
414 surface = (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
418 if (surface->gdi.windowMapped)
419 IFCALL(context->UnmapWindowForSurface, context, surface->gdi.windowId);
422 h264_context_free(surface->gdi.h264);
424 surface->image->data = NULL;
425 XDestroyImage(surface->image);
426 winpr_aligned_free(surface->gdi.data);
427 winpr_aligned_free(surface->stage);
428 region16_uninit(&surface->gdi.invalidRegion);
429 codecs = surface->gdi.codecs;
433 status = context->SetSurfaceData(context, deleteSurface->surfaceId, NULL);
435 if (codecs && codecs->progressive)
436 progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
438 LeaveCriticalSection(&context->mux);
442 static UINT xf_UpdateWindowFromSurface(RdpgfxClientContext* context, gdiGfxSurface* surface)
444 WINPR_ASSERT(context);
445 WINPR_ASSERT(surface);
447 rdpGdi* gdi = (rdpGdi*)context->custom;
450 xfContext* xfc = (xfContext*)gdi->context;
451 WINPR_ASSERT(gdi->context);
454 return xf_AppUpdateWindowFromSurface(xfc, surface);
456 WLog_WARN(TAG,
"function not implemented");
457 return CHANNEL_RC_OK;
460 void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
463 const rdpSettings* settings = NULL;
467 settings = xfc->common.context.settings;
468 WINPR_ASSERT(settings);
470 gdi = xfc->common.context.gdi;
472 gdi_graphics_pipeline_init(gdi, gfx);
476 gfx->UpdateSurfaces = xf_UpdateSurfaces;
477 gfx->CreateSurface = xf_CreateSurface;
478 gfx->DeleteSurface = xf_DeleteSurface;
480 gfx->UpdateWindowFromSurface = xf_UpdateWindowFromSurface;
483 void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
489 gdi = xfc->common.context.gdi;
490 gdi_graphics_pipeline_uninit(gdi, gfx);
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.