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