Skip to content

Commit

Permalink
Detect OpenGL backend; Fix #64
Browse files Browse the repository at this point in the history
  • Loading branch information
coslyk committed Jul 27, 2018
1 parent 188b94d commit dda3d0a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
62 changes: 62 additions & 0 deletions src/detectopengl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <mpv/client.h>
#include <QSurfaceFormat>
#include <QSettings>

#ifdef Q_OS_MAC
void detectOpenGL()
{
// Request OpenGL 4.1 if possible on OSX, otherwise it defaults to 2.0
// This needs to be done before we create the QGuiApplication
//
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setMajorVersion(3);
format.setMinorVersion(2);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
}
#endif

#ifdef Q_OS_LINUX
// Attempt to reuse mpv's code for detecting whether we want GLX or EGL (which
// is tricky to do because of hardware decoding concerns). This is not pretty,
// but quite effective and without having to duplicate too much GLX/EGL code.
static QString probeHwdecInterop()
{
QString result;
mpv_handle *mpv = mpv_create();
if (!mpv)
return "";
mpv_set_option_string(mpv, "hwdec-preload", "auto");
mpv_set_option_string(mpv, "opengl-hwdec-interop", "auto");
// Actually creating a window is required. There is currently no way to keep
// this window hidden or invisible.
mpv_set_option_string(mpv, "force-window", "yes");
// As a mitigation, put the window in the top/right corner, and make it as
// small as possible by forcing 1x1 size and removing window borders.
mpv_set_option_string(mpv, "geometry", "1x1+0+0");
mpv_set_option_string(mpv, "border", "no");
if (mpv_initialize(mpv) < 0)
return "";
char *str = mpv_get_property_string(mpv, "hwdec-interop");
if (str)
{
qInfo("Detected OpenGL backend: %s", str);
result = str;
mpv_free(str);
}
mpv_terminate_destroy(mpv);
return result;
}

void detectOpenGL()
{
QString hwdec = QSettings("moonsoft", "moonplayer").value("Video/hwdec").toString();
if (hwdec == "vaapi")
qputenv("QT_XCB_GL_INTEGRATION", "xcb_egl");
else if (hwdec == "auto")
{
if (probeHwdecInterop() == "vaapi-egl")
qputenv("QT_XCB_GL_INTEGRATION", "xcb_egl");
}
}
#endif
6 changes: 6 additions & 0 deletions src/detectopengl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DETECTOPENGL_H
#define DETECTOPENGL_H

void detectOpenGL(void);

#endif // DETECTOPENGL_H
11 changes: 2 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#include <QLocale>
#include <QDebug>
#include <QSettings>
#include <QSurfaceFormat>
#include <QTextCodec>
#include <QNetworkAccessManager>
#include <Python.h>
#include "detectopengl.h"
#include "pyapi.h"
#include "platforms.h"
#include "playerview.h"
Expand Down Expand Up @@ -65,15 +65,8 @@ int main(int argc, char *argv[])

#if defined(Q_OS_LINUX)
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
if (QSettings("moonsoft", "moonplayer").value("Video/hwdec").toString() != "vdpau")
qputenv("QT_XCB_GL_INTEGRATION", "xcb_egl");
#elif defined(Q_OS_MAC)
// OpenGL version >= 3.0 is required for hardware decoding
QSurfaceFormat surface = QSurfaceFormat::defaultFormat();
surface.setVersion(3, 2);
surface.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(surface);
#endif
detectOpenGL();

#ifdef Q_OS_MAC
MyApplication a(argc, argv);
Expand Down
6 changes: 4 additions & 2 deletions src/moonplayer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ SOURCES += main.cpp\
danmakudelaygetter.cpp \
terminal.cpp \
ykdlbridge.cpp \
parserbridge.cpp
parserbridge.cpp \
detectopengl.cpp
!macx: SOURCES += localserver.cpp \
localsocket.cpp

Expand Down Expand Up @@ -82,7 +83,8 @@ HEADERS +=\
playerview.h \
terminal.h \
ykdlbridge.h \
parserbridge.h
parserbridge.h \
detectopengl.h
!macx: HEADERS += localserver.h \
localsocket.h

Expand Down

0 comments on commit dda3d0a

Please sign in to comment.