aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGunnar Sletta <gunnar@sletta.org>2014-09-28 12:26:55 +0200
committerGunnar Sletta <gunnar@sletta.org>2014-09-29 12:19:16 +0200
commit2bd7438d77c8544d263c51be432206970eae5ccf (patch)
tree6b632a39ef54b24779183d93e895e0873dfaa41c
parent26387cf498843ce10dd4411d538e1e44a823b8a8 (diff)
Give Item::layer documentation some much needed love.
Change-Id: I31e038d961d3aa09a36db0c091c4e4910e395c2e Reviewed-by: Laszlo Agocs <laszlo.agocs@digia.com>
-rw-r--r--src/quick/doc/images/qml-blending-layered.pngbin0 -> 7582 bytes
-rw-r--r--src/quick/doc/images/qml-blending-nonlayered.pngbin0 -> 7707 bytes
-rw-r--r--src/quick/doc/images/qml-shadereffect-layereffect.pngbin0 -> 22264 bytes
-rw-r--r--src/quick/doc/images/qml-shadereffect-nolayereffect.pngbin0 -> 26718 bytes
-rw-r--r--src/quick/doc/images/qml-shadereffect-opacitymask.pngbin0 -> 15661 bytes
-rw-r--r--src/quick/doc/snippets/qml/layerblending.qml111
-rw-r--r--src/quick/doc/snippets/qml/layerwitheffect.qml111
-rw-r--r--src/quick/doc/snippets/qml/opacitymask.qml114
-rw-r--r--src/quick/items/qquickitem.cpp102
-rw-r--r--src/quick/items/qquickshadereffect.cpp24
10 files changed, 460 insertions, 2 deletions
diff --git a/src/quick/doc/images/qml-blending-layered.png b/src/quick/doc/images/qml-blending-layered.png
new file mode 100644
index 0000000000..fa1c24a98e
--- /dev/null
+++ b/src/quick/doc/images/qml-blending-layered.png
Binary files differ
diff --git a/src/quick/doc/images/qml-blending-nonlayered.png b/src/quick/doc/images/qml-blending-nonlayered.png
new file mode 100644
index 0000000000..265abf6530
--- /dev/null
+++ b/src/quick/doc/images/qml-blending-nonlayered.png
Binary files differ
diff --git a/src/quick/doc/images/qml-shadereffect-layereffect.png b/src/quick/doc/images/qml-shadereffect-layereffect.png
new file mode 100644
index 0000000000..44b05a9949
--- /dev/null
+++ b/src/quick/doc/images/qml-shadereffect-layereffect.png
Binary files differ
diff --git a/src/quick/doc/images/qml-shadereffect-nolayereffect.png b/src/quick/doc/images/qml-shadereffect-nolayereffect.png
new file mode 100644
index 0000000000..f21e3f0c4e
--- /dev/null
+++ b/src/quick/doc/images/qml-shadereffect-nolayereffect.png
Binary files differ
diff --git a/src/quick/doc/images/qml-shadereffect-opacitymask.png b/src/quick/doc/images/qml-shadereffect-opacitymask.png
new file mode 100644
index 0000000000..e056aa763b
--- /dev/null
+++ b/src/quick/doc/images/qml-shadereffect-opacitymask.png
Binary files differ
diff --git a/src/quick/doc/snippets/qml/layerblending.qml b/src/quick/doc/snippets/qml/layerblending.qml
new file mode 100644
index 0000000000..0ef23465a5
--- /dev/null
+++ b/src/quick/doc/snippets/qml/layerblending.qml
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+
+Item {
+ width: 300
+ height: 200
+
+ // The checkers background
+ ShaderEffect {
+ id: tileBackground
+ anchors.fill: parent
+
+ property real tileSize: 10
+ property color color1: Qt.rgba(0.9, 0.9, 0.9, 1);
+ property color color2: Qt.rgba(0.85, 0.85, 0.85, 1);
+
+ property size pixelSize: Qt.size(width / tileSize, height / tileSize);
+
+ fragmentShader:
+ "
+ uniform lowp vec4 color1;
+ uniform lowp vec4 color2;
+ uniform highp vec2 pixelSize;
+ varying highp vec2 qt_TexCoord0;
+ void main() {
+ highp vec2 tc = sign(sin(3.14152 * qt_TexCoord0 * pixelSize));
+ if (tc.x != tc.y)
+ gl_FragColor = color1;
+ else
+ gl_FragColor = color2;
+ }
+ "
+ }
+
+
+ Row {
+ height: 100
+ anchors.centerIn: parent
+ spacing: 50
+
+//! [non-layered]
+Item {
+ id: nonLayered
+
+ opacity: 0.5
+
+ width: 100
+ height: 100
+
+ Rectangle { width: 80; height: 80; border.width: 1 }
+ Rectangle { x: 20; y: 20; width: 80; height: 80; border.width: 1 }
+}
+//! [non-layered]
+
+//! [layered]
+Item {
+ id: layered
+
+ opacity: 0.5
+
+ layer.enabled: true
+
+ width: 100
+ height: 100
+
+ Rectangle { width: 80; height: 80; border.width: 1 }
+ Rectangle { x: 20; y: 20; width: 80; height: 80; border.width: 1 }
+}
+//! [layered]
+
+ }
+}
diff --git a/src/quick/doc/snippets/qml/layerwitheffect.qml b/src/quick/doc/snippets/qml/layerwitheffect.qml
new file mode 100644
index 0000000000..8cfac0bb19
--- /dev/null
+++ b/src/quick/doc/snippets/qml/layerwitheffect.qml
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.2
+
+Rectangle {
+ id: root
+ width: 320
+ height: 320
+
+ gradient: Gradient {
+ GradientStop { position: 0; color: "steelblue" }
+ GradientStop { position: 1; color: "black" }
+ }
+
+ property real cx: width / 2;
+ property real cy: height / 2;
+ property real boxSize: root.width * 0.2;
+ property real radius: width / 4;
+
+
+//! [1]
+Item {
+ id: layerRoot
+ layer.enabled: true
+ layer.effect: ShaderEffect {
+ fragmentShader: "
+ uniform lowp sampler2D source; // this item
+ uniform lowp float qt_Opacity; // inherited opacity of this item
+ varying highp vec2 qt_TexCoord0;
+ void main() {
+ lowp vec4 p = texture2D(source, qt_TexCoord0);
+ lowp float g = dot(p.xyz, vec3(0.344, 0.5, 0.156));
+ gl_FragColor = vec4(g, g, g, p.a) * qt_Opacity;
+ }"
+ }
+//! [1]
+
+ anchors.fill: parent
+
+
+ Repeater {
+ id: repeater
+ model: 200
+
+ Rectangle {
+ id: box
+
+ property real t: index / (repeater.model - 1);
+
+ width: 0
+ height: 0
+ x: root.cx - Math.sin(Math.PI * 2 * t) * root.radius;
+ y: root.cy - Math.cos(Math.PI * 2 * t) * root.radius;
+
+ Rectangle {
+ width: root.boxSize
+ height: root.boxSize
+ anchors.centerIn: parent
+ color: Qt.hsla(box.t, 0.5, 0.5);
+ border.color: "white"
+ antialiasing: true;
+ rotation: box.t * 360;
+// RotationAnimator on rotation {
+// from: box.t * 360;
+// to: box.t * 360 + 360;
+// duration: 7592;
+// loops: Animation.Infinite
+// }
+ }
+ }
+ }
+ }
+}
diff --git a/src/quick/doc/snippets/qml/opacitymask.qml b/src/quick/doc/snippets/qml/opacitymask.qml
new file mode 100644
index 0000000000..e458f810ea
--- /dev/null
+++ b/src/quick/doc/snippets/qml/opacitymask.qml
@@ -0,0 +1,114 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+
+Item {
+ id: root
+ width: 480
+ height: 320
+
+ // The checkers background
+ ShaderEffect {
+ id: tileBackground
+ anchors.fill: parent
+
+ property real tileSize: 20
+ property color color1: Qt.hsla(0, 0, 0.1);
+ property color color2: Qt.hsla(0, 0, 0.2);
+
+ property size pixelSize: Qt.size(width / tileSize, height / tileSize);
+
+ fragmentShader:
+ "
+ uniform lowp vec4 color1;
+ uniform lowp vec4 color2;
+ uniform highp vec2 pixelSize;
+ varying highp vec2 qt_TexCoord0;
+ void main() {
+ highp vec2 tc = sign(sin(3.14152 * qt_TexCoord0 * pixelSize));
+ if (tc.x != tc.y)
+ gl_FragColor = color1;
+ else
+ gl_FragColor = color2;
+ }
+ "
+ }
+
+//! [1]
+ Rectangle {
+ id: gradientRect;
+ width: 10
+ height: 10
+ gradient: Gradient {
+ GradientStop { position: 0; color: "white" }
+ GradientStop { position: 1; color: "steelblue" }
+ }
+ visible: false; // should not be visible on screen.
+ layer.enabled: true;
+ layer.smooth: true
+ }
+
+ Text {
+ id: textItem
+ font.pixelSize: 48
+ text: "Gradient Text"
+ anchors.centerIn: parent
+ layer.enabled: true
+ // This item should be used as the 'mask'
+ layer.samplerName: "maskSource"
+ layer.effect: ShaderEffect {
+ property var colorSource: gradientRect;
+ fragmentShader: "
+ uniform lowp sampler2D colorSource;
+ uniform lowp sampler2D maskSource;
+ uniform lowp float qt_Opacity;
+ varying highp vec2 qt_TexCoord0;
+ void main() {
+ gl_FragColor =
+ texture2D(colorSource, qt_TexCoord0)
+ * texture2D(maskSource, qt_TexCoord0).a
+ * qt_Opacity;
+ }
+ "
+ }
+ }
+//! [1]
+}
diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp
index 38ae0ed3c8..b8362c9dac 100644
--- a/src/quick/items/qquickitem.cpp
+++ b/src/quick/items/qquickitem.cpp
@@ -1838,6 +1838,101 @@ void QQuickItemPrivate::updateSubFocusItem(QQuickItem *scope, bool focus)
their layouts.
See LayoutMirroring for more details.
+
+ \section1 Item Layers
+
+ An Item will normally be rendered directly into the window it
+ belongs to. However, by setting \l layer.enabled, it is possible
+ to delegate the item and its entire subtree into an offscreen
+ surface. Only the offscreen surface, a texture, will be then drawn
+ into the window.
+
+ If it is desired to have a texture size different from that of the
+ item, this is possible using \l layer.textureSize. To render only
+ a section of the item into the texture, use \l
+ layer.sourceRect. It is also possible to specify \l
+ layer.sourceRect so it extends beyond the bounds of the item. In
+ this case, the exterior will be padded with transparent pixels.
+
+ The item will use linear interpolation for scaling if
+ \l layer.smooth is set to \c true and will use mipmap for
+ downsampling if \l layer.mipmap is set to \c true. Mipmapping may
+ improve visual quality of downscaled items. For mipmapping of
+ single Image items, prefer Image::mipmap.
+
+ \section2 Layer Opacity vs Item Opacity
+
+ When applying \l opacity to an item hierarchy the opacity is
+ applied to each item individually. This can lead to undesired
+ visual results when the opacity is applied to a subtree. Consider
+ the following example:
+
+ \table
+ \row
+ \li \inlineimage qml-blending-nonlayered.png
+ \li \b {Non-layered Opacity} \snippet qml/layerblending.qml non-layered
+ \endtable
+
+ A layer is rendered with the root item's opacity being 1, and then
+ the root item's opacity is applied to the texture when it is
+ drawn. This means that fading in a large item hierarchy from
+ transparent to opaque, or vice versa, can be done without the
+ overlap artifacts that the normal item by item alpha blending
+ has. Here is the same example with layer enabled:
+
+ \table
+ \row
+ \li \image qml-blending-layered.png
+ \li \b {Layered Opacity} \snippet qml/layerblending.qml layered
+ \endtable
+
+ \section2 Combined with ShaderEffects
+
+ Setting \l layer.enabled to true will turn the item into a \l
+ {QQuickItem::isTextureProvider}{texture provider}, making it
+ possible to use the item directly as a texture, for instance
+ in combination with the ShaderEffect type.
+
+ It is possible to apply an effect on a layer at runtime using
+ layer.effect:
+
+ \snippet qml/layerwitheffect.qml 1
+
+ In this example, we implement the shader effect manually. The \l
+ {Qt Graphical Effects} module contains a suite of ready-made
+ effects for use with Qt Quick.
+
+ See ShaderEffect for more information about using effects.
+
+ \note \l layer.enabled is actually just a more convenient way of using
+ ShaderEffectSource.
+
+
+ \section2 Memory and Performance
+
+ When an item's layer is enabled, the scene graph will allocate memory
+ in the GPU equal to \c {width x height x 4}. In memory constrained
+ configurations, large layers should be used with care.
+
+ In the QPainter / QWidget world, it is some times favorable to
+ cache complex content in a pixmap, image or texture. In Qt Quick,
+ because of the techniques already applied by the \l {Qt Quick
+ Scene Graph Renderer} {scene graph renderer}, this will in most
+ cases not be the case. Excessive draw calls are already reduced
+ because of batching and a cache will in most cases end up blending
+ more pixels than the original content. The overhead of rendering
+ to an offscreen and the blending involved with drawing the
+ resulting texture is therefore often more costly than simply
+ letting the item and its children be drawn normally.
+
+ Also, an item using a layer can not be \l {Batching} {batched} during
+ rendering. This means that a scene with many layered items may
+ have performance problems.
+
+ Layering can be convenient and useful for visual effects, but
+ should in most cases be enabled for the duration of the effect and
+ disabled afterwards.
+
*/
/*!
@@ -7486,12 +7581,15 @@ void QQuickItemLayer::setMipmap(bool mipmap)
allow you to save some texture memory.
\list
- \li ShaderEffectSource.Alpha - GL_ALPHA
+ \li ShaderEffectSource.Alpha - GL_ALPHA;
\li ShaderEffectSource.RGB - GL_RGB
\li ShaderEffectSource.RGBA - GL_RGBA
\endlist
- \note Some OpenGL implementations do not support the GL_ALPHA format.
+ \note ShaderEffectSource.RGB and ShaderEffectSource.Alpha should
+ be used with caution, as support for these formats in the underlying
+ hardare and driver is often not present.
+
*/
void QQuickItemLayer::setFormat(QQuickShaderEffectSource::Format f)
diff --git a/src/quick/items/qquickshadereffect.cpp b/src/quick/items/qquickshadereffect.cpp
index fb5dac1080..0358495a3b 100644
--- a/src/quick/items/qquickshadereffect.cpp
+++ b/src/quick/items/qquickshadereffect.cpp
@@ -641,10 +641,34 @@ void QQuickShaderEffectCommon::propertyChanged(QQuickItem *item, int mappedId,
corner. For non-linear vertex transformations, like page curl, you can
specify a fine grid of vertices by specifying a \l mesh resolution.
+ \section1 ShaderEffect and Item Layers
+
+ The ShaderEffect type can be combined with \l {Item Layers} {layered items}.
+
+ \table
+ \row
+ \li \b {Layer with effect disabled} \inlineimage qml-shadereffect-nolayereffect.png
+ \li \b {Layer with effect enabled} \inlineimage qml-shadereffect-layereffect.png
+ \li \snippet qml/layerwitheffect.qml 1
+ \endtable
+
+ It is also possible to combine multiple layered items:
+
+ \table
+ \row
+ \li \inlineimage qml-shadereffect-opacitymask.png
+ \li \snippet qml/opacitymask.qml 1
+ \endtable
+
+ The \l {Qt Graphical Effects} module contains several ready-made effects
+ for using with Qt Quick applications.
+
\note Scene Graph textures have origin in the top-left corner rather than
bottom-left which is common in OpenGL.
For information about the GLSL version being used, see \l QtQuick::OpenGLInfo.
+
+ \sa {Item Layers}
*/
QQuickShaderEffect::QQuickShaderEffect(QQuickItem *parent)