source: trunk/src/gui/egl/qegl.cpp@ 605

Last change on this file since 605 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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