0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 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 QtGui module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui/qpaintdevice.h>
|
|
43 |
#include <QtGui/qpixmap.h>
|
|
44 |
#include <QtGui/qwidget.h>
|
|
45 |
#include <QtCore/qdebug.h>
|
|
46 |
#include "qegl_p.h"
|
|
47 |
|
|
48 |
QT_BEGIN_NAMESPACE
|
|
49 |
|
|
50 |
// Current GL and VG contexts. These are used to determine if
|
|
51 |
// we can avoid an eglMakeCurrent() after a call to lazyDoneCurrent().
|
|
52 |
// If a background thread modifies the value, the worst that will
|
|
53 |
// happen is a redundant eglMakeCurrent() in the foreground thread.
|
|
54 |
static QEglContext * volatile currentGLContext = 0;
|
|
55 |
static QEglContext * volatile currentVGContext = 0;
|
|
56 |
|
|
57 |
QEglContext::QEglContext()
|
|
58 |
: apiType(QEgl::OpenGL)
|
|
59 |
, dpy(EGL_NO_DISPLAY)
|
|
60 |
, ctx(EGL_NO_CONTEXT)
|
|
61 |
, cfg(0)
|
|
62 |
, currentSurface(EGL_NO_SURFACE)
|
|
63 |
, current(false)
|
|
64 |
, ownsContext(true)
|
|
65 |
{
|
|
66 |
}
|
|
67 |
|
|
68 |
QEglContext::~QEglContext()
|
|
69 |
{
|
|
70 |
destroy();
|
|
71 |
|
|
72 |
if (currentGLContext == this)
|
|
73 |
currentGLContext = 0;
|
|
74 |
if (currentVGContext == this)
|
|
75 |
currentVGContext = 0;
|
|
76 |
}
|
|
77 |
|
|
78 |
bool QEglContext::isValid() const
|
|
79 |
{
|
|
80 |
return (ctx != EGL_NO_CONTEXT);
|
|
81 |
}
|
|
82 |
|
|
83 |
bool QEglContext::isCurrent() const
|
|
84 |
{
|
|
85 |
return current;
|
|
86 |
}
|
|
87 |
|
|
88 |
// Open the EGL display associated with "device".
|
|
89 |
bool QEglContext::openDisplay(QPaintDevice *device)
|
|
90 |
{
|
|
91 |
if (dpy == EGL_NO_DISPLAY)
|
|
92 |
dpy = defaultDisplay(device);
|
|
93 |
return (dpy != EGL_NO_DISPLAY);
|
|
94 |
}
|
|
95 |
|
|
96 |
// Choose a configuration that matches "properties".
|
|
97 |
bool QEglContext::chooseConfig
|
|
98 |
(const QEglProperties& properties, QEgl::PixelFormatMatch match)
|
|
99 |
{
|
|
100 |
QEglProperties props(properties);
|
|
101 |
do {
|
|
102 |
// Get the number of matching configurations for this set of properties.
|
|
103 |
EGLint matching = 0;
|
|
104 |
if (!eglChooseConfig(dpy, props.properties(), 0, 0, &matching) || !matching)
|
|
105 |
continue;
|
|
106 |
|
|
107 |
// If we want the best pixel format, then return the first
|
|
108 |
// matching configuration.
|
|
109 |
if (match == QEgl::BestPixelFormat) {
|
|
110 |
eglChooseConfig(dpy, props.properties(), &cfg, 1, &matching);
|
|
111 |
if (matching < 1)
|
|
112 |
continue;
|
|
113 |
return true;
|
|
114 |
}
|
|
115 |
|
|
116 |
// Fetch all of the matching configurations and find the
|
|
117 |
// first that matches the pixel format we wanted.
|
|
118 |
EGLint size = matching;
|
|
119 |
EGLConfig *configs = new EGLConfig [size];
|
|
120 |
eglChooseConfig(dpy, props.properties(), configs, size, &matching);
|
|
121 |
for (EGLint index = 0; index < size; ++index) {
|
|
122 |
EGLint red, green, blue, alpha;
|
|
123 |
eglGetConfigAttrib(dpy, configs[index], EGL_RED_SIZE, &red);
|
|
124 |
eglGetConfigAttrib(dpy, configs[index], EGL_GREEN_SIZE, &green);
|
|
125 |
eglGetConfigAttrib(dpy, configs[index], EGL_BLUE_SIZE, &blue);
|
|
126 |
eglGetConfigAttrib(dpy, configs[index], EGL_ALPHA_SIZE, &alpha);
|
|
127 |
if (red == props.value(EGL_RED_SIZE) &&
|
|
128 |
green == props.value(EGL_GREEN_SIZE) &&
|
|
129 |
blue == props.value(EGL_BLUE_SIZE) &&
|
|
130 |
(props.value(EGL_ALPHA_SIZE) == 0 ||
|
|
131 |
alpha == props.value(EGL_ALPHA_SIZE))) {
|
|
132 |
cfg = configs[index];
|
|
133 |
delete [] configs;
|
|
134 |
return true;
|
|
135 |
}
|
|
136 |
}
|
|
137 |
delete [] configs;
|
|
138 |
} while (props.reduceConfiguration());
|
|
139 |
|
|
140 |
#ifdef EGL_BIND_TO_TEXTURE_RGBA
|
|
141 |
// Don't report an error just yet if we failed to get a pbuffer
|
|
142 |
// configuration with texture rendering. Only report failure if
|
|
143 |
// we cannot get any pbuffer configurations at all.
|
|
144 |
if (props.value(EGL_BIND_TO_TEXTURE_RGBA) == EGL_DONT_CARE &&
|
|
145 |
props.value(EGL_BIND_TO_TEXTURE_RGB) == EGL_DONT_CARE)
|
|
146 |
#endif
|
|
147 |
{
|
|
148 |
qWarning() << "QEglContext::chooseConfig(): Could not find a suitable EGL configuration";
|
|
149 |
qWarning() << "Requested:" << props.toString();
|
|
150 |
qWarning() << "Available:";
|
|
151 |
dumpAllConfigs();
|
|
152 |
}
|
|
153 |
return false;
|
|
154 |
}
|
|
155 |
|
|
156 |
// Create the EGLContext.
|
|
157 |
bool QEglContext::createContext(QEglContext *shareContext, const QEglProperties *properties)
|
|
158 |
{
|
|
159 |
// We need to select the correct API before calling eglCreateContext().
|
|
160 |
#ifdef EGL_OPENGL_ES_API
|
|
161 |
if (apiType == QEgl::OpenGL)
|
|
162 |
eglBindAPI(EGL_OPENGL_ES_API);
|
|
163 |
#endif
|
|
164 |
#ifdef EGL_OPENVG_API
|
|
165 |
if (apiType == QEgl::OpenVG)
|
|
166 |
eglBindAPI(EGL_OPENVG_API);
|
|
167 |
#endif
|
|
168 |
|
|
169 |
// Create a new context for the configuration.
|
|
170 |
QEglProperties contextProps;
|
|
171 |
if (properties)
|
|
172 |
contextProps = *properties;
|
|
173 |
#if defined(QT_OPENGL_ES_2)
|
|
174 |
if (apiType == QEgl::OpenGL)
|
|
175 |
contextProps.setValue(EGL_CONTEXT_CLIENT_VERSION, 2);
|
|
176 |
#endif
|
|
177 |
if (shareContext && shareContext->ctx == EGL_NO_CONTEXT)
|
|
178 |
shareContext = 0;
|
|
179 |
if (shareContext) {
|
|
180 |
ctx = eglCreateContext(dpy, cfg, shareContext->ctx, contextProps.properties());
|
|
181 |
if (ctx == EGL_NO_CONTEXT) {
|
|
182 |
qWarning() << "QEglContext::createContext(): Could not share context:" << errorString(eglGetError());
|
|
183 |
shareContext = 0;
|
|
184 |
}
|
|
185 |
}
|
|
186 |
if (ctx == EGL_NO_CONTEXT) {
|
|
187 |
ctx = eglCreateContext(dpy, cfg, 0, contextProps.properties());
|
|
188 |
if (ctx == EGL_NO_CONTEXT) {
|
|
189 |
qWarning() << "QEglContext::createContext(): Unable to create EGL context:" << errorString(eglGetError());
|
|
190 |
return false;
|
|
191 |
}
|
|
192 |
}
|
|
193 |
return true;
|
|
194 |
}
|
|
195 |
|
|
196 |
// Destroy an EGL surface object. If it was current on this context
|
|
197 |
// then call doneCurrent() for it first.
|
|
198 |
void QEglContext::destroySurface(EGLSurface surface)
|
|
199 |
{
|
|
200 |
if (surface != EGL_NO_SURFACE) {
|
|
201 |
if (surface == currentSurface)
|
|
202 |
doneCurrent();
|
|
203 |
eglDestroySurface(dpy, surface);
|
|
204 |
}
|
|
205 |
}
|
|
206 |
|
|
207 |
// Destroy the context. Note: this does not destroy the surface.
|
|
208 |
void QEglContext::destroy()
|
|
209 |
{
|
|
210 |
if (ctx != EGL_NO_CONTEXT && ownsContext)
|
|
211 |
eglDestroyContext(dpy, ctx);
|
|
212 |
dpy = EGL_NO_DISPLAY;
|
|
213 |
ctx = EGL_NO_CONTEXT;
|
|
214 |
cfg = 0;
|
|
215 |
}
|
|
216 |
|
|
217 |
bool QEglContext::makeCurrent(EGLSurface surface)
|
|
218 |
{
|
|
219 |
if (ctx == EGL_NO_CONTEXT) {
|
|
220 |
qWarning() << "QEglContext::makeCurrent(): Cannot make invalid context current";
|
|
221 |
return false;
|
|
222 |
}
|
|
223 |
|
|
224 |
// If lazyDoneCurrent() was called on the surface, then we may be able
|
|
225 |
// to assume that it is still current within the thread.
|
|
226 |
if (surface == currentSurface && currentContext(apiType) == this) {
|
|
227 |
current = true;
|
|
228 |
return true;
|
|
229 |
}
|
|
230 |
|
|
231 |
current = true;
|
|
232 |
currentSurface = surface;
|
|
233 |
setCurrentContext(apiType, this);
|
|
234 |
|
|
235 |
bool ok = eglMakeCurrent(dpy, surface, surface, ctx);
|
|
236 |
if (!ok)
|
|
237 |
qWarning() << "QEglContext::makeCurrent():" << errorString(eglGetError());
|
|
238 |
return ok;
|
|
239 |
}
|
|
240 |
|
|
241 |
bool QEglContext::doneCurrent()
|
|
242 |
{
|
|
243 |
// If the context is invalid, we assume that an error was reported
|
|
244 |
// when makeCurrent() was called.
|
|
245 |
if (ctx == EGL_NO_CONTEXT)
|
|
246 |
return false;
|
|
247 |
|
|
248 |
current = false;
|
|
249 |
currentSurface = EGL_NO_SURFACE;
|
|
250 |
setCurrentContext(apiType, 0);
|
|
251 |
|
|
252 |
// We need to select the correct API before calling eglMakeCurrent()
|
|
253 |
// with EGL_NO_CONTEXT because threads can have both OpenGL and OpenVG
|
|
254 |
// contexts active at the same time.
|
|
255 |
#ifdef EGL_OPENGL_ES_API
|
|
256 |
if (apiType == QEgl::OpenGL)
|
|
257 |
eglBindAPI(EGL_OPENGL_ES_API);
|
|
258 |
#endif
|
|
259 |
#ifdef EGL_OPENVG_API
|
|
260 |
if (apiType == QEgl::OpenVG)
|
|
261 |
eglBindAPI(EGL_OPENVG_API);
|
|
262 |
#endif
|
|
263 |
|
|
264 |
bool ok = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
265 |
if (!ok)
|
|
266 |
qWarning() << "QEglContext::doneCurrent():" << errorString(eglGetError());
|
|
267 |
return ok;
|
|
268 |
}
|
|
269 |
|
|
270 |
// Act as though doneCurrent() was called, but keep the context
|
|
271 |
// and the surface active for the moment. This allows makeCurrent()
|
|
272 |
// to skip a call to eglMakeCurrent() if we are using the same
|
|
273 |
// surface as the last set of painting operations. We leave the
|
|
274 |
// currentContext() pointer as-is for now.
|
|
275 |
bool QEglContext::lazyDoneCurrent()
|
|
276 |
{
|
|
277 |
current = false;
|
|
278 |
return true;
|
|
279 |
}
|
|
280 |
|
|
281 |
bool QEglContext::swapBuffers(EGLSurface surface)
|
|
282 |
{
|
|
283 |
if(ctx == EGL_NO_CONTEXT)
|
|
284 |
return false;
|
|
285 |
|
|
286 |
bool ok = eglSwapBuffers(dpy, surface);
|
|
287 |
if (!ok)
|
|
288 |
qWarning() << "QEglContext::swapBuffers():" << errorString(eglGetError());
|
|
289 |
return ok;
|
|
290 |
}
|
|
291 |
|
|
292 |
// Wait for native rendering operations to complete before starting
|
|
293 |
// to use OpenGL/OpenVG operations.
|
|
294 |
void QEglContext::waitNative()
|
|
295 |
{
|
|
296 |
#ifdef EGL_CORE_NATIVE_ENGINE
|
|
297 |
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
|
|
298 |
#endif
|
|
299 |
}
|
|
300 |
|
|
301 |
// Wait for client OpenGL/OpenVG operations to complete before
|
|
302 |
// using native rendering operations.
|
|
303 |
void QEglContext::waitClient()
|
|
304 |
{
|
|
305 |
#ifdef EGL_OPENGL_ES_API
|
|
306 |
if (apiType == QEgl::OpenGL) {
|
|
307 |
eglBindAPI(EGL_OPENGL_ES_API);
|
|
308 |
eglWaitClient();
|
|
309 |
}
|
|
310 |
#else
|
|
311 |
if (apiType == QEgl::OpenGL)
|
|
312 |
eglWaitGL();
|
|
313 |
#endif
|
|
314 |
#ifdef EGL_OPENVG_API
|
|
315 |
if (apiType == QEgl::OpenVG) {
|
|
316 |
eglBindAPI(EGL_OPENVG_API);
|
|
317 |
eglWaitClient();
|
|
318 |
}
|
|
319 |
#endif
|
|
320 |
}
|
|
321 |
|
|
322 |
// Query the value of a configuration attribute.
|
|
323 |
bool QEglContext::configAttrib(int name, EGLint *value) const
|
|
324 |
{
|
|
325 |
return eglGetConfigAttrib(dpy, cfg, name, value);
|
|
326 |
}
|
|
327 |
|
|
328 |
// Retrieve all of the properties on "cfg". If zero, return
|
|
329 |
// the context's configuration.
|
|
330 |
QEglProperties QEglContext::configProperties(EGLConfig cfg) const
|
|
331 |
{
|
|
332 |
if (!cfg)
|
|
333 |
cfg = config();
|
|
334 |
QEglProperties props;
|
|
335 |
for (int name = 0x3020; name <= 0x304F; ++name) {
|
|
336 |
EGLint value;
|
|
337 |
if (name != EGL_NONE && eglGetConfigAttrib(dpy, cfg, name, &value))
|
|
338 |
props.setValue(name, value);
|
|
339 |
}
|
|
340 |
eglGetError(); // Clear the error state.
|
|
341 |
return props;
|
|
342 |
}
|
|
343 |
|
|
344 |
// Initialize and return the default display.
|
|
345 |
EGLDisplay QEglContext::defaultDisplay(QPaintDevice *device)
|
|
346 |
{
|
|
347 |
static EGLDisplay dpy = EGL_NO_DISPLAY;
|
|
348 |
if (dpy == EGL_NO_DISPLAY) {
|
|
349 |
dpy = getDisplay(device);
|
|
350 |
if (dpy == EGL_NO_DISPLAY) {
|
|
351 |
qWarning() << "QEglContext::defaultDisplay(): Cannot open EGL display";
|
|
352 |
return EGL_NO_DISPLAY;
|
|
353 |
}
|
|
354 |
if (!eglInitialize(dpy, NULL, NULL)) {
|
|
355 |
qWarning() << "QEglContext::defaultDisplay(): Cannot initialize EGL display:" << errorString(eglGetError());
|
|
356 |
return EGL_NO_DISPLAY;
|
|
357 |
}
|
|
358 |
#ifdef EGL_OPENGL_ES_API
|
|
359 |
eglBindAPI(EGL_OPENGL_ES_API);
|
|
360 |
#endif
|
|
361 |
}
|
|
362 |
return dpy;
|
|
363 |
}
|
|
364 |
|
|
365 |
// Return the error string associated with a specific code.
|
|
366 |
QString QEglContext::errorString(EGLint code)
|
|
367 |
{
|
|
368 |
static const char * const errors[] = {
|
|
369 |
"Success (0x3000)", // No tr
|
|
370 |
"Not initialized (0x3001)", // No tr
|
|
371 |
"Bad access (0x3002)", // No tr
|
|
372 |
"Bad alloc (0x3003)", // No tr
|
|
373 |
"Bad attribute (0x3004)", // No tr
|
|
374 |
"Bad config (0x3005)", // No tr
|
|
375 |
"Bad context (0x3006)", // No tr
|
|
376 |
"Bad current surface (0x3007)", // No tr
|
|
377 |
"Bad display (0x3008)", // No tr
|
|
378 |
"Bad match (0x3009)", // No tr
|
|
379 |
"Bad native pixmap (0x300A)", // No tr
|
|
380 |
"Bad native window (0x300B)", // No tr
|
|
381 |
"Bad parameter (0x300C)", // No tr
|
|
382 |
"Bad surface (0x300D)", // No tr
|
|
383 |
"Context lost (0x300E)" // No tr
|
|
384 |
};
|
|
385 |
if (code >= 0x3000 && code <= 0x300E) {
|
|
386 |
return QString::fromLatin1(errors[code - 0x3000]);
|
|
387 |
} else {
|
|
388 |
return QLatin1String("0x") + QString::number(int(code), 16);
|
|
389 |
}
|
|
390 |
}
|
|
391 |
|
|
392 |
// Dump all of the EGL configurations supported by the system.
|
|
393 |
void QEglContext::dumpAllConfigs()
|
|
394 |
{
|
|
395 |
QEglProperties props;
|
|
396 |
EGLint count = 0;
|
|
397 |
if (!eglGetConfigs(dpy, 0, 0, &count) || count < 1)
|
|
398 |
return;
|
|
399 |
EGLConfig *configs = new EGLConfig [count];
|
|
400 |
eglGetConfigs(dpy, configs, count, &count);
|
|
401 |
for (EGLint index = 0; index < count; ++index) {
|
|
402 |
props = configProperties(configs[index]);
|
|
403 |
qWarning() << props.toString();
|
|
404 |
}
|
|
405 |
delete [] configs;
|
|
406 |
}
|
|
407 |
|
|
408 |
QString QEglContext::extensions()
|
|
409 |
{
|
|
410 |
const char* exts = eglQueryString(QEglContext::defaultDisplay(0), EGL_EXTENSIONS);
|
|
411 |
return QString(QLatin1String(exts));
|
|
412 |
}
|
|
413 |
|
|
414 |
bool QEglContext::hasExtension(const char* extensionName)
|
|
415 |
{
|
|
416 |
return extensions().contains(QLatin1String(extensionName));
|
|
417 |
}
|
|
418 |
|
|
419 |
QEglContext *QEglContext::currentContext(QEgl::API api)
|
|
420 |
{
|
|
421 |
if (api == QEgl::OpenGL)
|
|
422 |
return currentGLContext;
|
|
423 |
else
|
|
424 |
return currentVGContext;
|
|
425 |
}
|
|
426 |
|
|
427 |
void QEglContext::setCurrentContext(QEgl::API api, QEglContext *context)
|
|
428 |
{
|
|
429 |
if (api == QEgl::OpenGL)
|
|
430 |
currentGLContext = context;
|
|
431 |
else
|
|
432 |
currentVGContext = context;
|
|
433 |
}
|
|
434 |
|
|
435 |
QT_END_NAMESPACE
|