Qt 4.8
qglpixelbuffer_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
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
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 
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 
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 
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 {
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 
297 {
298 }
299 
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 
327 {
328  return true;
329 }
330 
double d
Definition: qnumeric_p.h:62
#define GL_TEXTURE_MIN_FILTER
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int greenBufferSize() const
Returns the green buffer size.
Definition: qgl.cpp:1070
const QGLContext * context() const
Returns the context of this widget.
Definition: qgl.cpp:5303
static bool hasOpenGLPbuffers()
Returns true if the OpenGL pbuffer extension is present on this system; otherwise returns false...
int redBufferSize() const
Returns the red buffer size.
Definition: qgl.cpp:1035
#define Q_D(Class)
Definition: qglobal.h:2482
#define GL_TEXTURE_2D
#define Q_Q(Class)
Definition: qglobal.h:2483
bool sampleBuffers() const
Returns true if multisample buffer support is enabled; otherwise returns false.
Definition: qgl.h:658
int width() const
Returns the width.
Definition: qsize.h:126
int plane() const
Returns the plane of this format.
Definition: qgl.cpp:924
int alphaBufferSize() const
Returns the alpha buffer size.
Definition: qgl.cpp:1132
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool depth() const
Returns true if the depth buffer is enabled; otherwise returns false.
Definition: qgl.h:618
QGLFormat format() const
Returns the frame buffer format that was obtained (this may be a subset of what was requested)...
Definition: qgl.cpp:3495
int accumBufferSize() const
Returns the accumulation buffer size.
Definition: qgl.cpp:1159
The QGLFormat class specifies the display format of an OpenGL rendering context.
Definition: qgl.h:175
virtual bool create(const QGLContext *shareContext=0)
Creates the GL context.
Definition: qgl.cpp:3454
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
int depthBufferSize() const
Returns the depth buffer size.
Definition: qgl.cpp:1000
bool stencil() const
Returns true if the stencil buffer is enabled; otherwise returns false.
Definition: qgl.h:638
Q_CORE_EXPORT void qWarning(const char *,...)
The QGLPixelBuffer class encapsulates an OpenGL pbuffer.
bool accum() const
Returns true if the accumulation buffer is enabled; otherwise returns false.
Definition: qgl.h:633
#define GL_FRONT
bool bindToDynamicTexture(GLuint texture)
Generates and binds a 2D GL texture that is the same size as the pbuffer, and returns the texture&#39;s I...
#define GL_LINEAR
The QGLWidget class is a widget for rendering OpenGL graphics.
Definition: qgl.h:474
unsigned int GLenum
Definition: main.cpp:50
bool isSharing() const
Returns true if this context is sharing its GL context with another QGLContext, otherwise false is re...
Definition: qgl.cpp:3489
bool alpha() const
Returns true if the alpha buffer in the framebuffer is enabled; otherwise returns false...
Definition: qgl.h:628
int height() const
Returns the height.
Definition: qsize.h:129
int blueBufferSize() const
Returns the blue buffer size.
Definition: qgl.cpp:1105
static Extensions glExtensions()
Definition: qgl.cpp:5781
static int nearest_gl_texture_size(int v)
bool init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
typedef GLint
Definition: glfunctions.h:67
#define GL_TEXTURE_RECTANGLE_EXT
#define GL_RGBA
GLuint generateDynamicTexture() const
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define GL_TEXTURE_MAG_FILTER
int stencilBufferSize() const
Returns the stencil buffer size.
Definition: qgl.cpp:1185
bool stereo() const
Returns true if stereo buffering is enabled; otherwise returns false.
Definition: qgl.h:643
int samples() const
Returns the number of samples per pixel when multisampling is enabled.
Definition: qgl.cpp:824
void releaseFromDynamicTexture()
Releases the pbuffer from any previously bound texture.