Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qxcbnativepainting.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtCore/qrandom.h>
6
10#include "qt_x11_p.h"
11
13
15
16void qt_xcb_native_x11_info_init(QXcbConnection *conn)
17{
19 X11->display = static_cast<Display *>(conn->xlib_display());
20 X11->defaultScreen = DefaultScreen(X11->display);
21 X11->screenCount = ScreenCount(X11->display);
22
24 X11->argbVisuals = new Visual *[X11->screenCount];
26
27 for (int s = 0; s < X11->screenCount; s++) {
28 QX11InfoData *screen = X11->screens + s;
29 //screen->ref = 1; // ensures it doesn't get deleted
30 screen->screen = s;
31
32 int widthMM = DisplayWidthMM(X11->display, s);
33 if (widthMM != 0) {
34 screen->dpiX = (DisplayWidth(X11->display, s) * 254 + widthMM * 5) / (widthMM * 10);
35 } else {
36 screen->dpiX = 72;
37 }
38
39 int heightMM = DisplayHeightMM(X11->display, s);
40 if (heightMM != 0) {
41 screen->dpiY = (DisplayHeight(X11->display, s) * 254 + heightMM * 5) / (heightMM * 10);
42 } else {
43 screen->dpiY = 72;
44 }
45
46 X11->argbVisuals[s] = 0;
47 X11->argbColormaps[s] = 0;
48 }
49
50 X11->use_xrender = conn->hasXRender() && !qEnvironmentVariableIsSet("QT_XCB_NATIVE_PAINTING_NO_XRENDER");
51
52#if QT_CONFIG(xrender)
53 memset(X11->solid_fills, 0, sizeof(X11->solid_fills));
54 for (int i = 0; i < X11->solid_fill_count; ++i)
55 X11->solid_fills[i].screen = -1;
56 memset(X11->pattern_fills, 0, sizeof(X11->pattern_fills));
57 for (int i = 0; i < X11->pattern_fill_count; ++i)
58 X11->pattern_fills[i].screen = -1;
59#endif
60
62
63#if QT_CONFIG(xrender)
64 if (X11->use_xrender) {
65 // XRender is supported, let's see if we have a PictFormat for the
66 // default visual
67 XRenderPictFormat *format =
68 XRenderFindVisualFormat(X11->display,
69 (Visual *) QXcbX11Info::appVisual(X11->defaultScreen));
70
71 if (!format) {
72 X11->use_xrender = false;
73 }
74 }
75#endif // QT_CONFIG(xrender)
76}
77
79{
80 const int numRects = r.rectCount();
81 const auto input = r.begin();
82 QList<XRectangle> output(numRects);
83 for (int i = 0; i < numRects; ++i) {
84 const QRect &in = input[i];
85 XRectangle &out = output[i];
86 out.x = qMax(SHRT_MIN, in.x());
87 out.y = qMax(SHRT_MIN, in.y());
88 out.width = qMin((int)USHRT_MAX, in.width());
89 out.height = qMin((int)USHRT_MAX, in.height());
90 }
91 return output;
92}
93
95{};
96
98 : d(nullptr)
99{}
100
103
105 : d(other.d)
106{}
107
109{
110 d = other.d;
111 return *this;
112}
113
115{
117 xd->screen = screen;
122 xd->visual = (Visual *)QXcbX11Info::appVisual(screen);
124
125 QXcbX11Info info;
126 info.d = xd;
127 return info;
128}
129
130void QXcbX11Info::setDepth(int depth)
131{
132 if (!d)
134
135 d->depth = depth;
136}
137
139{
140 return X11 ? X11->display : 0;
141}
142
143int QXcbX11Info::screen() const
144{
145 return d ? d->screen : QXcbX11Info::appScreen();
146}
147
148int QXcbX11Info::depth() const
149{
150 return d ? d->depth : QXcbX11Info::appDepth();
151}
152
153Colormap QXcbX11Info::colormap() const
154{
155 return d ? d->colormap : QXcbX11Info::appColormap();
156}
157
158void *QXcbX11Info::visual() const
159{
160 return d ? d->visual : QXcbX11Info::appVisual();
161}
162
163void QXcbX11Info::setVisual(void *visual)
164{
165 if (!d)
167
168 d->visual = (Visual *) visual;
169}
170
172{
173 return X11 ? X11->defaultScreen : 0;
174}
175
176int QXcbX11Info::appDepth(int screen)
177{
178 return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].depth : 32;
179}
180
181int QXcbX11Info::appCells(int screen)
182{
183 return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].cells : 0;
184}
185
186Colormap QXcbX11Info::appColormap(int screen)
187{
188 return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].colormap : 0;
189}
190
191void *QXcbX11Info::appVisual(int screen)
192{
193 return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].visual : 0;
194}
195
196Window QXcbX11Info::appRootWindow(int screen)
197{
198 return X11 ? RootWindow(X11->display, screen == -1 ? X11->defaultScreen : screen) : 0;
199}
200
202{
203 return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].defaultColormap : true;
204}
205
207{
208 return X11 ? X11->screens[screen == -1 ? X11->defaultScreen : screen].defaultVisual : true;
209}
210
211int QXcbX11Info::appDpiX(int screen)
212{
213 if (!X11)
214 return 75;
215 if (screen < 0)
216 screen = X11->defaultScreen;
217 if (screen > X11->screenCount)
218 return 0;
219 return X11->screens[screen].dpiX;
220}
221
222int QXcbX11Info::appDpiY(int screen)
223{
224 if (!X11)
225 return 75;
226 if (screen < 0)
227 screen = X11->defaultScreen;
228 if (screen > X11->screenCount)
229 return 0;
230 return X11->screens[screen].dpiY;
231}
232
233#if QT_CONFIG(xrender)
234Picture QXcbX11Data::getSolidFill(int screen, const QColor &c)
235{
236 if (!X11->use_xrender)
237 return XNone;
238
239 XRenderColor color = preMultiply(c);
240 for (int i = 0; i < X11->solid_fill_count; ++i) {
241 if (X11->solid_fills[i].screen == screen
242 && X11->solid_fills[i].color.alpha == color.alpha
243 && X11->solid_fills[i].color.red == color.red
244 && X11->solid_fills[i].color.green == color.green
245 && X11->solid_fills[i].color.blue == color.blue)
246 return X11->solid_fills[i].picture;
247 }
248 // none found, replace one
249 int i = QRandomGenerator::global()->generate() % 16;
250
251 if (X11->solid_fills[i].screen != screen && X11->solid_fills[i].picture) {
252 XRenderFreePicture (X11->display, X11->solid_fills[i].picture);
253 X11->solid_fills[i].picture = 0;
254 }
255
256 if (!X11->solid_fills[i].picture) {
257 Pixmap pixmap = XCreatePixmap (X11->display, RootWindow (X11->display, screen), 1, 1, 32);
258 XRenderPictureAttributes attrs;
259 attrs.repeat = True;
260 X11->solid_fills[i].picture = XRenderCreatePicture (X11->display, pixmap,
261 XRenderFindStandardFormat(X11->display, PictStandardARGB32),
262 CPRepeat, &attrs);
263 XFreePixmap (X11->display, pixmap);
264 }
265
266 X11->solid_fills[i].color = color;
267 X11->solid_fills[i].screen = screen;
268 XRenderFillRectangle (X11->display, PictOpSrc, X11->solid_fills[i].picture, &color, 0, 0, 1, 1);
269 return X11->solid_fills[i].picture;
270}
271
272XRenderColor QXcbX11Data::preMultiply(const QColor &c)
273{
274 XRenderColor color;
275 const uint A = c.alpha(),
276 R = c.red(),
277 G = c.green(),
278 B = c.blue();
279 color.alpha = (A | A << 8);
280 color.red = (R | R << 8) * color.alpha / 0x10000;
281 color.green = (G | G << 8) * color.alpha / 0x10000;
282 color.blue = (B | B << 8) * color.alpha / 0x10000;
283 return color;
284}
285#endif // QT_CONFIG(xrender)
286
287
288QT_END_NAMESPACE
static void initialize()
void setDepth(int depth)
static bool appDefaultVisual(int screen=-1)
static int appScreen()
static QXcbX11Info fromScreen(int screen)
static Colormap appColormap(int screen=-1)
static Window appRootWindow(int screen=-1)
static bool appDefaultColormap(int screen=-1)
static int appDepth(int screen=-1)
void setVisual(void *visual)
void * visual() const
static Display * display()
Colormap colormap() const
static int appDpiY(int screen=-1)
QXcbX11Info & operator=(const QXcbX11Info &other)
static int appCells(int screen=-1)
static void * appVisual(int screen=-1)
QXcbX11Info(const QXcbX11Info &other)
static int appDpiX(int screen=-1)
#define X11
QXcbX11Data * qt_x11Data
QList< XRectangle > qt_region_to_xrectangles(const QRegion &r)
void qt_xcb_native_x11_info_init(QXcbConnection *conn)
Visual * visual
Definition qt_x11_p.h:150
bool defaultColormap
Definition qt_x11_p.h:151
Colormap colormap
Definition qt_x11_p.h:149
bool defaultVisual
Definition qt_x11_p.h:152
Visual ** argbVisuals
Definition qt_x11_p.h:99
Display * display
Definition qt_x11_p.h:91
int screenCount
Definition qt_x11_p.h:101
bool use_xrender
Definition qt_x11_p.h:94
Colormap * argbColormaps
Definition qt_x11_p.h:100
int defaultScreen
Definition qt_x11_p.h:102
QX11InfoData * screens
Definition qt_x11_p.h:98