1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the either Technology Preview License Agreement or the
13 ** Beta Release License Agreement.
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.
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
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.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at http://www.qtsoftware.com/contact.
40 ****************************************************************************/
42 #include "qglpixelbuffer.h"
43 #include "qglpixelbuffer_p.h"
45 #ifndef QT_MAC_USE_COCOA
50 #include <private/qgl_p.h>
55 #ifndef GL_TEXTURE_RECTANGLE_EXT
56 #define GL_TEXTURE_RECTANGLE_EXT 0x84F5
59 static int nearest_gl_texture_size(int v)
62 for (int s = 0; s < 32; ++s) {
63 if (((v>>s) & 1) == 1) {
73 bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
75 #ifdef QT_MAC_USE_COCOA
77 // create a dummy context
78 QGLContext context(f, q);
79 context.create(shareWidget ? shareWidget->context() : 0);
81 if (context.isSharing())
82 share_ctx = shareWidget->context()->d_func()->cx;
84 // steal the NSOpenGLContext and update the format
85 ctx = context.d_func()->cx;
86 context.d_func()->cx = 0;
87 // d->cx will be set to ctx later in
88 // QGLPixelBufferPrivate::common_init, so we need to retain it:
89 [static_cast<NSOpenGLContext *>(ctx) retain];
91 format = context.format();
93 GLenum target = GL_TEXTURE_2D;
95 if ((QGLExtensions::glExtensions & QGLExtensions::TextureRectangle)
96 && (size.width() != nearest_gl_texture_size(size.width())
97 || size.height() != nearest_gl_texture_size(size.height())))
99 target = GL_TEXTURE_RECTANGLE_EXT;
102 pbuf = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:target
103 textureInternalFormat:GL_RGBA
104 textureMaxMipMapLevel:0
105 pixelsWide:size.width()
106 pixelsHigh:size.height()];
108 qWarning("QGLPixelBuffer: Cannot create a pbuffer");
112 [static_cast<NSOpenGLContext *>(ctx) setPixelBuffer:static_cast<NSOpenGLPixelBuffer *>(pbuf)
115 currentVirtualScreen:0];
118 GLint attribs[40], i=0;
119 attribs[i++] = AGL_RGBA;
120 attribs[i++] = AGL_BUFFER_SIZE;
122 attribs[i++] = AGL_LEVEL;
123 attribs[i++] = f.plane();
124 if (f.redBufferSize() != -1) {
125 attribs[i++] = AGL_RED_SIZE;
126 attribs[i++] = f.redBufferSize();
128 if (f.greenBufferSize() != -1) {
129 attribs[i++] = AGL_GREEN_SIZE;
130 attribs[i++] = f.greenBufferSize();
132 if (f.blueBufferSize() != -1) {
133 attribs[i++] = AGL_BLUE_SIZE;
134 attribs[i++] = f.blueBufferSize();
137 attribs[i++] = AGL_STEREO;
139 attribs[i++] = AGL_ALPHA_SIZE;
140 attribs[i++] = f.alphaBufferSize() == -1 ? 8 : f.alphaBufferSize();
143 attribs[i++] = AGL_STENCIL_SIZE;
144 attribs[i++] = f.stencilBufferSize() == -1 ? 8 : f.stencilBufferSize();
147 attribs[i++] = AGL_DEPTH_SIZE;
148 attribs[i++] = f.depthBufferSize() == -1 ? 32 : f.depthBufferSize();
151 attribs[i++] = AGL_ACCUM_RED_SIZE;
152 attribs[i++] = f.accumBufferSize() == -1 ? 16 : f.accumBufferSize();
153 attribs[i++] = AGL_ACCUM_BLUE_SIZE;
154 attribs[i++] = f.accumBufferSize() == -1 ? 16 : f.accumBufferSize();
155 attribs[i++] = AGL_ACCUM_GREEN_SIZE;
156 attribs[i++] = f.accumBufferSize() == -1 ? 16 : f.accumBufferSize();
157 attribs[i++] = AGL_ACCUM_ALPHA_SIZE;
158 attribs[i++] = f.accumBufferSize() == -1 ? 16 : f.accumBufferSize();
161 if (f.sampleBuffers()) {
162 attribs[i++] = AGL_SAMPLE_BUFFERS_ARB;
164 attribs[i++] = AGL_SAMPLES_ARB;
165 attribs[i++] = f.samples() == -1 ? 4 : f.samples();
167 attribs[i] = AGL_NONE;
169 AGLPixelFormat format = aglChoosePixelFormat(0, 0, attribs);
171 qWarning("QGLPixelBuffer: Unable to find a pixel format (AGL error %d).",
172 (int) aglGetError());
177 aglDescribePixelFormat(format, AGL_LEVEL, &res);
178 this->format.setPlane(res);
179 aglDescribePixelFormat(format, AGL_DOUBLEBUFFER, &res);
180 this->format.setDoubleBuffer(res);
181 aglDescribePixelFormat(format, AGL_DEPTH_SIZE, &res);
182 this->format.setDepth(res);
183 if (this->format.depth())
184 this->format.setDepthBufferSize(res);
185 aglDescribePixelFormat(format, AGL_RGBA, &res);
186 this->format.setRgba(res);
187 aglDescribePixelFormat(format, AGL_RED_SIZE, &res);
188 this->format.setRedBufferSize(res);
189 aglDescribePixelFormat(format, AGL_GREEN_SIZE, &res);
190 this->format.setGreenBufferSize(res);
191 aglDescribePixelFormat(format, AGL_BLUE_SIZE, &res);
192 this->format.setBlueBufferSize(res);
193 aglDescribePixelFormat(format, AGL_ALPHA_SIZE, &res);
194 this->format.setAlpha(res);
195 if (this->format.alpha())
196 this->format.setAlphaBufferSize(res);
197 aglDescribePixelFormat(format, AGL_ACCUM_RED_SIZE, &res);
198 this->format.setAccum(res);
199 if (this->format.accum())
200 this->format.setAccumBufferSize(res);
201 aglDescribePixelFormat(format, AGL_STENCIL_SIZE, &res);
202 this->format.setStencil(res);
203 if (this->format.stencil())
204 this->format.setStencilBufferSize(res);
205 aglDescribePixelFormat(format, AGL_STEREO, &res);
206 this->format.setStereo(res);
207 aglDescribePixelFormat(format, AGL_SAMPLE_BUFFERS_ARB, &res);
208 this->format.setSampleBuffers(res);
209 if (this->format.sampleBuffers()) {
210 aglDescribePixelFormat(format, AGL_SAMPLES_ARB, &res);
211 this->format.setSamples(res);
214 AGLContext share = 0;
216 share = share_ctx = static_cast<AGLContext>(shareWidget->d_func()->glcx->d_func()->cx);
217 ctx = aglCreateContext(format, share);
219 qWarning("QGLPixelBuffer: Unable to create a context (AGL error %d).",
220 (int) aglGetError());
224 GLenum target = GL_TEXTURE_2D;
226 if ((QGLExtensions::glExtensions & QGLExtensions::TextureRectangle)
227 && (size.width() != nearest_gl_texture_size(size.width())
228 || size.height() != nearest_gl_texture_size(size.height())))
230 target = GL_TEXTURE_RECTANGLE_EXT;
233 if (!aglCreatePBuffer(size.width(), size.height(), target, GL_RGBA, 0, &pbuf)) {
234 qWarning("QGLPixelBuffer: Unable to create a pbuffer (AGL error %d).",
235 (int) aglGetError());
239 if (!aglSetPBuffer(ctx, pbuf, 0, 0, 0)) {
240 qWarning("QGLPixelBuffer: Unable to set pbuffer (AGL error %d).",
241 (int) aglGetError());
245 aglDestroyPixelFormat(format);
251 bool QGLPixelBufferPrivate::cleanup()
253 #ifdef QT_MAC_USE_COCOA
254 [static_cast<NSOpenGLPixelBuffer *>(pbuf) release];
256 [static_cast<NSOpenGLContext *>(ctx) release];
259 aglDestroyPBuffer(pbuf);
264 bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id)
267 if (d->invalid || !d->share_ctx)
270 #ifdef QT_MAC_USE_COCOA
271 NSOpenGLContext *oldContext = [NSOpenGLContext currentContext];
272 if (d->share_ctx != oldContext)
273 [static_cast<NSOpenGLContext *>(d->share_ctx) makeCurrentContext];
274 glBindTexture(GL_TEXTURE_2D, texture_id);
275 [static_cast<NSOpenGLContext *>(d->share_ctx)
276 setTextureImageToPixelBuffer:static_cast<NSOpenGLPixelBuffer *>(d->pbuf)
277 colorBuffer:GL_FRONT];
278 if (oldContext && oldContext != d->share_ctx)
279 [oldContext makeCurrentContext];
282 aglSetCurrentContext(d->share_ctx);
283 glBindTexture(GL_TEXTURE_2D, texture_id);
284 aglTexImagePBuffer(d->share_ctx, d->pbuf, GL_FRONT);
289 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
290 bool QGLPixelBuffer::bindToDynamicTexture(QMacCompatGLuint texture_id)
292 return bindToDynamicTexture(GLuint(texture_id));
296 void QGLPixelBuffer::releaseFromDynamicTexture()
300 GLuint QGLPixelBuffer::generateDynamicTexture() const
302 #ifdef QT_MAC_USE_COCOA
303 Q_D(const QGLPixelBuffer);
304 NSOpenGLContext *oldContext = [NSOpenGLContext currentContext];
305 if (d->share_ctx != oldContext)
306 [static_cast<NSOpenGLContext *>(d->share_ctx) makeCurrentContext];
308 glGenTextures(1, &texture);
309 glBindTexture(GL_TEXTURE_2D, texture);
310 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
313 if (oldContext && oldContext != d->share_ctx)
314 [oldContext makeCurrentContext];
318 glGenTextures(1, &texture);
319 glBindTexture(GL_TEXTURE_2D, texture);
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
326 bool QGLPixelBuffer::hasOpenGLPbuffers()