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

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

trunk: Merged in qt 4.6.2 sources.

File size: 10.9 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]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**
[561]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.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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
[651]95 if ((QGLExtensions::glExtensions() & QGLExtensions::TextureRectangle)
[2]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();