MaskedBlur.qml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // Copyright (C) 2017 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
  3. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  4. // Qt-Security score:significant reason:default
  5. import QtQuick
  6. import Qt5Compat.GraphicalEffects.private
  7. /*!
  8. \qmltype MaskedBlur
  9. \inqmlmodule Qt5Compat.GraphicalEffects
  10. \since QtGraphicalEffects 1.0
  11. \inherits QtQuick2::Item
  12. \ingroup qtgraphicaleffects-blur
  13. \brief Applies a blur effect with a varying intesity.
  14. MaskedBlur effect softens the image by blurring it. The intensity of the
  15. blur can be controlled for each pixel using maskSource so that some parts of
  16. the source are blurred more than others.
  17. Performing blur live is a costly operation. Fullscreen gaussian blur
  18. with even a moderate number of samples will only run at 60 fps on highend
  19. graphics hardware.
  20. \table
  21. \header
  22. \li Source
  23. \li MaskSource
  24. \li Effect applied
  25. \row
  26. \li \image Original_bug.png
  27. \li \image MaskedBlur_mask.png
  28. \li \image MaskedBlur_bug.png
  29. \endtable
  30. \note This effect is available when running with OpenGL.
  31. \section1 Example
  32. The following example shows how to apply the effect.
  33. \snippet MaskedBlur-example.qml example
  34. */
  35. Item {
  36. id: root
  37. /*!
  38. This property defines the source item that is going to be blurred.
  39. \note It is not supported to let the effect include itself, for
  40. instance by setting source to the effect's parent.
  41. */
  42. property alias source: blur.source
  43. /*!
  44. This property defines the item that is controlling the final intensity
  45. of the blur. The pixel alpha channel value from maskSource defines the
  46. actual blur radius that is going to be used for blurring the
  47. corresponding source pixel.
  48. Opaque maskSource pixels produce blur with specified
  49. \l{MaskedBlur::radius}{radius}, while transparent pixels suppress the
  50. blur completely. Semitransparent maskSource pixels produce blur with a
  51. radius that is interpolated according to the pixel transparency level.
  52. */
  53. property alias maskSource: maskProxy.input
  54. /*!
  55. This property defines the distance of the neighboring pixels which
  56. affect the blurring of an individual pixel. A larger radius increases
  57. the blur effect.
  58. Depending on the radius value, value of the
  59. \l{MaskedBlur::samples}{samples} should be set to sufficiently large to
  60. ensure the visual quality.
  61. The value ranges from 0.0 (no blur) to inf. By default, the property is
  62. set to \c 0.0 (no blur).
  63. \table
  64. \header
  65. \li Output examples with different radius values
  66. \li
  67. \li
  68. \row
  69. \li \image MaskedBlur_radius1.png
  70. \li \image MaskedBlur_radius2.png
  71. \li \image MaskedBlur_radius3.png
  72. \row
  73. \li \b { radius: 0 }
  74. \li \b { radius: 8 }
  75. \li \b { radius: 16 }
  76. \row
  77. \li \l samples: 25
  78. \li \l samples: 25
  79. \li \l samples: 25
  80. \endtable
  81. */
  82. property alias radius: blur.radius
  83. /*!
  84. This property defines how many samples are taken per pixel when blur
  85. calculation is done. Larger value produces better quality, but is slower
  86. to render.
  87. Ideally, this value should be twice as large as the highest required
  88. radius value plus 1, for example, if the radius is animated between 0.0
  89. and 4.0, samples should be set to 9.
  90. By default, the property is set to \c 9.
  91. This property is not intended to be animated. Changing this property may
  92. cause the underlying OpenGL shaders to be recompiled.
  93. */
  94. property alias samples: blur.samples
  95. /*!
  96. This property allows the effect output pixels to be cached in order to
  97. improve the rendering performance. Every time the source or effect
  98. properties are changed, the pixels in the cache must be updated. Memory
  99. consumption is increased, because an extra buffer of memory is required
  100. for storing the effect output.
  101. It is recommended to disable the cache when the source or the effect
  102. properties are animated.
  103. By default, the property is set to \c false.
  104. */
  105. property alias cached: cacheItem.visible
  106. GaussianBlur {
  107. id: blur
  108. source: root.source;
  109. anchors.fill: parent
  110. _maskSource: maskProxy.output;
  111. SourceProxy {
  112. id: maskProxy
  113. }
  114. }
  115. ShaderEffectSource {
  116. id: cacheItem
  117. x: -blur._kernelRadius
  118. y: -blur._kernelRadius
  119. width: blur.width + 2 * blur._kernelRadius
  120. height: blur.height + 2 * blur._kernelRadius
  121. visible: false
  122. smooth: true
  123. sourceRect: Qt.rect(-blur._kernelRadius, -blur._kernelRadius, width, height);
  124. sourceItem: blur
  125. hideSource: visible
  126. }
  127. }