aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGunnar Sletta <gunnar.sletta@jollamobile.com>2015-01-26 08:14:29 +0100
committerGunnar Sletta <gunnar@sletta.org>2015-03-24 12:13:29 +0000
commitebf3ea32e637e352f8afdcc54979515f8adb9945 (patch)
tree8f7cfe3a0821bbea0cd86c453aa10f5b321b92a4
parent636286dcc69261159e9d50e0c0afe87521ad8b9c (diff)
Implement DropShadow using the new gaussian blur.
The 'fast' path has been discarded because it fails to deliver on the fast promise. For radii less than 30-40 the normal blur is faster, so little point [ChangeLog] DropShadow uses the new gaussian blur implementation. 'fast' no longer has any effect. The default values have been updated so that 'layer.effect: DropShadow { }' works out of the box. Change-Id: I7e4ace4623613f0969a35e0c9bb873c18a158450 Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
-rw-r--r--src/effects/DropShadow.qml212
-rw-r--r--src/effects/doc/images/DropShadow-transparentBorder.pngbin885 -> 8932 bytes
-rw-r--r--src/effects/doc/snippets/DropShadow-example.qml2
-rw-r--r--src/effects/doc/snippets/DropShadow-transparentBorder-example.qml19
-rw-r--r--src/effects/private/DropShadowBase.qml99
-rw-r--r--src/effects/private/private.pro1
-rw-r--r--src/effects/private/qmldir1
7 files changed, 184 insertions, 150 deletions
diff --git a/src/effects/DropShadow.qml b/src/effects/DropShadow.qml
index d134e02..0a08f45 100644
--- a/src/effects/DropShadow.qml
+++ b/src/effects/DropShadow.qml
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
+** Copyright (C) 2015 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Graphical Effects module.
@@ -47,18 +48,14 @@ import QtGraphicalEffects.private 1.0
\since QtGraphicalEffects 1.0
\inherits QtQuick2::Item
\ingroup qtgraphicaleffects-drop-shadow
- \brief Generates a colorized and blurred shadow image of the
- source and places it behind the original, giving the impression that
- source item is raised from the background.
- By default the effect produces a high quality shadow image, thus the
- rendering speed of the shadow might not be the highest possible. The
- rendering speed is reduced especially if the shadow edges are heavily
- softened.
+ \brief Generates a soft shadow behind the source item.
- For use cases that require faster rendering speed and for which the highest
- possible visual quality is not necessary, the
- \l{DropShadow::fast}{fast} property can be set to \c true.
+ The DropShadow effect blurs the alpha channel of the input, colorizes the
+ result and places it behind the source object to create a soft shadow. The
+ shadow's color can be changed using the \l color property. The location of
+ the shadow can be changed with the \l horizontalOffset and \l
+ verticalOffset properties.
\table
\header
@@ -69,6 +66,14 @@ import QtGraphicalEffects.private 1.0
\li \image DropShadow_butterfly.png
\endtable
+ The soft shadow is created by blurring the image live using a gaussian
+ blur. Performing blur live is a costly operation. Fullscreen gaussian blur
+ with even a moderate number of samples will only run at 60 fps on highend
+ graphics hardware.
+
+ When the source is static, the \l cached property can be set to allocate
+ another buffer to avoid performing the blur every time it is drawn.
+
\section1 Example
The following example shows how to apply the effect.
@@ -76,7 +81,12 @@ import QtGraphicalEffects.private 1.0
*/
Item {
- id: rootItem
+ id: root
+
+ DropShadowBase {
+ id: dbs
+ anchors.fill: parent
+ }
/*!
This property defines the source item that is going to be used as the
@@ -85,18 +95,26 @@ Item {
\note It is not supported to let the effect include itself, for
instance by setting source to the effect's parent.
*/
- property variant source
+ property alias source: dbs.source
/*!
+ \qmlproperty int DropShadow::radius
+
Radius defines the softness of the shadow. A larger radius causes the
edges of the shadow to appear more blurry.
- Depending on the radius value, value of the
- \l{DropShadow::samples}{samples} should be set to sufficiently large to
- ensure the visual quality.
+ The ideal blur is acheived by selecting \c samples and \c radius such
+ that \c {samples = 1 + radius * 2}, such as:
- The value ranges from 0.0 (no blur) to inf. By default, the property is
- set to \c 0.0 (no blur).
+ \table
+ \header \li Radius \li Samples
+ \row \li 0 \e{(no blur)} \li 1
+ \row \li 1 \li 3
+ \row \li 2 \li 5
+ \row \li 3 \li 7
+ \endtable
+
+ By default, the property is set to \c {floor(samples/2)}.
\table
\header
@@ -112,9 +130,9 @@ Item {
\li \b { radius: 6 }
\li \b { radius: 12 }
\row
- \li \l samples: 24
- \li \l samples: 24
- \li \l samples: 24
+ \li \l samples: 25
+ \li \l samples: 25
+ \li \l samples: 25
\row
\li \l color: #000000
\li \l color: #000000
@@ -132,9 +150,8 @@ Item {
\li \l spread: 0
\li \l spread: 0
\endtable
-
*/
- property real radius: 0.0
+ property alias radius: dbs.radius;
/*!
This property defines how many samples are taken per pixel when edge
@@ -142,19 +159,22 @@ Item {
quality, but is slower to render.
Ideally, this value should be twice as large as the highest required
- radius value, for example, if the radius is animated between 0.0 and
- 4.0, samples should be set to 8.
+ radius value plus one, such as:
- The value ranges from 0 to 32. By default, the property is set to \c 0.
-
- This property is not intended to be animated. Changing this property may
- cause the underlying OpenGL shaders to be recompiled.
+ \table
+ \header \li Radius \li Samples
+ \row \li 0 \e{(no blur)} \li 1
+ \row \li 1 \li 3
+ \row \li 2 \li 5
+ \row \li 3 \li 7
+ \endtable
- When \l{DropShadow::fast}{fast} property is set to true, this property
- has no effect.
+ By default, the property is set to \c 9.
+ This property is not intended to be animated. Changing this property will
+ cause the underlying OpenGL shaders to be recompiled.
*/
- property int samples: 0
+ property alias samples: dbs.samples
/*!
This property defines the RGBA color value which is used for the shadow.
@@ -179,9 +199,9 @@ Item {
\li \l radius: 8
\li \l radius: 8
\row
- \li \l samples: 16
- \li \l samples: 16
- \li \l samples: 16
+ \li \l samples: 17
+ \li \l samples: 17
+ \li \l samples: 17
\row
\li \l horizontalOffset: 0
\li \l horizontalOffset: 0
@@ -195,13 +215,12 @@ Item {
\li \l spread: 0
\li \l spread: 0
\endtable
-
*/
- property color color: "black"
+ property alias color: dbs.color
/*!
- \qmlproperty real QtGraphicalEffects1::DropShadow::horizontalOffset
- \qmlproperty real QtGraphicalEffects1::DropShadow::verticalOffset
+ \qmlproperty real QtGraphicalEffects::DropShadow::horizontalOffset
+ \qmlproperty real QtGraphicalEffects::DropShadow::verticalOffset
HorizontalOffset and verticalOffset properties define the offset for the
rendered shadow compared to the DropShadow item position. Often, the
@@ -232,9 +251,9 @@ Item {
\li \l radius: 4
\li \l radius: 4
\row
- \li \l samples: 8
- \li \l samples: 8
- \li \l samples: 8
+ \li \l samples: 9
+ \li \l samples: 9
+ \li \l samples: 9
\row
\li \l color: #000000
\li \l color: #000000
@@ -248,17 +267,16 @@ Item {
\li \l spread: 0
\li \l spread: 0
\endtable
-
*/
- property real horizontalOffset: 0.0
- property real verticalOffset: 0.0
+ property alias horizontalOffset: dbs.horizontalOffset
+ property alias verticalOffset: dbs.verticalOffset
/*!
This property defines how large part of the shadow color is strenghtened
near the source edges.
The value ranges from 0.0 to 1.0. By default, the property is set to \c
- 0.5.
+ 0.0.
\table
\header
@@ -278,9 +296,9 @@ Item {
\li \l radius: 8
\li \l radius: 8
\row
- \li \l samples: 16
- \li \l samples: 16
- \li \l samples: 16
+ \li \l samples: 17
+ \li \l samples: 17
+ \li \l samples: 17
\row
\li \l color: #000000
\li \l color: #000000
@@ -294,48 +312,16 @@ Item {
\li \l verticalOffset: 20
\li \l verticalOffset: 20
\endtable
-
*/
- property real spread: 0.0
+ property alias spread: dbs.spread
/*!
- This property selects the blurring algorithm that is used to produce the
- softness for the effect. Setting this to true enables fast algorithm,
- setting value to false produces higher quality result.
-
- By default, the property is set to \c false.
+ \internal
- \table
- \header
- \li Output examples with different fast values
- \li
- \li
- \row
- \li \image DropShadow_fast1.png
- \li \image DropShadow_fast2.png
- \row
- \li \b { fast: false }
- \li \b { fast: true }
- \row
- \li \l radius: 16
- \li \l radius: 16
- \row
- \li \l samples: 24
- \li \l samples: 24
- \row
- \li \l color: #000000
- \li \l color: #000000
- \row
- \li \l horizontalOffset: 0
- \li \l horizontalOffset: 0
- \row
- \li \l verticalOffset: 20
- \li \l verticalOffset: 20
- \row
- \li \l spread: 0
- \li \l spread: 0
- \endtable
+ Starting Qt 5.6, this property has no effect. It is left here
+ for source compatibility only.
+ ### Qt 6: remove
*/
property bool fast: false
@@ -350,9 +336,8 @@ Item {
properties are animated.
By default, the property is set to \c false.
-
*/
- property bool cached: false
+ property alias cached: dbs.cached
/*!
This property determines whether or not the effect has a transparent
@@ -361,59 +346,16 @@ Item {
When set to \c true, the exterior of the item is padded with a 1 pixel
wide transparent edge, making sampling outside the source texture use
transparency instead of the edge pixels. Without this property, an
- image which has opaque edges will not get a blurred edge.
+ image which has opaque edges will not get a blurred shadow.
In the image below, the Rectangle on the left has transparent borders
and has blurred edges, whereas the Rectangle on the right does not:
+ By default, this property is set to \c true.
+
\snippet DropShadow-transparentBorder-example.qml example
- \image transparentBorder.png
+ \image DropShadow-transparentBorder.png
*/
- property bool transparentBorder: false
-
- Loader {
- x: rootItem.horizontalOffset
- y: rootItem.verticalOffset
- width: parent.width
- height: parent.height
- sourceComponent: rootItem.fast ? fastGlow : gaussianGlow
- }
-
- Component {
- id: gaussianGlow
- GaussianGlow {
- anchors.fill: parent
- source: sourceProxy.output
- radius: rootItem.radius
- maximumRadius: rootItem.samples * 0.5
- color: rootItem.color
- cached: rootItem.cached
- spread: rootItem.spread
- transparentBorder: rootItem.transparentBorder
- }
- }
-
- Component {
- id: fastGlow
- FastGlow {
- anchors.fill: parent
- source: sourceProxy.output
- blur: Math.pow(rootItem.radius / 64.0, 0.4)
- color: rootItem.color
- cached: rootItem.cached
- spread: rootItem.spread
- transparentBorder: rootItem.transparentBorder
- }
- }
-
- SourceProxy {
- id: sourceProxy
- input: rootItem.source
- sourceRect: rootItem.transparentBorder ? Qt.rect(-1, -1, parent.width + 2.0, parent.height + 2.0) : Qt.rect(0, 0, 0, 0)
- }
- ShaderEffect {
- anchors.fill: parent
- property variant source: sourceProxy.output
- }
+ property alias transparentBorder: dbs.transparentBorder
}
diff --git a/src/effects/doc/images/DropShadow-transparentBorder.png b/src/effects/doc/images/DropShadow-transparentBorder.png
index bfdaebc..bb1e3b9 100644
--- a/src/effects/doc/images/DropShadow-transparentBorder.png
+++ b/src/effects/doc/images/DropShadow-transparentBorder.png
Binary files differ
diff --git a/src/effects/doc/snippets/DropShadow-example.qml b/src/effects/doc/snippets/DropShadow-example.qml
index fa901f7..53afd50 100644
--- a/src/effects/doc/snippets/DropShadow-example.qml
+++ b/src/effects/doc/snippets/DropShadow-example.qml
@@ -63,7 +63,7 @@ Item {
horizontalOffset: 3
verticalOffset: 3
radius: 8.0
- samples: 16
+ samples: 17
color: "#80000000"
source: butterfly
}
diff --git a/src/effects/doc/snippets/DropShadow-transparentBorder-example.qml b/src/effects/doc/snippets/DropShadow-transparentBorder-example.qml
index ecbcc4e..fe05597 100644
--- a/src/effects/doc/snippets/DropShadow-transparentBorder-example.qml
+++ b/src/effects/doc/snippets/DropShadow-transparentBorder-example.qml
@@ -54,15 +54,11 @@ Rectangle {
id: normalRect
width: 60
height: 60
- color: "#444"
-
+ radius: 10
+ color: "steelblue"
layer.enabled: true
-
layer.effect: DropShadow {
- radius: 4
- samples: radius * 2
- source: normalRect
- color: Qt.rgba(0, 0, 0, 0.5)
+ transparentBorder: false
}
}
@@ -70,15 +66,10 @@ Rectangle {
id: transparentBorderRect
width: 60
height: 60
- color: "#444"
-
+ radius: 10
+ color: "steelblue"
layer.enabled: true
-
layer.effect: DropShadow {
- radius: 4
- samples: radius * 2
- source: transparentBorderRect
- color: Qt.rgba(0, 0, 0, 0.5)
transparentBorder: true
}
}
diff --git a/src/effects/private/DropShadowBase.qml b/src/effects/private/DropShadowBase.qml
new file mode 100644
index 0000000..3c2d8d8
--- /dev/null
+++ b/src/effects/private/DropShadowBase.qml
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Graphical Effects module.
+**
+** $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 The Qt Company Ltd 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
+import QtGraphicalEffects.private 1.0
+import QtGraphicalEffects 1.0
+
+Item {
+ id: root
+
+ property variant source
+ property int radius: samples / 2
+ property int samples: 9
+ property color color: "black"
+ property int horizontalOffset: 10
+ property int verticalOffset: 10
+ property real spread: 0.0
+ property bool cached: false
+ property bool transparentBorder: true
+
+ GaussianBlur {
+ id: blur
+ width: parent.width
+ height: parent.height
+ x: horizontalOffset
+ y: verticalOffset
+ source: root.source
+ radius: root.radius
+ samples: root.samples
+ _thickness: root.spread
+ transparentBorder: root.transparentBorder
+
+
+ _color: root.color;
+ _alphaOnly: true
+ // ignoreDevicePixelRatio: root.ignoreDevicePixelRatio
+
+ ShaderEffect {
+ x: blur._outputRect.x - parent.x
+ y: blur._outputRect.y - parent.y
+ width: transparentBorder ? blur._outputRect.width : blur.width
+ height: transparentBorder ? blur._outputRect.height : blur.height
+ property variant source: blur._output;
+ }
+
+ }
+
+ ShaderEffectSource {
+ id: cacheItem
+ x: -blur._kernelRadius + horizontalOffset
+ y: -blur._kernelRadius + verticalOffset
+ width: blur.width + 2 * blur._kernelRadius
+ height: blur.height + 2 * blur._kernelRadius
+ visible: root.cached
+ smooth: true
+ sourceRect: Qt.rect(-blur._kernelRadius, -blur._kernelRadius, width, height);
+ sourceItem: blur
+ hideSource: visible
+ }
+
+
+}
diff --git a/src/effects/private/private.pro b/src/effects/private/private.pro
index 9ae7e80..2260a00 100644
--- a/src/effects/private/private.pro
+++ b/src/effects/private/private.pro
@@ -15,6 +15,7 @@ HEADERS += \
qgfxshaderbuilder_p.h
QML_FILES = \
+ DropShadowBase.qml \
FastGlow.qml \
FastInnerShadow.qml \
FastMaskedBlur.qml \
diff --git a/src/effects/private/qmldir b/src/effects/private/qmldir
index e17d9bb..aad4a1c 100644
--- a/src/effects/private/qmldir
+++ b/src/effects/private/qmldir
@@ -7,3 +7,4 @@ GaussianDirectionalBlur 1.0 GaussianDirectionalBlur.qml
GaussianGlow 1.0 GaussianGlow.qml
GaussianInnerShadow 1.0 GaussianInnerShadow.qml
GaussianMaskedBlur 1.0 GaussianMaskedBlur.qml
+DropShadowBase 1.0 DropShadowBase.qml