FreeRDP/client/X11/xf_gfx.c

419 lines
12 KiB
C
Raw Normal View History

/**
* FreeRDP: A Remote Desktop Protocol Implementation
* X11 Graphics Pipeline
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2016 Armin Novak <armin.novak@thincast.com>
* Copyright 2016 Thincast Technologies GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2014-09-12 19:13:01 +04:00
#include <freerdp/log.h>
#include "xf_gfx.h"
#include "xf_rail.h"
2018-02-12 13:14:54 +03:00
#include <X11/Xutil.h>
2014-09-12 19:13:01 +04:00
#define TAG CLIENT_TAG("x11")
static UINT xf_OutputUpdate(xfContext* xfc, xfGfxSurface* surface)
{
UINT rc = ERROR_INTERNAL_ERROR;
UINT32 surfaceX, surfaceY;
2014-06-05 20:36:01 +04:00
RECTANGLE_16 surfaceRect;
2016-08-03 15:16:20 +03:00
rdpGdi* gdi;
UINT32 nbRects, x;
2019-05-07 11:22:02 +03:00
double sx, sy;
const RECTANGLE_16* rects;
2016-08-03 15:16:20 +03:00
gdi = xfc->context.gdi;
surfaceX = surface->gdi.outputOriginX;
surfaceY = surface->gdi.outputOriginY;
surfaceRect.left = 0;
surfaceRect.top = 0;
surfaceRect.right = surface->gdi.mappedWidth;
surfaceRect.bottom = surface->gdi.mappedHeight;
XSetClipMask(xfc->display, xfc->gc, None);
XSetFunction(xfc->display, xfc->gc, GXcopy);
XSetFillStyle(xfc->display, xfc->gc, FillSolid);
2019-11-06 17:24:51 +03:00
region16_intersect_rect(&(surface->gdi.invalidRegion), &(surface->gdi.invalidRegion),
&surfaceRect);
sx = surface->gdi.outputTargetWidth / (double)surface->gdi.mappedWidth;
sy = surface->gdi.outputTargetHeight / (double)surface->gdi.mappedHeight;
if (!(rects = region16_rects(&surface->gdi.invalidRegion, &nbRects)))
return CHANNEL_RC_OK;
2014-06-05 20:36:01 +04:00
for (x = 0; x < nbRects; x++)
{
const UINT32 nXSrc = rects[x].left;
const UINT32 nYSrc = rects[x].top;
2019-05-07 11:22:02 +03:00
const UINT32 swidth = rects[x].right - nXSrc;
const UINT32 sheight = rects[x].bottom - nYSrc;
const UINT32 nXDst = surfaceX + nXSrc * sx;
const UINT32 nYDst = surfaceY + nYSrc * sy;
2019-05-08 13:58:01 +03:00
const UINT32 dwidth = swidth * sx;
2019-05-07 11:22:02 +03:00
const UINT32 dheight = sheight * sy;
2014-06-05 20:36:01 +04:00
2014-09-17 22:29:56 +04:00
if (surface->stage)
{
2019-11-06 17:24:51 +03:00
if (!freerdp_image_scale(surface->stage, gdi->dstFormat, surface->stageScanline, nXSrc,
nYSrc, dwidth, dheight, surface->gdi.data, surface->gdi.format,
surface->gdi.scanline, nXSrc, nYSrc, swidth, sheight))
goto fail;
2014-09-17 22:29:56 +04:00
}
if (xfc->remote_app)
{
2019-11-06 17:24:51 +03:00
XPutImage(xfc->display, xfc->primary, xfc->gc, surface->image, nXSrc, nYSrc, nXDst,
nYDst, dwidth, dheight);
xf_lock_x11(xfc);
2019-05-07 11:22:02 +03:00
xf_rail_paint(xfc, nXDst, nYDst, nXDst + dwidth, nYDst + dheight);
xf_unlock_x11(xfc);
}
else
#ifdef WITH_XRENDER
2019-11-06 17:24:51 +03:00
if (xfc->context.settings->SmartSizing || xfc->context.settings->MultiTouchGestures)
{
XPutImage(xfc->display, xfc->primary, xfc->gc, surface->image, nXSrc, nYSrc, nXDst,
nYDst, dwidth, dheight);
xf_draw_screen(xfc, nXDst, nYDst, dwidth, dheight);
}
else
#endif
2019-11-06 17:24:51 +03:00
{
XPutImage(xfc->display, xfc->drawable, xfc->gc, surface->image, nXSrc, nYSrc, nXDst,
nYDst, dwidth, dheight);
}
}
2014-06-05 20:36:01 +04:00
rc = CHANNEL_RC_OK;
fail:
region16_clear(&surface->gdi.invalidRegion);
XSetClipMask(xfc->display, xfc->gc, None);
XSync(xfc->display, False);
return rc;
2014-06-05 20:36:01 +04:00
}
static UINT xf_UpdateSurfaces(RdpgfxClientContext* context)
{
2015-06-09 16:22:26 +03:00
UINT16 count;
UINT32 index;
UINT status = CHANNEL_RC_OK;
UINT16* pSurfaceIds = NULL;
2016-07-13 15:58:11 +03:00
rdpGdi* gdi = (rdpGdi*)context->custom;
2019-10-16 19:49:24 +03:00
xfContext* xfc;
if (!gdi)
return status;
if (gdi->suppressOutput)
return CHANNEL_RC_OK;
2019-11-06 17:24:51 +03:00
xfc = (xfContext*)gdi->context;
EnterCriticalSection(&context->mux);
2015-06-09 16:22:26 +03:00
context->GetSurfaceIds(context, &pSurfaceIds, &count);
for (index = 0; index < count; index++)
{
2019-11-06 17:24:51 +03:00
xfGfxSurface* surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
2019-05-07 11:22:02 +03:00
if (!surface)
continue;
/* If UpdateSurfaceArea callback is available, the output has already been updated. */
if (context->UpdateSurfaceArea)
{
if (surface->gdi.windowId != 0)
continue;
}
2019-05-07 11:22:02 +03:00
status = ERROR_INTERNAL_ERROR;
if (surface->gdi.outputMapped)
status = xf_OutputUpdate(xfc, surface);
if (status != 0)
break;
}
free(pSurfaceIds);
LeaveCriticalSection(&context->mux);
return status;
}
2019-11-06 17:24:51 +03:00
UINT xf_OutputExpose(xfContext* xfc, UINT32 x, UINT32 y, UINT32 width, UINT32 height)
{
2015-06-09 16:22:26 +03:00
UINT16 count;
UINT32 index;
UINT status = ERROR_INTERNAL_ERROR;
xfGfxSurface* surface;
RECTANGLE_16 invalidRect;
RECTANGLE_16 surfaceRect;
RECTANGLE_16 intersection;
UINT16* pSurfaceIds = NULL;
2018-02-08 15:55:36 +03:00
RdpgfxClientContext* context = xfc->context.gdi->gfx;
invalidRect.left = x;
invalidRect.top = y;
2014-06-20 01:08:50 +04:00
invalidRect.right = x + width;
invalidRect.bottom = y + height;
status = context->GetSurfaceIds(context, &pSurfaceIds, &count);
if (status != CHANNEL_RC_OK)
goto fail;
if (!TryEnterCriticalSection(&context->mux))
{
free(pSurfaceIds);
return CHANNEL_RC_OK;
}
for (index = 0; index < count; index++)
{
2019-11-06 17:24:51 +03:00
surface = (xfGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
if (!surface || !surface->gdi.outputMapped)
continue;
surfaceRect.left = surface->gdi.outputOriginX;
surfaceRect.top = surface->gdi.outputOriginY;
2019-05-07 11:22:02 +03:00
surfaceRect.right = surface->gdi.outputOriginX + surface->gdi.outputTargetWidth;
surfaceRect.bottom = surface->gdi.outputOriginY + surface->gdi.outputTargetHeight;
if (rectangles_intersection(&invalidRect, &surfaceRect, &intersection))
{
/* Invalid rects are specified relative to surface origin */
intersection.left -= surfaceRect.left;
intersection.top -= surfaceRect.top;
intersection.right -= surfaceRect.left;
intersection.bottom -= surfaceRect.top;
2019-11-06 17:24:51 +03:00
region16_union_rect(&surface->gdi.invalidRegion, &surface->gdi.invalidRegion,
2016-07-14 17:08:06 +03:00
&intersection);
}
}
free(pSurfaceIds);
LeaveCriticalSection(&context->mux);
IFCALLRET(context->UpdateSurfaces, status, context);
if (status != CHANNEL_RC_OK)
goto fail;
fail:
return status;
}
UINT32 x11_pad_scanline(UINT32 scanline, UINT32 inPad)
2016-11-24 11:02:06 +03:00
{
/* Ensure X11 alignment is met */
2016-11-24 11:02:06 +03:00
if (inPad > 0)
{
const UINT32 align = inPad / 8;
const UINT32 pad = align - scanline % align;
if (align != pad)
scanline += pad;
}
/* 16 byte alingment is required for ASM optimized code */
if (scanline % 16)
scanline += 16 - scanline % 16;
2016-11-24 11:02:06 +03:00
return scanline;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT xf_CreateSurface(RdpgfxClientContext* context,
2016-07-14 17:08:06 +03:00
const RDPGFX_CREATE_SURFACE_PDU* createSurface)
{
UINT ret = CHANNEL_RC_NO_MEMORY;
size_t size;
xfGfxSurface* surface;
2016-07-13 15:58:11 +03:00
rdpGdi* gdi = (rdpGdi*)context->custom;
2019-11-06 17:24:51 +03:00
xfContext* xfc = (xfContext*)gdi->context;
surface = (xfGfxSurface*)calloc(1, sizeof(xfGfxSurface));
if (!surface)
return CHANNEL_RC_NO_MEMORY;
surface->gdi.codecs = context->codecs;
2018-02-08 13:55:57 +03:00
if (!surface->gdi.codecs)
{
WLog_ERR(TAG, "%s: global GDI codecs aren't set", __FUNCTION__);
goto out_free;
}
surface->gdi.surfaceId = createSurface->surfaceId;
surface->gdi.width = x11_pad_scanline(createSurface->width, 0);
surface->gdi.height = x11_pad_scanline(createSurface->height, 0);
surface->gdi.mappedWidth = createSurface->width;
surface->gdi.mappedHeight = createSurface->height;
surface->gdi.outputTargetWidth = createSurface->width;
surface->gdi.outputTargetHeight = createSurface->height;
2016-07-14 17:08:06 +03:00
switch (createSurface->pixelFormat)
{
2016-07-14 17:08:06 +03:00
case GFX_PIXEL_FORMAT_ARGB_8888:
surface->gdi.format = PIXEL_FORMAT_BGRA32;
break;
case GFX_PIXEL_FORMAT_XRGB_8888:
surface->gdi.format = PIXEL_FORMAT_BGRX32;
break;
default:
2019-11-06 17:24:51 +03:00
WLog_ERR(TAG, "%s: unknown pixelFormat 0x%" PRIx32 "", __FUNCTION__,
createSurface->pixelFormat);
ret = ERROR_INTERNAL_ERROR;
goto out_free;
}
surface->gdi.scanline = surface->gdi.width * GetBytesPerPixel(surface->gdi.format);
2016-11-24 11:02:06 +03:00
surface->gdi.scanline = x11_pad_scanline(surface->gdi.scanline, xfc->scanline_pad);
2021-01-25 12:06:01 +03:00
size = surface->gdi.scanline * surface->gdi.height * 1ULL;
surface->gdi.data = (BYTE*)_aligned_malloc(size, 16);
2018-02-08 13:55:57 +03:00
if (!surface->gdi.data)
{
WLog_ERR(TAG, "%s: unable to allocate GDI data", __FUNCTION__);
goto out_free;
}
2018-02-08 13:55:57 +03:00
ZeroMemory(surface->gdi.data, size);
2016-07-14 17:08:06 +03:00
if (AreColorFormatsEqualNoAlpha(gdi->dstFormat, surface->gdi.format))
{
2019-11-06 17:24:51 +03:00
surface->image =
XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
(char*)surface->gdi.data, surface->gdi.mappedWidth,
surface->gdi.mappedHeight, xfc->scanline_pad, surface->gdi.scanline);
}
else
{
UINT32 width = surface->gdi.width;
2016-08-03 15:16:20 +03:00
UINT32 bytes = GetBytesPerPixel(gdi->dstFormat);
surface->stageScanline = width * bytes;
2016-11-24 11:02:06 +03:00
surface->stageScanline = x11_pad_scanline(surface->stageScanline, xfc->scanline_pad);
2021-01-25 12:06:01 +03:00
size = surface->stageScanline * surface->gdi.height * 1ULL;
2019-11-06 17:24:51 +03:00
surface->stage = (BYTE*)_aligned_malloc(size, 16);
2018-02-08 13:55:57 +03:00
if (!surface->stage)
{
WLog_ERR(TAG, "%s: unable to allocate stage buffer", __FUNCTION__);
goto out_free_gdidata;
}
2018-02-08 13:55:57 +03:00
ZeroMemory(surface->stage, size);
2019-11-06 17:24:51 +03:00
surface->image =
XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0, (char*)surface->stage,
surface->gdi.mappedWidth, surface->gdi.mappedHeight, xfc->scanline_pad,
surface->stageScanline);
2016-03-02 17:16:49 +03:00
}
if (!surface->image)
{
WLog_ERR(TAG, "%s: an error occurred when creating the XImage", __FUNCTION__);
goto error_surface_image;
}
Fix colors on big endian (#4135) * client/x11: Fix colors on big endian The bitmaps are recieved in little endian byte order (LSBFirst in terms of X11). This is a problem on systems, where bitmaps are expected in big endian byte order (MSBFirst). X11 client tries to handle this situation by different color formats (e.g. RGBX vs. BGRX), but this doesn't work. BGRX is not MSBFirst variant of RGBX, it should be XRGB. But this would work only for 32/24 depths, where color channels matches bytes. Let's say to the XServer that all the bitmaps are in LSBFirst encoding instead of changing the color formats on big endian systems. https://github.com/FreeRDP/FreeRDP/issues/2520 * client/x11: Fix cursor color on big endian The cursor color is wrong on big endian systems. It is not probably possible to force bitmap_order for XcursorImage as it is possible for XImage. Fortunately, cursors are always 32 bit so we can use ABGR instead of BGRA to deal with. https://github.com/FreeRDP/FreeRDP/issues/2520 * client/x11: Fix comment indentation The comment has wrong indentation for some reason, let's fix it. * client/x11: Fix BGR vs. RGB detection The BGR vs. RGB detection code is leftover from history and I am conviced that it is wrong with the current color handling, where invert flag is TRUE by default. However, I suppose that the detection still makes sense and XServer may use the inverted formats in some cases. Maybe we can force XServer to use our masks somehow, but let's just fix the value to FALSE now. * client/x11: Remove unused color shifts The color shifts are lefover from history and are not used in current code. Let's remove them.
2017-09-18 11:47:56 +03:00
surface->image->byte_order = LSBFirst;
surface->image->bitmap_bit_order = LSBFirst;
surface->gdi.outputMapped = FALSE;
region16_init(&surface->gdi.invalidRegion);
2018-02-08 13:55:57 +03:00
2019-11-06 17:24:51 +03:00
if (context->SetSurfaceData(context, surface->gdi.surfaceId, (void*)surface) != CHANNEL_RC_OK)
{
WLog_ERR(TAG, "%s: an error occurred during SetSurfaceData", __FUNCTION__);
goto error_set_surface_data;
}
2018-02-08 13:55:57 +03:00
return CHANNEL_RC_OK;
error_set_surface_data:
2018-02-12 13:14:54 +03:00
surface->image->data = NULL;
XDestroyImage(surface->image);
error_surface_image:
_aligned_free(surface->stage);
out_free_gdidata:
_aligned_free(surface->gdi.data);
out_free:
free(surface);
return ret;
2016-03-02 17:16:49 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT xf_DeleteSurface(RdpgfxClientContext* context,
2016-07-14 17:08:06 +03:00
const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
{
rdpCodecs* codecs = NULL;
xfGfxSurface* surface = NULL;
UINT status;
EnterCriticalSection(&context->mux);
2019-11-06 17:24:51 +03:00
surface = (xfGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
if (surface)
{
2019-05-07 11:22:02 +03:00
if (surface->gdi.windowId > 0)
IFCALL(context->UnmapWindowForSurface, context, surface->gdi.windowId);
#ifdef WITH_GFX_H264
h264_context_free(surface->gdi.h264);
#endif
2018-02-12 13:14:54 +03:00
surface->image->data = NULL;
XDestroyImage(surface->image);
_aligned_free(surface->gdi.data);
_aligned_free(surface->stage);
region16_uninit(&surface->gdi.invalidRegion);
codecs = surface->gdi.codecs;
free(surface);
}
status = context->SetSurfaceData(context, deleteSurface->surfaceId, NULL);
if (codecs && codecs->progressive)
2019-11-06 17:24:51 +03:00
progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
LeaveCriticalSection(&context->mux);
return status;
}
void xf_graphics_pipeline_init(xfContext* xfc, RdpgfxClientContext* gfx)
{
rdpGdi* gdi = xfc->context.gdi;
gdi_graphics_pipeline_init(gdi, gfx);
if (!xfc->context.settings->SoftwareGdi)
{
gfx->UpdateSurfaces = xf_UpdateSurfaces;
gfx->CreateSurface = xf_CreateSurface;
gfx->DeleteSurface = xf_DeleteSurface;
}
}
void xf_graphics_pipeline_uninit(xfContext* xfc, RdpgfxClientContext* gfx)
{
rdpGdi* gdi = xfc->context.gdi;
gdi_graphics_pipeline_uninit(gdi, gfx);
}