Changeset View
Changeset View
Standalone View
Standalone View
plugins/qpa/eglhelpers.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2015 Martin Flöser <mgraesslin@kde.org> | ||||
6 | Copyright (C) 2019 Vlad Zagorodniy <vladzzag@gmail.com> | ||||
7 | | ||||
8 | This program is free software; you can redistribute it and/or modify | ||||
9 | it under the terms of the GNU General Public License as published by | ||||
10 | the Free Software Foundation; either version 2 of the License, or | ||||
11 | (at your option) any later version. | ||||
12 | | ||||
13 | This program is distributed in the hope that it will be useful, | ||||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | GNU General Public License for more details. | ||||
17 | | ||||
18 | You should have received a copy of the GNU General Public License | ||||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
20 | *********************************************************************/ | ||||
21 | | ||||
22 | #include "eglhelpers.h" | ||||
23 | | ||||
24 | #include <logging.h> | ||||
25 | | ||||
26 | #include <QOpenGLContext> | ||||
27 | | ||||
28 | namespace KWin | ||||
29 | { | ||||
30 | namespace QPA | ||||
31 | { | ||||
32 | | ||||
33 | bool isOpenGLES() | ||||
34 | { | ||||
35 | if (qstrcmp(qgetenv("KWIN_COMPOSE"), "O2ES") == 0) { | ||||
36 | return true; | ||||
37 | } | ||||
38 | | ||||
39 | return QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES; | ||||
40 | } | ||||
41 | | ||||
42 | EGLConfig configFromFormat(EGLDisplay display, const QSurfaceFormat &surfaceFormat, EGLint surfaceType) | ||||
43 | { | ||||
44 | // qMax as these values are initialized to -1 by default. | ||||
45 | const EGLint redSize = qMax(surfaceFormat.redBufferSize(), 0); | ||||
46 | const EGLint greenSize = qMax(surfaceFormat.greenBufferSize(), 0); | ||||
47 | const EGLint blueSize = qMax(surfaceFormat.blueBufferSize(), 0); | ||||
48 | const EGLint alphaSize = qMax(surfaceFormat.alphaBufferSize(), 0); | ||||
49 | const EGLint depthSize = qMax(surfaceFormat.depthBufferSize(), 0); | ||||
50 | const EGLint stencilSize = qMax(surfaceFormat.stencilBufferSize(), 0); | ||||
51 | | ||||
52 | const EGLint renderableType = isOpenGLES() ? EGL_OPENGL_ES2_BIT : EGL_OPENGL_BIT; | ||||
53 | | ||||
54 | // Not setting samples as QtQuick doesn't need it. | ||||
55 | const QVector<EGLint> attributes { | ||||
56 | EGL_SURFACE_TYPE, surfaceType, | ||||
57 | EGL_RED_SIZE, redSize, | ||||
58 | EGL_GREEN_SIZE, greenSize, | ||||
59 | EGL_BLUE_SIZE, blueSize, | ||||
60 | EGL_ALPHA_SIZE, alphaSize, | ||||
61 | EGL_DEPTH_SIZE, depthSize, | ||||
62 | EGL_STENCIL_SIZE, stencilSize, | ||||
63 | EGL_RENDERABLE_TYPE, renderableType, | ||||
64 | EGL_NONE | ||||
65 | }; | ||||
66 | | ||||
67 | EGLint configCount; | ||||
68 | EGLConfig configs[1024]; | ||||
69 | if (!eglChooseConfig(display, attributes.data(), configs, 1, &configCount)) { | ||||
70 | // FIXME: Don't bail out yet, we should try to find the most suitable config. | ||||
71 | qCWarning(KWIN_QPA, "eglChooseConfig failed: %x", eglGetError()); | ||||
72 | return EGL_NO_CONFIG_KHR; | ||||
73 | } | ||||
74 | | ||||
75 | if (configCount != 1) { | ||||
76 | qCWarning(KWIN_QPA) << "eglChooseConfig did not return any configs"; | ||||
77 | return EGL_NO_CONFIG_KHR; | ||||
78 | } | ||||
79 | | ||||
80 | return configs[0]; | ||||
81 | } | ||||
82 | | ||||
83 | QSurfaceFormat formatFromConfig(EGLDisplay display, EGLConfig config) | ||||
84 | { | ||||
85 | int redSize = 0; | ||||
86 | int blueSize = 0; | ||||
87 | int greenSize = 0; | ||||
88 | int alphaSize = 0; | ||||
89 | int stencilSize = 0; | ||||
90 | int depthSize = 0; | ||||
91 | int sampleCount = 0; | ||||
92 | | ||||
93 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &redSize); | ||||
94 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &greenSize); | ||||
95 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &blueSize); | ||||
96 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &alphaSize); | ||||
97 | eglGetConfigAttrib(display, config, EGL_STENCIL_SIZE, &stencilSize); | ||||
98 | eglGetConfigAttrib(display, config, EGL_DEPTH_SIZE, &depthSize); | ||||
99 | eglGetConfigAttrib(display, config, EGL_SAMPLES, &sampleCount); | ||||
100 | | ||||
101 | QSurfaceFormat format; | ||||
102 | format.setRedBufferSize(redSize); | ||||
103 | format.setGreenBufferSize(greenSize); | ||||
104 | format.setBlueBufferSize(blueSize); | ||||
105 | format.setAlphaBufferSize(alphaSize); | ||||
106 | format.setStencilBufferSize(stencilSize); | ||||
107 | format.setDepthBufferSize(depthSize); | ||||
108 | format.setSamples(sampleCount); | ||||
109 | format.setRenderableType(isOpenGLES() ? QSurfaceFormat::OpenGLES : QSurfaceFormat::OpenGL); | ||||
110 | format.setStereo(false); | ||||
111 | | ||||
112 | return format; | ||||
113 | } | ||||
114 | | ||||
115 | } // namespace QPA | ||||
116 | } // namespace KWin |