From f92053f38142c9b4a589f5623e3055958d145a01 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 9 Nov 2022 23:24:37 +0200 Subject: [PATCH] QPixmap::grabWindow() optimization TODO: replace WId with Qt::HANDLE or vice-versa, eventually Signed-off-by: Ivailo Monev --- src/gui/image/qpixmap.cpp | 66 +++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index aa5c5fcd3..d891986aa 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -1401,41 +1401,31 @@ QPixmap QPixmap::grabWindow(WId window, int x, int y, int w, int h) if (w == 0 || h == 0) return QPixmap(); - XWindowAttributes window_attr; - if (!XGetWindowAttributes(qt_x11Data->display, window, &window_attr)) - return QPixmap(); - - if (w < 0) - w = window_attr.width - x; - if (h < 0) - h = window_attr.height - y; - - return QPixmap::fromX11Pixmap(window).copy(x, y, w, h); -} - -#if defined(Q_WS_X11) -/*! - \since 4.5 - - Creates a QPixmap from the native X11 Pixmap handle \a pixmap. - - \warning This function is X11 specific; using it is non-portable. -*/ -QPixmap QPixmap::fromX11Pixmap(Qt::HANDLE pixmap) -{ - Window root; - int x; - int y; + Window unusedroot; + int unusedx; + int unusedy; uint width; uint height; - uint border_width; + uint unusedborderwidth; uint depth; - XGetGeometry(qt_x11Data->display, pixmap, &root, &x, &y, &width, &height, &border_width, &depth); + XGetGeometry( + qt_x11Data->display, + window, + &unusedroot, &unusedx, &unusedy, + &width, &height, + &unusedborderwidth, + &depth + ); + + if (w < 0) + w = width - x; + if (h < 0) + h = height - y; XImage *ximage = XGetImage( - qt_x11Data->display, pixmap, - 0, 0, // x and y - width, height, + qt_x11Data->display, window, + x, y, + w, h, AllPlanes, (depth == 1) ? XYPixmap : ZPixmap ); if (Q_UNLIKELY(!ximage)) { @@ -1451,7 +1441,7 @@ QPixmap QPixmap::fromX11Pixmap(Qt::HANDLE pixmap) } else if (depth == 32 && qt_x11Data->use_xrender) { format = QImage::Format_ARGB32_Premultiplied; } - QImage image(width, height, format); + QImage image(w, h, format); if (image.depth() == 1) { image.setColorTable(monoColorTable()); } @@ -1460,6 +1450,20 @@ QPixmap QPixmap::fromX11Pixmap(Qt::HANDLE pixmap) return QPixmap::fromImage(image); } +#if defined(Q_WS_X11) +/*! + \since 4.5 + + Creates a QPixmap from the native X11 Pixmap handle \a pixmap. + + \warning This function is X11 specific; using it is non-portable. +*/ +QPixmap QPixmap::fromX11Pixmap(Qt::HANDLE pixmap) +{ + // to X Pixmap and Window are just "Drawable" + return QPixmap::grabWindow(pixmap); +} + /*! Returns X11 Pixmap handle of the pixmap. -- 2.11.0