Fixed missing way of choosing EGL renderable type with QSurfaceFormat.
[profile/ivi/qtbase.git] / src / platformsupport / eglconvenience / qeglplatformcontext.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qeglplatformcontext_p.h"
43
44 #include "qeglconvenience_p.h"
45
46 #include <qpa/qplatformwindow.h>
47
48 #include <EGL/egl.h>
49
50 static inline void bindApi(const QSurfaceFormat &format)
51 {
52     if (format.renderableType() == QSurfaceFormat::OpenVG)
53         eglBindAPI(EGL_OPENVG_API);
54 #ifdef EGL_VERSION_1_4
55     else if (format.renderableType() == QSurfaceFormat::OpenGL)
56         eglBindAPI(EGL_OPENGL_API);
57 #endif
58     else
59         eglBindAPI(EGL_OPENGL_ES_API);
60 }
61
62 QEGLPlatformContext::QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display,
63                                          EGLenum eglApi)
64     : m_eglDisplay(display)
65     , m_eglApi(eglApi)
66     , m_eglConfig(q_configFromGLFormat(display, format, true))
67     , m_format(q_glFormatFromConfig(display, m_eglConfig))
68 {
69     m_shareContext = share ? static_cast<QEGLPlatformContext *>(share)->m_eglContext : 0;
70
71     QVector<EGLint> contextAttrs;
72     contextAttrs.append(EGL_CONTEXT_CLIENT_VERSION);
73     contextAttrs.append(format.majorVersion());
74     contextAttrs.append(EGL_NONE);
75
76     bindApi(m_format);
77     m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, m_shareContext, contextAttrs.constData());
78     if (m_eglContext == EGL_NO_CONTEXT && m_shareContext != EGL_NO_CONTEXT) {
79         m_shareContext = 0;
80         m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, 0, contextAttrs.constData());
81     }
82 }
83
84 bool QEGLPlatformContext::makeCurrent(QPlatformSurface *surface)
85 {
86     Q_ASSERT(surface->surface()->surfaceType() == QSurface::OpenGLSurface);
87
88 #ifdef QEGL_EXTRA_DEBUG
89     qWarning("QEglContext::makeCurrent: %p\n",this);
90 #endif
91     bindApi(m_format);
92
93     EGLSurface eglSurface = eglSurfaceForPlatformSurface(surface);
94
95     bool ok = eglMakeCurrent(m_eglDisplay, eglSurface, eglSurface, m_eglContext);
96     if (!ok)
97         qWarning("QEGLPlatformContext::makeCurrent: eglError: %x, this: %p \n", eglGetError(), this);
98 #ifdef QEGL_EXTRA_DEBUG
99     static bool showDebug = true;
100     if (showDebug) {
101         showDebug = false;
102         const char *str = (const char*)glGetString(GL_VENDOR);
103         qWarning("Vendor %s\n", str);
104         str = (const char*)glGetString(GL_RENDERER);
105         qWarning("Renderer %s\n", str);
106         str = (const char*)glGetString(GL_VERSION);
107         qWarning("Version %s\n", str);
108
109         str = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
110         qWarning("Extensions %s\n",str);
111
112         str = (const char*)glGetString(GL_EXTENSIONS);
113         qWarning("Extensions %s\n", str);
114
115     }
116 #endif
117     return ok;
118 }
119
120 QEGLPlatformContext::~QEGLPlatformContext()
121 {
122 #ifdef QEGL_EXTRA_DEBUG
123     qWarning("QEglContext::~QEglContext(): %p\n",this);
124 #endif
125     if (m_eglContext != EGL_NO_CONTEXT) {
126         eglDestroyContext(m_eglDisplay, m_eglContext);
127         m_eglContext = EGL_NO_CONTEXT;
128     }
129 }
130
131 void QEGLPlatformContext::doneCurrent()
132 {
133 #ifdef QEGL_EXTRA_DEBUG
134     qWarning("QEglContext::doneCurrent:%p\n",this);
135 #endif
136     bindApi(m_format);
137     bool ok = eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138     if (!ok)
139         qWarning("QEGLPlatformContext::doneCurrent(): eglError: %d, this: %p \n", eglGetError(), this);
140 }
141
142 void QEGLPlatformContext::swapBuffers(QPlatformSurface *surface)
143 {
144 #ifdef QEGL_EXTRA_DEBUG
145     qWarning("QEglContext::swapBuffers:%p\n",this);
146 #endif
147     bindApi(m_format);
148     EGLSurface eglSurface = eglSurfaceForPlatformSurface(surface);
149     bool ok = eglSwapBuffers(m_eglDisplay, eglSurface);
150     if (!ok)
151         qWarning("QEGLPlatformContext::swapBuffers(): eglError: %d, this: %p \n", eglGetError(), this);
152 }
153
154 void (*QEGLPlatformContext::getProcAddress(const QByteArray &procName)) ()
155 {
156 #ifdef QEGL_EXTRA_DEBUG
157     qWarning("QEglContext::getProcAddress%p\n",this);
158 #endif
159     bindApi(m_format);
160     return eglGetProcAddress(procName.constData());
161 }
162
163 QSurfaceFormat QEGLPlatformContext::format() const
164 {
165     return m_format;
166 }
167
168 EGLContext QEGLPlatformContext::eglContext() const
169 {
170     return m_eglContext;
171 }
172
173 EGLDisplay QEGLPlatformContext::eglDisplay() const
174 {
175     return m_eglDisplay;
176 }
177
178 EGLConfig QEGLPlatformContext::eglConfig() const
179 {
180     return m_eglConfig;
181 }