~alan-griffiths/miral/fix-1645284

« back to all changes in this revision

Viewing changes to miral-qt/src/platforms/mirserver/miropenglcontext.cpp

  • Committer: Alan Griffiths
  • Date: 2016-11-07 17:59:19 UTC
  • mfrom: (436.1.1 miral2)
  • Revision ID: alan@octopull.co.uk-20161107175919-stbb64i7j1htgog2
[miral-qt] delete all as qtmir work on MirAL has shifted to lp:~unity-team/qtmir/miral-qt-integration and this is a needless distration

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013-2016 Canonical, Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it under
5
 
 * the terms of the GNU Lesser General Public License version 3, as published by
6
 
 * the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
 
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
 * Lesser General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 */
16
 
 
17
 
#include "miropenglcontext.h"
18
 
 
19
 
#include "offscreensurface.h"
20
 
#include "mirglconfig.h"
21
 
#include "screenwindow.h"
22
 
 
23
 
#include <QDebug>
24
 
 
25
 
#include <QOpenGLFramebufferObject>
26
 
#include <QSurfaceFormat>
27
 
#include <QtPlatformSupport/private/qeglconvenience_p.h>
28
 
#include <QtGui/private/qopenglcontext_p.h>
29
 
 
30
 
// Mir
31
 
#include <mir/graphics/display.h>
32
 
#include <mir/version.h>
33
 
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 25, 0)
34
 
#include <mir/renderer/gl/context_source.h>
35
 
#include <mir/renderer/gl/context.h>
36
 
#else
37
 
#include <mir/graphics/gl_context.h>
38
 
#endif
39
 
 
40
 
 
41
 
// Qt supports one GL context per screen, but also shared contexts.
42
 
// The Mir "Display" generates a shared GL context for all DisplayBuffers
43
 
// (i.e. individual display output buffers) to use as a common base context.
44
 
 
45
 
MirOpenGLContext::MirOpenGLContext(
46
 
    mir::graphics::Display &display,
47
 
    mir::graphics::GLConfig &gl_config,
48
 
    const QSurfaceFormat &format)
49
 
    : m_currentWindow(nullptr)
50
 
#ifdef QGL_DEBUG
51
 
      , m_logger(new QOpenGLDebugLogger(this))
52
 
#endif
53
 
{
54
 
    // create a temporary GL context to fetch the EGL display and config, so Qt can determine the surface format
55
 
#if MIR_SERVER_VERSION < MIR_VERSION_NUMBER(0, 25, 0)
56
 
    std::unique_ptr<mir::graphics::GLContext> mirContext = display.create_gl_context();
57
 
#else
58
 
    auto const mirContext = dynamic_cast<mir::renderer::gl::ContextSource&>(display).create_gl_context();
59
 
#endif
60
 
    mirContext->make_current();
61
 
 
62
 
    EGLDisplay eglDisplay = eglGetCurrentDisplay();
63
 
    if (eglDisplay == EGL_NO_DISPLAY) {
64
 
        qFatal("Unable to determine current EGL Display");
65
 
    }
66
 
    EGLContext eglContext = eglGetCurrentContext();
67
 
    if (eglContext == EGL_NO_CONTEXT) {
68
 
        qFatal("Unable to determine current EGL Context");
69
 
    }
70
 
    EGLint eglConfigId = -1;
71
 
    EGLBoolean result;
72
 
    result = eglQueryContext(eglDisplay, eglContext, EGL_CONFIG_ID, &eglConfigId);
73
 
    if (result != EGL_TRUE || eglConfigId < 0) {
74
 
        qFatal("Unable to determine current EGL Config ID");
75
 
    }
76
 
 
77
 
    EGLConfig eglConfig;
78
 
    EGLint matchingEglConfigCount;
79
 
    EGLint const attribList[] = {
80
 
        EGL_CONFIG_ID, eglConfigId,
81
 
        EGL_NONE
82
 
    };
83
 
    result = eglChooseConfig(eglDisplay, attribList, &eglConfig, 1, &matchingEglConfigCount);
84
 
    if (result != EGL_TRUE || eglConfig == nullptr || matchingEglConfigCount < 1) {
85
 
        qFatal("Unable to select EGL Config with the supposed current config ID");
86
 
    }
87
 
 
88
 
    QSurfaceFormat formatCopy = format;
89
 
    formatCopy.setRenderableType(QSurfaceFormat::OpenGLES);
90
 
 
91
 
    m_format = q_glFormatFromConfig(eglDisplay, eglConfig, formatCopy);
92
 
    mirContext->release_current(); // Need to release as it doesn't happen when GLContext goes out of scope
93
 
 
94
 
    // FIXME: the temporary gl context created by Mir does not have the attributes we specified
95
 
    // in the GLConfig, so need to set explicitly for now
96
 
    m_format.setDepthBufferSize(gl_config.depth_buffer_bits());
97
 
    m_format.setStencilBufferSize(gl_config.stencil_buffer_bits());
98
 
    m_format.setSamples(-1);
99
 
 
100
 
#ifdef QGL_DEBUG
101
 
    const char* string = (const char*) glGetString(GL_VENDOR);
102
 
    qDebug() << "OpenGL ES vendor: " << qPrintable(string);
103
 
    string = (const char*) glGetString(GL_RENDERER);
104
 
    qDebug() << "OpenGL ES renderer"  << qPrintable(string);
105
 
    string = (const char*) glGetString(GL_VERSION);
106
 
    qDebug() << "OpenGL ES version" << qPrintable(string);
107
 
    string = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);
108
 
    qDebug() << "OpenGL ES Shading Language version:" << qPrintable(string);
109
 
    string = (const char*) glGetString(GL_EXTENSIONS);
110
 
    qDebug() << "OpenGL ES extensions:" << qPrintable(string);
111
 
    q_printEglConfig(eglDisplay, eglConfig);
112
 
 
113
 
    QObject::connect(m_logger, &QOpenGLDebugLogger::messageLogged,
114
 
                     this, &MirOpenGLContext::onGlDebugMessageLogged, Qt::DirectConnection);
115
 
#endif // debug
116
 
    mirContext->release_current(); // Need to release as it doesn't happen when GLContext goes out of scope
117
 
}
118
 
 
119
 
QSurfaceFormat MirOpenGLContext::format() const
120
 
{
121
 
    return m_format;
122
 
}
123
 
 
124
 
void MirOpenGLContext::swapBuffers(QPlatformSurface *surface)
125
 
{
126
 
    if (surface->surface()->surfaceClass() == QSurface::Offscreen) {
127
 
        // NOOP
128
 
    } else {
129
 
        // ultimately calls Mir's DisplayBuffer::post_update()
130
 
        ScreenWindow *screenWindow = static_cast<ScreenWindow*>(surface);
131
 
        screenWindow->swapBuffers(); //blocks for vsync
132
 
    }
133
 
}
134
 
 
135
 
static bool needsFBOReadBackWorkaround()
136
 
{
137
 
    static bool set = false;
138
 
    static bool needsWorkaround = false;
139
 
 
140
 
    if (Q_UNLIKELY(!set)) {
141
 
        const char *rendererString = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
142
 
        // Keep in sync with qtubuntu
143
 
        needsWorkaround = qstrncmp(rendererString, "Mali-400", 8) == 0
144
 
                          || qstrncmp(rendererString, "Mali-T7", 7) == 0
145
 
                          || qstrncmp(rendererString, "PowerVR Rogue G6200", 19) == 0;
146
 
        set = true;
147
 
    }
148
 
 
149
 
    return needsWorkaround;
150
 
}
151
 
 
152
 
bool MirOpenGLContext::makeCurrent(QPlatformSurface *surface)
153
 
{
154
 
    if (surface->surface()->surfaceClass() == QSurface::Offscreen) {
155
 
        auto offscreen = static_cast<OffscreenSurface *>(surface);
156
 
        if (!offscreen->buffer()) {
157
 
            auto buffer = new QOpenGLFramebufferObject(surface->surface()->size());
158
 
            offscreen->setBuffer(buffer);
159
 
        }
160
 
        return offscreen->buffer()->bind();
161
 
    }
162
 
 
163
 
    // ultimately calls Mir's DisplayBuffer::make_current()
164
 
    ScreenWindow *screenWindow = static_cast<ScreenWindow*>(surface);
165
 
    if (Q_LIKELY(screenWindow)) {
166
 
        m_currentWindow = screenWindow;
167
 
        screenWindow->makeCurrent();
168
 
 
169
 
#ifdef QGL_DEBUG
170
 
        if (!m_logger->isLogging() && m_logger->initialize()) {
171
 
            m_logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
172
 
            m_logger->enableMessages();
173
 
        }
174
 
#endif
175
 
 
176
 
        QOpenGLContextPrivate *ctx_d = QOpenGLContextPrivate::get(context());
177
 
        if (!ctx_d->workaround_brokenFBOReadBack && needsFBOReadBackWorkaround())
178
 
            ctx_d->workaround_brokenFBOReadBack = true;
179
 
 
180
 
        return true;
181
 
    }
182
 
 
183
 
    return false;
184
 
}
185
 
 
186
 
void MirOpenGLContext::doneCurrent()
187
 
{
188
 
    if (m_currentWindow) {
189
 
        m_currentWindow->doneCurrent();
190
 
        m_currentWindow = nullptr;
191
 
    }
192
 
}
193
 
 
194
 
QFunctionPointer MirOpenGLContext::getProcAddress(const QByteArray &procName)
195
 
{
196
 
    return eglGetProcAddress(procName.constData());
197
 
}