|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 QtOpenGL 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 "qgl.h" |
|
43 #include <private/qt_x11_p.h> |
|
44 #include <private/qpixmap_x11_p.h> |
|
45 #include <private/qimagepixmapcleanuphooks_p.h> |
|
46 #include <private/qgl_p.h> |
|
47 #include <private/qpaintengine_opengl_p.h> |
|
48 #include "qgl_egl_p.h" |
|
49 #include "qcolormap.h" |
|
50 #include <QDebug> |
|
51 |
|
52 |
|
53 QT_BEGIN_NAMESPACE |
|
54 |
|
55 |
|
56 bool qt_egl_setup_x11_visual(XVisualInfo &vi, EGLDisplay display, EGLConfig config, |
|
57 const QX11Info &x11Info, bool useArgbVisual); |
|
58 |
|
59 /* |
|
60 QGLTemporaryContext implementation |
|
61 */ |
|
62 |
|
63 class QGLTemporaryContextPrivate |
|
64 { |
|
65 public: |
|
66 bool initialized; |
|
67 Window window; |
|
68 EGLContext context; |
|
69 EGLSurface surface; |
|
70 EGLDisplay display; |
|
71 }; |
|
72 |
|
73 QGLTemporaryContext::QGLTemporaryContext(bool, QWidget *) |
|
74 : d(new QGLTemporaryContextPrivate) |
|
75 { |
|
76 d->initialized = false; |
|
77 d->window = 0; |
|
78 d->context = 0; |
|
79 d->surface = 0; |
|
80 int screen = 0; |
|
81 |
|
82 d->display = eglGetDisplay(EGLNativeDisplayType(X11->display)); |
|
83 |
|
84 if (!eglInitialize(d->display, NULL, NULL)) { |
|
85 qWarning("QGLTemporaryContext: Unable to initialize EGL display."); |
|
86 return; |
|
87 } |
|
88 |
|
89 EGLConfig config; |
|
90 int numConfigs = 0; |
|
91 EGLint attribs[] = { |
|
92 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
|
93 #ifdef QT_OPENGL_ES_2 |
|
94 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
|
95 #endif |
|
96 EGL_NONE |
|
97 }; |
|
98 |
|
99 eglChooseConfig(d->display, attribs, &config, 1, &numConfigs); |
|
100 if (!numConfigs) { |
|
101 qWarning("QGLTemporaryContext: No EGL configurations available."); |
|
102 return; |
|
103 } |
|
104 |
|
105 XVisualInfo visualInfo; |
|
106 XVisualInfo *vi; |
|
107 int numVisuals; |
|
108 EGLint id = 0; |
|
109 |
|
110 eglGetConfigAttrib(d->display, config, EGL_NATIVE_VISUAL_ID, &id); |
|
111 if (id == 0) { |
|
112 // EGL_NATIVE_VISUAL_ID is optional and might not be supported |
|
113 // on some implementations - we'll have to do it the hard way |
|
114 QX11Info xinfo; |
|
115 qt_egl_setup_x11_visual(visualInfo, d->display, config, xinfo, false); |
|
116 } else { |
|
117 visualInfo.visualid = id; |
|
118 } |
|
119 vi = XGetVisualInfo(X11->display, VisualIDMask, &visualInfo, &numVisuals); |
|
120 if (!vi || numVisuals < 1) { |
|
121 qWarning("QGLTemporaryContext: Unable to get X11 visual info id."); |
|
122 return; |
|
123 } |
|
124 |
|
125 d->window = XCreateWindow(X11->display, RootWindow(X11->display, screen), |
|
126 0, 0, 1, 1, 0, |
|
127 vi->depth, InputOutput, vi->visual, |
|
128 0, 0); |
|
129 |
|
130 d->surface = eglCreateWindowSurface(d->display, config, (EGLNativeWindowType) d->window, NULL); |
|
131 |
|
132 if (d->surface == EGL_NO_SURFACE) { |
|
133 qWarning("QGLTemporaryContext: Error creating EGL surface."); |
|
134 XFree(vi); |
|
135 XDestroyWindow(X11->display, d->window); |
|
136 return; |
|
137 } |
|
138 |
|
139 EGLint contextAttribs[] = { |
|
140 #ifdef QT_OPENGL_ES_2 |
|
141 EGL_CONTEXT_CLIENT_VERSION, 2, |
|
142 #endif |
|
143 EGL_NONE |
|
144 }; |
|
145 d->context = eglCreateContext(d->display, config, 0, contextAttribs); |
|
146 if (d->context != EGL_NO_CONTEXT |
|
147 && eglMakeCurrent(d->display, d->surface, d->surface, d->context)) |
|
148 { |
|
149 d->initialized = true; |
|
150 } else { |
|
151 qWarning("QGLTemporaryContext: Error creating EGL context."); |
|
152 eglDestroySurface(d->display, d->surface); |
|
153 XDestroyWindow(X11->display, d->window); |
|
154 } |
|
155 XFree(vi); |
|
156 } |
|
157 |
|
158 QGLTemporaryContext::~QGLTemporaryContext() |
|
159 { |
|
160 if (d->initialized) { |
|
161 eglMakeCurrent(d->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
|
162 eglDestroyContext(d->display, d->context); |
|
163 eglDestroySurface(d->display, d->surface); |
|
164 XDestroyWindow(X11->display, d->window); |
|
165 } |
|
166 } |
|
167 |
|
168 bool QGLFormat::hasOpenGLOverlays() |
|
169 { |
|
170 return false; |
|
171 } |
|
172 |
|
173 void qt_egl_add_platform_config(QEglProperties& props, QPaintDevice *device) |
|
174 { |
|
175 if (device->devType() == QInternal::Image) |
|
176 props.setPixelFormat(static_cast<QImage *>(device)->format()); |
|
177 } |
|
178 |
|
179 // Chooses the EGL config and creates the EGL context |
|
180 bool QGLContext::chooseContext(const QGLContext* shareContext) |
|
181 { |
|
182 Q_D(QGLContext); |
|
183 |
|
184 if (!device()) |
|
185 return false; |
|
186 |
|
187 int devType = device()->devType(); |
|
188 |
|
189 // Get the display and initialize it. |
|
190 if (d->eglContext == 0) { |
|
191 d->eglContext = new QEglContext(); |
|
192 d->eglContext->setApi(QEgl::OpenGL); |
|
193 |
|
194 // Construct the configuration we need for this surface. |
|
195 QEglProperties configProps; |
|
196 qt_egl_set_format(configProps, devType, d->glFormat); |
|
197 qt_egl_add_platform_config(configProps, device()); |
|
198 configProps.setRenderableType(QEgl::OpenGL); |
|
199 |
|
200 #if We_have_an_EGL_library_which_bothers_to_check_EGL_BUFFER_SIZE |
|
201 if (device()->depth() == 16 && configProps.value(EGL_ALPHA_SIZE) <= 0) { |
|
202 qDebug("Setting EGL_BUFFER_SIZE to 16"); |
|
203 configProps.setValue(EGL_BUFFER_SIZE, 16); |
|
204 configProps.setValue(EGL_ALPHA_SIZE, 0); |
|
205 } |
|
206 |
|
207 if (!d->eglContext->chooseConfig(configProps, QEgl::BestPixelFormat)) { |
|
208 delete d->eglContext; |
|
209 d->eglContext = 0; |
|
210 return false; |
|
211 } |
|
212 #else |
|
213 QEgl::PixelFormatMatch matchType = QEgl::BestPixelFormat; |
|
214 if ((device()->depth() == 16) && configProps.value(EGL_ALPHA_SIZE) == 0) { |
|
215 configProps.setValue(EGL_RED_SIZE, 5); |
|
216 configProps.setValue(EGL_GREEN_SIZE, 6); |
|
217 configProps.setValue(EGL_BLUE_SIZE, 5); |
|
218 configProps.setValue(EGL_ALPHA_SIZE, 0); |
|
219 matchType = QEgl::ExactPixelFormat; |
|
220 } |
|
221 |
|
222 // Search for a matching configuration, reducing the complexity |
|
223 // each time until we get something that matches. |
|
224 if (!d->eglContext->chooseConfig(configProps, matchType)) { |
|
225 delete d->eglContext; |
|
226 d->eglContext = 0; |
|
227 return false; |
|
228 } |
|
229 #endif |
|
230 |
|
231 // qDebug("QGLContext::chooseContext() - using EGL config %d:", d->eglContext->config()); |
|
232 // qDebug() << QEglProperties(d->eglContext->config()).toString(); |
|
233 |
|
234 // Create a new context for the configuration. |
|
235 if (!d->eglContext->createContext |
|
236 (shareContext ? shareContext->d_func()->eglContext : 0)) { |
|
237 delete d->eglContext; |
|
238 d->eglContext = 0; |
|
239 return false; |
|
240 } |
|
241 d->sharing = d->eglContext->isSharing(); |
|
242 if (d->sharing && shareContext) |
|
243 const_cast<QGLContext *>(shareContext)->d_func()->sharing = true; |
|
244 |
|
245 #if defined(EGL_VERSION_1_1) |
|
246 if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget) |
|
247 eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval()); |
|
248 #endif |
|
249 } |
|
250 |
|
251 // Inform the higher layers about the actual format properties. |
|
252 qt_egl_update_format(*(d->eglContext), d->glFormat); |
|
253 |
|
254 return true; |
|
255 } |
|
256 |
|
257 void QGLWidget::resizeEvent(QResizeEvent *) |
|
258 { |
|
259 Q_D(QGLWidget); |
|
260 if (!isValid()) |
|
261 return; |
|
262 makeCurrent(); |
|
263 if (!d->glcx->initialized()) |
|
264 glInit(); |
|
265 resizeGL(width(), height()); |
|
266 //handle overlay |
|
267 } |
|
268 |
|
269 const QGLContext* QGLWidget::overlayContext() const |
|
270 { |
|
271 return 0; |
|
272 } |
|
273 |
|
274 void QGLWidget::makeOverlayCurrent() |
|
275 { |
|
276 //handle overlay |
|
277 } |
|
278 |
|
279 void QGLWidget::updateOverlayGL() |
|
280 { |
|
281 //handle overlay |
|
282 } |
|
283 |
|
284 //#define QT_DEBUG_X11_VISUAL_SELECTION 1 |
|
285 |
|
286 bool qt_egl_setup_x11_visual(XVisualInfo &vi, EGLDisplay display, EGLConfig config, const QX11Info &x11Info, bool useArgbVisual) |
|
287 { |
|
288 bool foundVisualIsArgb = useArgbVisual; |
|
289 |
|
290 #ifdef QT_DEBUG_X11_VISUAL_SELECTION |
|
291 qDebug("qt_egl_setup_x11_visual() - useArgbVisual=%d", useArgbVisual); |
|
292 #endif |
|
293 |
|
294 memset(&vi, 0, sizeof(XVisualInfo)); |
|
295 |
|
296 EGLint eglConfigColorSize; |
|
297 eglGetConfigAttrib(display, config, EGL_BUFFER_SIZE, &eglConfigColorSize); |
|
298 |
|
299 // Check to see if EGL is suggesting an appropriate visual id: |
|
300 EGLint nativeVisualId; |
|
301 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &nativeVisualId); |
|
302 vi.visualid = nativeVisualId; |
|
303 |
|
304 if (vi.visualid) { |
|
305 // EGL has suggested a visual id, so get the rest of the visual info for that id: |
|
306 XVisualInfo *chosenVisualInfo; |
|
307 int matchingCount = 0; |
|
308 chosenVisualInfo = XGetVisualInfo(x11Info.display(), VisualIDMask, &vi, &matchingCount); |
|
309 if (chosenVisualInfo) { |
|
310 #if !defined(QT_NO_XRENDER) |
|
311 if (useArgbVisual) { |
|
312 // Check to make sure the visual provided by EGL is ARGB |
|
313 XRenderPictFormat *format; |
|
314 format = XRenderFindVisualFormat(x11Info.display(), chosenVisualInfo->visual); |
|
315 if (format->type == PictTypeDirect && format->direct.alphaMask) { |
|
316 #ifdef QT_DEBUG_X11_VISUAL_SELECTION |
|
317 qDebug("Using ARGB X Visual ID (%d) provided by EGL", (int)vi.visualid); |
|
318 #endif |
|
319 foundVisualIsArgb = true; |
|
320 vi = *chosenVisualInfo; |
|
321 } |
|
322 else { |
|
323 qWarning("Warning: EGL suggested using X visual ID %d for config %d, but this is not ARGB", |
|
324 nativeVisualId, (int)config); |
|
325 vi.visualid = 0; |
|
326 } |
|
327 } else |
|
328 #endif |
|
329 { |
|
330 if (eglConfigColorSize == chosenVisualInfo->depth) { |
|
331 #ifdef QT_DEBUG_X11_VISUAL_SELECTION |
|
332 qDebug("Using opaque X Visual ID (%d) provided by EGL", (int)vi.visualid); |
|
333 #endif |
|
334 vi = *chosenVisualInfo; |
|
335 } else |
|
336 qWarning("Warning: EGL suggested using X visual ID %d (%d bpp) for config %d (%d bpp), but the depths do not match!", |
|
337 nativeVisualId, chosenVisualInfo->depth, (int)config, eglConfigColorSize); |
|
338 } |
|
339 XFree(chosenVisualInfo); |
|
340 } |
|
341 else { |
|
342 qWarning("Warning: EGL suggested using X visual ID %d for config %d, but this seems to be invalid!", |
|
343 nativeVisualId, (int)config); |
|
344 vi.visualid = 0; |
|
345 } |
|
346 } |
|
347 |
|
348 // If EGL does not know the visual ID, so try to select an appropriate one ourselves, first |
|
349 // using XRender if we're supposed to have an alpha, then falling back to XGetVisualInfo |
|
350 |
|
351 #if !defined(QT_NO_XRENDER) |
|
352 if (vi.visualid == 0 && useArgbVisual) { |
|
353 // Try to use XRender to find an ARGB visual we can use |
|
354 vi.screen = x11Info.screen(); |
|
355 vi.depth = 32; //### We might at some point (soon) get ARGB4444 |
|
356 vi.c_class = TrueColor; |
|
357 XVisualInfo *matchingVisuals; |
|
358 int matchingCount = 0; |
|
359 matchingVisuals = XGetVisualInfo(x11Info.display(), |
|
360 VisualScreenMask|VisualDepthMask|VisualClassMask, |
|
361 &vi, &matchingCount); |
|
362 |
|
363 for (int i = 0; i < matchingCount; ++i) { |
|
364 XRenderPictFormat *format; |
|
365 format = XRenderFindVisualFormat(x11Info.display(), matchingVisuals[i].visual); |
|
366 if (format->type == PictTypeDirect && format->direct.alphaMask) { |
|
367 vi = matchingVisuals[i]; |
|
368 foundVisualIsArgb = true; |
|
369 #ifdef QT_DEBUG_X11_VISUAL_SELECTION |
|
370 qDebug("Using X Visual ID (%d) for ARGB visual as provided by XRender", (int)vi.visualid); |
|
371 #endif |
|
372 break; |
|
373 } |
|
374 } |
|
375 XFree(matchingVisuals); |
|
376 } |
|
377 #endif |
|
378 |
|
379 if (vi.visualid == 0) { |
|
380 EGLint depth; |
|
381 eglGetConfigAttrib(display, config, EGL_BUFFER_SIZE, &depth); |
|
382 int err; |
|
383 err = XMatchVisualInfo(x11Info.display(), x11Info.screen(), depth, TrueColor, &vi); |
|
384 if (err == 0) { |
|
385 qWarning("Warning: Can't find an X visual which matches the EGL config(%d)'s depth (%d)!", |
|
386 (int)config, depth); |
|
387 depth = x11Info.depth(); |
|
388 err = XMatchVisualInfo(x11Info.display(), x11Info.screen(), depth, TrueColor, &vi); |
|
389 if (err == 0) { |
|
390 qWarning("Error: Couldn't get any matching X visual!"); |
|
391 return false; |
|
392 } else |
|
393 qWarning(" - Falling back to X11 suggested depth (%d)", depth); |
|
394 } |
|
395 #ifdef QT_DEBUG_X11_VISUAL_SELECTION |
|
396 else |
|
397 qDebug("Using X Visual ID (%d) for EGL provided depth (%d)", (int)vi.visualid, depth); |
|
398 #endif |
|
399 |
|
400 // Don't try to use ARGB now unless the visual is 32-bit - even then it might stil fail :-( |
|
401 if (useArgbVisual) |
|
402 foundVisualIsArgb = vi.depth == 32; //### We might at some point (soon) get ARGB4444 |
|
403 } |
|
404 |
|
405 #ifdef QT_DEBUG_X11_VISUAL_SELECTION |
|
406 qDebug("Visual Info:"); |
|
407 qDebug(" bits_per_rgb=%d", vi.bits_per_rgb); |
|
408 qDebug(" red_mask=0x%x", vi.red_mask); |
|
409 qDebug(" green_mask=0x%x", vi.green_mask); |
|
410 qDebug(" blue_mask=0x%x", vi.blue_mask); |
|
411 qDebug(" colormap_size=%d", vi.colormap_size); |
|
412 qDebug(" c_class=%d", vi.c_class); |
|
413 qDebug(" depth=%d", vi.depth); |
|
414 qDebug(" screen=%d", vi.screen); |
|
415 qDebug(" visualid=%d", vi.visualid); |
|
416 #endif |
|
417 return foundVisualIsArgb; |
|
418 } |
|
419 |
|
420 void QGLWidget::setContext(QGLContext *context, const QGLContext* shareContext, bool deleteOldContext) |
|
421 { |
|
422 Q_D(QGLWidget); |
|
423 if (context == 0) { |
|
424 qWarning("QGLWidget::setContext: Cannot set null context"); |
|
425 return; |
|
426 } |
|
427 if (!context->deviceIsPixmap() && context->device() != this) { |
|
428 qWarning("QGLWidget::setContext: Context must refer to this widget"); |
|
429 return; |
|
430 } |
|
431 |
|
432 if (d->glcx) |
|
433 d->glcx->doneCurrent(); |
|
434 QGLContext* oldcx = d->glcx; |
|
435 d->glcx = context; |
|
436 |
|
437 if (parentWidget()) { |
|
438 // force creation of delay-created widgets |
|
439 parentWidget()->winId(); |
|
440 if (parentWidget()->x11Info().screen() != x11Info().screen()) |
|
441 d_func()->xinfo = parentWidget()->d_func()->xinfo; |
|
442 } |
|
443 |
|
444 // If the application has set WA_TranslucentBackground and not explicitly set |
|
445 // the alpha buffer size to zero, modify the format so it have an alpha channel |
|
446 QGLFormat& fmt = d->glcx->d_func()->glFormat; |
|
447 const bool tryArgbVisual = testAttribute(Qt::WA_TranslucentBackground) || fmt.alpha(); |
|
448 if (tryArgbVisual && fmt.alphaBufferSize() == -1) |
|
449 fmt.setAlphaBufferSize(1); |
|
450 |
|
451 bool createFailed = false; |
|
452 if (!d->glcx->isValid()) { |
|
453 // Create the QGLContext here, which in turn chooses the EGL config |
|
454 // and creates the EGL context: |
|
455 if (!d->glcx->create(shareContext ? shareContext : oldcx)) |
|
456 createFailed = true; |
|
457 } |
|
458 if (createFailed) { |
|
459 if (deleteOldContext) |
|
460 delete oldcx; |
|
461 return; |
|
462 } |
|
463 |
|
464 if (d->glcx->windowCreated() || d->glcx->deviceIsPixmap()) { |
|
465 if (deleteOldContext) |
|
466 delete oldcx; |
|
467 return; |
|
468 } |
|
469 |
|
470 bool visible = isVisible(); |
|
471 if (visible) |
|
472 hide(); |
|
473 |
|
474 XVisualInfo vi; |
|
475 QEglContext *eglContext = d->glcx->d_func()->eglContext; |
|
476 bool usingArgbVisual = qt_egl_setup_x11_visual(vi, eglContext->display(), eglContext->config(), |
|
477 x11Info(), tryArgbVisual); |
|
478 |
|
479 XSetWindowAttributes a; |
|
480 |
|
481 Window p = RootWindow(x11Info().display(), x11Info().screen()); |
|
482 if (parentWidget()) |
|
483 p = parentWidget()->winId(); |
|
484 |
|
485 QColormap colmap = QColormap::instance(vi.screen); |
|
486 a.background_pixel = colmap.pixel(palette().color(backgroundRole())); |
|
487 a.border_pixel = colmap.pixel(Qt::black); |
|
488 |
|
489 unsigned int valueMask = CWBackPixel|CWBorderPixel; |
|
490 if (usingArgbVisual) { |
|
491 a.colormap = XCreateColormap(x11Info().display(), p, vi.visual, AllocNone); |
|
492 valueMask |= CWColormap; |
|
493 } |
|
494 |
|
495 Window w = XCreateWindow(x11Info().display(), p, x(), y(), width(), height(), |
|
496 0, vi.depth, InputOutput, vi.visual, valueMask, &a); |
|
497 |
|
498 if (deleteOldContext) |
|
499 delete oldcx; |
|
500 oldcx = 0; |
|
501 |
|
502 create(w); // Create with the ID of the window we've just created |
|
503 |
|
504 |
|
505 // Create the EGL surface to draw into. |
|
506 QGLContextPrivate *ctxpriv = d->glcx->d_func(); |
|
507 ctxpriv->eglSurface = ctxpriv->eglContext->createSurface(this); |
|
508 if (ctxpriv->eglSurface == EGL_NO_SURFACE) { |
|
509 delete ctxpriv->eglContext; |
|
510 ctxpriv->eglContext = 0; |
|
511 return; |
|
512 } |
|
513 |
|
514 d->eglSurfaceWindowId = w; // Remember the window id we created the surface for |
|
515 |
|
516 if (visible) |
|
517 show(); |
|
518 |
|
519 XFlush(X11->display); |
|
520 d->glcx->setWindowCreated(true); |
|
521 } |
|
522 |
|
523 void QGLWidgetPrivate::init(QGLContext *context, const QGLWidget* shareWidget) |
|
524 { |
|
525 Q_Q(QGLWidget); |
|
526 |
|
527 initContext(context, shareWidget); |
|
528 |
|
529 if(q->isValid() && glcx->format().hasOverlay()) { |
|
530 //no overlay |
|
531 qWarning("QtOpenGL ES doesn't currently support overlays"); |
|
532 } |
|
533 } |
|
534 |
|
535 void QGLWidgetPrivate::cleanupColormaps() |
|
536 { |
|
537 } |
|
538 |
|
539 const QGLColormap & QGLWidget::colormap() const |
|
540 { |
|
541 return d_func()->cmap; |
|
542 } |
|
543 |
|
544 void QGLWidget::setColormap(const QGLColormap &) |
|
545 { |
|
546 } |
|
547 |
|
548 // Re-creates the EGL surface if the window ID has changed or if force is true |
|
549 void QGLWidgetPrivate::recreateEglSurface(bool force) |
|
550 { |
|
551 Q_Q(QGLWidget); |
|
552 |
|
553 Window currentId = q->winId(); |
|
554 |
|
555 if ( force || (currentId != eglSurfaceWindowId) ) { |
|
556 // The window id has changed so we need to re-create the EGL surface |
|
557 QEglContext *ctx = glcx->d_func()->eglContext; |
|
558 EGLSurface surface = glcx->d_func()->eglSurface; |
|
559 if (surface != EGL_NO_SURFACE) |
|
560 ctx->destroySurface(surface); // Will force doneCurrent() if nec. |
|
561 surface = ctx->createSurface(q); |
|
562 if (surface == EGL_NO_SURFACE) |
|
563 qWarning("Error creating EGL window surface: 0x%x", eglGetError()); |
|
564 glcx->d_func()->eglSurface = surface; |
|
565 |
|
566 eglSurfaceWindowId = currentId; |
|
567 } |
|
568 } |
|
569 |
|
570 // Selects which configs should be used |
|
571 EGLConfig Q_OPENGL_EXPORT qt_chooseEGLConfigForPixmap(bool hasAlpha, bool readOnly) |
|
572 { |
|
573 // Cache the configs we select as they wont change: |
|
574 static EGLConfig roPixmapRGBConfig = 0; |
|
575 static EGLConfig roPixmapRGBAConfig = 0; |
|
576 static EGLConfig rwPixmapRGBConfig = 0; |
|
577 static EGLConfig rwPixmapRGBAConfig = 0; |
|
578 |
|
579 EGLConfig* targetConfig; |
|
580 |
|
581 if (hasAlpha) { |
|
582 if (readOnly) |
|
583 targetConfig = &roPixmapRGBAConfig; |
|
584 else |
|
585 targetConfig = &rwPixmapRGBAConfig; |
|
586 } |
|
587 else { |
|
588 if (readOnly) |
|
589 targetConfig = &roPixmapRGBConfig; |
|
590 else |
|
591 targetConfig = &rwPixmapRGBConfig; |
|
592 } |
|
593 |
|
594 if (*targetConfig == 0) { |
|
595 QEglProperties configAttribs; |
|
596 configAttribs.setValue(EGL_SURFACE_TYPE, EGL_PIXMAP_BIT); |
|
597 configAttribs.setRenderableType(QEgl::OpenGL); |
|
598 if (hasAlpha) |
|
599 configAttribs.setValue(EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE); |
|
600 else |
|
601 configAttribs.setValue(EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE); |
|
602 |
|
603 // If this is going to be a render target, it needs to have a depth, stencil & sample buffer |
|
604 if (!readOnly) { |
|
605 configAttribs.setValue(EGL_DEPTH_SIZE, 1); |
|
606 configAttribs.setValue(EGL_STENCIL_SIZE, 1); |
|
607 configAttribs.setValue(EGL_SAMPLE_BUFFERS, 1); |
|
608 } |
|
609 |
|
610 EGLint configCount = 0; |
|
611 do { |
|
612 eglChooseConfig(QEglContext::display(), configAttribs.properties(), targetConfig, 1, &configCount); |
|
613 if (configCount > 0) { |
|
614 // Got one |
|
615 qDebug() << "Found an" << (hasAlpha ? "ARGB" : "RGB") << (readOnly ? "readonly" : "target" ) |
|
616 << "config (" << int(*targetConfig) << ") to create a pixmap surface:"; |
|
617 |
|
618 // QEglProperties configProps(*targetConfig); |
|
619 // qDebug() << configProps.toString(); |
|
620 break; |
|
621 } |
|
622 qWarning("choosePixmapConfig() - No suitible config found, reducing requirements"); |
|
623 } while (configAttribs.reduceConfiguration()); |
|
624 } |
|
625 |
|
626 if (*targetConfig == 0) |
|
627 qWarning("choosePixmapConfig() - Couldn't find a suitable config"); |
|
628 |
|
629 return *targetConfig; |
|
630 } |
|
631 |
|
632 bool Q_OPENGL_EXPORT qt_createEGLSurfaceForPixmap(QPixmapData* pmd, bool readOnly) |
|
633 { |
|
634 Q_ASSERT(pmd->classId() == QPixmapData::X11Class); |
|
635 QX11PixmapData* pixmapData = static_cast<QX11PixmapData*>(pmd); |
|
636 |
|
637 bool hasAlpha = pixmapData->hasAlphaChannel(); |
|
638 |
|
639 EGLConfig pixmapConfig = qt_chooseEGLConfigForPixmap(hasAlpha, readOnly); |
|
640 |
|
641 QEglProperties pixmapAttribs; |
|
642 |
|
643 // If the pixmap can't be bound to a texture, it's pretty useless |
|
644 pixmapAttribs.setValue(EGL_TEXTURE_TARGET, EGL_TEXTURE_2D); |
|
645 if (hasAlpha) |
|
646 pixmapAttribs.setValue(EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA); |
|
647 else |
|
648 pixmapAttribs.setValue(EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB); |
|
649 |
|
650 EGLSurface pixmapSurface; |
|
651 pixmapSurface = eglCreatePixmapSurface(QEglContext::display(), |
|
652 pixmapConfig, |
|
653 (EGLNativePixmapType) pixmapData->handle(), |
|
654 pixmapAttribs.properties()); |
|
655 // qDebug("qt_createEGLSurfaceForPixmap() created surface 0x%x for pixmap 0x%x", |
|
656 // pixmapSurface, pixmapData->handle()); |
|
657 if (pixmapSurface == EGL_NO_SURFACE) { |
|
658 qWarning() << "Failed to create a pixmap surface using config" << (int)pixmapConfig |
|
659 << ":" << QEglContext::errorString(eglGetError()); |
|
660 return false; |
|
661 } |
|
662 |
|
663 static bool doneOnce = false; |
|
664 if (!doneOnce) { |
|
665 // Make sure QGLTextureCache is instanciated so it can install cleanup hooks |
|
666 // which cleanup the EGL surface. |
|
667 QGLTextureCache::instance(); |
|
668 doneOnce = true; |
|
669 } |
|
670 |
|
671 Q_ASSERT(sizeof(Qt::HANDLE) >= sizeof(EGLSurface)); // Just to make totally sure! |
|
672 pixmapData->gl_surface = (Qt::HANDLE)pixmapSurface; |
|
673 QImagePixmapCleanupHooks::enableCleanupHooks(pixmapData); // Make sure the cleanup hook gets called |
|
674 |
|
675 return true; |
|
676 } |
|
677 |
|
678 |
|
679 QGLTexture *QGLContextPrivate::bindTextureFromNativePixmap(QPixmapData* pd, const qint64 key, |
|
680 QGLContext::BindOptions options) |
|
681 { |
|
682 Q_Q(QGLContext); |
|
683 |
|
684 // The EGL texture_from_pixmap has no facility to invert the y coordinate |
|
685 if (!(options & QGLContext::CanFlipNativePixmapBindOption)) |
|
686 return 0; |
|
687 |
|
688 Q_ASSERT(pd->classId() == QPixmapData::X11Class); |
|
689 |
|
690 static bool checkedForTFP = false; |
|
691 static bool haveTFP = false; |
|
692 |
|
693 if (!checkedForTFP) { |
|
694 // Check for texture_from_pixmap egl extension |
|
695 checkedForTFP = true; |
|
696 if (eglContext->hasExtension("EGL_NOKIA_texture_from_pixmap") || |
|
697 eglContext->hasExtension("EGL_EXT_texture_from_pixmap")) |
|
698 { |
|
699 qDebug("Found texture_from_pixmap EGL extension!"); |
|
700 haveTFP = true; |
|
701 } |
|
702 } |
|
703 |
|
704 if (!haveTFP) |
|
705 return 0; |
|
706 |
|
707 QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(pd); |
|
708 |
|
709 bool hasAlpha = pixmapData->hasAlphaChannel(); |
|
710 |
|
711 // Check to see if the surface is still valid |
|
712 if (pixmapData->gl_surface && |
|
713 hasAlpha != (pixmapData->flags & QX11PixmapData::GlSurfaceCreatedWithAlpha)) |
|
714 { |
|
715 // Surface is invalid! |
|
716 destroyGlSurfaceForPixmap(pixmapData); |
|
717 } |
|
718 |
|
719 if (pixmapData->gl_surface == 0) { |
|
720 bool success = qt_createEGLSurfaceForPixmap(pixmapData, true); |
|
721 if (!success) { |
|
722 haveTFP = false; |
|
723 return 0; |
|
724 } |
|
725 } |
|
726 |
|
727 Q_ASSERT(pixmapData->gl_surface); |
|
728 |
|
729 GLuint textureId; |
|
730 glGenTextures(1, &textureId); |
|
731 glBindTexture(GL_TEXTURE_2D, textureId); |
|
732 |
|
733 // bind the egl pixmap surface to a texture |
|
734 EGLBoolean success; |
|
735 success = eglBindTexImage(eglContext->display(), (EGLSurface)pixmapData->gl_surface, EGL_BACK_BUFFER); |
|
736 if (success == EGL_FALSE) { |
|
737 qWarning() << "eglBindTexImage() failed:" << eglContext->errorString(eglGetError()); |
|
738 eglDestroySurface(eglContext->display(), (EGLSurface)pixmapData->gl_surface); |
|
739 pixmapData->gl_surface = (Qt::HANDLE)EGL_NO_SURFACE; |
|
740 haveTFP = false; |
|
741 return 0; |
|
742 } |
|
743 |
|
744 QGLTexture *texture = new QGLTexture(q, textureId, GL_TEXTURE_2D, options); |
|
745 pixmapData->flags |= QX11PixmapData::InvertedWhenBoundToTexture; |
|
746 |
|
747 // We assume the cost of bound pixmaps is zero |
|
748 QGLTextureCache::instance()->insert(q, key, texture, 0); |
|
749 |
|
750 glBindTexture(GL_TEXTURE_2D, textureId); |
|
751 return texture; |
|
752 } |
|
753 |
|
754 void QGLContextPrivate::destroyGlSurfaceForPixmap(QPixmapData* pmd) |
|
755 { |
|
756 Q_ASSERT(pmd->classId() == QPixmapData::X11Class); |
|
757 QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(pmd); |
|
758 if (pixmapData->gl_surface) { |
|
759 EGLBoolean success; |
|
760 success = eglDestroySurface(QEglContext::display(), (EGLSurface)pixmapData->gl_surface); |
|
761 if (success == EGL_FALSE) { |
|
762 qWarning() << "destroyGlSurfaceForPixmap() - Error deleting surface: " |
|
763 << QEglContext::errorString(eglGetError()); |
|
764 } |
|
765 pixmapData->gl_surface = 0; |
|
766 } |
|
767 } |
|
768 |
|
769 void QGLContextPrivate::unbindPixmapFromTexture(QPixmapData* pmd) |
|
770 { |
|
771 Q_ASSERT(pmd->classId() == QPixmapData::X11Class); |
|
772 QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(pmd); |
|
773 if (pixmapData->gl_surface) { |
|
774 EGLBoolean success; |
|
775 success = eglReleaseTexImage(QEglContext::display(), |
|
776 (EGLSurface)pixmapData->gl_surface, |
|
777 EGL_BACK_BUFFER); |
|
778 if (success == EGL_FALSE) { |
|
779 qWarning() << "unbindPixmapFromTexture() - Unable to release bound texture: " |
|
780 << QEglContext::errorString(eglGetError()); |
|
781 } |
|
782 } |
|
783 } |
|
784 |
|
785 QT_END_NAMESPACE |