2016-06-29 16:48:53 +02:00
|
|
|
#include <utils/Logger.h>
|
2014-11-21 21:24:33 +01:00
|
|
|
#include <grabber/X11Grabber.h>
|
|
|
|
|
2020-07-12 09:23:13 +02:00
|
|
|
#include <xcb/randr.h>
|
|
|
|
#include <xcb/xcb_event.h>
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
X11Grabber::X11Grabber(int cropLeft, int cropRight, int cropTop, int cropBottom, int pixelDecimation)
|
2017-08-04 23:08:15 +02:00
|
|
|
: Grabber("X11GRABBER", 0, 0, cropLeft, cropRight, cropTop, cropBottom)
|
2016-08-22 11:59:07 +02:00
|
|
|
, _x11Display(nullptr)
|
|
|
|
, _pixmap(None)
|
|
|
|
, _srcFormat(nullptr)
|
|
|
|
, _dstFormat(nullptr)
|
|
|
|
, _srcPicture(None)
|
|
|
|
, _dstPicture(None)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _pixelDecimation(pixelDecimation)
|
2016-08-22 11:59:07 +02:00
|
|
|
, _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);
|
2020-07-12 09:23:13 +02:00
|
|
|
if (_XRandRAvailable)
|
|
|
|
{
|
|
|
|
qApp->removeNativeEventFilter(this);
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
if(_XShmAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
XShmDetach(_x11Display, &_shminfo);
|
|
|
|
shmdt(_shminfo.shmaddr);
|
|
|
|
shmctl(_shminfo.shmid, IPC_RMID, 0);
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
if (_XRenderAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
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()
|
|
|
|
{
|
2020-07-12 09:23:13 +02:00
|
|
|
if (_XRandRAvailable)
|
|
|
|
{
|
|
|
|
qApp->installNativeEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
if(_XShmAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual, _windowAttr.depth, ZPixmap, NULL, &_shminfo, _width, _height);
|
2019-06-05 18:19:08 +02:00
|
|
|
_shminfo.shmid = shmget(IPC_PRIVATE, (size_t) _xImage->bytes_per_line * _xImage->height, IPC_CREAT|0777);
|
2016-05-26 23:44:27 +02:00
|
|
|
_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
|
|
|
}
|
2020-08-03 12:31:39 +02:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
if (_XRenderAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
2020-08-03 12:31:39 +02:00
|
|
|
_useImageResampler = false;
|
|
|
|
_imageResampler.setHorizontalPixelDecimation(1);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(1);
|
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
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);
|
2020-08-03 12:31:39 +02:00
|
|
|
|
2016-08-23 07:04:57 +02:00
|
|
|
XRenderSetPictureFilter(_x11Display, _srcPicture, FilterBilinear, NULL, 0);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2020-08-03 12:31:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_useImageResampler = true;
|
|
|
|
_imageResampler.setHorizontalPixelDecimation(_pixelDecimation);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(_pixelDecimation);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
_window = DefaultRootWindow(_x11Display);
|
|
|
|
|
|
|
|
int dummy, pixmaps_supported;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2020-07-12 09:23:13 +02:00
|
|
|
_XRandRAvailable = XRRQueryExtension(_x11Display, &_XRandREventBase, &dummy);
|
2016-05-26 23:44:27 +02:00
|
|
|
_XRenderAvailable = XRenderQueryExtension(_x11Display, &dummy, &dummy);
|
|
|
|
_XShmAvailable = XShmQueryExtension(_x11Display);
|
|
|
|
XShmQueryVersion(_x11Display, &dummy, &dummy, &pixmaps_supported);
|
|
|
|
_XShmPixmapAvailable = pixmaps_supported && XShmPixmapFormat(_x11Display) == ZPixmap;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
bool result = (updateScreenDimensions(true) >=0);
|
|
|
|
ErrorIf(!result, _log, "X11 Grabber start failed");
|
2018-12-28 18:12:45 +01:00
|
|
|
setEnabled(result);
|
2017-08-12 07:55:32 +02:00
|
|
|
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-12-27 23:11:32 +01:00
|
|
|
if (forceUpdate)
|
|
|
|
updateScreenDimensions(forceUpdate);
|
|
|
|
|
|
|
|
if (_XRenderAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
double scale_x = static_cast<double>(_windowAttr.width / _pixelDecimation) / static_cast<double>(_windowAttr.width);
|
|
|
|
double scale_y = static_cast<double>(_windowAttr.height / _pixelDecimation) / static_cast<double>(_windowAttr.height);
|
2017-08-04 12:01:45 +02:00
|
|
|
double scale = qMin(scale_y, scale_x);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
XRenderSetPictureTransform (_x11Display, _srcPicture, &_transform);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
// display, op, src, mask, dest, src_x = cropLeft,
|
2018-12-27 23:11:32 +01:00
|
|
|
// src_y = cropTop, mask_x, mask_y, dest_x, dest_y, width, height
|
2017-08-12 07:55:32 +02:00
|
|
|
XRenderComposite(
|
2018-12-27 23:11:32 +01:00
|
|
|
_x11Display, PictOpSrc, _srcPicture, None, _dstPicture, ( _src_x/_pixelDecimation),
|
|
|
|
(_src_y/_pixelDecimation), 0, 0, 0, 0, _width, _height);
|
|
|
|
|
2016-05-25 18:53:09 +02:00
|
|
|
XSync(_x11Display, False);
|
2018-12-27 23:11:32 +01: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
|
|
|
|
{
|
2018-12-27 23:11:32 +01: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
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
else if (_XShmAvailable)
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
_imageResampler.processImage(reinterpret_cast<const uint8_t *>(_xImage->data), _xImage->width, _xImage->height, _xImage->bytes_per_line, PixelFormat::BGR32, image);
|
2016-07-24 15:18:34 +02:00
|
|
|
|
|
|
|
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;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int width=0, height=0;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-08-22 11:59:07 +02:00
|
|
|
// Image scaling is performed by XRender when available, otherwise by ImageResampler
|
2018-12-27 23:11:32 +01:00
|
|
|
if (_XRenderAvailable)
|
2016-08-22 11:59:07 +02:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
width = (_screenWidth > unsigned(_cropLeft + _cropRight))
|
2018-12-27 23:11:32 +01:00
|
|
|
? ((_screenWidth - _cropLeft - _cropRight) / _pixelDecimation)
|
|
|
|
: _screenWidth / _pixelDecimation;
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
height = (_screenHeight > unsigned(_cropTop + _cropBottom))
|
2018-12-27 23:11:32 +01:00
|
|
|
? ((_screenHeight - _cropTop - _cropBottom) / _pixelDecimation)
|
|
|
|
: _screenHeight / _pixelDecimation;
|
|
|
|
|
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;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
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;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoMode::VIDEO_3DSBS:
|
2017-08-12 07:55:32 +02:00
|
|
|
_width = width /2;
|
|
|
|
_height = height;
|
|
|
|
_src_x = _cropLeft / 2;
|
|
|
|
_src_y = _cropTop;
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoMode::VIDEO_3DTAB:
|
2017-08-12 07:55:32 +02:00
|
|
|
_width = width;
|
|
|
|
_height = height / 2;
|
|
|
|
_src_x = _cropLeft;
|
|
|
|
_src_y = _cropTop / 2;
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoMode::VIDEO_2D:
|
2017-08-12 07:55:32 +02:00
|
|
|
default:
|
|
|
|
_width = width;
|
|
|
|
_height = height;
|
|
|
|
_src_x = _cropLeft;
|
|
|
|
_src_y = _cropTop;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
Grabber::setVideoMode(mode);
|
|
|
|
updateScreenDimensions(true);
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
void X11Grabber::setPixelDecimation(int pixelDecimation)
|
|
|
|
{
|
|
|
|
if(_pixelDecimation != pixelDecimation)
|
|
|
|
{
|
|
|
|
_pixelDecimation = pixelDecimation;
|
|
|
|
updateScreenDimensions(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void X11Grabber::setCropping(unsigned cropLeft, unsigned cropRight, unsigned cropTop, unsigned cropBottom)
|
|
|
|
{
|
|
|
|
Grabber::setCropping(cropLeft, cropRight, cropTop, cropBottom);
|
|
|
|
if(_x11Display != nullptr) updateScreenDimensions(true); // segfault on init
|
|
|
|
}
|
2020-07-12 09:23:13 +02:00
|
|
|
|
|
|
|
bool X11Grabber::nativeEventFilter(const QByteArray & eventType, void * message, long int * /*result*/)
|
|
|
|
{
|
|
|
|
if (!_XRandRAvailable || eventType != "xcb_generic_event_t") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_generic_event_t *e = static_cast<xcb_generic_event_t*>(message);
|
|
|
|
const uint8_t xEventType = XCB_EVENT_RESPONSE_TYPE(e);
|
|
|
|
|
|
|
|
if (xEventType == _XRandREventBase + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
|
|
|
|
{
|
|
|
|
updateScreenDimensions(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|