source: trunk/src/opengl/qpixmapdata_gl.cpp@ 1023

Last change on this file since 1023 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 22.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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**
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qpixmap.h"
43#include "qglframebufferobject.h"
44
45#include <private/qpaintengine_raster_p.h>
46
47#include "qpixmapdata_gl_p.h"
48
49#include <private/qgl_p.h>
50#include <private/qdrawhelper_p.h>
51#include <private/qimage_p.h>
52
53#include <private/qpaintengineex_opengl2_p.h>
54
55#include <qdesktopwidget.h>
56#include <qfile.h>
57#include <qimagereader.h>
58
59QT_BEGIN_NAMESPACE
60
61extern QGLWidget* qt_gl_share_widget();
62
63/*!
64 \class QGLFramebufferObjectPool
65 \since 4.6
66
67 \brief The QGLFramebufferObject class provides a pool of framebuffer
68 objects for offscreen rendering purposes.
69
70 When requesting an FBO of a given size and format, an FBO of the same
71 format and a size at least as big as the requested size will be returned.
72
73 \internal
74*/
75
76static inline int areaDiff(const QSize &size, const QGLFramebufferObject *fbo)
77{
78 return qAbs(size.width() * size.height() - fbo->width() * fbo->height());
79}
80
81extern int qt_next_power_of_two(int v);
82
83static inline QSize maybeRoundToNextPowerOfTwo(const QSize &sz)
84{
85#ifdef QT_OPENGL_ES_2
86 QSize rounded(qt_next_power_of_two(sz.width()), qt_next_power_of_two(sz.height()));
87 if (rounded.width() * rounded.height() < 1.20 * sz.width() * sz.height())
88 return rounded;
89#endif
90 return sz;
91}
92
93
94QGLFramebufferObject *QGLFramebufferObjectPool::acquire(const QSize &requestSize, const QGLFramebufferObjectFormat &requestFormat, bool strictSize)
95{
96 QGLFramebufferObject *chosen = 0;
97 QGLFramebufferObject *candidate = 0;
98 for (int i = 0; !chosen && i < m_fbos.size(); ++i) {
99 QGLFramebufferObject *fbo = m_fbos.at(i);
100
101 if (strictSize) {
102 if (fbo->size() == requestSize && fbo->format() == requestFormat) {
103 chosen = fbo;
104 break;
105 } else {
106 continue;
107 }
108 }
109
110 if (fbo->format() == requestFormat) {
111 // choose the fbo with a matching format and the closest size
112 if (!candidate || areaDiff(requestSize, candidate) > areaDiff(requestSize, fbo))
113 candidate = fbo;
114 }
115
116 if (candidate) {
117 m_fbos.removeOne(candidate);
118
119 const QSize fboSize = candidate->size();
120 QSize sz = fboSize;
121
122 if (sz.width() < requestSize.width())
123 sz.setWidth(qMax(requestSize.width(), qRound(sz.width() * 1.5)));
124 if (sz.height() < requestSize.height())
125 sz.setHeight(qMax(requestSize.height(), qRound(sz.height() * 1.5)));
126
127 // wasting too much space?
128 if (sz.width() * sz.height() > requestSize.width() * requestSize.height() * 4)
129 sz = requestSize;
130
131 if (sz != fboSize) {
132 delete candidate;
133 candidate = new QGLFramebufferObject(maybeRoundToNextPowerOfTwo(sz), requestFormat);
134 }
135
136 chosen = candidate;
137 }
138 }
139
140 if (!chosen) {
141 if (strictSize)
142 chosen = new QGLFramebufferObject(requestSize, requestFormat);
143 else
144 chosen = new QGLFramebufferObject(maybeRoundToNextPowerOfTwo(requestSize), requestFormat);
145 }
146
147 if (!chosen->isValid()) {
148 delete chosen;
149 chosen = 0;
150 }
151
152 return chosen;
153}
154
155void QGLFramebufferObjectPool::release(QGLFramebufferObject *fbo)
156{
157 if (fbo)
158 m_fbos << fbo;
159}
160
161
162QPaintEngine* QGLPixmapGLPaintDevice::paintEngine() const
163{
164 return data->paintEngine();
165}
166
167void QGLPixmapGLPaintDevice::beginPaint()
168{
169 if (!data->isValid())
170 return;
171
172 // QGLPaintDevice::beginPaint will store the current binding and replace
173 // it with m_thisFBO:
174 m_thisFBO = data->m_renderFbo->handle();
175 QGLPaintDevice::beginPaint();
176
177 Q_ASSERT(data->paintEngine()->type() == QPaintEngine::OpenGL2);