Qt 4.8
qglpaintdevice.cpp
Go to the documentation of this file.
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 
56 
58  : m_thisFBO(0)
59 {
60 }
61 
63 {
64 }
65 
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 
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;
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 
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;
121  }
122 
123  ctx->d_ptr->default_fbo = m_thisFBO;
124 }
125 
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) {
133  }
134 
135  ctx->d_ptr->default_fbo = 0;
136 }
137 
139 {
140  return context()->format();
141 }
142 
144 {
145  return context()->d_func()->reqFormat.alpha();
146 }
147 
149 {
150  return false;
151 }
152 
154 
156 {
157 }
158 
160 {
161  return glWidget->paintEngine();
162 }
163 
165 {
166  glWidget = w;
167 }
168 
170 {
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)
182  else
183  glClear(GL_COLOR_BUFFER_BIT);
184  }
185 }
186 
188 {
189  if (glWidget->autoBufferSwap())
190  glWidget->swapBuffers();
192 }
193 
194 
196 {
197  return glWidget->size();
198 }
199 
201 {
202  return const_cast<QGLContext*>(glWidget->context());
203 }
204 
205 // returns the QGLPaintDevice for the given QPaintDevice
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;
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 
virtual QGLFormat format() const
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
QScopedPointer< QGLContextPrivate > d_ptr
Definition: qgl.h:430
qreal alphaF() const
Returns the alpha color component of this color.
Definition: qcolor.cpp:1106
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int greenBufferSize() const
Returns the green buffer size.
Definition: qgl.cpp:1070
qreal greenF() const
Returns the green color component of this color.
Definition: qcolor.cpp:1241
The QGLFramebufferObject class encapsulates an OpenGL framebuffer object.
#define GL_STENCIL_BUFFER_BIT
void setWidget(QGLWidget *)
virtual ~QGLPaintDevice()
#define GL_FRAMEBUFFER_EXT
virtual QSize size() const =0
virtual QGLContext * context() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int redBufferSize() const
Returns the red buffer size.
Definition: qgl.cpp:1035
#define glBindFramebuffer
virtual int devType() const
Definition: qpaintdevice.h:167
virtual QGLContext * context() const =0
int metric(QPaintDevice::PaintDeviceMetric metric) const
virtual QPaintEngine * paintEngine() const
static const QGLContext * currentContext()
Returns the current context, i.e.
Definition: qgl.cpp:3545
virtual bool alphaRequested() const
int width() const
Returns the width.
Definition: qsize.h:126
int alphaBufferSize() const
Returns the alpha buffer size.
Definition: qgl.cpp:1132
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void ensureActiveTarget()
QGLFormat format() const
Returns the frame buffer format that was obtained (this may be a subset of what was requested)...
Definition: qgl.cpp:3495
The QGLFormat class specifies the display format of an OpenGL rendering context.
Definition: qgl.h:175
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
Q_CORE_EXPORT void qWarning(const char *,...)
#define GL_COLOR_BUFFER_BIT
The QGLPixelBuffer class encapsulates an OpenGL pbuffer.
ClassId classId() const
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:90
#define GL_DEPTH_BUFFER_BIT
virtual bool isFlipped() const
GLuint current_fbo
Definition: qgl_p.h:456
The QGLWidget class is a widget for rendering OpenGL graphics.
Definition: qgl.h:474
virtual QSize size() const
virtual void beginPaint()
qreal redF() const
Returns the red color component of this color.
Definition: qcolor.cpp:1213
#define ctx
Definition: qgl.cpp:6094
GLuint default_fbo
Definition: qgl_p.h:457
int height() const
Returns the height.
Definition: qsize.h:129
qreal blueF() const
Returns the blue color component of this color.
Definition: qcolor.cpp:1269
virtual void makeCurrent()
Makes this context the current OpenGL rendering context.
Definition: qgl_egl.cpp:213
int blueBufferSize() const
Returns the blue buffer size.
Definition: qgl.cpp:1105
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
static QGLPaintDevice * getDevice(QPaintDevice *)
virtual void endPaint()