OpacityMask.qml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2020 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import Qt5Compat.GraphicalEffects.private
  6. /*!
  7. \qmltype OpacityMask
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since QtGraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-mask
  12. \brief Masks the source item with another item.
  13. \table
  14. \header
  15. \li Source
  16. \li MaskSource
  17. \li Effect applied
  18. \row
  19. \li \image Original_bug.png
  20. \li \image OpacityMask_mask.png
  21. \li \image OpacityMask_bug.png
  22. \endtable
  23. \section1 Example
  24. The following example shows how to apply the effect.
  25. \snippet OpacityMask-example.qml example
  26. */
  27. Item {
  28. id: rootItem
  29. /*!
  30. This property defines the source item that is going to be masked.
  31. \note It is not supported to let the effect include itself, for
  32. instance by setting source to the effect's parent.
  33. */
  34. property variant source
  35. /*!
  36. This property defines the item that is going to be used as the mask. The
  37. mask item gets rendered into an intermediate pixel buffer and the alpha
  38. values from the result are used to determine the source item's pixels
  39. visibility in the display.
  40. \table
  41. \header
  42. \li Original
  43. \li Mask
  44. \li Effect applied
  45. \row
  46. \li \image Original_bug.png
  47. \li \image OpacityMask_mask.png
  48. \li \image OpacityMask_bug.png
  49. \endtable
  50. */
  51. property variant maskSource
  52. /*!
  53. This property allows the effect output pixels to be cached in order to
  54. improve the rendering performance.
  55. Every time the source or effect properties are changed, the pixels in
  56. the cache must be updated. Memory consumption is increased, because an
  57. extra buffer of memory is required for storing the effect output.
  58. It is recommended to disable the cache when the source or the effect
  59. properties are animated.
  60. By default, the property is set to \c false.
  61. \note It is not supported to let the effect include itself, for
  62. instance by setting maskSource to the effect's parent.
  63. */
  64. property bool cached: false
  65. /*!
  66. This property controls how the alpha values of the sourceMask will behave.
  67. If this property is \c false, the resulting opacity is the source alpha
  68. multiplied with the mask alpha, \c{As * Am}.
  69. If this property is \c true, the resulting opacity is the source alpha
  70. multiplied with the inverse of the mask alpha, \c{As * (1 - Am)}.
  71. The default is \c false.
  72. \since 5.7
  73. */
  74. property bool invert: false
  75. SourceProxy {
  76. id: sourceProxy
  77. input: rootItem.source
  78. }
  79. SourceProxy {
  80. id: maskSourceProxy
  81. input: rootItem.maskSource
  82. }
  83. ShaderEffectSource {
  84. id: cacheItem
  85. anchors.fill: parent
  86. visible: rootItem.cached
  87. smooth: true
  88. sourceItem: shaderItem
  89. live: true
  90. hideSource: visible
  91. }
  92. ShaderEffect {
  93. id: shaderItem
  94. property variant source: sourceProxy.output
  95. property variant maskSource: maskSourceProxy.output
  96. anchors.fill: parent
  97. fragmentShader: invert ? "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/opacitymask_invert.frag.qsb" : "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/opacitymask.frag.qsb"
  98. }
  99. }