Lomiri
OpacityMask.qml
1 /*
2  * Copyright (C) 2016 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 
19 Item {
20  id: root
21  anchors.fill: sourceItem
22  anchors.margins: -units.gu(2)
23  visible: sourceItem !== null
24 
25  property var sourceItem: null
26  property int maskX: 0
27  property int maskY: 0
28  property int maskWidth: 0
29  property int maskHeight: 0
30 
31  property real opacityValue: 1
32 
33  Item {
34  id: opacityMask
35  anchors.fill: parent
36 
37  Rectangle {
38  id: clipRect
39  color: "black"
40  x: root.maskX - root.anchors.margins
41  y: root.maskY - root.anchors.margins
42  width: root.maskWidth
43  height: root.maskHeight
44  opacity: 1 - root.opacityValue
45  }
46  }
47 
48  ShaderEffect {
49  id: opacityEffect
50  anchors.fill: parent
51 
52  property variant source: ShaderEffectSource {
53  id: shaderEffectSource
54  sourceItem: root.sourceItem
55  sourceRect: root.sourceItem ? Qt.rect(sourceItem.x + root.anchors.margins,
56  sourceItem.y + root.anchors.margins,
57  sourceItem.width - root.anchors.margins * 2,
58  sourceItem.height - root.anchors.margins * 2)
59  : Qt.rect(0,0,0,0)
60  hideSource: true
61  }
62 
63  property var mask: ShaderEffectSource {
64  sourceItem: opacityMask
65  hideSource: true
66  }
67 
68  fragmentShader: "
69  varying highp vec2 qt_TexCoord0;
70  uniform sampler2D source;
71  uniform sampler2D mask;
72  void main(void)
73  {
74  highp vec4 sourceColor = texture2D(source, qt_TexCoord0);
75  highp vec4 maskColor = texture2D(mask, qt_TexCoord0);
76 
77  sourceColor *= 1.0 - maskColor.a;
78 
79  gl_FragColor = sourceColor;
80  }"
81  }
82 }