source: trunk/src/opengl/qglpixelbuffer_mac.mm@ 454

Last change on this file since 454 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtOpenGL module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
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.
22**
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
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
53QT_BEGIN_NAMESPACE
54
55#ifndef GL_TEXTURE_RECTANGLE_EXT
56#define GL_TEXTURE_RECTANGLE_EXT 0x84F5
57#endif
58
59static 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
73bool 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();