1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | |
41 | #include <QtCore/qbytearray.h> |
42 | #include <QtGui/qopenglcontext.h> |
43 | |
44 | #ifdef Q_OS_LINUX |
45 | #include <sys/ioctl.h> |
46 | #include <linux/fb.h> |
47 | #endif |
48 | #include <QtGui/private/qmath_p.h> |
49 | |
50 | #include "qeglconvenience_p.h" |
51 | |
52 | #ifndef EGL_OPENGL_ES3_BIT_KHR |
53 | #define EGL_OPENGL_ES3_BIT_KHR 0x0040 |
54 | #endif |
55 | |
56 | QT_BEGIN_NAMESPACE |
57 | |
58 | QList<EGLint> q_createConfigAttributesFromFormat(const QSurfaceFormat &format) |
59 | { |
60 | int redSize = format.redBufferSize(); |
61 | int greenSize = format.greenBufferSize(); |
62 | int blueSize = format.blueBufferSize(); |
63 | int alphaSize = format.alphaBufferSize(); |
64 | int depthSize = format.depthBufferSize(); |
65 | int stencilSize = format.stencilBufferSize(); |
66 | int sampleCount = format.samples(); |
67 | |
68 | QList<EGLint> configAttributes; |
69 | |
70 | // Map default, unspecified values (-1) to 0. This is important due to sorting rule #3 |
71 | // in section 3.4.1 of the spec and allows picking a potentially faster 16-bit config |
72 | // over 32-bit ones when there is no explicit request for the color channel sizes: |
73 | // |
74 | // The red/green/blue sizes have a sort priority of 3, so they are sorted by |
75 | // first. (unless a caveat like SLOW or NON_CONFORMANT is present) The sort order is |
76 | // Special and described as "by larger _total_ number of color bits.". So EGL will put |
77 | // 32-bit configs in the list before the 16-bit configs. However, the spec also goes |
78 | // on to say "If the requested number of bits in attrib_list for a particular |
79 | // component is 0, then the number of bits for that component is not considered". This |
80 | // part of the spec also seems to imply that setting the red/green/blue bits to zero |
81 | // means none of the components are considered and EGL disregards the entire sorting |
82 | // rule. It then looks to the next highest priority rule, which is |
83 | // EGL_BUFFER_SIZE. Despite the selection criteria being "AtLeast" for |
84 | // EGL_BUFFER_SIZE, it's sort order is "smaller" meaning 16-bit configs are put in the |
85 | // list before 32-bit configs. |
86 | // |
87 | // This also means that explicitly specifying a size like 565 will still result in |
88 | // having larger (888) configs first in the returned list. We need to handle this |
89 | // ourselves later by manually filtering the list, instead of just blindly taking the |
90 | // first config from it. |
91 | |
92 | configAttributes.append(EGL_RED_SIZE); |
93 | configAttributes.append(redSize > 0 ? redSize : 0); |
94 | |
95 | configAttributes.append(EGL_GREEN_SIZE); |
96 | configAttributes.append(greenSize > 0 ? greenSize : 0); |
97 | |
98 | configAttributes.append(EGL_BLUE_SIZE); |
99 | configAttributes.append(blueSize > 0 ? blueSize : 0); |
100 | |
101 | configAttributes.append(EGL_ALPHA_SIZE); |
102 | configAttributes.append(alphaSize > 0 ? alphaSize : 0); |
103 | |
104 | configAttributes.append(EGL_SAMPLES); |
105 | configAttributes.append(sampleCount > 0 ? sampleCount : 0); |
106 | |
107 | configAttributes.append(EGL_SAMPLE_BUFFERS); |
108 | configAttributes.append(sampleCount > 0); |
109 | |
110 | if (format.renderableType() != QSurfaceFormat::OpenVG) { |
111 | configAttributes.append(EGL_DEPTH_SIZE); |
112 | configAttributes.append(depthSize > 0 ? depthSize : 0); |
113 | |
114 | configAttributes.append(EGL_STENCIL_SIZE); |
115 | configAttributes.append(stencilSize > 0 ? stencilSize : 0); |
116 | } else { |
117 | // OpenVG needs alpha mask for clipping |
118 | configAttributes.append(EGL_ALPHA_MASK_SIZE); |
119 | configAttributes.append(8); |
120 | } |
121 | |
122 | return configAttributes; |
123 | } |
124 | |
125 | bool q_reduceConfigAttributes(QList<EGLint> *configAttributes) |
126 | { |
127 | int i = -1; |
128 | // Reduce the complexity of a configuration request to ask for less |
129 | // because the previous request did not result in success. Returns |
130 | // true if the complexity was reduced, or false if no further |
131 | // reductions in complexity are possible. |
132 | |
133 | i = configAttributes->indexOf(EGL_SWAP_BEHAVIOR); |
134 | if (i >= 0) { |
135 | configAttributes->remove(i,2); |
136 | } |
137 | |
138 | #ifdef EGL_VG_ALPHA_FORMAT_PRE_BIT |
139 | // For OpenVG, we sometimes try to create a surface using a pre-multiplied format. If we can't |
140 | // find a config which supports pre-multiplied formats, remove the flag on the surface type: |
141 | |
142 | i = configAttributes->indexOf(EGL_SURFACE_TYPE); |
143 | if (i >= 0) { |
144 | EGLint surfaceType = configAttributes->at(i +1); |
145 | if (surfaceType & EGL_VG_ALPHA_FORMAT_PRE_BIT) { |
146 | surfaceType ^= EGL_VG_ALPHA_FORMAT_PRE_BIT; |
147 | configAttributes->replace(i+1,surfaceType); |
148 | return true; |
149 | } |
150 | } |
151 | #endif |
152 | |
153 | // EGL chooses configs with the highest color depth over |
154 | // those with smaller (but faster) lower color depths. One |
155 | // way around this is to set EGL_BUFFER_SIZE to 16, which |
156 | // trumps the others. Of course, there may not be a 16-bit |
157 | // config available, so it's the first restraint we remove. |
158 | i = configAttributes->indexOf(EGL_BUFFER_SIZE); |
159 | if (i >= 0) { |
160 | if (configAttributes->at(i+1) == 16) { |
161 | configAttributes->remove(i,2); |
162 | return true; |
163 | } |
164 | } |
165 | |
166 | i = configAttributes->indexOf(EGL_SAMPLES); |
167 | if (i >= 0) { |
168 | EGLint value = configAttributes->value(i+1, 0); |
169 | if (value > 1) |
170 | configAttributes->replace(i+1, qMin(EGLint(16), value / 2)); |
171 | else |
172 | configAttributes->remove(i, 2); |
173 | return true; |
174 | } |
175 | |
176 | i = configAttributes->indexOf(EGL_SAMPLE_BUFFERS); |
177 | if (i >= 0) { |
178 | configAttributes->remove(i,2); |
179 | return true; |
180 | } |
181 | |
182 | i = configAttributes->indexOf(EGL_DEPTH_SIZE); |
183 | if (i >= 0) { |
184 | if (configAttributes->at(i + 1) >= 32) |
185 | configAttributes->replace(i + 1, 24); |
186 | else if (configAttributes->at(i + 1) > 1) |
187 | configAttributes->replace(i + 1, 1); |
188 | else |
189 | configAttributes->remove(i, 2); |
190 | return true; |
191 | } |
192 | |
193 | i = configAttributes->indexOf(EGL_ALPHA_SIZE); |
194 | if (i >= 0) { |
195 | configAttributes->remove(i,2); |
196 | #if defined(EGL_BIND_TO_TEXTURE_RGBA) && defined(EGL_BIND_TO_TEXTURE_RGB) |
197 | i = configAttributes->indexOf(EGL_BIND_TO_TEXTURE_RGBA); |
198 | if (i >= 0) { |
199 | configAttributes->replace(i,EGL_BIND_TO_TEXTURE_RGB); |
200 | configAttributes->replace(i+1,true); |
201 | |
202 | } |
203 | #endif |
204 | return true; |
205 | } |
206 | |
207 | i = configAttributes->indexOf(EGL_STENCIL_SIZE); |
208 | if (i >= 0) { |
209 | if (configAttributes->at(i + 1) > 1) |
210 | configAttributes->replace(i + 1, 1); |
211 | else |
212 | configAttributes->remove(i, 2); |
213 | return true; |
214 | } |
215 | |
216 | #ifdef EGL_BIND_TO_TEXTURE_RGB |
217 | i = configAttributes->indexOf(EGL_BIND_TO_TEXTURE_RGB); |
218 | if (i >= 0) { |
219 | configAttributes->remove(i,2); |
220 | return true; |
221 | } |
222 | #endif |
223 | |
224 | return false; |
225 | } |
226 | |
227 | QEglConfigChooser::QEglConfigChooser(EGLDisplay display) |
228 | : m_display(display) |
229 | , m_surfaceType(EGL_WINDOW_BIT) |
230 | , m_ignore(false) |
231 | , m_confAttrRed(0) |
232 | , m_confAttrGreen(0) |
233 | , m_confAttrBlue(0) |
234 | , m_confAttrAlpha(0) |
235 | { |
236 | } |
237 | |
238 | QEglConfigChooser::~QEglConfigChooser() |
239 | { |
240 | } |
241 | |
242 | EGLConfig QEglConfigChooser::chooseConfig() |
243 | { |
244 | QList<EGLint> configureAttributes = q_createConfigAttributesFromFormat(m_format); |
245 | configureAttributes.append(EGL_SURFACE_TYPE); |
246 | configureAttributes.append(surfaceType()); |
247 | |
248 | configureAttributes.append(EGL_RENDERABLE_TYPE); |
249 | bool needsES2Plus = false; |
250 | switch (m_format.renderableType()) { |
251 | case QSurfaceFormat::OpenVG: |
252 | configureAttributes.append(EGL_OPENVG_BIT); |
253 | break; |
254 | #ifdef EGL_VERSION_1_4 |
255 | case QSurfaceFormat::DefaultRenderableType: |
256 | #ifndef QT_NO_OPENGL |
257 | if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) |
258 | configureAttributes.append(EGL_OPENGL_BIT); |
259 | else |
260 | #endif // QT_NO_OPENGL |
261 | needsES2Plus = true; |
262 | break; |
263 | case QSurfaceFormat::OpenGL: |
264 | configureAttributes.append(EGL_OPENGL_BIT); |
265 | break; |
266 | #endif |
267 | case QSurfaceFormat::OpenGLES: |
268 | if (m_format.majorVersion() == 1) { |
269 | configureAttributes.append(EGL_OPENGL_ES_BIT); |
270 | break; |
271 | } |
272 | Q_FALLTHROUGH(); |
273 | default: |
274 | needsES2Plus = true; |
275 | break; |
276 | } |
277 | if (needsES2Plus) { |
278 | if (m_format.majorVersion() >= 3 && q_hasEglExtension(display(), "EGL_KHR_create_context" )) |
279 | configureAttributes.append(EGL_OPENGL_ES3_BIT_KHR); |
280 | else |
281 | configureAttributes.append(EGL_OPENGL_ES2_BIT); |
282 | } |
283 | configureAttributes.append(EGL_NONE); |
284 | |
285 | EGLConfig cfg = nullptr; |
286 | do { |
287 | // Get the number of matching configurations for this set of properties. |
288 | EGLint matching = 0; |
289 | if (!eglChooseConfig(display(), configureAttributes.constData(), nullptr, 0, &matching) || !matching) |
290 | continue; |
291 | |
292 | // Fetch all of the matching configurations and find the |
293 | // first that matches the pixel format we wanted. |
294 | int i = configureAttributes.indexOf(EGL_RED_SIZE); |
295 | m_confAttrRed = configureAttributes.at(i+1); |
296 | i = configureAttributes.indexOf(EGL_GREEN_SIZE); |
297 | m_confAttrGreen = configureAttributes.at(i+1); |
298 | i = configureAttributes.indexOf(EGL_BLUE_SIZE); |
299 | m_confAttrBlue = configureAttributes.at(i+1); |
300 | i = configureAttributes.indexOf(EGL_ALPHA_SIZE); |
301 | m_confAttrAlpha = i == -1 ? 0 : configureAttributes.at(i+1); |
302 | |
303 | QList<EGLConfig> configs(matching); |
304 | eglChooseConfig(display(), configureAttributes.constData(), configs.data(), configs.size(), &matching); |
305 | if (!cfg && matching > 0) |
306 | cfg = configs.first(); |
307 | |
308 | // Filter the list. Due to the EGL sorting rules configs with higher depth are |
309 | // placed first when the minimum color channel sizes have been specified (i.e. the |
310 | // QSurfaceFormat contains color sizes > 0). To prevent returning a 888 config |
311 | // when the QSurfaceFormat explicitly asked for 565, go through the returned |
312 | // configs and look for one that exactly matches the requested sizes. When no |
313 | // sizes have been given, take the first, which will be a config with the smaller |
314 | // (e.g. 16-bit) depth. |
315 | for (int i = 0; i < configs.size(); ++i) { |
316 | if (filterConfig(configs[i])) |
317 | return configs.at(i); |
318 | } |
319 | } while (q_reduceConfigAttributes(&configureAttributes)); |
320 | |
321 | if (!cfg) |
322 | qWarning("Cannot find EGLConfig, returning null config" ); |
323 | return cfg; |
324 | } |
325 | |
326 | bool QEglConfigChooser::filterConfig(EGLConfig config) const |
327 | { |
328 | // If we are fine with the highest depth (e.g. RGB888 configs) even when something |
329 | // smaller (565) was explicitly requested, do nothing. |
330 | if (m_ignore) |
331 | return true; |
332 | |
333 | EGLint red = 0; |
334 | EGLint green = 0; |
335 | EGLint blue = 0; |
336 | EGLint alpha = 0; |
337 | |
338 | // Compare only if a size was given. Otherwise just accept. |
339 | if (m_confAttrRed) |
340 | eglGetConfigAttrib(display(), config, EGL_RED_SIZE, &red); |
341 | if (m_confAttrGreen) |
342 | eglGetConfigAttrib(display(), config, EGL_GREEN_SIZE, &green); |
343 | if (m_confAttrBlue) |
344 | eglGetConfigAttrib(display(), config, EGL_BLUE_SIZE, &blue); |
345 | if (m_confAttrAlpha) |
346 | eglGetConfigAttrib(display(), config, EGL_ALPHA_SIZE, &alpha); |
347 | |
348 | return red == m_confAttrRed && green == m_confAttrGreen |
349 | && blue == m_confAttrBlue && alpha == m_confAttrAlpha; |
350 | } |
351 | |
352 | EGLConfig q_configFromGLFormat(EGLDisplay display, const QSurfaceFormat &format, bool highestPixelFormat, int surfaceType) |
353 | { |
354 | QEglConfigChooser chooser(display); |
355 | chooser.setSurfaceFormat(format); |
356 | chooser.setSurfaceType(surfaceType); |
357 | chooser.setIgnoreColorChannels(highestPixelFormat); |
358 | |
359 | return chooser.chooseConfig(); |
360 | } |
361 | |
362 | QSurfaceFormat q_glFormatFromConfig(EGLDisplay display, const EGLConfig config, const QSurfaceFormat &referenceFormat) |
363 | { |
364 | QSurfaceFormat format; |
365 | EGLint redSize = 0; |
366 | EGLint greenSize = 0; |
367 | EGLint blueSize = 0; |
368 | EGLint alphaSize = 0; |
369 | EGLint depthSize = 0; |
370 | EGLint stencilSize = 0; |
371 | EGLint sampleCount = 0; |
372 | EGLint renderableType = 0; |
373 | |
374 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &redSize); |
375 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &greenSize); |
376 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &blueSize); |
377 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &alphaSize); |
378 | eglGetConfigAttrib(display, config, EGL_DEPTH_SIZE, &depthSize); |
379 | eglGetConfigAttrib(display, config, EGL_STENCIL_SIZE, &stencilSize); |
380 | eglGetConfigAttrib(display, config, EGL_SAMPLES, &sampleCount); |
381 | eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType); |
382 | |
383 | if (referenceFormat.renderableType() == QSurfaceFormat::OpenVG && (renderableType & EGL_OPENVG_BIT)) |
384 | format.setRenderableType(QSurfaceFormat::OpenVG); |
385 | #ifdef EGL_VERSION_1_4 |
386 | else if (referenceFormat.renderableType() == QSurfaceFormat::OpenGL |
387 | && (renderableType & EGL_OPENGL_BIT)) |
388 | format.setRenderableType(QSurfaceFormat::OpenGL); |
389 | else if (referenceFormat.renderableType() == QSurfaceFormat::DefaultRenderableType |
390 | #ifndef QT_NO_OPENGL |
391 | && QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL |
392 | #endif |
393 | && (renderableType & EGL_OPENGL_BIT)) |
394 | format.setRenderableType(QSurfaceFormat::OpenGL); |
395 | #endif |
396 | else |
397 | format.setRenderableType(QSurfaceFormat::OpenGLES); |
398 | |
399 | format.setRedBufferSize(redSize); |
400 | format.setGreenBufferSize(greenSize); |
401 | format.setBlueBufferSize(blueSize); |
402 | format.setAlphaBufferSize(alphaSize); |
403 | format.setDepthBufferSize(depthSize); |
404 | format.setStencilBufferSize(stencilSize); |
405 | format.setSamples(sampleCount); |
406 | format.setStereo(false); // EGL doesn't support stereo buffers |
407 | format.setSwapInterval(referenceFormat.swapInterval()); |
408 | |
409 | // Clear the EGL error state because some of the above may |
410 | // have errored out because the attribute is not applicable |
411 | // to the surface type. Such errors don't matter. |
412 | eglGetError(); |
413 | |
414 | return format; |
415 | } |
416 | |
417 | bool q_hasEglExtension(EGLDisplay display, const char* extensionName) |
418 | { |
419 | QList<QByteArray> extensions = |
420 | QByteArray(reinterpret_cast<const char *> |
421 | (eglQueryString(display, EGL_EXTENSIONS))).split(' '); |
422 | return extensions.contains(extensionName); |
423 | } |
424 | |
425 | struct AttrInfo { EGLint attr; const char *name; }; |
426 | static struct AttrInfo attrs[] = { |
427 | {EGL_BUFFER_SIZE, "EGL_BUFFER_SIZE" }, |
428 | {EGL_ALPHA_SIZE, "EGL_ALPHA_SIZE" }, |
429 | {EGL_BLUE_SIZE, "EGL_BLUE_SIZE" }, |
430 | {EGL_GREEN_SIZE, "EGL_GREEN_SIZE" }, |
431 | {EGL_RED_SIZE, "EGL_RED_SIZE" }, |
432 | {EGL_DEPTH_SIZE, "EGL_DEPTH_SIZE" }, |
433 | {EGL_STENCIL_SIZE, "EGL_STENCIL_SIZE" }, |
434 | {EGL_CONFIG_CAVEAT, "EGL_CONFIG_CAVEAT" }, |
435 | {EGL_CONFIG_ID, "EGL_CONFIG_ID" }, |
436 | {EGL_LEVEL, "EGL_LEVEL" }, |
437 | {EGL_MAX_PBUFFER_HEIGHT, "EGL_MAX_PBUFFER_HEIGHT" }, |
438 | {EGL_MAX_PBUFFER_PIXELS, "EGL_MAX_PBUFFER_PIXELS" }, |
439 | {EGL_MAX_PBUFFER_WIDTH, "EGL_MAX_PBUFFER_WIDTH" }, |
440 | {EGL_NATIVE_RENDERABLE, "EGL_NATIVE_RENDERABLE" }, |
441 | {EGL_NATIVE_VISUAL_ID, "EGL_NATIVE_VISUAL_ID" }, |
442 | {EGL_NATIVE_VISUAL_TYPE, "EGL_NATIVE_VISUAL_TYPE" }, |
443 | {EGL_SAMPLES, "EGL_SAMPLES" }, |
444 | {EGL_SAMPLE_BUFFERS, "EGL_SAMPLE_BUFFERS" }, |
445 | {EGL_SURFACE_TYPE, "EGL_SURFACE_TYPE" }, |
446 | {EGL_TRANSPARENT_TYPE, "EGL_TRANSPARENT_TYPE" }, |
447 | {EGL_TRANSPARENT_BLUE_VALUE, "EGL_TRANSPARENT_BLUE_VALUE" }, |
448 | {EGL_TRANSPARENT_GREEN_VALUE, "EGL_TRANSPARENT_GREEN_VALUE" }, |
449 | {EGL_TRANSPARENT_RED_VALUE, "EGL_TRANSPARENT_RED_VALUE" }, |
450 | {EGL_BIND_TO_TEXTURE_RGB, "EGL_BIND_TO_TEXTURE_RGB" }, |
451 | {EGL_BIND_TO_TEXTURE_RGBA, "EGL_BIND_TO_TEXTURE_RGBA" }, |
452 | {EGL_MIN_SWAP_INTERVAL, "EGL_MIN_SWAP_INTERVAL" }, |
453 | {EGL_MAX_SWAP_INTERVAL, "EGL_MAX_SWAP_INTERVAL" }, |
454 | {-1, nullptr}}; |
455 | |
456 | void q_printEglConfig(EGLDisplay display, EGLConfig config) |
457 | { |
458 | EGLint index; |
459 | for (index = 0; attrs[index].attr != -1; ++index) { |
460 | EGLint value; |
461 | if (eglGetConfigAttrib(display, config, attrs[index].attr, &value)) { |
462 | qDebug("\t%s: %d" , attrs[index].name, (int)value); |
463 | } |
464 | } |
465 | } |
466 | |
467 | #ifdef Q_OS_UNIX |
468 | |
469 | QSizeF q_physicalScreenSizeFromFb(int framebufferDevice, const QSize &screenSize) |
470 | { |
471 | #ifndef Q_OS_LINUX |
472 | Q_UNUSED(framebufferDevice); |
473 | #endif |
474 | const int defaultPhysicalDpi = 100; |
475 | static QSizeF size; |
476 | |
477 | if (size.isEmpty()) { |
478 | // Note: in millimeters |
479 | int width = qEnvironmentVariableIntValue("QT_QPA_EGLFS_PHYSICAL_WIDTH" ); |
480 | int height = qEnvironmentVariableIntValue("QT_QPA_EGLFS_PHYSICAL_HEIGHT" ); |
481 | |
482 | if (width && height) { |
483 | size.setWidth(width); |
484 | size.setHeight(height); |
485 | return size; |
486 | } |
487 | |
488 | int w = -1; |
489 | int h = -1; |
490 | QSize screenResolution; |
491 | #ifdef Q_OS_LINUX |
492 | struct fb_var_screeninfo vinfo; |
493 | |
494 | if (framebufferDevice != -1) { |
495 | if (ioctl(framebufferDevice, FBIOGET_VSCREENINFO, &vinfo) == -1) { |
496 | qWarning("eglconvenience: Could not query screen info" ); |
497 | } else { |
498 | w = vinfo.width; |
499 | h = vinfo.height; |
500 | screenResolution = QSize(vinfo.xres, vinfo.yres); |
501 | } |
502 | } else |
503 | #endif |
504 | { |
505 | // Use the provided screen size, when available, since some platforms may have their own |
506 | // specific way to query it. Otherwise try querying it from the framebuffer. |
507 | screenResolution = screenSize.isEmpty() ? q_screenSizeFromFb(framebufferDevice) : screenSize; |
508 | } |
509 | |
510 | size.setWidth(w <= 0 ? screenResolution.width() * Q_MM_PER_INCH / defaultPhysicalDpi : qreal(w)); |
511 | size.setHeight(h <= 0 ? screenResolution.height() * Q_MM_PER_INCH / defaultPhysicalDpi : qreal(h)); |
512 | |
513 | if (w <= 0 || h <= 0) |
514 | qWarning("Unable to query physical screen size, defaulting to %d dpi.\n" |
515 | "To override, set QT_QPA_EGLFS_PHYSICAL_WIDTH " |
516 | "and QT_QPA_EGLFS_PHYSICAL_HEIGHT (in millimeters)." , defaultPhysicalDpi); |
517 | } |
518 | |
519 | return size; |
520 | } |
521 | |
522 | QSize q_screenSizeFromFb(int framebufferDevice) |
523 | { |
524 | #ifndef Q_OS_LINUX |
525 | Q_UNUSED(framebufferDevice); |
526 | #endif |
527 | const int defaultWidth = 800; |
528 | const int defaultHeight = 600; |
529 | static QSize size; |
530 | |
531 | if (size.isEmpty()) { |
532 | int width = qEnvironmentVariableIntValue("QT_QPA_EGLFS_WIDTH" ); |
533 | int height = qEnvironmentVariableIntValue("QT_QPA_EGLFS_HEIGHT" ); |
534 | |
535 | if (width && height) { |
536 | size.setWidth(width); |
537 | size.setHeight(height); |
538 | return size; |
539 | } |
540 | |
541 | #ifdef Q_OS_LINUX |
542 | struct fb_var_screeninfo vinfo; |
543 | int xres = -1; |
544 | int yres = -1; |
545 | |
546 | if (framebufferDevice != -1) { |
547 | if (ioctl(framebufferDevice, FBIOGET_VSCREENINFO, &vinfo) == -1) { |
548 | qWarning("eglconvenience: Could not read screen info" ); |
549 | } else { |
550 | xres = vinfo.xres; |
551 | yres = vinfo.yres; |
552 | } |
553 | } |
554 | |
555 | size.setWidth(xres <= 0 ? defaultWidth : xres); |
556 | size.setHeight(yres <= 0 ? defaultHeight : yres); |
557 | #else |
558 | size.setWidth(defaultWidth); |
559 | size.setHeight(defaultHeight); |
560 | #endif |
561 | } |
562 | |
563 | return size; |
564 | } |
565 | |
566 | int q_screenDepthFromFb(int framebufferDevice) |
567 | { |
568 | #ifndef Q_OS_LINUX |
569 | Q_UNUSED(framebufferDevice); |
570 | #endif |
571 | const int defaultDepth = 32; |
572 | static int depth = qEnvironmentVariableIntValue("QT_QPA_EGLFS_DEPTH" ); |
573 | |
574 | if (depth == 0) { |
575 | #ifdef Q_OS_LINUX |
576 | struct fb_var_screeninfo vinfo; |
577 | |
578 | if (framebufferDevice != -1) { |
579 | if (ioctl(framebufferDevice, FBIOGET_VSCREENINFO, &vinfo) == -1) |
580 | qWarning("eglconvenience: Could not query screen info" ); |
581 | else |
582 | depth = vinfo.bits_per_pixel; |
583 | } |
584 | |
585 | if (depth <= 0) |
586 | depth = defaultDepth; |
587 | #else |
588 | depth = defaultDepth; |
589 | #endif |
590 | } |
591 | |
592 | return depth; |
593 | } |
594 | |
595 | qreal q_refreshRateFromFb(int framebufferDevice) |
596 | { |
597 | #ifndef Q_OS_LINUX |
598 | Q_UNUSED(framebufferDevice); |
599 | #endif |
600 | |
601 | static qreal rate = 0; |
602 | |
603 | #ifdef Q_OS_LINUX |
604 | if (rate == 0) { |
605 | if (framebufferDevice != -1) { |
606 | struct fb_var_screeninfo vinfo; |
607 | if (ioctl(framebufferDevice, FBIOGET_VSCREENINFO, &vinfo) != -1) { |
608 | const quint64 quot = quint64(vinfo.left_margin + vinfo.right_margin + vinfo.xres + vinfo.hsync_len) |
609 | * quint64(vinfo.upper_margin + vinfo.lower_margin + vinfo.yres + vinfo.vsync_len) |
610 | * vinfo.pixclock; |
611 | if (quot) |
612 | rate = 1000000000000LLU / quot; |
613 | } else { |
614 | qWarning("eglconvenience: Could not query screen info" ); |
615 | } |
616 | } |
617 | } |
618 | #endif |
619 | |
620 | if (rate == 0) |
621 | rate = 60; |
622 | |
623 | return rate; |
624 | } |
625 | |
626 | #endif // Q_OS_UNIX |
627 | |
628 | QT_END_NAMESPACE |
629 | |