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