source: trunk/src/opengl/qglpaintdevice.cpp@ 624

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

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 6.9 KB
RevLine 
[556]1/****************************************************************************
2**
3** Copyright (C) 2009 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 <private/qglpaintdevice_p.h>
43#include <private/qgl_p.h>
44#include <private/qglpixelbuffer_p.h>
45#include <private/qglframebufferobject_p.h>
46#include <private/qwindowsurface_gl_p.h>
47#ifdef Q_WS_X11
48#include <private/qpixmapdata_x11gl_p.h>
49#endif
50
51#if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
52#include <private/qpixmapdata_gl_p.h>
53#endif
54
55#if defined(QT_OPENGL_ES_1_CL)
56#include "qgl_cl_p.h"
57#endif
58
59QT_BEGIN_NAMESPACE
60
61QGLPaintDevice::QGLPaintDevice()
62 : m_thisFBO(0)
63{
64}
65
66QGLPaintDevice::~QGLPaintDevice()
67{
68}
69
70
71void QGLPaintDevice::beginPaint()
72{
73 // Make sure our context is the current one:
74 QGLContext *ctx = context();
75 if (ctx != QGLContext::currentContext())
76 ctx->makeCurrent();
77
78 // Record the currently bound FBO so we can restore it again
79 // in endPaint() and bind this device's FBO
80 //
81 // Note: m_thisFBO could be zero if the paint device is not
82 // backed by an FBO (e.g. window back buffer). But there could
83 // be a previous FBO bound to the context which we need to
84 // explicitly unbind. Otherwise the painting will go into
85 // the previous FBO instead of to the window.
86 m_previousFBO = ctx->d_func()->current_fbo;
87
88 if (m_previousFBO != m_thisFBO) {
89 ctx->d_ptr->current_fbo = m_thisFBO;
90 glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO);
91 }
92
93 // Set the default fbo for the context to m_thisFBO so that
94 // if some raw GL code between beginNativePainting() and
95 // endNativePainting() calls QGLFramebufferObject::release(),
96 // painting will revert to the window surface's fbo.
97 ctx->d_ptr->default_fbo = m_thisFBO;
98}
99
100void QGLPaintDevice::ensureActiveTarget()
101{
102 QGLContext* ctx = context();
103 if (ctx != QGLContext::currentContext())
104 ctx->makeCurrent();
105
106 if (ctx->d_ptr->current_fbo != m_thisFBO) {
107 ctx->d_ptr->current_fbo = m_thisFBO;
108 glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO);
109 }
110
111 ctx->d_ptr->default_fbo = m_thisFBO;
112}
113
114void QGLPaintDevice::endPaint()
115{
116 // Make sure the FBO bound at beginPaint is re-bound again here:
117 QGLContext *ctx = context();
118 if (m_previousFBO != ctx->d_func()->current_fbo) {
119 ctx->d_ptr->current_fbo = m_previousFBO;
120 glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_previousFBO);
121 }
122
123 ctx->d_ptr->default_fbo = 0;
124}
125
126QGLFormat QGLPaintDevice::format() const
127{
128 return context()->format();
129}
130
131
132
133
134////////////////// QGLWidgetGLPaintDevice //////////////////
135
136QGLWidgetGLPaintDevice::QGLWidgetGLPaintDevice()
137{
138}
139
140QPaintEngine* QGLWidgetGLPaintDevice::paintEngine() const
141{
142 return glWidget->paintEngine();
143}
144
145void QGLWidgetGLPaintDevice::setWidget(QGLWidget* w)
146{
147 glWidget = w;
148}
149
150void QGLWidgetGLPaintDevice::beginPaint()
151{
152 QGLPaintDevice::beginPaint();
153 if (!glWidget->d_func()->disable_clear_on_painter_begin && glWidget->autoFillBackground()) {
154 if (glWidget->testAttribute(Qt::WA_TranslucentBackground))
155 glClearColor(0.0, 0.0, 0.0, 0.0);
156 else {
157 const QColor &c = glWidget->palette().brush(glWidget->backgroundRole()).color();
158 float alpha = c.alphaF();
159 glClearColor(c.redF() * alpha, c.greenF() * alpha, c.blueF() * alpha, alpha);
160 }
161 glClear(GL_COLOR_BUFFER_BIT);
162 }
163}
164
165void QGLWidgetGLPaintDevice::endPaint()
166{
167 if (glWidget->autoBufferSwap())
168 glWidget->swapBuffers();
169 QGLPaintDevice::endPaint();
170}
171
172
173QSize QGLWidgetGLPaintDevice::size() const
174{
175 return glWidget->size();
176}
177
178QGLContext* QGLWidgetGLPaintDevice::context() const
179{
180 return const_cast<QGLContext*>(glWidget->context());
181}
182
183// returns the QGLPaintDevice for the given QPaintDevice
184QGLPaintDevice* QGLPaintDevice::getDevice(QPaintDevice* pd)
185{
186 QGLPaintDevice* glpd = 0;
187
188 switch(pd->devType()) {
189 case QInternal::Widget:
190 // Should not be called on a non-gl widget:
191 Q_ASSERT(qobject_cast<QGLWidget*>(static_cast<QWidget*>(pd)));
192 glpd = &(static_cast<QGLWidget*>(pd)->d_func()->glDevice);
193 break;
194 case QInternal::Pbuffer:
195 glpd = &(static_cast<QGLPixelBuffer*>(pd)->d_func()->glDevice);
196 break;
197 case QInternal::FramebufferObject:
198 glpd = &(static_cast<QGLFramebufferObject*>(pd)->d_func()->glDevice);
199 break;
200 case QInternal::Pixmap: {
201#if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
202 QPixmapData* pmd = static_cast<QPixmap*>(pd)->pixmapData();
203 if (pmd->classId() == QPixmapData::OpenGLClass)
204 glpd = static_cast<QGLPixmapData*>(pmd)->glDevice();
205#ifdef Q_WS_X11
206 else if (pmd->classId() == QPixmapData::X11Class)
207 glpd = static_cast<QX11GLPixmapData*>(pmd);
208#endif
209 else
210 qWarning("Pixmap type not supported for GL rendering");
211#else
212 qWarning("Pixmap render targets not supported on OpenGL ES 1.x");
213#endif
214 break;
215 }
216 default:
217 qWarning("QGLPaintDevice::getDevice() - Unknown device type %d", pd->devType());
218 break;
219 }
220
221 return glpd;
222}
223
224QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.