Qt 4.8
shadereffectsource.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 QML Shaders plugin 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 "shadereffectsource.h"
43 #include "shadereffectbuffer.h"
44 #include "shadereffect.h"
45 #include "glfunctions.h"
46 
47 #include <QtOpenGL>
48 
73  : QDeclarativeItem(parent)
74  , m_sourceItem(0)
75  , m_wrapMode(ClampToEdge)
76  , m_sourceRect(0, 0, 0, 0)
77  , m_textureSize(0, 0)
78  , m_format(RGBA)
79  , m_size(0, 0)
80  , m_fbo(0)
81  , m_multisampledFbo(0)
82  , m_refs(0)
83  , m_dirtyTexture(true)
84  , m_dirtySceneGraph(true)
85  , m_multisamplingSupported(false)
86  , m_checkedForMultisamplingSupport(false)
87  , m_live(true)
88  , m_hideSource(false)
89  , m_mirrored(false)
90 {
91 }
92 
94 {
95  if (m_refs && m_sourceItem)
97 
98  delete m_fbo;
99  delete m_multisampledFbo;
100 }
101 
115 {
116  if (item == m_sourceItem)
117  return;
118 
119  if (m_sourceItem) {
122 
123  if (m_refs)
125  }
126 
127  m_sourceItem = item;
128 
129  if (m_sourceItem) {
130 
131  // Must have some item as parent
132  if (m_sourceItem->parentItem() == 0)
134 
135  if (m_refs)
137 
140  }
141 
145 }
146 
167 {
168  if (rect == m_sourceRect)
169  return;
170  m_sourceRect = rect;
174 
175  m_dirtyTexture = true;
177 }
178 
194 {
195  if (size == m_textureSize)
196  return;
197 
198  m_textureSize = size;
202 
203  m_dirtyTexture = true;
205 }
206 
224 {
225  if (s == m_live)
226  return;
227 
228  m_live = s;
229 
230  emit liveChanged();
232 }
233 
246 {
247  if (hide == m_hideSource)
248  return;
249 
250  m_hideSource = hide;
251 
254 }
255 
273 {
274  if (mode == m_wrapMode)
275  return;
276 
277  m_wrapMode = mode;
279 
280  m_dirtyTexture = true;
282 }
283 
298 {
299  m_dirtyTexture = true;
301 }
302 
304 {
305  GLint filtering = smooth() ? GL_LINEAR : GL_NEAREST;
308 
309 #if !defined(QT_OPENGL_ES_2)
310  glEnable(GL_TEXTURE_2D);
311 #endif
312 
313  if (m_fbo && m_fbo->isValid()) {
314  glBindTexture(GL_TEXTURE_2D, m_fbo->texture());
315  } else {
316  m_dirtyTexture = true;
319  glBindTexture(GL_TEXTURE_2D, 0);
320  }
321 
322  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
324  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, hwrap);
325  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, vwrap);
326 }
327 
329 {
330  if (m_refs++ == 0) {
333  }
334 }
335 
337 {
338  if (--m_refs == 0) {
341  }
342  Q_ASSERT(m_refs >= 0);
343 }
344 
346 {
348  return;
349 
350  // Multisampling is not (for now) supported.
352  if (!m_textureSize.isEmpty())
353  size = m_textureSize;
354 
355  if (size.height() > 0 && size.width() > 0) {
359 
360  if (!m_fbo) {
361  m_fbo = new ShaderEffectBuffer(size, format);
362  } else {
363  if (!m_fbo->isValid() || m_fbo->size() != size || m_fbo->format().internalTextureFormat() != GLenum(m_format)) {
364  delete m_fbo;
365  m_fbo = 0;
366  m_fbo = new ShaderEffectBuffer(size, format);
367  }
368  }
369  }
370 
371  // Note that real update for the source content happens in shadereffect.cpp
372  m_dirtyTexture = false;
373 }
374 
376 {
377  m_dirtySceneGraph = true;
379 }
380 
382 {
384  if (m_textureSize.isEmpty())
386  if (m_refs)
388 }
389 
391 {
392  m_dirtyTexture = true;
393  if (m_sourceItem) {
395  if (effect)
396  effect->m_changed = true;
397  }
398 }
399 
401 {
402  if (m_sourceItem) {
403  QSize size = m_textureSize;
404  if (size.isEmpty())
405  size = QSize(m_sourceItem->width(), m_sourceItem->height());
406  if (size.width() < 1)
407  size.setWidth(1);
408  if (size.height() < 1)
409  size.setHeight(1);
410  if (m_fbo && (m_fbo->size() != size || !m_fbo->isValid())) {
411  delete m_fbo;
412  m_fbo = 0;
413  delete m_multisampledFbo;
414  m_fbo = m_multisampledFbo = 0;
415  }
416  if (m_size.width() != size.width()) {
417  m_size.setWidth(size.width());
418  emit widthChanged();
419  }
420  if (m_size.height() != size.height()) {
421  m_size.setHeight(size.height());
423  }
424  m_dirtyTexture = true;
425  } else {
426  if (m_size.width() != 0) {
427  m_size.setWidth(0);
428  emit widthChanged();
429  }
430  if (m_size.height() != 0) {
431  m_size.setHeight(0);
433  }
434  }
435 }
436 
438 {
439  if (!m_sourceItem)
440  return;
441 
443 
444  if (!effect) {
445  effect = new ShaderEffect();
447  }
448 
449  if (effect)
450  effect->addRenderTarget(this);
451 
452  m_sourceItem->update();
453 }
454 
456 {
457  if (!m_sourceItem)
458  return;
459 
461 
462  if (effect)
463  effect->removeRenderTarget(this);
464 
465  delete m_fbo;
466  m_fbo = 0;
467 
468  delete m_multisampledFbo;
469  m_multisampledFbo = 0;
470 
471  m_dirtyTexture = true;
472 }
QSize size() const
Returns the size of the texture attached to this framebuffer object.
GLuint texture() const
Returns the texture id for the texture attached as the default rendering target in this framebuffer o...
void setAttachment(QGLFramebufferObject::Attachment attachment)
Sets the attachment configuration of a framebuffer object to attachment.
#define GL_CLAMP_TO_EDGE
Definition: glfunctions.h:62
#define GL_TEXTURE_MIN_FILTER
QGraphicsEffect * graphicsEffect() const
Returns a pointer to this item&#39;s effect if it has one; otherwise 0.
void heightChanged()
bool isValid() const
Returns true if the framebuffer object is valid.
void setInternalTextureFormat(GLenum internalTextureFormat)
Sets the internal format of a framebuffer object&#39;s texture or multisample framebuffer object&#39;s color ...
#define SLOT(a)
Definition: qobjectdefs.h:226
QGraphicsEffect * effect
the effect attached to this item
void setTextureSize(const QSize &size)
#define GL_TEXTURE_WRAP_S
QPointer< QDeclarativeItem > m_sourceItem
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
T * qobject_cast(QObject *object)
Definition: qobject.h:375
void update(const QRectF &rect=QRectF())
Schedules a redraw of the area covered by rect in this item.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define GL_TEXTURE_2D
void addRenderTarget(ShaderEffectSource *target)
ShaderEffectBuffer * m_multisampledFbo
void setSourceItem(QDeclarativeItem *item)
static const QGLContext * currentContext()
Returns the current context, i.e.
Definition: qgl.cpp:3545
void setGraphicsEffect(QGraphicsEffect *effect)
Sets effect as the item&#39;s effect.
#define GL_REPEAT
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
void setParentItem(QDeclarativeItem *parent)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
int width() const
Returns the width.
Definition: qsize.h:126
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QDeclarativeItem class provides the most basic of all visual items in QML.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define emit
Definition: qobjectdefs.h:76
QGLFramebufferObjectFormat format() const
Returns the format of this framebuffer object.
GLenum internalTextureFormat() const
Returns the internal format of a framebuffer object&#39;s texture or multisample framebuffer object&#39;s col...
void setHideSource(bool hide)
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
ShaderEffectSource(QDeclarativeItem *parent=0)
#define GL_TEXTURE_WRAP_T
#define GL_LINEAR
bool smooth() const
QDeclarativeItem * parentItem() const
Returns the QDeclarativeItem parent of this item.
unsigned int GLenum
Definition: main.cpp:50
void hide()
Hides the item.
Q_INVOKABLE void grab()
Repaints the source item content into the texture.
int height() const
Returns the height.
Definition: qsize.h:129
void removeRenderTarget(ShaderEffectSource *target)
typedef GLint
Definition: glfunctions.h:67
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define GL_TEXTURE_MAG_FILTER
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
The QGLFramebufferObjectFormat class specifies the format of an OpenGL framebuffer object...
void setSourceRect(const QRectF &rect)
ShaderEffectBuffer * m_fbo
void setWrapMode(WrapMode mode)
#define GL_NEAREST