KWin
Loading...
Searching...
No Matches
eglhelpers.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2015 Martin Flöser <mgraesslin@kde.org>
6 SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include "eglhelpers.h"
12#include "opengl/egldisplay.h"
13
14#include <logging.h>
15
16#include <QOpenGLContext>
17
18namespace KWin
19{
20namespace QPA
21{
22
24{
25 if (qstrcmp(qgetenv("KWIN_COMPOSE"), "O2ES") == 0) {
26 return true;
27 }
28
29 return QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES;
30}
31
32EGLConfig configFromFormat(EglDisplay *display, const QSurfaceFormat &surfaceFormat, EGLint surfaceType)
33{
34 // std::max as these values are initialized to -1 by default.
35 const EGLint redSize = std::max(surfaceFormat.redBufferSize(), 0);
36 const EGLint greenSize = std::max(surfaceFormat.greenBufferSize(), 0);
37 const EGLint blueSize = std::max(surfaceFormat.blueBufferSize(), 0);
38 const EGLint alphaSize = std::max(surfaceFormat.alphaBufferSize(), 0);
39 const EGLint depthSize = std::max(surfaceFormat.depthBufferSize(), 0);
40 const EGLint stencilSize = std::max(surfaceFormat.stencilBufferSize(), 0);
41
42 const EGLint renderableType = isOpenGLES() ? EGL_OPENGL_ES2_BIT : EGL_OPENGL_BIT;
43
44 // Not setting samples as QtQuick doesn't need it.
45 const QList<EGLint> attributes{
46 EGL_SURFACE_TYPE, surfaceType,
47 EGL_RED_SIZE, redSize,
48 EGL_GREEN_SIZE, greenSize,
49 EGL_BLUE_SIZE, blueSize,
50 EGL_ALPHA_SIZE, alphaSize,
51 EGL_DEPTH_SIZE, depthSize,
52 EGL_STENCIL_SIZE, stencilSize,
53 EGL_RENDERABLE_TYPE, renderableType,
54 EGL_NONE};
55
56 EGLint configCount;
57 if (!eglChooseConfig(display->handle(), attributes.data(), nullptr, 0, &configCount)) {
58 qCWarning(KWIN_QPA, "eglChooseConfig failed: %x", eglGetError());
59 return EGL_NO_CONFIG_KHR;
60 }
61 if (configCount == 0) {
62 qCWarning(KWIN_QPA, "eglChooseConfig did not return any configs");
63 return EGL_NO_CONFIG_KHR;
64 }
65
66 QList<EGLConfig> configs(configCount);
67 if (!eglChooseConfig(display->handle(), attributes.data(), configs.data(), configCount, &configCount)) {
68 qCWarning(KWIN_QPA, "eglChooseConfig failed: %x", eglGetError());
69 return EGL_NO_CONFIG_KHR;
70 }
71 if (configCount != configs.size()) {
72 qCWarning(KWIN_QPA, "eglChooseConfig did not return requested configs");
73 return EGL_NO_CONFIG_KHR;
74 }
75
76 for (const EGLConfig &config : std::as_const(configs)) {
77 EGLint redConfig, greenConfig, blueConfig, alphaConfig;
78 eglGetConfigAttrib(display->handle(), config, EGL_RED_SIZE, &redConfig);
79 eglGetConfigAttrib(display->handle(), config, EGL_GREEN_SIZE, &greenConfig);
80 eglGetConfigAttrib(display->handle(), config, EGL_BLUE_SIZE, &blueConfig);
81 eglGetConfigAttrib(display->handle(), config, EGL_ALPHA_SIZE, &alphaConfig);
82
83 if ((redSize == 0 || redSize == redConfig) && (greenSize == 0 || greenSize == greenConfig) && (blueSize == 0 || blueSize == blueConfig) && (alphaSize == 0 || alphaSize == alphaConfig)) {
84 return config;
85 }
86 }
87
88 // Return first config as a fallback.
89 return configs[0];
90}
91
92QSurfaceFormat formatFromConfig(EglDisplay *display, EGLConfig config)
93{
94 int redSize = 0;
95 int blueSize = 0;
96 int greenSize = 0;
97 int alphaSize = 0;
98 int stencilSize = 0;
99 int depthSize = 0;
100 int sampleCount = 0;
101
102 eglGetConfigAttrib(display->handle(), config, EGL_RED_SIZE, &redSize);
103 eglGetConfigAttrib(display->handle(), config, EGL_GREEN_SIZE, &greenSize);
104 eglGetConfigAttrib(display->handle(), config, EGL_BLUE_SIZE, &blueSize);
105 eglGetConfigAttrib(display->handle(), config, EGL_ALPHA_SIZE, &alphaSize);
106 eglGetConfigAttrib(display->handle(), config, EGL_STENCIL_SIZE, &stencilSize);
107 eglGetConfigAttrib(display->handle(), config, EGL_DEPTH_SIZE, &depthSize);
108 eglGetConfigAttrib(display->handle(), config, EGL_SAMPLES, &sampleCount);
109
110 QSurfaceFormat format;
111 format.setRedBufferSize(redSize);
112 format.setGreenBufferSize(greenSize);
113 format.setBlueBufferSize(blueSize);
114 format.setAlphaBufferSize(alphaSize);
115 format.setStencilBufferSize(stencilSize);
116 format.setDepthBufferSize(depthSize);
117 format.setSamples(sampleCount);
118 format.setRenderableType(isOpenGLES() ? QSurfaceFormat::OpenGLES : QSurfaceFormat::OpenGL);
119 format.setStereo(false);
120
121 return format;
122}
123
124} // namespace QPA
125} // namespace KWin
::EGLDisplay handle() const
QSurfaceFormat formatFromConfig(EglDisplay *display, EGLConfig config)
EGLConfig configFromFormat(EglDisplay *display, const QSurfaceFormat &surfaceFormat, EGLint surfaceType)
bool isOpenGLES()
GLenum format
Definition gltexture.cpp:49