desktop-main.cpp: add validateGL()

Add a new static function to validate the availability of OpenGL
on this particular desktop system. It makes some calls to create
a platform agnostic GL context that renders to a offscreen surface.
Then it makes a couple of glGetIntegerv((GL_xxx_VERSION, ...) calls
to see if the GL profile version is at least 2.1.

In case any of the steps fail, a stderr message is shown and all
QtQuick based widgets would be rendered using a software renderer.

Testing was done in the case of the Google maps plugin and the
fallback seems to work on Windows, but further testing will be
required on all OS.

For the time being, the automatic fallback is only supported
on Qt 5.8.0 or later.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
Lubomir I. Ivanov 2017-10-29 18:04:32 +02:00 committed by Dirk Hohndel
parent fd2e4803a0
commit c78a864e15

View file

@ -21,9 +21,14 @@
#include <QStringList>
#include <QApplication>
#include <QLoggingCategory>
#include <QOpenGLContext>
#include <QOffscreenSurface>
#include <QOpenGLFunctions>
#include <QQuickWindow>
#include <git2.h>
static bool filesOnCommandLine = false;
static void validateGL();
int main(int argc, char **argv)
{
@ -46,6 +51,8 @@ int main(int argc, char **argv)
subsurface_console_init(dedicated_console, false);
}
validateGL();
const char *default_directory = system_default_directory();
const char *default_filename = system_default_filename();
subsurface_mkdir(default_directory);
@ -129,3 +136,46 @@ bool haveFilesOnCommandLine()
{
return filesOnCommandLine;
}
void validateGL()
{
GLint verMajor, verMinor;
const char *glError = NULL;
QOpenGLContext ctx;
QOffscreenSurface surface;
QOpenGLFunctions *func;
surface.setFormat(ctx.format());
surface.create();
if (!ctx.create()) {
glError = "Cannot create OpenGL context";
goto exit;
}
ctx.makeCurrent(&surface);
func = ctx.functions();
if (!func) {
glError = "Cannot obtain QOpenGLFunctions";
goto exit;
}
func->glGetIntegerv(GL_MAJOR_VERSION, &verMajor);
func->glGetIntegerv(GL_MINOR_VERSION, &verMinor);
if (verMajor * 10 + verMinor < 21) // set 2.1 as the minimal version
glError = "OpenGL 2.1 or later is required";
exit:
ctx.makeCurrent(NULL);
surface.destroy();
if (glError) {
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
fprintf(stderr, "ERROR: %s.\n"
"Cannot automatically fallback to a software renderer!\n"
"Set the environment variable 'QT_QUICK_BACKEND' with the value of 'software'\n"
"before running Subsurface!\n",
glError);
exit(0);
#else
fprintf(stderr, "WARNING: %s. Using a software renderer!\n", glError);
QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
#endif
}
}