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 <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)
|
---|
52 | #include <private/qpixmapdata_gl_p.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | QGLPaintDevice::QGLPaintDevice()
|
---|
58 | : m_thisFBO(0)
|
---|
59 | {
|
---|
60 | }
|
---|
61 |
|
---|
62 | QGLPaintDevice::~QGLPaintDevice()
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | int 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 |
|
---|
83 | void 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 |
|
---|
112 | void 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 |
|
---|
126 | void 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 |
|
---|
138 | QGLFormat QGLPaintDevice::format() const
|
---|
139 | {
|
---|
140 | return context()->format();
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool QGLPaintDevice::alphaRequested() const
|
---|
144 | {
|
---|
145 | return context()->d_func()->reqFormat.alpha();
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | ////////////////// QGLWidgetGLPaintDevice //////////////////
|
---|
152 |
|
---|
153 | QGLWidgetGLPaintDevice::QGLWidgetGLPaintDevice()
|
---|
154 | {
|
---|
155 | }
|
---|
156 |
|
---|
157 | QPaintEngine* QGLWidgetGLPaintDevice::paintEngine() const
|
---|
158 | {
|
---|
159 | return glWidget->paintEngine();
|
---|
160 | }
|
---|
161 |
|
---|
162 | void QGLWidgetGLPaintDevice::setWidget(QGLWidget* w)
|
---|
163 | {
|
---|
164 | glWidget = w;
|
---|
165 | }
|
---|
166 |
|
---|
167 | void QGLWidgetGLPaintDevice::beginPaint()
|
---|
168 | {
|
---|
169 | QGLPaintDevice::beginPaint();
|
---|
170 | if (!glWidget->d_func()->disable_clear_on_painter_begin && glWidget->autoFillBackground()) {
|
---|
171 | if (glWidget->testAttribute(Qt::WA_TranslucentBackground))
|
---|
172 | glClearColor(0.0, 0.0, 0.0, 0.0);
|
---|
173 | else {
|
---|
174 | const QColor &c = glWidget->palette().brush(glWidget->backgroundRole()).color();
|
---|
175 | float alpha = c.alphaF();
|
---|
|
---|