source: trunk/src/opengl/qpixmapdata_x11gl_egl.cpp@ 769

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

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 QtOpenGL 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 <QDebug>
43
44#include <private/qgl_p.h>
45#include <private/qegl_p.h>
46#include <private/qeglproperties_p.h>
47
48#if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
49#include <private/qpaintengineex_opengl2_p.h>
50#endif
51
52#ifndef QT_OPENGL_ES_2
53#include <private/qpaintengine_opengl_p.h>
54#endif
55
56#include "qpixmapdata_x11gl_p.h"
57
58QT_BEGIN_NAMESPACE
59
60extern EGLConfig qt_chooseEGLConfigForPixmap(bool hasAlpha, bool readOnly); // in qgl_x11egl.cpp
61extern bool qt_createEGLSurfaceForPixmap(QPixmapData* pmd, bool readOnly); // in qgl_x11egl.cpp
62
63// On 16bpp systems, RGB & ARGB pixmaps are different bit-depths and therefore need
64// different contexts:
65static EGLContext qPixmapARGBSharedEglContext = EGL_NO_CONTEXT;
66static EGLContext qPixmapRGBSharedEglContext = EGL_NO_CONTEXT;
67
68bool QX11GLPixmapData::hasX11GLPixmaps()
69{
70 static bool checkedForX11Pixmaps = false;
71 static bool haveX11Pixmaps = false;
72
73 if (checkedForX11Pixmaps)
74 return haveX11Pixmaps;
75
76 checkedForX11Pixmaps = true;
77
78 QX11PixmapData *argbPixmapData = 0;
79 QX11PixmapData *rgbPixmapData = 0;
80 do {
81 if (qgetenv("QT_USE_X11GL_PIXMAPS").isEmpty())
82 break;
83
84 // Check we actually have EGL configs which support pixmaps
85 EGLConfig argbConfig = qt_chooseEGLConfigForPixmap(true, false);
86 EGLConfig rgbConfig = qt_chooseEGLConfigForPixmap(false, false);
87
88 if (argbConfig == 0 || rgbConfig == 0)
89 break;
90
91 // Create the shared contexts:
92 eglBindAPI(EGL_OPENGL_ES_API);
93 EGLint contextAttribs[] = {
94#if defined(QT_OPENGL_ES_2)
95 EGL_CONTEXT_CLIENT_VERSION, 2,
96#endif
97 EGL_NONE
98 };
99 qPixmapARGBSharedEglContext = eglCreateContext(QEglContext::display(),
100 argbConfig, 0, contextAttribs);
101
102 if (argbConfig == rgbConfig) {
103 // If the configs are the same, we can re-use the same context.
104 qPixmapRGBSharedEglContext = qPixmapARGBSharedEglContext;
105 } else {
106 qPixmapRGBSharedEglContext = eglCreateContext(QEglContext::display(),
107 rgbConfig, 0, contextAttribs);
108 }
109
110 argbPixmapData = new QX11PixmapData(QPixmapData::PixmapType);
111 argbPixmapData->resize(100, 100);
112 argbPixmapData->fill(Qt::transparent); // Force ARGB
113
114 if (!qt_createEGLSurfaceForPixmap(argbPixmapData, false))
115 break;
116
117 haveX11Pixmaps = eglMakeCurrent(QEglContext::display(),
118 (EGLSurface)argbPixmapData->gl_surface,
119 (EGLSurface)argbPixmapData->gl_surface,
120 qPixmapARGBSharedEglContext);
121 if (!haveX11Pixmaps) {
122 EGLint err = eglGetError();
123 qWarning() << "Unable to make pixmap config current:" << err << QEglContext::errorString(err);
124 break;
125 }
126
127 // If the ARGB & RGB configs are the same, we don't need to check RGB too
128 if (haveX11Pixmaps && (argbConfig != rgbConfig)) {
129 rgbPixmapData = new QX11PixmapData(QPixmapData::PixmapType);
130 rgbPixmapData->resize(100, 100);
131 rgbPixmapData->fill(Qt::red);
132
133 // Try to actually create an EGL pixmap surface
134 if (!qt_createEGLSurfaceForPixmap(rgbPixmapData, false))
135 break;
136
137 haveX11Pixmaps = eglMakeCurrent(QEglContext::display(),
138 (EGLSurface)rgbPixmapData->gl_surface,
139 (EGLSurface)rgbPixmapData->gl_surface,
140 qPixmapRGBSharedEglContext);
141 if (!haveX11Pixmaps) {
142 EGLint err = eglGetError();
143 qWarning() << "Unable to make pixmap config current:" << err << QEglContext::errorString(err);
144 break;
145 }
146 }
147 } while (0);
148
149 if (qPixmapARGBSharedEglContext || qPixmapRGBSharedEglContext) {
150 eglMakeCurrent(QEglContext::display(),
151 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
152 }
153
154 if (argbPixmapData) {
155 if (argbPixmapData->gl_surface)
156 QGLContextPrivate::destroyGlSurfaceForPixmap(argbPixmapData);
157 delete argbPixmapData;
158 argbPixmapData = 0;
159 }
160 if (rgbPixmapData) {
161 if (rgbPixmapData->gl_surface)
162 QGLContextPrivate::destroyGlSurfaceForPixmap(rgbPixmapData);
163 delete rgbPixmapData;
164 rgbPixmapData = 0;
165 }
166
167 if (!haveX11Pixmaps) {
168 // Clean up the context(s) if we can't use X11GL pixmaps
169 if (qPixmapARGBSharedEglContext != EGL_NO_CONTEXT)
170 eglDestroyContext(QEglContext::display(), qPixmapARGBSharedEglContext);
171
172 if (qPixmapRGBSharedEglContext != qPixmapARGBSharedEglContext &&
173 qPixmapRGBSharedEglContext != EGL_NO_CONTEXT)
174 {
175 eglDestroyContext(QEglContext::display(), qPixmapRGBSharedEglContext);
176 }
177 qPixmapRGBSharedEglContext = EGL_NO_CONTEXT;
178 qPixmapARGBSharedEglContext = EGL_NO_CONTEXT;
179 }
180
181 if (haveX11Pixmaps)
182 qDebug("QX11GLPixmapData is supported");
183 else
184 qDebug("QX11GLPixmapData is *NOT* being used");
185
186 return haveX11Pixmaps;
187}
188
189QX11GLPixmapData::QX11GLPixmapData()
190 : QX11PixmapData(QPixmapData::PixmapType),
191 ctx(0)
192{
193}
194
195QX11GLPixmapData::~QX11GLPixmapData()
196{
197}
198
199#if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
200Q_GLOBAL_STATIC(QGL2PaintEngineEx, qt_gl_pixmap_2_engine)
201#endif
202
203#ifndef QT_OPENGL_ES_2
204Q_GLOBAL_STATIC(QOpenGLPaintEngine, qt_gl_pixmap_engine)
205#endif
206
207
208QPaintEngine* QX11GLPixmapData::paintEngine() const
209{
210 // We need to create the context before beginPaint - do it here:
211 if (!ctx) {
212 ctx = new QGLContext(glFormat());
213 if (ctx->d_func()->eglContext == 0)
214 ctx->d_func()->eglContext = new QEglContext();
215 ctx->d_func()->eglContext->setApi(QEgl::OpenGL);
216 ctx->d_func()->eglContext->setContext(hasAlphaChannel() ? qPixmapARGBSharedEglContext
217 : qPixmapRGBSharedEglContext);
218 }
219
220 QPaintEngine* engine;
221
222#if defined(QT_OPENGL_ES_1) || defined(QT_OPENGL_ES_1_CL)
223 engine = qt_gl_pixmap_engine();
224#elif defined(QT_OPENGL_ES_2)
225 engine = qt_gl_pixmap_2_engine();
226#else
227 if (qt_gl_preferGL2Engine())
228 engine = qt_gl_pixmap_2_engine();
229 else
230 engine = qt_gl_pixmap_engine();
231#endif
232
233
234
235 // Support multiple painters on multiple pixmaps simultaniously
236 if (engine->isActive()) {
237 qWarning("Pixmap paint engine already active");
238
239#if defined(QT_OPENGL_ES_1) || defined(QT_OPENGL_ES_1_CL)
240 engine = new QOpenGLPaintEngine;
241#elif defined(QT_OPENGL_ES_2)
242 engine = new QGL2PaintEngineEx;
243#else
244 if (qt_gl_preferGL2Engine())
245 engine = new QGL2PaintEngineEx;
246 else
247 engine = new QOpenGLPaintEngine;
248#endif
249
250 engine->setAutoDestruct(true);
251 return engine;
252 }
253
254 return engine;
255}
256
257void QX11GLPixmapData::beginPaint()
258{
259// qDebug("QX11GLPixmapData::beginPaint()");
260 if ((EGLSurface)gl_surface == EGL_NO_SURFACE) {
261 qt_createEGLSurfaceForPixmap(this, false);
262 ctx->d_func()->eglSurface = (EGLSurface)gl_surface;
263 ctx->d_func()->valid = true; // ;-)
264 }
265 QGLPaintDevice::beginPaint();
266}
267
268void QX11GLPixmapData::endPaint()
269{
270 glFinish();
271 QGLPaintDevice::endPaint();
272}
273
274QGLContext* QX11GLPixmapData::context() const
275{
276 return ctx;
277}
278
279QSize QX11GLPixmapData::size() const
280{
281 return QSize(w, h);
282}
283
284
285QGLFormat QX11GLPixmapData::glFormat()
286{
287 return QGLFormat::defaultFormat(); //###
288}
289
290QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.