mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Update X11 Grabber (scaling improvements) (#191)
* Add transform Matrix for scaling * Add Image scaling by XRender * Coding Style correction
This commit is contained in:
parent
c8318ff0c7
commit
6acf68c87e
@ -73,6 +73,10 @@ private:
|
|||||||
Picture _srcPicture;
|
Picture _srcPicture;
|
||||||
Picture _dstPicture;
|
Picture _dstPicture;
|
||||||
|
|
||||||
|
XTransform _transform;
|
||||||
|
int _horizontalDecimation;
|
||||||
|
int _verticalDecimation;
|
||||||
|
|
||||||
unsigned _screenWidth;
|
unsigned _screenWidth;
|
||||||
unsigned _screenHeight;
|
unsigned _screenHeight;
|
||||||
unsigned _croppedWidth;
|
unsigned _croppedWidth;
|
||||||
|
@ -6,29 +6,29 @@
|
|||||||
// X11Grabber includes
|
// X11Grabber includes
|
||||||
#include <grabber/X11Grabber.h>
|
#include <grabber/X11Grabber.h>
|
||||||
|
|
||||||
X11Grabber::X11Grabber(bool useXGetImage, int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation) :
|
X11Grabber::X11Grabber(bool useXGetImage, int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation)
|
||||||
_imageResampler(),
|
: _imageResampler()
|
||||||
_useXGetImage(useXGetImage),
|
, _useXGetImage(useXGetImage)
|
||||||
_cropLeft(cropLeft),
|
, _cropLeft(cropLeft)
|
||||||
_cropRight(cropRight),
|
, _cropRight(cropRight)
|
||||||
_cropTop(cropTop),
|
, _cropTop(cropTop)
|
||||||
_cropBottom(cropBottom),
|
, _cropBottom(cropBottom)
|
||||||
_x11Display(nullptr),
|
, _x11Display(nullptr)
|
||||||
_pixmap(None),
|
, _pixmap(None)
|
||||||
_srcFormat(nullptr),
|
, _srcFormat(nullptr)
|
||||||
_dstFormat(nullptr),
|
, _dstFormat(nullptr)
|
||||||
_srcPicture(None),
|
, _srcPicture(None)
|
||||||
_dstPicture(None),
|
, _dstPicture(None)
|
||||||
_screenWidth(0),
|
, _horizontalDecimation(horizontalPixelDecimation)
|
||||||
_screenHeight(0),
|
, _verticalDecimation(verticalPixelDecimation)
|
||||||
_croppedWidth(0),
|
, _screenWidth(0)
|
||||||
_croppedHeight(0),
|
, _screenHeight(0)
|
||||||
_image(0,0),
|
, _croppedWidth(0)
|
||||||
_log(Logger::getInstance("X11GRABBER"))
|
, _croppedHeight(0)
|
||||||
|
, _image(0,0)
|
||||||
|
, _log(Logger::getInstance("X11GRABBER"))
|
||||||
{
|
{
|
||||||
_imageResampler.setHorizontalPixelDecimation(horizontalPixelDecimation);
|
_imageResampler.setCropping(0, 0, 0, 0); // cropping is performed by XRender, XShmGetImage or XGetImage
|
||||||
_imageResampler.setVerticalPixelDecimation(verticalPixelDecimation);
|
|
||||||
_imageResampler.setCropping(0, 0, 0, 0); // cropping is performed by XShmGetImage or XGetImage
|
|
||||||
memset(&_pictAttr, 0, sizeof(_pictAttr));
|
memset(&_pictAttr, 0, sizeof(_pictAttr));
|
||||||
_pictAttr.repeat = RepeatNone;
|
_pictAttr.repeat = RepeatNone;
|
||||||
}
|
}
|
||||||
@ -51,12 +51,14 @@ void X11Grabber::freeResources()
|
|||||||
{
|
{
|
||||||
// Cleanup allocated resources of the X11 grab
|
// Cleanup allocated resources of the X11 grab
|
||||||
XDestroyImage(_xImage);
|
XDestroyImage(_xImage);
|
||||||
if(_XShmAvailable && !_useXGetImage) {
|
if(_XShmAvailable && !_useXGetImage)
|
||||||
|
{
|
||||||
XShmDetach(_x11Display, &_shminfo);
|
XShmDetach(_x11Display, &_shminfo);
|
||||||
shmdt(_shminfo.shmaddr);
|
shmdt(_shminfo.shmaddr);
|
||||||
shmctl(_shminfo.shmid, IPC_RMID, 0);
|
shmctl(_shminfo.shmid, IPC_RMID, 0);
|
||||||
}
|
}
|
||||||
if (_XRenderAvailable && !_useXGetImage) {
|
if (_XRenderAvailable && !_useXGetImage)
|
||||||
|
{
|
||||||
XRenderFreePicture(_x11Display, _srcPicture);
|
XRenderFreePicture(_x11Display, _srcPicture);
|
||||||
XRenderFreePicture(_x11Display, _dstPicture);
|
XRenderFreePicture(_x11Display, _dstPicture);
|
||||||
XFreePixmap(_x11Display, _pixmap);
|
XFreePixmap(_x11Display, _pixmap);
|
||||||
@ -65,29 +67,31 @@ void X11Grabber::freeResources()
|
|||||||
|
|
||||||
void X11Grabber::setupResources()
|
void X11Grabber::setupResources()
|
||||||
{
|
{
|
||||||
if(_XShmAvailable && !_useXGetImage) {
|
if(_XShmAvailable && !_useXGetImage)
|
||||||
|
{
|
||||||
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual,
|
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual,
|
||||||
_windowAttr.depth, ZPixmap, NULL, &_shminfo,
|
_windowAttr.depth, ZPixmap, NULL, &_shminfo,
|
||||||
_croppedWidth, _croppedHeight);
|
_croppedWidth, _croppedHeight);
|
||||||
|
|
||||||
_shminfo.shmid = shmget(IPC_PRIVATE, _xImage->bytes_per_line * _xImage->height, IPC_CREAT|0777);
|
_shminfo.shmid = shmget(IPC_PRIVATE, _xImage->bytes_per_line * _xImage->height, IPC_CREAT|0777);
|
||||||
_xImage->data = (char*)shmat(_shminfo.shmid,0,0);
|
_xImage->data = (char*)shmat(_shminfo.shmid,0,0);
|
||||||
_shminfo.shmaddr = _xImage->data;
|
_shminfo.shmaddr = _xImage->data;
|
||||||
_shminfo.readOnly = False;
|
_shminfo.readOnly = False;
|
||||||
|
|
||||||
XShmAttach(_x11Display, &_shminfo);
|
XShmAttach(_x11Display, &_shminfo);
|
||||||
}
|
}
|
||||||
if (_XRenderAvailable && !_useXGetImage) {
|
if (_XRenderAvailable && !_useXGetImage)
|
||||||
if(_XShmPixmapAvailable) {
|
{
|
||||||
|
if(_XShmPixmapAvailable)
|
||||||
|
{
|
||||||
_pixmap = XShmCreatePixmap(_x11Display, _window, _xImage->data, &_shminfo, _croppedWidth, _croppedHeight, _windowAttr.depth);
|
_pixmap = XShmCreatePixmap(_x11Display, _window, _xImage->data, &_shminfo, _croppedWidth, _croppedHeight, _windowAttr.depth);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_pixmap = XCreatePixmap(_x11Display, _window, _croppedWidth, _croppedHeight, _windowAttr.depth);
|
_pixmap = XCreatePixmap(_x11Display, _window, _croppedWidth, _croppedHeight, _windowAttr.depth);
|
||||||
}
|
}
|
||||||
_srcFormat = XRenderFindVisualFormat(_x11Display, _windowAttr.visual);
|
_srcFormat = XRenderFindVisualFormat(_x11Display, _windowAttr.visual);
|
||||||
_dstFormat = XRenderFindVisualFormat(_x11Display, _windowAttr.visual);
|
_dstFormat = XRenderFindVisualFormat(_x11Display, _windowAttr.visual);
|
||||||
_srcPicture = XRenderCreatePicture(_x11Display, _window, _srcFormat, CPRepeat, &_pictAttr);
|
_srcPicture = XRenderCreatePicture(_x11Display, _window, _srcFormat, CPRepeat, &_pictAttr);
|
||||||
_dstPicture = XRenderCreatePicture(_x11Display, _pixmap, _dstFormat, CPRepeat, &_pictAttr);
|
_dstPicture = XRenderCreatePicture(_x11Display, _pixmap, _dstFormat, CPRepeat, &_pictAttr);
|
||||||
XRenderSetPictureFilter(_x11Display, _srcPicture, "bilinear", NULL, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,9 +101,12 @@ bool X11Grabber::Setup()
|
|||||||
if (_x11Display == nullptr)
|
if (_x11Display == nullptr)
|
||||||
{
|
{
|
||||||
Error(_log, "Unable to open display");
|
Error(_log, "Unable to open display");
|
||||||
if (getenv("DISPLAY")) {
|
if (getenv("DISPLAY"))
|
||||||
|
{
|
||||||
Error(_log, "%s",getenv("DISPLAY"));
|
Error(_log, "%s",getenv("DISPLAY"));
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Error(_log, "DISPLAY environment variable not set");
|
Error(_log, "DISPLAY environment variable not set");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -114,6 +121,10 @@ bool X11Grabber::Setup()
|
|||||||
XShmQueryVersion(_x11Display, &dummy, &dummy, &pixmaps_supported);
|
XShmQueryVersion(_x11Display, &dummy, &dummy, &pixmaps_supported);
|
||||||
_XShmPixmapAvailable = pixmaps_supported && XShmPixmapFormat(_x11Display) == ZPixmap;
|
_XShmPixmapAvailable = pixmaps_supported && XShmPixmapFormat(_x11Display) == ZPixmap;
|
||||||
|
|
||||||
|
// Image scaling is performed by XRender when available, otherwise by ImageResampler
|
||||||
|
_imageResampler.setHorizontalPixelDecimation(_XRenderAvailable ? 1 : _horizontalDecimation);
|
||||||
|
_imageResampler.setVerticalPixelDecimation(_XRenderAvailable ? 1 : _verticalDecimation);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,14 +132,43 @@ Image<ColorRgb> & X11Grabber::grab()
|
|||||||
{
|
{
|
||||||
updateScreenDimensions();
|
updateScreenDimensions();
|
||||||
|
|
||||||
if (_XRenderAvailable && !_useXGetImage) {
|
if (_XRenderAvailable && !_useXGetImage)
|
||||||
XRenderComposite( _x11Display, // *dpy,
|
{
|
||||||
PictOpSrc, // op,
|
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);
|
||||||
|
double scale = std::min(scale_y, scale_x);
|
||||||
|
|
||||||
|
_transform =
|
||||||
|
{
|
||||||
|
{
|
||||||
|
{
|
||||||
|
XDoubleToFixed(1),
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(0)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(1),
|
||||||
|
XDoubleToFixed(0)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(scale)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
XRenderSetPictureTransform (_x11Display, _srcPicture, &_transform);
|
||||||
|
XRenderSetPictureFilter(_x11Display, _srcPicture, FilterBilinear, NULL, 0);
|
||||||
|
|
||||||
|
XRenderComposite( _x11Display, // dpy
|
||||||
|
PictOpSrc, // op
|
||||||
_srcPicture, // src
|
_srcPicture, // src
|
||||||
None, // mask
|
None, // mask
|
||||||
_dstPicture, // dst
|
_dstPicture, // dst
|
||||||
_cropLeft, // src_x
|
_cropLeft / _horizontalDecimation, // src_x _cropLeft
|
||||||
_cropTop, // src_y
|
_cropTop / _verticalDecimation, // src_y _cropTop
|
||||||
0, // mask_x
|
0, // mask_x
|
||||||
0, // mask_y
|
0, // mask_y
|
||||||
0, // dst_x
|
0, // dst_x
|
||||||
@ -138,15 +178,22 @@ Image<ColorRgb> & X11Grabber::grab()
|
|||||||
|
|
||||||
XSync(_x11Display, False);
|
XSync(_x11Display, False);
|
||||||
|
|
||||||
if (_XShmAvailable) {
|
if (!_XShmAvailable)
|
||||||
|
{
|
||||||
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
|
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (_XShmAvailable && !_useXGetImage) {
|
if (_XShmAvailable && !_useXGetImage) {
|
||||||
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
|
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,14 +211,43 @@ Image<ColorRgb> & X11Grabber::grab()
|
|||||||
|
|
||||||
int X11Grabber::grabFrame(Image<ColorRgb> & image)
|
int X11Grabber::grabFrame(Image<ColorRgb> & image)
|
||||||
{
|
{
|
||||||
if (_XRenderAvailable && !_useXGetImage) {
|
if (_XRenderAvailable && !_useXGetImage)
|
||||||
XRenderComposite( _x11Display, // *dpy,
|
{
|
||||||
PictOpSrc, // op,
|
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);
|
||||||
|
double scale = std::min(scale_y, scale_x);
|
||||||
|
|
||||||
|
_transform =
|
||||||
|
{
|
||||||
|
{
|
||||||
|
{
|
||||||
|
XDoubleToFixed(1),
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(0)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(1),
|
||||||
|
XDoubleToFixed(0)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(0),
|
||||||
|
XDoubleToFixed(scale)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
XRenderSetPictureTransform (_x11Display, _srcPicture, &_transform);
|
||||||
|
XRenderSetPictureFilter(_x11Display, _srcPicture, FilterBilinear, NULL, 0);
|
||||||
|
|
||||||
|
XRenderComposite( _x11Display, // dpy
|
||||||
|
PictOpSrc, // op
|
||||||
_srcPicture, // src
|
_srcPicture, // src
|
||||||
None, // mask
|
None, // mask
|
||||||
_dstPicture, // dst
|
_dstPicture, // dst
|
||||||
_cropLeft, // src_x
|
_cropLeft / _horizontalDecimation, // src_x _cropLeft
|
||||||
_cropTop, // src_y
|
_cropTop / _verticalDecimation, // src_y _cropTop
|
||||||
0, // mask_x
|
0, // mask_x
|
||||||
0, // mask_y
|
0, // mask_y
|
||||||
0, // dst_x
|
0, // dst_x
|
||||||
@ -181,15 +257,22 @@ int X11Grabber::grabFrame(Image<ColorRgb> & image)
|
|||||||
|
|
||||||
XSync(_x11Display, False);
|
XSync(_x11Display, False);
|
||||||
|
|
||||||
if (_XShmAvailable) {
|
if (!_XShmAvailable)
|
||||||
|
{
|
||||||
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
|
XShmGetImage(_x11Display, _pixmap, _xImage, 0, 0, AllPlanes);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
_xImage = XGetImage(_x11Display, _pixmap, 0, 0, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (_XShmAvailable && !_useXGetImage) {
|
if (_XShmAvailable && !_useXGetImage) {
|
||||||
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
|
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, AllPlanes);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
_xImage = XGetImage(_x11Display, _window, _cropLeft, _cropTop, _croppedWidth, _croppedHeight, AllPlanes, ZPixmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -220,7 +303,8 @@ int X11Grabber::updateScreenDimensions()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_screenWidth || _screenHeight) {
|
if (_screenWidth || _screenHeight)
|
||||||
|
{
|
||||||
freeResources();
|
freeResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,6 +312,21 @@ int X11Grabber::updateScreenDimensions()
|
|||||||
_screenWidth = _windowAttr.width;
|
_screenWidth = _windowAttr.width;
|
||||||
_screenHeight = _windowAttr.height;
|
_screenHeight = _windowAttr.height;
|
||||||
|
|
||||||
|
// Image scaling is performed by XRender when available, otherwise by ImageResampler
|
||||||
|
if (_XRenderAvailable && !_useXGetImage)
|
||||||
|
{
|
||||||
|
_croppedWidth = (_screenWidth > unsigned(_cropLeft + _cropRight))
|
||||||
|
? ((_screenWidth - _cropLeft - _cropRight) / _horizontalDecimation)
|
||||||
|
: _screenWidth / _horizontalDecimation;
|
||||||
|
|
||||||
|
_croppedHeight = (_screenHeight > unsigned(_cropTop + _cropBottom))
|
||||||
|
? ((_screenHeight - _cropTop - _cropBottom) / _verticalDecimation)
|
||||||
|
: _screenHeight / _verticalDecimation;
|
||||||
|
|
||||||
|
Info(_log, "Using XRender for grabbing");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
_croppedWidth = (_screenWidth > unsigned(_cropLeft + _cropRight))
|
_croppedWidth = (_screenWidth > unsigned(_cropLeft + _cropRight))
|
||||||
? (_screenWidth - _cropLeft - _cropRight)
|
? (_screenWidth - _cropLeft - _cropRight)
|
||||||
: _screenWidth;
|
: _screenWidth;
|
||||||
@ -236,9 +335,6 @@ int X11Grabber::updateScreenDimensions()
|
|||||||
? (_screenHeight - _cropTop - _cropBottom)
|
? (_screenHeight - _cropTop - _cropBottom)
|
||||||
: _screenHeight;
|
: _screenHeight;
|
||||||
|
|
||||||
if (_XRenderAvailable && !_useXGetImage) {
|
|
||||||
Info(_log, "Using XRender for grabbing");
|
|
||||||
} else {
|
|
||||||
Info(_log, "Using XGetImage for grabbing");
|
Info(_log, "Using XGetImage for grabbing");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user