|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 "qglpixelbuffer.h" |
|
43 #include "qglpixelbuffer_p.h" |
|
44 |
|
45 #ifndef QT_MAC_USE_COCOA |
|
46 #include <AGL/agl.h> |
|
47 #endif |
|
48 |
|
49 #include <qimage.h> |
|
50 #include <private/qgl_p.h> |
|
51 #include <qdebug.h> |
|
52 |
|
53 QT_BEGIN_NAMESPACE |
|
54 |
|
55 #ifndef GL_TEXTURE_RECTANGLE_EXT |
|
56 #define GL_TEXTURE_RECTANGLE_EXT 0x84F5 |
|
57 #endif |
|
58 |
|
59 static int nearest_gl_texture_size(int v) |
|
60 { |
|
61 int n = 0, last = 0; |
|
62 for (int s = 0; s < 32; ++s) { |
|
63 if (((v>>s) & 1) == 1) { |
|
64 ++n; |
|
65 last = s; |
|
66 } |
|
67 } |
|
68 if (n > 1) |
|
69 return 1 << (last+1); |
|
70 return 1 << last; |
|
71 } |
|
72 |
|
73 bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget) |
|
74 { |
|
75 #ifdef QT_MAC_USE_COCOA |
|
76 Q_Q(QGLPixelBuffer); |
|
77 // create a dummy context |
|
78 QGLContext context(f, q); |
|
79 context.create(shareWidget ? shareWidget->context() : 0); |
|
80 |
|
81 if (context.isSharing()) |
|
82 share_ctx = shareWidget->context()->d_func()->cx; |
|
83 |
|
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]; |
|
90 |
|
91 format = context.format(); |
|
92 |
|
93 GLenum target = GL_TEXTURE_2D; |
|
94 |
|
95 if ((QGLExtensions::glExtensions & QGLExtensions::TextureRectangle) |
|
96 && (size.width() != nearest_gl_texture_size(size.width()) |
|
97 || size.height() != nearest_gl_texture_size(size.height()))) |
|
98 { |
|
99 target = GL_TEXTURE_RECTANGLE_EXT; |
|
100 } |
|
101 |
|
102 pbuf = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:target |
|
103 textureInternalFormat:GL_RGBA |
|
104 textureMaxMipMapLevel:0 |
|
105 pixelsWide:size.width() |
|
106 pixelsHigh:size.height()]; |
|
107 if (!pbuf) { |
|
108 qWarning("QGLPixelBuffer: Cannot create a pbuffer"); |
|
109 return false; |
|
110 } |
|
111 |
|
112 [static_cast<NSOpenGLContext *>(ctx) setPixelBuffer:static_cast<NSOpenGLPixelBuffer *>(pbuf) |
|
113 cubeMapFace:0 |
|
114 mipMapLevel:0 |
|
115 currentVirtualScreen:0]; |
|
116 return true; |
|
117 #else |
|
118 GLint attribs[40], i=0; |
|
119 attribs[i++] = AGL_RGBA; |
|
120 attribs[i++] = AGL_BUFFER_SIZE; |
|
121 attribs[i++] = 32; |
|
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(); |
|
127 } |
|
128 if (f.greenBufferSize() != -1) { |
|
129 attribs[i++] = AGL_GREEN_SIZE; |
|
130 attribs[i++] = f.greenBufferSize(); |
|
131 } |
|
132 if (f.blueBufferSize() != -1) { |
|
133 attribs[i++] = AGL_BLUE_SIZE; |
|
134 attribs[i++] = f.blueBufferSize(); |
|
135 } |
|
136 if (f.stereo()) |
|
137 attribs[i++] = AGL_STEREO; |
|
138 if (f.alpha()) { |
|
139 attribs[i++] = AGL_ALPHA_SIZE; |
|
140 attribs[i++] = f.alphaBufferSize() == -1 ? 8 : f.alphaBufferSize(); |
|
141 } |
|
142 if (f.stencil()) { |
|
143 attribs[i++] = AGL_STENCIL_SIZE; |
|
144 attribs[i++] = f.stencilBufferSize() == -1 ? 8 : f.stencilBufferSize(); |
|
145 } |
|
146 if (f.depth()) { |
|
147 attribs[i++] = AGL_DEPTH_SIZE; |
|
148 attribs[i++] = f.depthBufferSize() == -1 ? 32 : f.depthBufferSize(); |
|
149 } |
|
150 if (f.accum()) { |
|
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(); |
|
159 } |
|
160 |
|
161 if (f.sampleBuffers()) { |
|
162 attribs[i++] = AGL_SAMPLE_BUFFERS_ARB; |
|
163 attribs[i++] = 1; |
|
164 attribs[i++] = AGL_SAMPLES_ARB; |
|
165 attribs[i++] = f.samples() == -1 ? 4 : f.samples(); |
|
166 } |
|
167 attribs[i] = AGL_NONE; |
|
168 |
|
169 AGLPixelFormat format = aglChoosePixelFormat(0, 0, attribs); |
|
170 if (!format) { |
|
171 qWarning("QGLPixelBuffer: Unable to find a pixel format (AGL error %d).", |
|
172 (int) aglGetError()); |
|
173 return false; |
|
174 } |
|
175 |
|
176 GLint res; |
|
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); |
|
212 } |
|
213 |
|
214 AGLContext share = 0; |
|
215 if (shareWidget) |
|
216 share = share_ctx = static_cast<AGLContext>(shareWidget->d_func()->glcx->d_func()->cx); |
|
217 ctx = aglCreateContext(format, share); |
|
218 if (!ctx) { |
|
219 qWarning("QGLPixelBuffer: Unable to create a context (AGL error %d).", |
|
220 (int) aglGetError()); |
|
221 return false; |
|
222 } |
|
223 |
|
224 GLenum target = GL_TEXTURE_2D; |
|
225 |
|
226 if ((QGLExtensions::glExtensions & QGLExtensions::TextureRectangle) |
|
227 && (size.width() != nearest_gl_texture_size(size.width()) |
|
228 || size.height() != nearest_gl_texture_size(size.height()))) |
|
229 { |
|
230 target = GL_TEXTURE_RECTANGLE_EXT; |
|
231 } |
|
232 |
|
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()); |
|
236 return false; |
|
237 } |
|
238 |
|
239 if (!aglSetPBuffer(ctx, pbuf, 0, 0, 0)) { |
|
240 qWarning("QGLPixelBuffer: Unable to set pbuffer (AGL error %d).", |
|
241 (int) aglGetError()); |
|
242 return false; |
|
243 } |
|
244 |
|
245 aglDestroyPixelFormat(format); |
|
246 return true; |
|
247 |
|
248 #endif |
|
249 } |
|
250 |
|
251 bool QGLPixelBufferPrivate::cleanup() |
|
252 { |
|
253 #ifdef QT_MAC_USE_COCOA |
|
254 [static_cast<NSOpenGLPixelBuffer *>(pbuf) release]; |
|
255 pbuf = 0; |
|
256 [static_cast<NSOpenGLContext *>(ctx) release]; |
|
257 ctx = 0; |
|
258 #else |
|
259 aglDestroyPBuffer(pbuf); |
|
260 #endif |
|
261 return true; |
|
262 } |
|
263 |
|
264 bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id) |
|
265 { |
|
266 Q_D(QGLPixelBuffer); |
|
267 if (d->invalid || !d->share_ctx) |
|
268 return false; |
|
269 |
|
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]; |
|
280 return true; |
|
281 #else |
|
282 aglSetCurrentContext(d->share_ctx); |
|
283 glBindTexture(GL_TEXTURE_2D, texture_id); |
|
284 aglTexImagePBuffer(d->share_ctx, d->pbuf, GL_FRONT); |
|
285 return true; |
|
286 #endif |
|
287 } |
|
288 |
|
289 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS |
|
290 bool QGLPixelBuffer::bindToDynamicTexture(QMacCompatGLuint texture_id) |
|
291 { |
|
292 return bindToDynamicTexture(GLuint(texture_id)); |
|
293 } |
|
294 #endif |
|
295 |
|
296 void QGLPixelBuffer::releaseFromDynamicTexture() |
|
297 { |
|
298 } |
|
299 |
|
300 GLuint QGLPixelBuffer::generateDynamicTexture() const |
|
301 { |
|
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]; |
|
307 GLuint texture; |
|
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); |
|
312 |
|
313 if (oldContext && oldContext != d->share_ctx) |
|
314 [oldContext makeCurrentContext]; |
|
315 return texture; |
|
316 #else |
|
317 GLuint texture; |
|
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); |
|
322 return texture; |
|
323 #endif |
|
324 } |
|
325 |
|
326 bool QGLPixelBuffer::hasOpenGLPbuffers() |
|
327 { |
|
328 return true; |
|
329 } |
|
330 |
|
331 QT_END_NAMESPACE |