| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 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 | #if defined(QT_OPENGL_ES) || defined(QT_OPENVG)
|
|---|
| 49 |
|
|---|
| 50 | QT_BEGIN_NAMESPACE
|
|---|
| 51 |
|
|---|
| 52 | QEglContext::QEglContext()
|
|---|
| 53 | {
|
|---|
| 54 | apiType = OpenGL;
|
|---|
| 55 | dpy = EGL_NO_DISPLAY;
|
|---|
| 56 | ctx = EGL_NO_CONTEXT;
|
|---|
| 57 | surf = EGL_NO_SURFACE;
|
|---|
| 58 | cfg = 0;
|
|---|
| 59 | share = false;
|
|---|
| 60 | reserved = 0;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | QEglContext::~QEglContext()
|
|---|
| 64 | {
|
|---|
| 65 | destroy();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | bool QEglContext::isValid() const
|
|---|
| 69 | {
|
|---|
| 70 | return (ctx != EGL_NO_CONTEXT);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | bool QEglContext::isSharing() const
|
|---|
| 74 | {
|
|---|
| 75 | return share;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | // Open the EGL display associated with "device".
|
|---|
| 79 | bool QEglContext::openDisplay(QPaintDevice *device)
|
|---|
| 80 | {
|
|---|
| 81 | if (dpy == EGL_NO_DISPLAY)
|
|---|
| 82 | dpy = defaultDisplay(device);
|
|---|
| 83 | return (dpy != EGL_NO_DISPLAY);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | // Choose a configuration that matches "properties".
|
|---|
| 87 | bool QEglContext::chooseConfig
|
|---|
| 88 | (const QEglProperties& properties, PixelFormatMatch match)
|
|---|
| 89 | {
|
|---|
| 90 | QEglProperties props(properties);
|
|---|
| 91 | EGLConfig *configs;
|
|---|
| 92 | EGLint matching, size;
|
|---|
| 93 | do {
|
|---|
| 94 | // Get the number of matching configurations for this set of properties.
|
|---|
| 95 | matching = 0;
|
|---|
| 96 | if (!eglChooseConfig(dpy, props.properties(), 0, 256, &matching) || !matching)
|
|---|
| 97 | continue;
|
|---|
| 98 |
|
|---|
| 99 | // If we want the best pixel format, then return the first
|
|---|
| 100 | // matching configuration.
|
|---|
| 101 | if (match == BestPixelFormat) {
|
|---|
| 102 | eglChooseConfig(dpy, props.properties(), &cfg, 1, &matching);
|
|---|
| 103 | if (matching < 1)
|
|---|
| 104 | continue;
|
|---|
| 105 | return true;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | // Fetch all of the matching configurations and find the
|
|---|
| 109 | // first that matches the pixel format we wanted.
|
|---|
| 110 | size = matching;
|
|---|
| 111 | configs = new EGLConfig [size];
|
|---|
| 112 | eglChooseConfig(dpy, props.properties(), configs, size, &matching);
|
|---|
| 113 | for (EGLint index = 0; index < size; ++index) {
|
|---|
| 114 | EGLint red, green, blue, alpha;
|
|---|
| 115 | eglGetConfigAttrib(dpy, configs[index], EGL_RED_SIZE, &red);
|
|---|
| 116 | eglGetConfigAttrib(dpy, configs[index], EGL_GREEN_SIZE, &green);
|
|---|
| 117 | eglGetConfigAttrib(dpy, configs[index], EGL_BLUE_SIZE, &blue);
|
|---|
| 118 | eglGetConfigAttrib(dpy, configs[index], EGL_ALPHA_SIZE, &alpha);
|
|---|
| 119 | if (red == props.value(EGL_RED_SIZE) &&
|
|---|
| 120 | green == props.value(EGL_GREEN_SIZE) &&
|
|---|
| 121 | blue == props.value(EGL_BLUE_SIZE) &&
|
|---|
| 122 | (props.value(EGL_ALPHA_SIZE) == EGL_DONT_CARE ||
|
|---|
| 123 | alpha == props.value(EGL_ALPHA_SIZE))) {
|
|---|
| 124 | cfg = configs[index];
|
|---|
| 125 | delete [] configs;
|
|---|
| 126 | return true;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | delete [] configs;
|
|---|
| 130 | } while (props.reduceConfiguration());
|
|---|
| 131 |
|
|---|
| 132 | #ifdef EGL_BIND_TO_TEXTURE_RGBA
|
|---|
| 133 | // Don't report an error just yet if we failed to get a pbuffer
|
|---|
| 134 | // configuration with texture rendering. Only report failure if
|
|---|
| 135 | // we cannot get any pbuffer configurations at all.
|
|---|
| 136 | if (props.value(EGL_BIND_TO_TEXTURE_RGBA) == EGL_DONT_CARE &&
|
|---|
| 137 | props.value(EGL_BIND_TO_TEXTURE_RGB) == EGL_DONT_CARE)
|
|---|
| 138 | #endif
|
|---|
| 139 | {
|
|---|
| 140 | qWarning() << "QEglContext::chooseConfig(): Could not find a suitable EGL configuration";
|
|---|
| 141 | qWarning() << "Requested:" << props.toString();
|
|---|
| 142 | qWarning() << "Available:";
|
|---|
| 143 | dumpAllConfigs();
|
|---|
| 144 | }
|
|---|
| 145 | return false;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | // Create the EGLContext.
|
|---|
| 149 | bool QEglContext::createContext(QEglContext *shareContext)
|
|---|
| 150 | {
|
|---|
| 151 | // We need to select the correct API before calling eglCreateContext().
|
|---|
| 152 | #ifdef EGL_OPENGL_ES_API
|
|---|
| 153 | if (apiType == OpenGL)
|
|---|
| 154 | eglBindAPI(EGL_OPENGL_ES_API);
|
|---|
| 155 | #endif
|
|---|
| 156 | #ifdef EGL_OPENVG_API
|
|---|
| 157 | if (apiType == OpenVG)
|
|---|
| 158 | eglBindAPI(EGL_OPENVG_API);
|
|---|
| 159 | #endif
|
|---|
| 160 |
|
|---|
| 161 | // Create a new context for the configuration.
|
|---|
| 162 | QEglProperties contextProps;
|
|---|
| 163 | #if defined(QT_OPENGL_ES_2)
|
|---|
| 164 | if (apiType == OpenGL)
|
|---|
| 165 | contextProps.setValue(EGL_CONTEXT_CLIENT_VERSION, 2);
|
|---|
| 166 | #endif
|
|---|
| 167 | if (shareContext && shareContext->ctx == EGL_NO_CONTEXT)
|
|---|
| 168 | shareContext = 0;
|
|---|
| 169 | if (shareContext) {
|
|---|
| 170 | ctx = eglCreateContext(dpy, cfg, shareContext->ctx, contextProps.properties());
|
|---|
| 171 | if (ctx == EGL_NO_CONTEXT) {
|
|---|
| 172 | qWarning() << "QEglContext::createContext(): Could not share context:" << errorString(eglGetError());
|
|---|
| 173 | shareContext = 0;
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | if (ctx == EGL_NO_CONTEXT) {
|
|---|
| 177 | ctx = eglCreateContext(dpy, cfg, 0, contextProps.properties());
|
|---|
| 178 | if (ctx == EGL_NO_CONTEXT) {
|
|---|
| 179 | qWarning() << "QEglContext::createContext(): Unable to create EGL context:" << errorString(eglGetError());
|
|---|
| 180 | return false;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | share = (shareContext != 0);
|
|---|
| 184 | return true;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | // Recreate the surface for a paint device because the native id has changed.
|
|---|
| 188 | bool QEglContext::recreateSurface(QPaintDevice *device)
|
|---|
| 189 | {
|
|---|
| 190 | // Bail out if the surface has not been created for the first time yet.
|
|---|
| 191 | if (surf == EGL_NO_SURFACE)
|
|---|
| 192 | return true;
|
|---|
| 193 |
|
|---|
| 194 | // Destroy the old surface.
|
|---|
| 195 | eglDestroySurface(dpy, surf);
|
|---|
| 196 |
|
|---|
| 197 | // Create a new one.
|
|---|
| 198 | return createSurface(device);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | void QEglContext::destroy()
|
|---|
| 202 | {
|
|---|
| 203 | if (ctx != EGL_NO_CONTEXT)
|
|---|
| 204 | eglDestroyContext(dpy, ctx);
|
|---|
| 205 | dpy = EGL_NO_DISPLAY;
|
|---|
| 206 | ctx = EGL_NO_CONTEXT;
|
|---|
| 207 | surf = EGL_NO_SURFACE;
|
|---|
| 208 | cfg = 0;
|
|---|
| 209 | share = false;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | bool QEglContext::makeCurrent()
|
|---|
| 213 | {
|
|---|
| 214 | if(ctx == EGL_NO_CONTEXT) {
|
|---|
| 215 | qWarning() << "QEglContext::makeCurrent(): Cannot make invalid context current";
|
|---|
| 216 | return false;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | bool ok = eglMakeCurrent(dpy, surf, surf, ctx);
|
|---|
| 220 | if (!ok) {
|
|---|
| 221 | EGLint err = eglGetError();
|
|---|
| 222 | qWarning() << "QEglContext::makeCurrent():" << errorString(err);
|
|---|
| 223 | }
|
|---|
| 224 | return ok;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | bool QEglContext::doneCurrent()
|
|---|
| 228 | {
|
|---|
| 229 | // If the context is invalid, we assume that an error was reported
|
|---|
| 230 | // when makeCurrent() was called.
|
|---|
| 231 | if (ctx == EGL_NO_CONTEXT)
|
|---|
| 232 | return false;
|
|---|
| 233 |
|
|---|
| 234 | // We need to select the correct API before calling eglMakeCurrent()
|
|---|
| 235 | // with EGL_NO_CONTEXT because threads can have both OpenGL and OpenVG
|
|---|
| 236 | // contexts active at the same time.
|
|---|
| 237 | #ifdef EGL_OPENGL_ES_API
|
|---|
| 238 | if (apiType == OpenGL)
|
|---|
| 239 | eglBindAPI(EGL_OPENGL_ES_API);
|
|---|
| 240 | #endif
|
|---|
| 241 | #ifdef EGL_OPENVG_API
|
|---|
| 242 | if (apiType == OpenVG)
|
|---|
| 243 | eglBindAPI(EGL_OPENVG_API);
|
|---|
| 244 | #endif
|
|---|
| 245 |
|
|---|
| 246 | bool ok = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|---|
| 247 | if (!ok) {
|
|---|
| 248 | EGLint err = eglGetError();
|
|---|
| 249 | qWarning() << "QEglContext::doneCurrent():" << errorString(err);
|
|---|
| 250 | }
|
|---|
| 251 | return ok;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | bool QEglContext::swapBuffers()
|
|---|
| 255 | {
|
|---|
| 256 | if(ctx == EGL_NO_CONTEXT)
|
|---|
| 257 | return false;
|
|---|
| 258 |
|
|---|
| 259 | bool ok = eglSwapBuffers(dpy, surf);
|
|---|
| 260 | if (!ok) {
|
|---|
| 261 | EGLint err = eglGetError();
|
|---|
| 262 | qWarning() << "QEglContext::swapBuffers():" << errorString(err);
|
|---|
| 263 | }
|
|---|
| 264 | return ok;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | // Wait for native rendering operations to complete before starting
|
|---|
| 268 | // to use OpenGL/OpenVG operations.
|
|---|
| 269 | void QEglContext::waitNative()
|
|---|
| 270 | {
|
|---|
| 271 | #ifdef EGL_CORE_NATIVE_ENGINE
|
|---|
| 272 | eglWaitNative(EGL_CORE_NATIVE_ENGINE);
|
|---|
| 273 | #endif
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | // Wait for client OpenGL/OpenVG operations to complete before
|
|---|
| 277 | // using native rendering operations.
|
|---|
| 278 | void QEglContext::waitClient()
|
|---|
| 279 | {
|
|---|
| 280 | #ifdef EGL_OPENGL_ES_API
|
|---|
| 281 | if (apiType == OpenGL) {
|
|---|
| 282 | eglBindAPI(EGL_OPENGL_ES_API);
|
|---|
| 283 | eglWaitClient();
|
|---|
| 284 | }
|
|---|
| 285 | #else
|
|---|
| 286 | if (apiType == OpenGL)
|
|---|
| 287 | eglWaitGL();
|
|---|
| 288 | #endif
|
|---|
| 289 | #ifdef EGL_OPENVG_API
|
|---|
| 290 | if (apiType == OpenVG) {
|
|---|
| 291 | eglBindAPI(EGL_OPENVG_API);
|
|---|
| 292 | eglWaitClient();
|
|---|
| 293 | }
|
|---|
| 294 | #endif
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | // Query the actual size of the EGL surface.
|
|---|
| 298 | QSize QEglContext::surfaceSize() const
|
|---|
| 299 | {
|
|---|
| 300 | int w, h;
|
|---|
| 301 | eglQuerySurface(dpy, surf, EGL_WIDTH, &w);
|
|---|
| 302 | eglQuerySurface(dpy, surf, EGL_HEIGHT, &h);
|
|---|
| 303 | return QSize(w, h);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | // Query the value of a configuration attribute.
|
|---|
| 307 | bool QEglContext::configAttrib(int name, EGLint *value) const
|
|---|
| 308 | {
|
|---|
| 309 | return eglGetConfigAttrib(dpy, cfg, name, value);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | // Retrieve all of the properties on "cfg". If zero, return
|
|---|
| 313 | // the context's configuration.
|
|---|
| 314 | QEglProperties QEglContext::configProperties(EGLConfig cfg) const
|
|---|
| 315 | {
|
|---|
| 316 | if (!cfg)
|
|---|
| 317 | cfg = config();
|
|---|
| 318 | QEglProperties props;
|
|---|
| 319 | for (int name = 0x3020; name <= 0x304F; ++name) {
|
|---|
| 320 | EGLint value;
|
|---|
| 321 | if (name != EGL_NONE && eglGetConfigAttrib(dpy, cfg, name, &value))
|
|---|
| 322 | props.setValue(name, value);
|
|---|
| 323 | }
|
|---|
| 324 | eglGetError(); // Clear the error state.
|
|---|
| 325 | return props;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | // Initialize and return the default display.
|
|---|
| 329 | EGLDisplay QEglContext::defaultDisplay(QPaintDevice *device)
|
|---|
| 330 | {
|
|---|
| 331 | static EGLDisplay dpy = EGL_NO_DISPLAY;
|
|---|
| 332 | if (dpy == EGL_NO_DISPLAY) {
|
|---|
| 333 | dpy = getDisplay(device);
|
|---|
| 334 | if (dpy == EGL_NO_DISPLAY) {
|
|---|
| 335 | qWarning() << "QEglContext::defaultDisplay(): Cannot open EGL display";
|
|---|
| 336 | return EGL_NO_DISPLAY;
|
|---|
| 337 | }
|
|---|
| 338 | if (!eglInitialize(dpy, NULL, NULL)) {
|
|---|
| 339 | EGLint err = eglGetError();
|
|---|
| 340 | qWarning() << "QEglContext::defaultDisplay(): Cannot initialize EGL display:" << errorString(err);
|
|---|
| 341 | return EGL_NO_DISPLAY;
|
|---|
| 342 | }
|
|---|
| 343 | #ifdef EGL_OPENGL_ES_API
|
|---|
| 344 | eglBindAPI(EGL_OPENGL_ES_API);
|
|---|
| 345 | #endif
|
|---|
| 346 | }
|
|---|
| 347 | return dpy;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | // Return the error string associated with a specific code.
|
|---|
| 351 | QString QEglContext::errorString(int code)
|
|---|
| 352 | {
|
|---|
| 353 | static const char * const errors[] = {
|
|---|
| 354 | "Success (0x3000)", // No tr
|
|---|
| 355 | "Not initialized (0x3001)", // No tr
|
|---|
| 356 | "Bad access (0x3002)", // No tr
|
|---|
| 357 | "Bad alloc (0x3003)", // No tr
|
|---|
| 358 | "Bad attribute (0x3004)", // No tr
|
|---|
| 359 | "Bad config (0x3005)", // No tr
|
|---|
| 360 | "Bad context (0x3006)", // No tr
|
|---|
| 361 | "Bad current surface (0x3007)", // No tr
|
|---|
| 362 | "Bad display (0x3008)", // No tr
|
|---|
| 363 | "Bad match (0x3009)", // No tr
|
|---|
| 364 | "Bad native pixmap (0x300A)", // No tr
|
|---|
| 365 | "Bad native window (0x300B)", // No tr
|
|---|
| 366 | "Bad parameter (0x300C)", // No tr
|
|---|
| 367 | "Bad surface (0x300D)", // No tr
|
|---|
| 368 | "Context lost (0x300E)" // No tr
|
|---|
| 369 | };
|
|---|
| 370 | if (code >= 0x3000 && code <= 0x300E) {
|
|---|
| 371 | return QString::fromLatin1(errors[code - 0x3000]);
|
|---|
| 372 | } else {
|
|---|
| 373 | return QLatin1String("0x") + QString::number(code, 16);
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | // Dump all of the EGL configurations supported by the system.
|
|---|
| 378 | void QEglContext::dumpAllConfigs()
|
|---|
| 379 | {
|
|---|
| 380 | QEglProperties props;
|
|---|
| 381 | EGLint count = 0;
|
|---|
| 382 | if (!eglGetConfigs(dpy, 0, 0, &count))
|
|---|
| 383 | return;
|
|---|
| 384 | if (count < 1)
|
|---|
| 385 | return;
|
|---|
| 386 | EGLConfig *configs = new EGLConfig [count];
|
|---|
| 387 | eglGetConfigs(dpy, configs, count, &count);
|
|---|
| 388 | for (EGLint index = 0; index < count; ++index) {
|
|---|
| 389 | props = configProperties(configs[index]);
|
|---|
| 390 | qWarning() << props.toString();
|
|---|
| 391 | }
|
|---|
| 392 | delete [] configs;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | // Initialize a property block.
|
|---|
| 396 | QEglProperties::QEglProperties()
|
|---|
| 397 | {
|
|---|
| 398 | props.append(EGL_NONE);
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | // Fetch the current value associated with a property.
|
|---|
| 402 | int QEglProperties::value(int name) const
|
|---|
| 403 | {
|
|---|
| 404 | for (int index = 0; index < (props.size() - 1); index += 2) {
|
|---|
| 405 | if (props[index] == name)
|
|---|
| 406 | return props[index + 1];
|
|---|
| 407 | }
|
|---|
| 408 | return EGL_DONT_CARE;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | // Set the value associated with a property, replacing an existing
|
|---|
| 412 | // value if there is one.
|
|---|
| 413 | void QEglProperties::setValue(int name, int value)
|
|---|
| 414 | {
|
|---|
| 415 | for (int index = 0; index < (props.size() - 1); index += 2) {
|
|---|
| 416 | if (props[index] == name) {
|
|---|
| 417 | props[index + 1] = value;
|
|---|
| 418 | return;
|
|---|
| 419 | }
|
|---|
| 420 | }
|
|---|
| 421 | props[props.size() - 1] = name;
|
|---|
| 422 | props.append(value);
|
|---|
| 423 | props.append(EGL_NONE);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | // Remove a property value. Returns false if the property is not present.
|
|---|
| 427 | bool QEglProperties::removeValue(int name)
|
|---|
| 428 | {
|
|---|
| 429 | for (int index = 0; index < (props.size() - 1); index += 2) {
|
|---|
| 430 | if (props[index] == name) {
|
|---|
| 431 | while ((index + 2) < props.size()) {
|
|---|
| 432 | props[index] = props[index + 2];
|
|---|
| 433 | ++index;
|
|---|
| 434 | }
|
|---|
| 435 | props.resize(props.size() - 2);
|
|---|
| 436 | return true;
|
|---|
| 437 | }
|
|---|
| 438 | }
|
|---|
| 439 | return false;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | // Sets the red, green, blue, and alpha sizes based on a pixel format.
|
|---|
| 443 | // Normally used to match a configuration request to the screen format.
|
|---|
| 444 | void QEglProperties::setPixelFormat(QImage::Format pixelFormat)
|
|---|
| 445 | {
|
|---|
| 446 | int red, green, blue, alpha;
|
|---|
| 447 | switch (pixelFormat) {
|
|---|
| 448 | case QImage::Format_RGB32:
|
|---|
| 449 | case QImage::Format_RGB888:
|
|---|
| 450 | red = green = blue = 8; alpha = 0; break;
|
|---|
| 451 | case QImage::Format_ARGB32:
|
|---|
| 452 | case QImage::Format_ARGB32_Premultiplied:
|
|---|
| 453 | red = green = blue = alpha = 8; break;
|
|---|
| 454 | case QImage::Format_RGB16:
|
|---|
| 455 | red = 5; green = 6; blue = 5; alpha = 0; break;
|
|---|
| 456 | case QImage::Format_ARGB8565_Premultiplied:
|
|---|
| 457 | red = 5; green = 6; blue = 5; alpha = 8; break;
|
|---|
| 458 | case QImage::Format_RGB666:
|
|---|
| 459 | red = green = blue = 6; alpha = 0; break;
|
|---|
| 460 | case QImage::Format_ARGB6666_Premultiplied:
|
|---|
| 461 | red = green = blue = alpha = 6; break;
|
|---|
| 462 | case QImage::Format_RGB555:
|
|---|
| 463 | red = green = blue = 5; alpha = 0; break;
|
|---|
| 464 | case QImage::Format_ARGB8555_Premultiplied:
|
|---|
| 465 | red = green = blue = 5; alpha = 8; break;
|
|---|
| 466 | case QImage::Format_RGB444:
|
|---|
| 467 | red = green = blue = 4; alpha = 0; break;
|
|---|
| 468 | case QImage::Format_ARGB4444_Premultiplied:
|
|---|
| 469 | red = green = blue = alpha = 4; break;
|
|---|
| 470 | default:
|
|---|
| 471 | qWarning() << "QEglProperties::setPixelFormat(): Unsupported pixel format";
|
|---|
| 472 | red = green = blue = alpha = 1; break;
|
|---|
| 473 | }
|
|---|
| 474 | setValue(EGL_RED_SIZE, red);
|
|---|
| 475 | setValue(EGL_GREEN_SIZE, green);
|
|---|
| 476 | setValue(EGL_BLUE_SIZE, blue);
|
|---|
| 477 | setValue(EGL_ALPHA_SIZE, alpha);
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | void QEglProperties::setRenderableType(int api)
|
|---|
| 481 | {
|
|---|
| 482 | #if defined(EGL_RENDERABLE_TYPE)
|
|---|
| 483 | #if defined(QT_OPENGL_ES_2)
|
|---|
| 484 | if (api == QEglContext::OpenGL)
|
|---|
| 485 | setValue(EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT);
|
|---|
| 486 | #elif defined(QT_OPENGL_ES)
|
|---|
| 487 | if (api == QEglContext::OpenGL)
|
|---|
| 488 | setValue(EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT);
|
|---|
| 489 | #endif
|
|---|
| 490 | #if defined(EGL_OPENVG_BIT)
|
|---|
| 491 | if (api == QEglContext::OpenVG)
|
|---|
| 492 | setValue(EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT);
|
|---|
| 493 | #endif
|
|---|
| 494 | #else
|
|---|
| 495 | Q_UNUSED(api);
|
|---|
| 496 | #endif
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | // Reduce the complexity of a configuration request to ask for less
|
|---|
| 500 | // because the previous request did not result in success. Returns
|
|---|
| 501 | // true if the complexity was reduced, or false if no further
|
|---|
| 502 | // reductions in complexity are possible.
|
|---|
| 503 | bool QEglProperties::reduceConfiguration()
|
|---|
| 504 | {
|
|---|
| 505 | if (removeValue(EGL_SAMPLE_BUFFERS)) {
|
|---|
| 506 | removeValue(EGL_SAMPLES);
|
|---|
| 507 | return true;
|
|---|
| 508 | }
|
|---|
| 509 | if (removeValue(EGL_ALPHA_SIZE))
|
|---|
| 510 | return true;
|
|---|
| 511 | if (removeValue(EGL_STENCIL_SIZE))
|
|---|
| 512 | return true;
|
|---|
| 513 | if (removeValue(EGL_DEPTH_SIZE))
|
|---|
| 514 | return true;
|
|---|
| 515 | return false;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | static void addTag(QString& str, const QString& tag)
|
|---|
| 519 | {
|
|---|
| 520 | int lastnl = str.lastIndexOf(QLatin1String("\n"));
|
|---|
| 521 | if (lastnl == -1)
|
|---|
| 522 | lastnl = 0;
|
|---|
| 523 | if ((str.length() - lastnl) >= 50)
|
|---|
| 524 | str += QLatin1String("\n ");
|
|---|
| 525 | str += tag;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | // Convert a property list to a string suitable for debug output.
|
|---|
| 529 | QString QEglProperties::toString() const
|
|---|
| 530 | {
|
|---|
| 531 | QString str;
|
|---|
| 532 | int val;
|
|---|
| 533 |
|
|---|
| 534 | val = value(EGL_CONFIG_ID);
|
|---|
| 535 | if (val != EGL_DONT_CARE) {
|
|---|
| 536 | str += QLatin1String("id=");
|
|---|
| 537 | str += QString::number(val);
|
|---|
| 538 | str += QLatin1String(" ");
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | #ifdef EGL_RENDERABLE_TYPE
|
|---|
| 542 | val = value(EGL_RENDERABLE_TYPE);
|
|---|
| 543 | if (val != EGL_DONT_CARE) {
|
|---|
| 544 | str += QLatin1String("type=");
|
|---|
| 545 | QStringList types;
|
|---|
| 546 | if ((val & EGL_OPENGL_ES_BIT) != 0)
|
|---|
| 547 | types += QLatin1String("es1");
|
|---|
| 548 | #ifdef EGL_OPENGL_ES2_BIT
|
|---|
| 549 | if ((val & EGL_OPENGL_ES2_BIT) != 0)
|
|---|
| 550 | types += QLatin1String("es2");
|
|---|
| 551 | #endif
|
|---|
| 552 | if ((val & EGL_OPENVG_BIT) != 0)
|
|---|
| 553 | types += QLatin1String("vg");
|
|---|
| 554 | if ((val & ~7) != 0)
|
|---|
| 555 | types += QString::number(val);
|
|---|
| 556 | str += types.join(QLatin1String(","));
|
|---|
| 557 | } else {
|
|---|
| 558 | str += QLatin1String("type=any");
|
|---|
| 559 | }
|
|---|
| 560 | #else
|
|---|
| 561 | str += QLatin1String("type=es1");
|
|---|
| 562 | #endif
|
|---|
| 563 |
|
|---|
| 564 | int red = value(EGL_RED_SIZE);
|
|---|
| 565 | int green = value(EGL_GREEN_SIZE);
|
|---|
| 566 | int blue = value(EGL_BLUE_SIZE);
|
|---|
| 567 | int alpha = value(EGL_ALPHA_SIZE);
|
|---|
| 568 | int bufferSize = value(EGL_BUFFER_SIZE);
|
|---|
| 569 | if (bufferSize == (red + green + blue + alpha))
|
|---|
| 570 | bufferSize = EGL_DONT_CARE;
|
|---|
| 571 | str += QLatin1String(" rgba=");
|
|---|
| 572 | str += QString::number(red);
|
|---|
| 573 | str += QLatin1String(",");
|
|---|
| 574 | str += QString::number(green);
|
|---|
| 575 | str += QLatin1String(",");
|
|---|
| 576 | str += QString::number(blue);
|
|---|
| 577 | str += QLatin1String(",");
|
|---|
| 578 | str += QString::number(alpha);
|
|---|
| 579 | if (bufferSize != EGL_DONT_CARE) {
|
|---|
| 580 | // Only report buffer size if different than r+g+b+a.
|
|---|
| 581 | str += QLatin1String(" buffer-size=");
|
|---|
| 582 | str += QString::number(bufferSize);
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | #ifdef EGL_COLOR_BUFFER_TYPE
|
|---|
| 586 | val = value(EGL_COLOR_BUFFER_TYPE);
|
|---|
| 587 | if (val == EGL_LUMINANCE_BUFFER) {
|
|---|
| 588 | addTag(str, QLatin1String(" color-buffer-type=luminance"));
|
|---|
| 589 | } else if (val != EGL_DONT_CARE && val != EGL_RGB_BUFFER) {
|
|---|
| 590 | addTag(str, QLatin1String(" color-buffer-type="));
|
|---|
| 591 | str += QString::number(val, 16);
|
|---|
| 592 | }
|
|---|
| 593 | #endif
|
|---|
| 594 |
|
|---|
| 595 | val = value(EGL_DEPTH_SIZE);
|
|---|
| 596 | if (val != EGL_DONT_CARE) {
|
|---|
| 597 | addTag(str, QLatin1String(" depth="));
|
|---|
| 598 | str += QString::number(val);
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | val = value(EGL_STENCIL_SIZE);
|
|---|
| 602 | if (val != EGL_DONT_CARE) {
|
|---|
| 603 | addTag(str, QLatin1String(" stencil="));
|
|---|
| 604 | str += QString::number(val);
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | val = value(EGL_SURFACE_TYPE);
|
|---|
| 608 | if (val != EGL_DONT_CARE) {
|
|---|
| 609 | addTag(str, QLatin1String(" surface-type="));
|
|---|
| 610 | QStringList types;
|
|---|
| 611 | if ((val & EGL_WINDOW_BIT) != 0)
|
|---|
| 612 | types += QLatin1String("window");
|
|---|
| 613 | if ((val & EGL_PIXMAP_BIT) != 0)
|
|---|
| 614 | types += QLatin1String("pixmap");
|
|---|
| 615 | if ((val & EGL_PBUFFER_BIT) != 0)
|
|---|
| 616 | types += QLatin1String("pbuffer");
|
|---|
| 617 | #ifdef EGL_VG_COLORSPACE_LINEAR_BIT
|
|---|
| 618 | if ((val & EGL_VG_COLORSPACE_LINEAR_BIT) != 0)
|
|---|
| 619 | types += QLatin1String("vg-colorspace-linear");
|
|---|
| 620 | #endif
|
|---|
| 621 | #ifdef EGL_VG_ALPHA_FORMAT_PRE_BIT
|
|---|
| 622 | if ((val & EGL_VG_ALPHA_FORMAT_PRE_BIT) != 0)
|
|---|
| 623 | types += QLatin1String("vg-alpha-format-pre");
|
|---|
| 624 | #endif
|
|---|
| 625 | if ((val & ~(EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT
|
|---|
| 626 | #ifdef EGL_VG_COLORSPACE_LINEAR_BIT
|
|---|
| 627 | | EGL_VG_COLORSPACE_LINEAR_BIT
|
|---|
| 628 | #endif
|
|---|
| 629 | #ifdef EGL_VG_ALPHA_FORMAT_PRE_BIT
|
|---|
| 630 | | EGL_VG_ALPHA_FORMAT_PRE_BIT
|
|---|
| 631 | #endif
|
|---|
| 632 | )) != 0) {
|
|---|
| 633 | types += QString::number(val);
|
|---|
| 634 | }
|
|---|
| 635 | str += types.join(QLatin1String(","));
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | val = value(EGL_CONFIG_CAVEAT);
|
|---|
| 639 | if (val != EGL_DONT_CARE) {
|
|---|
| 640 | addTag(str, QLatin1String(" caveat="));
|
|---|
| 641 | if (val == EGL_NONE)
|
|---|
| 642 | str += QLatin1String("none");
|
|---|
| 643 | else if (val == EGL_SLOW_CONFIG)
|
|---|
| 644 | str += QLatin1String("slow");
|
|---|
| 645 | else if (val == EGL_NON_CONFORMANT_CONFIG)
|
|---|
| 646 | str += QLatin1String("non-conformant");
|
|---|
| 647 | else
|
|---|
| 648 | str += QString::number(val, 16);
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | val = value(EGL_LEVEL);
|
|---|
| 652 | if (val != EGL_DONT_CARE) {
|
|---|
| 653 | addTag(str, QLatin1String(" level="));
|
|---|
| 654 | str += QString::number(val);
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | int width, height, pixels;
|
|---|
| 658 | width = value(EGL_MAX_PBUFFER_WIDTH);
|
|---|
| 659 | height = value(EGL_MAX_PBUFFER_HEIGHT);
|
|---|
| 660 | pixels = value(EGL_MAX_PBUFFER_PIXELS);
|
|---|
| 661 | if (height != EGL_DONT_CARE || width != EGL_DONT_CARE) {
|
|---|
| 662 | addTag(str, QLatin1String(" max-pbuffer-size="));
|
|---|
| 663 | str += QString::number(width);
|
|---|
| 664 | str += QLatin1String("x");
|
|---|
| 665 | str += QString::number(height);
|
|---|
| 666 | if (pixels != (width * height)) {
|
|---|
| 667 | addTag(str, QLatin1String(" max-pbuffer-pixels="));
|
|---|
| 668 | str += QString::number(pixels);
|
|---|
| 669 | }
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | val = value(EGL_NATIVE_RENDERABLE);
|
|---|
| 673 | if (val != EGL_DONT_CARE) {
|
|---|
| 674 | if (val)
|
|---|
| 675 | addTag(str, QLatin1String(" native-renderable=true"));
|
|---|
| 676 | else
|
|---|
| 677 | addTag(str, QLatin1String(" native-renderable=false"));
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | val = value(EGL_NATIVE_VISUAL_ID);
|
|---|
| 681 | if (val != EGL_DONT_CARE) {
|
|---|
| 682 | addTag(str, QLatin1String(" visual-id="));
|
|---|
| 683 | str += QString::number(val);
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | val = value(EGL_NATIVE_VISUAL_TYPE);
|
|---|
| 687 | if (val != EGL_DONT_CARE) {
|
|---|
| 688 | addTag(str, QLatin1String(" visual-type="));
|
|---|
| 689 | str += QString::number(val);
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | #ifdef EGL_PRESERVED_RESOURCES
|
|---|
| 693 | val = value(EGL_PRESERVED_RESOURCES);
|
|---|
| 694 | if (val != EGL_DONT_CARE) {
|
|---|
| 695 | if (val)
|
|---|
| 696 | addTag(str, QLatin1String(" preserved-resources=true"));
|
|---|
| 697 | else
|
|---|
| 698 | addTag(str, QLatin1String(" preserved-resources=false"));
|
|---|
| 699 | }
|
|---|
| 700 | #endif
|
|---|
| 701 |
|
|---|
| 702 | val = value(EGL_SAMPLES);
|
|---|
| 703 | if (val != EGL_DONT_CARE) {
|
|---|
| 704 | addTag(str, QLatin1String(" samples="));
|
|---|
| 705 | str += QString::number(val);
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | val = value(EGL_SAMPLE_BUFFERS);
|
|---|
| 709 | if (val != EGL_DONT_CARE) {
|
|---|
| 710 | addTag(str, QLatin1String(" sample-buffers="));
|
|---|
| 711 | str += QString::number(val);
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | val = value(EGL_TRANSPARENT_TYPE);
|
|---|
| 715 | if (val == EGL_TRANSPARENT_RGB) {
|
|---|
| 716 | addTag(str, QLatin1String(" transparent-rgb="));
|
|---|
| 717 | str += QString::number(value(EGL_TRANSPARENT_RED_VALUE));
|
|---|
| 718 | str += QLatin1String(",");
|
|---|
| 719 | str += QString::number(value(EGL_TRANSPARENT_GREEN_VALUE));
|
|---|
| 720 | str += QLatin1String(",");
|
|---|
| 721 | str += QString::number(value(EGL_TRANSPARENT_BLUE_VALUE));
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | #if defined(EGL_BIND_TO_TEXTURE_RGB) && defined(EGL_BIND_TO_TEXTURE_RGBA)
|
|---|
| 725 | val = value(EGL_BIND_TO_TEXTURE_RGB);
|
|---|
| 726 | int val2 = value(EGL_BIND_TO_TEXTURE_RGBA);
|
|---|
| 727 | if (val != EGL_DONT_CARE || val2 != EGL_DONT_CARE) {
|
|---|
| 728 | addTag(str, QLatin1String(" bind-texture="));
|
|---|
| 729 | if (val == EGL_TRUE)
|
|---|
| 730 | str += QLatin1String("rgb");
|
|---|
| 731 | else
|
|---|
| 732 | str += QLatin1String("no-rgb");
|
|---|
| 733 | if (val2 == EGL_TRUE)
|
|---|
| 734 | str += QLatin1String(",rgba");
|
|---|
| 735 | else
|
|---|
| 736 | str += QLatin1String(",no-rgba");
|
|---|
| 737 | }
|
|---|
| 738 | #endif
|
|---|
| 739 |
|
|---|
| 740 | #ifdef EGL_MIN_SWAP_INTERVAL
|
|---|
| 741 | val = value(EGL_MIN_SWAP_INTERVAL);
|
|---|
| 742 | if (val != EGL_DONT_CARE) {
|
|---|
| 743 | addTag(str, QLatin1String(" min-swap-interval="));
|
|---|
| 744 | str += QString::number(val);
|
|---|
| 745 | }
|
|---|
| 746 | #endif
|
|---|
| 747 |
|
|---|
| 748 | #ifdef EGL_MIN_SWAP_INTERVAL
|
|---|
| 749 | val = value(EGL_MAX_SWAP_INTERVAL);
|
|---|
| 750 | if (val != EGL_DONT_CARE) {
|
|---|
| 751 | addTag(str, QLatin1String(" max-swap-interval="));
|
|---|
| 752 | str += QString::number(val);
|
|---|
| 753 | }
|
|---|
| 754 | #endif
|
|---|
| 755 |
|
|---|
| 756 | #ifdef EGL_LUMINANCE_SIZE
|
|---|
| 757 | val = value(EGL_LUMINANCE_SIZE);
|
|---|
| 758 | if (val != EGL_DONT_CARE) {
|
|---|
| 759 | addTag(str, QLatin1String(" luminance="));
|
|---|
| 760 | str += QString::number(val);
|
|---|
| 761 | }
|
|---|
| 762 | #endif
|
|---|
| 763 |
|
|---|
| 764 | #ifdef EGL_ALPHA_MASK_SIZE
|
|---|
| 765 | val = value(EGL_ALPHA_MASK_SIZE);
|
|---|
| 766 | if (val != EGL_DONT_CARE) {
|
|---|
| 767 | addTag(str, QLatin1String(" alpha-mask="));
|
|---|
| 768 | str += QString::number(val);
|
|---|
| 769 | }
|
|---|
| 770 | #endif
|
|---|
| 771 |
|
|---|
| 772 | #ifdef EGL_CONFORMANT
|
|---|
| 773 | val = value(EGL_CONFORMANT);
|
|---|
| 774 | if (val != EGL_DONT_CARE) {
|
|---|
| 775 | if (val)
|
|---|
| 776 | addTag(str, QLatin1String(" conformant=true"));
|
|---|
| 777 | else
|
|---|
| 778 | addTag(str, QLatin1String(" conformant=false"));
|
|---|
| 779 | }
|
|---|
| 780 | #endif
|
|---|
| 781 |
|
|---|
| 782 | return str;
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | QT_END_NAMESPACE
|
|---|
| 786 |
|
|---|
| 787 | #endif // QT_OPENGL_ES || QT_OPENVG
|
|---|