1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Graphical Effects module.
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40import QtQuick 2.12
41import QtGraphicalEffects.private 1.12
42
43/*!
44    \qmltype OpacityMask
45    \inqmlmodule QtGraphicalEffects
46    \since QtGraphicalEffects 1.0
47    \inherits QtQuick2::Item
48    \ingroup qtgraphicaleffects-mask
49    \brief Masks the source item with another item.
50
51    \table
52    \header
53        \li Source
54        \li MaskSource
55        \li Effect applied
56    \row
57        \li \image Original_bug.png
58        \li \image OpacityMask_mask.png
59        \li \image OpacityMask_bug.png
60    \endtable
61
62    \note This effect is available when running with OpenGL.
63
64    \section1 Example
65
66    The following example shows how to apply the effect.
67    \snippet OpacityMask-example.qml example
68
69*/
70Item {
71    id: rootItem
72
73    /*!
74        This property defines the source item that is going to be masked.
75
76        \note It is not supported to let the effect include itself, for
77        instance by setting source to the effect's parent.
78    */
79    property variant source
80
81    /*!
82        This property defines the item that is going to be used as the mask. The
83        mask item gets rendered into an intermediate pixel buffer and the alpha
84        values from the result are used to determine the source item's pixels
85        visibility in the display.
86
87        \table
88        \header
89            \li Original
90            \li Mask
91            \li Effect applied
92        \row
93            \li \image Original_bug.png
94            \li \image OpacityMask_mask.png
95            \li \image OpacityMask_bug.png
96        \endtable
97    */
98    property variant maskSource
99
100    /*!
101        This property allows the effect output pixels to be cached in order to
102        improve the rendering performance.
103
104        Every time the source or effect properties are changed, the pixels in
105        the cache must be updated. Memory consumption is increased, because an
106        extra buffer of memory is required for storing the effect output.
107
108        It is recommended to disable the cache when the source or the effect
109        properties are animated.
110
111        By default, the property is set to \c false.
112
113        \note It is not supported to let the effect include itself, for
114        instance by setting maskSource to the effect's parent.
115    */
116    property bool cached: false
117
118    /*!
119        This property controls how the alpha values of the sourceMask will behave.
120
121        If this property is \c false, the resulting opacity is the source alpha
122        multiplied with the mask alpha, \c{As * Am}.
123
124        If this property is \c true, the resulting opacity is the source alpha
125        multiplied with the inverse of the mask alpha, \c{As * (1 - Am)}.
126
127        The default is \c false.
128
129        \since 5.7
130    */
131    property bool invert: false
132
133    SourceProxy {
134        id: sourceProxy
135        input: rootItem.source
136    }
137
138    SourceProxy {
139        id: maskSourceProxy
140        input: rootItem.maskSource
141    }
142
143    ShaderEffectSource {
144        id: cacheItem
145        anchors.fill: parent
146        visible: rootItem.cached
147        smooth: true
148        sourceItem: shaderItem
149        live: true
150        hideSource: visible
151    }
152
153    ShaderEffect {
154        id: shaderItem
155        property variant source: sourceProxy.output
156        property variant maskSource: maskSourceProxy.output
157
158        anchors.fill: parent
159
160        fragmentShader: invert ? "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/opacitymask_invert.frag" : "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/opacitymask.frag"
161    }
162}
163