MaskedBlur.qml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Copyright (C) 2017 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
  5. ** Contact: https://www.qt.io/licensing/
  6. **
  7. ** This file is part of the Qt Graphical Effects module.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial License Usage
  11. ** Licensees holding valid commercial Qt licenses may use this file in
  12. ** accordance with the commercial license agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and The Qt Company. For licensing terms
  15. ** and conditions see https://www.qt.io/terms-conditions. For further
  16. ** information use the contact form at https://www.qt.io/contact-us.
  17. **
  18. ** GNU Lesser General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU Lesser
  20. ** General Public License version 3 as published by the Free Software
  21. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU Lesser General Public License version 3 requirements
  24. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  25. **
  26. ** GNU General Public License Usage
  27. ** Alternatively, this file may be used under the terms of the GNU
  28. ** General Public License version 2.0 or (at your option) the GNU General
  29. ** Public license version 3 or any later version approved by the KDE Free
  30. ** Qt Foundation. The licenses are as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  32. ** included in the packaging of this file. Please review the following
  33. ** information to ensure the GNU General Public License requirements will
  34. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  35. ** https://www.gnu.org/licenses/gpl-3.0.html.
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. import QtQuick 2.12
  41. import QtGraphicalEffects.private 1.12
  42. /*!
  43. \qmltype MaskedBlur
  44. \inqmlmodule QtGraphicalEffects
  45. \since QtGraphicalEffects 1.0
  46. \inherits QtQuick2::Item
  47. \ingroup qtgraphicaleffects-blur
  48. \brief Applies a blur effect with a varying intesity.
  49. MaskedBlur effect softens the image by blurring it. The intensity of the
  50. blur can be controlled for each pixel using maskSource so that some parts of
  51. the source are blurred more than others.
  52. Performing blur live is a costly operation. Fullscreen gaussian blur
  53. with even a moderate number of samples will only run at 60 fps on highend
  54. graphics hardware.
  55. \table
  56. \header
  57. \li Source
  58. \li MaskSource
  59. \li Effect applied
  60. \row
  61. \li \image Original_bug.png
  62. \li \image MaskedBlur_mask.png
  63. \li \image MaskedBlur_bug.png
  64. \endtable
  65. \note This effect is available when running with OpenGL.
  66. \section1 Example
  67. The following example shows how to apply the effect.
  68. \snippet MaskedBlur-example.qml example
  69. */
  70. Item {
  71. id: root
  72. /*!
  73. This property defines the source item that is going to be blurred.
  74. \note It is not supported to let the effect include itself, for
  75. instance by setting source to the effect's parent.
  76. */
  77. property alias source: blur.source
  78. /*!
  79. This property defines the item that is controlling the final intensity
  80. of the blur. The pixel alpha channel value from maskSource defines the
  81. actual blur radius that is going to be used for blurring the
  82. corresponding source pixel.
  83. Opaque maskSource pixels produce blur with specified
  84. \l{MaskedBlur::radius}{radius}, while transparent pixels suppress the
  85. blur completely. Semitransparent maskSource pixels produce blur with a
  86. radius that is interpolated according to the pixel transparency level.
  87. */
  88. property alias maskSource: maskProxy.input
  89. /*!
  90. This property defines the distance of the neighboring pixels which
  91. affect the blurring of an individual pixel. A larger radius increases
  92. the blur effect.
  93. Depending on the radius value, value of the
  94. \l{MaskedBlur::samples}{samples} should be set to sufficiently large to
  95. ensure the visual quality.
  96. The value ranges from 0.0 (no blur) to inf. By default, the property is
  97. set to \c 0.0 (no blur).
  98. \table
  99. \header
  100. \li Output examples with different radius values
  101. \li
  102. \li
  103. \row
  104. \li \image MaskedBlur_radius1.png
  105. \li \image MaskedBlur_radius2.png
  106. \li \image MaskedBlur_radius3.png
  107. \row
  108. \li \b { radius: 0 }
  109. \li \b { radius: 8 }
  110. \li \b { radius: 16 }
  111. \row
  112. \li \l samples: 25
  113. \li \l samples: 25
  114. \li \l samples: 25
  115. \endtable
  116. */
  117. property alias radius: blur.radius
  118. /*!
  119. This property defines how many samples are taken per pixel when blur
  120. calculation is done. Larger value produces better quality, but is slower
  121. to render.
  122. Ideally, this value should be twice as large as the highest required
  123. radius value plus 1, for example, if the radius is animated between 0.0
  124. and 4.0, samples should be set to 9.
  125. By default, the property is set to \c 9.
  126. This property is not intended to be animated. Changing this property may
  127. cause the underlying OpenGL shaders to be recompiled.
  128. */
  129. property alias samples: blur.samples
  130. /*!
  131. This property allows the effect output pixels to be cached in order to
  132. improve the rendering performance. Every time the source or effect
  133. properties are changed, the pixels in the cache must be updated. Memory
  134. consumption is increased, because an extra buffer of memory is required
  135. for storing the effect output.
  136. It is recommended to disable the cache when the source or the effect
  137. properties are animated.
  138. By default, the property is set to \c false.
  139. */
  140. property alias cached: cacheItem.visible
  141. /*!
  142. \internal
  143. Kept for source compatibility only. Removed in Qt 5.6
  144. ### Qt6: remove
  145. */
  146. property bool fast: false
  147. /*!
  148. \internal
  149. Kept for source compatibility only. Removed in Qt 5.6
  150. Doing transparent border on a masked source doesn't make any sense
  151. as the padded exterior will have a mask alpha value of 0 which means
  152. no blurring and as the padded exterior of the source is a transparent
  153. pixel, the result is no pixels at all.
  154. In Qt 5.6 and before, this worked based on that the mask source
  155. was scaled up to fit the padded blur target rect, which would lead
  156. to inconsistent and buggy results.
  157. ### Qt6: remove
  158. */
  159. property bool transparentBorder;
  160. GaussianBlur {
  161. id: blur
  162. source: root.source;
  163. anchors.fill: parent
  164. _maskSource: maskProxy.output;
  165. SourceProxy {
  166. id: maskProxy
  167. }
  168. }
  169. ShaderEffectSource {
  170. id: cacheItem
  171. x: -blur._kernelRadius
  172. y: -blur._kernelRadius
  173. width: blur.width + 2 * blur._kernelRadius
  174. height: blur.height + 2 * blur._kernelRadius
  175. visible: false
  176. smooth: true
  177. sourceRect: Qt.rect(-blur._kernelRadius, -blur._kernelRadius, width, height);
  178. sourceItem: blur
  179. hideSource: visible
  180. }
  181. }