2016-06-29 16:48:53 +02:00
|
|
|
#include <utils/Logger.h>
|
2014-11-21 21:24:33 +01:00
|
|
|
#include <grabber/X11Grabber.h>
|
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
X11Grabber::X11Grabber(bool useXGetImage, int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation)
|
2017-08-04 23:08:15 +02:00
|
|
|
: Grabber("X11GRABBER", 0, 0, cropLeft, cropRight, cropTop, cropBottom)
|
2016-08-22 11:59:07 +02:00
|
|
|
, _useXGetImage(useXGetImage)
|
|
|
|
, _x11Display(nullptr)
|
|
|
|
, _pixmap(None)
|
|
|
|
, _srcFormat(nullptr)
|
|
|
|
, _dstFormat(nullptr)
|
|
|
|
, _srcPicture(None)
|
|
|
|
, _dstPicture(None)
|
|
|
|
, _horizontalDecimation(horizontalPixelDecimation)
|
|
|
|
, _verticalDecimation(verticalPixelDecimation)
|
|
|
|
, _screenWidth(0)
|
|
|
|
, _screenHeight(0)
|
2017-08-12 07:55:32 +02:00
|
|
|
, _src_x(cropLeft)
|
|
|
|
, _src_y(cropTop)
|
2016-08-22 11:59:07 +02:00
|
|
|
, _image(0,0)
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
_useImageResampler = false;
|
2016-08-22 11:59:07 +02:00
|
|
|
_imageResampler.setCropping(0, 0, 0, 0); // cropping is performed by XRender, XShmGetImage or XGetImage
|
2016-05-26 23:44:27 +02:00
|
|
|
memset(&_pictAttr, 0, sizeof(_pictAttr));
|
|
|
|
_pictAttr.repeat = RepeatNone;
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
X11Grabber::~X11Grabber()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
if (_x11Display != nullptr)
|
|
|
|
{
|
|
|
|
freeResources();
|
|
|
|
XCloseDisplay(_x11Display);
|
|
|
|
}
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
void X11Grabber::freeResources()
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// Cleanup allocated resources of the X11 grab
|
|
|
|
XDestroyImage(_xImage);
|
2016-08-22 11:59:07 +02:00
|
|
|
if(_XShmAvailable && !_useXGetImage)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
XShmDetach(_x11Display, &_shminfo);
|
|
|
|
shmdt(_shminfo.shmaddr);
|
|
|
|
shmctl(_shminfo.shmid, IPC_RMID, 0);
|
|
|
|
}
|
2016-08-22 11:59:07 +02:00
|
|
|
if (_XRenderAvailable && !_useXGetImage)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
XRenderFreePicture(_x11Display, _srcPicture);
|
|
|
|
XRenderFreePicture(_x11Display, _dstPicture);
|
|
|
|
XFreePixmap(_x11Display, _pixmap);
|
|
|
|
}
|
2016-01-21 16:40:46 +01:00
|
|
|
}
|
2014-11-21 21:24:33 +01:00
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
void X11Grabber::setupResources()
|
|
|
|
{
|
2016-08-22 11:59:07 +02:00
|
|
|
if(_XShmAvailable && !_useXGetImage)
|
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual, _windowAttr.depth, ZPixmap, NULL, &_shminfo, _width, _height);
|
2016-05-26 23:44:27 +02:00
|
|
|
_shminfo.shmid = shmget(IPC_PRIVATE, _xImage->bytes_per_line * _xImage->height, IPC_CREAT|0777);
|
|
|
|
_xImage->data = (char*)shmat(_shminfo.shmid,0,0);
|
|
|
|
_shminfo.shmaddr = _xImage->data;
|
|
|
|
_shminfo.readOnly = False;
|
2016-08-22 11:59:07 +02:00
|
|
|
XShmAttach(_x11Display, &_shminfo);
|
2016-05-24 19:55:50 +02:00
|
|
|
}
|
2016-08-22 11:59:07 +02:00
|
|
|
if (_XRenderAvailable && !_useXGetImage)
|
|
|
|
{
|
|
|
|
if(_XShmPixmapAvailable)
|
|
|
|
{
|
2017-08-04 23:08:15 +02:00
|
|
|
_pixmap = XShmCreatePixmap(_x11Display, _window, _xImage->data, &_shminfo, _width, _height, _windowAttr.depth);
|
2016-08-22 11:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-04 23:08:15 +02:00
|
|
|
_pixmap = XCreatePixmap(_x11Display, _window, _width, _height, _windowAttr.depth);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
_srcFormat = XRenderFindVisualFormat(_x11Display, _windowAttr.visual);
|
|
|
|
_dstFormat = XRenderFindVisualFormat(_x11Display, _windowAttr.visual);
|
|
|
|
_srcPicture = XRenderCreatePicture(_x11Display, _window, _srcFormat, CPRepeat, &_pictAttr);
|
|
|
|
_dstPicture = XRenderCreatePicture(_x11Display, _pixmap, _dstFormat, CPRepeat, &_pictAttr);
|
2016-08-23 07:04:57 +02:00
|
|
|
XRenderSetPictureFilter(_x11Display, _srcPicture, FilterBilinear, NULL, 0);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
bool X11Grabber::Setup()
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_x11Display = XOpenDisplay(NULL);
|
|
|
|
if (_x11Display == nullptr)
|
|
|
|
{
|
2016-06-29 16:48:53 +02:00
|
|
|
Error(_log, "Unable to open display");
|
2016-08-22 11:59:07 +02:00
|
|
|
if (getenv("DISPLAY"))
|
|
|
|
{
|
2016-06-29 16:48:53 +02:00
|
|
|
Error(_log, "%s",getenv("DISPLAY"));
|
2016-08-22 11:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-29 16:48:53 +02:00
|
|
|
Error(_log, "DISPLAY environment variable not set");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_window = DefaultRootWindow(_x11Display);
|
|
|
|
|
|
|
|
int dummy, pixmaps_supported;
|
|
|
|
|
|
|
|
_XRenderAvailable = XRenderQueryExtension(_x11Display, &dummy, &dummy);
|
|
|
|
_XShmAvailable = XShmQueryExtension(_x11Display);
|
|
|
|
XShmQueryVersion(_x11Display, &dummy, &dummy, &pixmaps_supported);
|
|
|
|
_XShmPixmapAvailable = pixmaps_supported && XShmPixmapFormat(_x11Display) == ZPixmap;
|
2016-08-22 11:59:07 +02:00
|
|
|
|
|
|
|
// Image scaling is performed by XRender when available, otherwise by ImageResampler
|
|
|
|
_imageResampler.setHorizontalPixelDecimation(_XRenderAvailable ? 1 : _horizontalDecimation);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(_XRenderAvailable ? 1 : _verticalDecimation);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
bool result = (updateScreenDimensions(true) >=0);
|
|
|
|
ErrorIf(!result, _log, "X11 Grabber start failed");
|
|
|
|
return result;
|
2016-05-24 19:55:50 +02:00
|
|
|
}
|
2014-11-21 21:24:33 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int X11Grabber::grabFrame(Image<ColorRgb> & image, bool forceUpdate)
|
2016-01-21 16:40:46 +01:00
|
|
|
{
|
2017-09-01 08:50:37 +02:00
|
|
|
if (!_enabled) return 0;
|
|
|
|
|
2018-08-04 16:01:53 +02:00
|
|
|
updateScreenDimensions(forceUpdate);
|
2017-08-12 07:55:32 +02:00
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
if (_XRenderAvailable && !_useXGetImage)
|
|
|
|
{
|
|
|
|
double scale_x = static_cast<double>(_windowAttr.width / _horizontalDecimation) / static_cast<double>(_windowAttr.width);
|
|
|
|
double scale_y = static_cast<double>(_windowAttr.height / _verticalDecimation) / static_cast<double>(_windowAttr.height);
|
2017-08-04 12:01:45 +02:00
|
|
|
double scale = qMin(scale_y, scale_x);
|
2016-08-22 11:59:07 +02:00
|
|
|
|
|
|
|
_transform =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
XDoubleToFixed(1),
|
|
|
|
XDoubleToFixed(0),
|
|
|
|
XDoubleToFixed(0)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
XDoubleToFixed(0),
|
|
|
|
XDoubleToFixed(1),
|
|
|
|
XDoubleToFixed(0)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
XDoubleToFixed(0),
|
|
|
|
XDoubleToFixed(0),
|
|
|
|
XDoubleToFixed(scale)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
XRenderSetPictureTransform (_x11Display, _srcPicture, &_transform);
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
// display, op, src, mask, dest, src_x = cropLeft,
|
|
|
|
// src_y = cropTop, mask_x, mask_y, dest_x, dest_y, width, height
|
|
|
|
XRenderComposite(
|
|
|
|
_x11Display, PictOpSrc, _srcPicture, None, _dstPicture, ( _src_x/_horizontalDecimation),
|
|
|
|
(_src_y/_verticalDecimation), 0, 0, 0, 0, _width, _height);
|
2016-08-22 11:59:07 +02:00
|
|
|
|
2016-05-25 18:53:09 +02:00
|
|
|
XSync(_x11Display, False);
|
2016-08-22 11:59:07 +02:00
|
|
|
|
2016-08-23 07:04:57 +02:00
|
|
|
if (_XShmAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
|
2016-08-22 11:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-04 23:08:15 +02:00
|
|
|
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _width, _height, AllPlanes, ZPixmap);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2016-08-22 11:59:07 +02:00
|
|
|
}
|
2017-08-12 07:55:32 +02:00
|
|
|
else if (_XShmAvailable && !_useXGetImage)
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
// use xshm
|
|
|
|
XShmGetImage(_x11Display, _window, _xImage, _src_x, _src_y, AllPlanes);
|
2016-08-22 11:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
// all things done by xgetimage
|
|
|
|
_xImage = XGetImage(_x11Display, _window, _src_x, _src_y, _width, _height, AllPlanes, ZPixmap);
|
2016-07-24 15:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_xImage == nullptr)
|
|
|
|
{
|
|
|
|
Error(_log, "Grab Failed!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_imageResampler.processImage(reinterpret_cast<const uint8_t *>(_xImage->data), _xImage->width, _xImage->height, _xImage->bytes_per_line, PIXELFORMAT_BGR32, image);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int X11Grabber::updateScreenDimensions(bool force)
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
const Status status = XGetWindowAttributes(_x11Display, _window, &_windowAttr);
|
|
|
|
if (status == 0)
|
|
|
|
{
|
2016-06-29 16:48:53 +02:00
|
|
|
Error(_log, "Failed to obtain window attributes");
|
2016-05-26 23:44:27 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
if (!force && _screenWidth == unsigned(_windowAttr.width) && _screenHeight == unsigned(_windowAttr.height))
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
// No update required
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
if (_screenWidth || _screenHeight)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
freeResources();
|
|
|
|
}
|
|
|
|
|
2016-07-24 15:18:34 +02:00
|
|
|
Info(_log, "Update of screen resolution: [%dx%d] to [%dx%d]", _screenWidth, _screenHeight, _windowAttr.width, _windowAttr.height);
|
2016-05-26 23:44:27 +02:00
|
|
|
_screenWidth = _windowAttr.width;
|
|
|
|
_screenHeight = _windowAttr.height;
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int width=0, height=0;
|
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
// Image scaling is performed by XRender when available, otherwise by ImageResampler
|
|
|
|
if (_XRenderAvailable && !_useXGetImage)
|
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
width = (_screenWidth > unsigned(_cropLeft + _cropRight))
|
2016-08-22 11:59:07 +02:00
|
|
|
? ((_screenWidth - _cropLeft - _cropRight) / _horizontalDecimation)
|
|
|
|
: _screenWidth / _horizontalDecimation;
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
height = (_screenHeight > unsigned(_cropTop + _cropBottom))
|
2016-08-22 11:59:07 +02:00
|
|
|
? ((_screenHeight - _cropTop - _cropBottom) / _verticalDecimation)
|
|
|
|
: _screenHeight / _verticalDecimation;
|
|
|
|
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Using XRender for grabbing");
|
2016-08-22 11:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
width = (_screenWidth > unsigned(_cropLeft + _cropRight))
|
2016-08-22 11:59:07 +02:00
|
|
|
? (_screenWidth - _cropLeft - _cropRight)
|
|
|
|
: _screenWidth;
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
height = (_screenHeight > unsigned(_cropTop + _cropBottom))
|
2016-08-22 11:59:07 +02:00
|
|
|
? (_screenHeight - _cropTop - _cropBottom)
|
|
|
|
: _screenHeight;
|
|
|
|
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Using XGetImage for grabbing");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
// calculate final image dimensions and adjust top/left cropping in 3D modes
|
|
|
|
switch (_videoMode)
|
|
|
|
{
|
|
|
|
case VIDEO_3DSBS:
|
|
|
|
_width = width /2;
|
|
|
|
_height = height;
|
|
|
|
_src_x = _cropLeft / 2;
|
|
|
|
_src_y = _cropTop;
|
|
|
|
break;
|
|
|
|
case VIDEO_3DTAB:
|
|
|
|
_width = width;
|
|
|
|
_height = height / 2;
|
|
|
|
_src_x = _cropLeft;
|
|
|
|
_src_y = _cropTop / 2;
|
|
|
|
break;
|
|
|
|
case VIDEO_2D:
|
|
|
|
default:
|
|
|
|
_width = width;
|
|
|
|
_height = height;
|
|
|
|
_src_x = _cropLeft;
|
|
|
|
_src_y = _cropTop;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Info(_log, "Update output image resolution: [%dx%d] to [%dx%d]", _image.width(), _image.height(), _width, _height);
|
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
_image.resize(_width, _height);
|
2016-05-26 23:44:27 +02:00
|
|
|
setupResources();
|
|
|
|
|
2016-07-24 15:18:34 +02:00
|
|
|
return 1;
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
2017-08-12 07:55:32 +02:00
|
|
|
|
|
|
|
void X11Grabber::setVideoMode(VideoMode mode)
|
|
|
|
{
|
|
|
|
Info(_log, "a %d", mode);
|
|
|
|
Grabber::setVideoMode(mode);
|
|
|
|
updateScreenDimensions(true);
|
|
|
|
}
|