Commit 3f4995fb authored by Martin Flöser's avatar Martin Flöser

Refactor the specification of OpenGL context attributes

Summary:
The current way to specify the OpenGL context attributes does no longer
scale as can be seen in D6344. There are too many different context
attribute sets and with every addition we grow lots of copied code. The
chances to introduce errors in that code which is difficult to debug are
very high. As can be seen in the glx backend which defines major 1,
minor 2, but it should be major 2, minor 1.

This change reworks this code by creating a builder class which contains
only an abstract definition of what needs to be in the attributes.
E.g. the version, whether it's robust and so on.

Now we can just have a list of possible attributes in a well described
way:
    auto builder;
    builder.setVersion(3, 1);
    builder.setRobust(true);

All possible builders are added to a list and operated on in a for loop
which tries to creat a context. Once it succeeded it breaks the list.
In addition a debug statement is added which prints out the set of
options which went into the context.

So far this is only done for EGL, GLX can follow once D6344 is merged.

Test Plan: New unit test added, kwin_wayland OpenGL tests run and verified

Reviewers: #kwin, #plasma

Subscribers: plasma-devel, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D6396
parent a6d0bc27
......@@ -440,6 +440,8 @@ set(kwin_KDEINIT_SRCS
xkb.cpp
gestures.cpp
popup_input_filter.cpp
abstract_opengl_context_attribute_builder.cpp
egl_context_attribute_builder.cpp
)
if(KWIN_BUILD_TABBOX)
......
......@@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "abstract_egl_backend.h"
#include "egl_context_attribute_builder.h"
#include "options.h"
#include "platform.h"
#include "wayland_server.h"
......@@ -30,6 +31,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QOpenGLContext>
#include <QOpenGLFramebufferObject>
#include <memory>
namespace KWin
{
......@@ -206,63 +209,44 @@ bool AbstractEglBackend::createContext()
const bool haveRobustness = hasExtension(QByteArrayLiteral("EGL_EXT_create_context_robustness"));
const bool haveCreateContext = hasExtension(QByteArrayLiteral("EGL_KHR_create_context"));
EGLContext ctx = EGL_NO_CONTEXT;
std::vector<std::unique_ptr<AbstractOpenGLContextAttributeBuilder>> candidates;
if (isOpenGLES()) {
if (haveCreateContext && haveRobustness) {
const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT, EGL_TRUE,
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT, EGL_LOSE_CONTEXT_ON_RESET_EXT,
EGL_NONE
};
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, context_attribs);
}
if (ctx == EGL_NO_CONTEXT) {
const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, context_attribs);
auto glesRobust = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglOpenGLESContextAttributeBuilder);
glesRobust->setVersion(2);
glesRobust->setRobust(true);
candidates.push_back(std::move(glesRobust));
}
auto gles = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglOpenGLESContextAttributeBuilder);
gles->setVersion(2);
candidates.push_back(std::move(gles));
} else {
// Try to create a 3.1 core context
if (options->glCoreProfile() && haveCreateContext) {
if (haveRobustness) {
const int attribs[] = {
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
EGL_CONTEXT_MINOR_VERSION_KHR, 1,
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR, EGL_LOSE_CONTEXT_ON_RESET_KHR,
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR,
EGL_NONE
};
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, attribs);
}
if (ctx == EGL_NO_CONTEXT) {
// try without robustness
const EGLint attribs[] = {
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
EGL_CONTEXT_MINOR_VERSION_KHR, 1,
EGL_NONE
};
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, attribs);
auto robustCore = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
robustCore->setVersion(3, 1);
robustCore->setRobust(true);
candidates.push_back(std::move(robustCore));
}
auto core = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
core->setVersion(3, 1);
candidates.push_back(std::move(core));
}
if (ctx == EGL_NO_CONTEXT && haveRobustness && haveCreateContext) {
const int attribs[] = {
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR,
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR, EGL_LOSE_CONTEXT_ON_RESET_KHR,
EGL_NONE
};
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, attribs);
if (haveRobustness && haveCreateContext) {
auto robust = std::unique_ptr<AbstractOpenGLContextAttributeBuilder>(new EglContextAttributeBuilder);
robust->setRobust(true);
candidates.push_back(std::move(robust));
}
if (ctx == EGL_NO_CONTEXT) {
// and last but not least: try without robustness
const EGLint attribs[] = {
EGL_NONE
};
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, attribs);
candidates.emplace_back(new EglContextAttributeBuilder);
}
EGLContext ctx = EGL_NO_CONTEXT;
for (auto it = candidates.begin(); it != candidates.end(); it++) {
const auto attribs = (*it)->build();
ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, attribs.data());
if (ctx != EGL_NO_CONTEXT) {
qCDebug(KWIN_CORE) << "Created EGL context with attributes:" << (*it).get();
break;
}
}
......
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "abstract_opengl_context_attribute_builder.h"
namespace KWin
{
QDebug AbstractOpenGLContextAttributeBuilder::operator<<(QDebug dbg) const
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "\nVersion requested:\t" << isVersionRequested() << "\n";
if (isVersionRequested()) {
dbg.nospace() << "Version:\t" << majorVersion() << "." << minorVersion() << "\n";
}
dbg.nospace() << "Robust:\t" << isRobust();
return dbg;
}
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#pragma once
#include <QDebug>
namespace KWin
{
class AbstractOpenGLContextAttributeBuilder
{
public:
virtual ~AbstractOpenGLContextAttributeBuilder() {
}
void setVersion(int major, int minor = 0) {
m_versionRequested = true;
m_majorVersion = major;
m_minorVersion = minor;
}
bool isVersionRequested() const {
return m_versionRequested;
}
int majorVersion() const {
return m_majorVersion;
}
int minorVersion() const {
return m_minorVersion;
}
void setRobust(bool robust) {
m_robust = robust;
}
bool isRobust() const {
return m_robust;
}
virtual std::vector<int> build() const = 0;
QDebug operator<<(QDebug dbg) const;
private:
bool m_versionRequested = false;
int m_majorVersion = 0;
int m_minorVersion = 0;
bool m_robust = false;
};
inline QDebug operator<<(QDebug dbg, const AbstractOpenGLContextAttributeBuilder *attribs)
{
return attribs->operator<<(dbg);
}
}
......@@ -361,3 +361,8 @@ target_link_libraries(testX11TimestampUpdate
)
add_test(kwin-testX11TimestampUpdate testX11TimestampUpdate)
ecm_mark_as_test(testX11TimestampUpdate)
add_executable(testOpenGLContextAttributeBuilder opengl_context_attribute_builder_test.cpp ../abstract_opengl_context_attribute_builder.cpp ../egl_context_attribute_builder.cpp)
target_link_libraries(testOpenGLContextAttributeBuilder Qt5::Test)
add_test(kwin-testOpenGLContextAttributeBuilder testOpenGLContextAttributeBuilder)
ecm_mark_as_test(testOpenGLContextAttributeBuilder)
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "../abstract_opengl_context_attribute_builder.h"
#include "../egl_context_attribute_builder.h"
#include <QtTest/QtTest>
#include <epoxy/egl.h>
using namespace KWin;
class OpenGLContextAttributeBuilderTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testCtor();
void testRobust();
void testVersionMajor();
void testVersionMajorAndMinor();
void testEgl_data();
void testEgl();
void testGles_data();
void testGles();
};
class MockOpenGLContextAttributeBuilder : public AbstractOpenGLContextAttributeBuilder
{
public:
std::vector<int> build() const override;
};
std::vector<int> MockOpenGLContextAttributeBuilder::build() const
{
return std::vector<int>();
}
void OpenGLContextAttributeBuilderTest::testCtor()
{
MockOpenGLContextAttributeBuilder builder;
QCOMPARE(builder.isVersionRequested(), false);
QCOMPARE(builder.majorVersion(), 0);
QCOMPARE(builder.minorVersion(), 0);
QCOMPARE(builder.isRobust(), false);
}
void OpenGLContextAttributeBuilderTest::testRobust()
{
MockOpenGLContextAttributeBuilder builder;
QCOMPARE(builder.isRobust(), false);
builder.setRobust(true);
QCOMPARE(builder.isRobust(), true);
builder.setRobust(false);
QCOMPARE(builder.isRobust(), false);
}
void OpenGLContextAttributeBuilderTest::testVersionMajor()
{
MockOpenGLContextAttributeBuilder builder;
builder.setVersion(2);
QCOMPARE(builder.isVersionRequested(), true);
QCOMPARE(builder.majorVersion(), 2);
QCOMPARE(builder.minorVersion(), 0);
builder.setVersion(3);
QCOMPARE(builder.isVersionRequested(), true);
QCOMPARE(builder.majorVersion(), 3);
QCOMPARE(builder.minorVersion(), 0);
}
void OpenGLContextAttributeBuilderTest::testVersionMajorAndMinor()
{
MockOpenGLContextAttributeBuilder builder;
builder.setVersion(2, 1);
QCOMPARE(builder.isVersionRequested(), true);
QCOMPARE(builder.majorVersion(), 2);
QCOMPARE(builder.minorVersion(), 1);
builder.setVersion(3, 2);
QCOMPARE(builder.isVersionRequested(), true);
QCOMPARE(builder.majorVersion(), 3);
QCOMPARE(builder.minorVersion(), 2);
}
void OpenGLContextAttributeBuilderTest::testEgl_data()
{
QTest::addColumn<bool>("requestVersion");
QTest::addColumn<int>("major");
QTest::addColumn<int>("minor");
QTest::addColumn<bool>("robust");
QTest::addColumn<std::vector<int>>("expectedAttribs");
QTest::newRow("fallback") << false << 0 << 0 << false << std::vector<int>{EGL_NONE};
QTest::newRow("legacy/robust") << false << 0 << 0 << true <<
std::vector<int>{
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR, EGL_LOSE_CONTEXT_ON_RESET_KHR,
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR,
EGL_NONE};
QTest::newRow("core") << true << 3 << 1 << false <<
std::vector<int>{
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
EGL_CONTEXT_MINOR_VERSION_KHR, 1,
EGL_NONE};
QTest::newRow("core/robust") << true << 3 << 1 << true <<
std::vector<int>{
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
EGL_CONTEXT_MINOR_VERSION_KHR, 1,
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR, EGL_LOSE_CONTEXT_ON_RESET_KHR,
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR,
EGL_NONE};
}
void OpenGLContextAttributeBuilderTest::testEgl()
{
QFETCH(bool, requestVersion);
QFETCH(int, major);
QFETCH(int, minor);
QFETCH(bool, robust);
EglContextAttributeBuilder builder;
if (requestVersion) {
builder.setVersion(major, minor);
}
builder.setRobust(robust);
auto attribs = builder.build();
QTEST(attribs, "expectedAttribs");
}
void OpenGLContextAttributeBuilderTest::testGles_data()
{
QTest::addColumn<bool>("robust");
QTest::addColumn<std::vector<int>>("expectedAttribs");
QTest::newRow("robust") << true << std::vector<int>{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT, EGL_TRUE,
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT, EGL_LOSE_CONTEXT_ON_RESET_EXT,
EGL_NONE};
QTest::newRow("normal") << false << std::vector<int>{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE};
}
void OpenGLContextAttributeBuilderTest::testGles()
{
QFETCH(bool, robust);
EglOpenGLESContextAttributeBuilder builder;
builder.setVersion(2);
builder.setRobust(robust);
auto attribs = builder.build();
QTEST(attribs, "expectedAttribs");
}
QTEST_GUILESS_MAIN(OpenGLContextAttributeBuilderTest)
#include "opengl_context_attribute_builder_test.moc"
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "egl_context_attribute_builder.h"
#include <epoxy/egl.h>
namespace KWin
{
std::vector<int> EglContextAttributeBuilder::build() const
{
std::vector<int> attribs;
if (isVersionRequested()) {
attribs.emplace_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
attribs.emplace_back(majorVersion());
attribs.emplace_back(EGL_CONTEXT_MINOR_VERSION_KHR);
attribs.emplace_back(minorVersion());
}
if (isRobust()) {
attribs.emplace_back(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR);
attribs.emplace_back(EGL_LOSE_CONTEXT_ON_RESET_KHR);
attribs.emplace_back(EGL_CONTEXT_FLAGS_KHR);
attribs.emplace_back(EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR);
}
attribs.emplace_back(EGL_NONE);
return attribs;
}
std::vector<int> EglOpenGLESContextAttributeBuilder::build() const
{
std::vector<int> attribs;
attribs.emplace_back(EGL_CONTEXT_CLIENT_VERSION);
attribs.emplace_back(majorVersion());
if (isRobust()) {
attribs.emplace_back(EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT);
attribs.emplace_back(EGL_TRUE);
attribs.emplace_back(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT);
attribs.emplace_back(EGL_LOSE_CONTEXT_ON_RESET_EXT);
}
attribs.emplace_back(EGL_NONE);
return attribs;
}
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#pragma once
#include "abstract_opengl_context_attribute_builder.h"
namespace KWin
{
class EglContextAttributeBuilder : public AbstractOpenGLContextAttributeBuilder
{
public:
std::vector<int> build() const override;
};
class EglOpenGLESContextAttributeBuilder : public AbstractOpenGLContextAttributeBuilder
{
public:
std::vector<int> build() const override;
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment