OpacityMask.qml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2021 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:COMM$
  9. **
  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. ** $QT_END_LICENSE$
  19. **
  20. **
  21. **
  22. **
  23. **
  24. **
  25. **
  26. **
  27. **
  28. **
  29. **
  30. **
  31. **
  32. **
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. /*!
  42. \qmltype OpacityMask
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-mask
  47. \brief Masks the source item with another item.
  48. \table
  49. \header
  50. \li Source
  51. \li MaskSource
  52. \li Effect applied
  53. \row
  54. \li \image Original_bug.png
  55. \li \image OpacityMask_mask.png
  56. \li \image OpacityMask_bug.png
  57. \endtable
  58. \note This effect is available when running with OpenGL.
  59. \section1 Example
  60. The following example shows how to apply the effect.
  61. \snippet OpacityMask-example.qml example
  62. */
  63. Item {
  64. id: rootItem
  65. /*!
  66. This property defines the source item that is going to be masked.
  67. \note It is not supported to let the effect include itself, for
  68. instance by setting source to the effect's parent.
  69. */
  70. property variant source
  71. /*!
  72. This property defines the item that is going to be used as the mask. The
  73. mask item gets rendered into an intermediate pixel buffer and the alpha
  74. values from the result are used to determine the source item's pixels
  75. visibility in the display.
  76. \table
  77. \header
  78. \li Original
  79. \li Mask
  80. \li Effect applied
  81. \row
  82. \li \image Original_bug.png
  83. \li \image OpacityMask_mask.png
  84. \li \image OpacityMask_bug.png
  85. \endtable
  86. */
  87. property variant maskSource
  88. /*!
  89. This property allows the effect output pixels to be cached in order to
  90. improve the rendering performance.
  91. Every time the source or effect properties are changed, the pixels in
  92. the cache must be updated. Memory consumption is increased, because an
  93. extra buffer of memory is required for storing the effect output.
  94. It is recommended to disable the cache when the source or the effect
  95. properties are animated.
  96. By default, the property is set to \c false.
  97. \note It is not supported to let the effect include itself, for
  98. instance by setting maskSource to the effect's parent.
  99. */
  100. property bool cached: false
  101. /*!
  102. This property controls how the alpha values of the sourceMask will behave.
  103. If this property is \c false, the resulting opacity is the source alpha
  104. multiplied with the mask alpha, \c{As * Am}.
  105. If this property is \c true, the resulting opacity is the source alpha
  106. multiplied with the inverse of the mask alpha, \c{As * (1 - Am)}.
  107. The default is \c false.
  108. \since 5.7
  109. */
  110. property bool invert: false
  111. SourceProxy {
  112. id: sourceProxy
  113. input: rootItem.source
  114. }
  115. SourceProxy {
  116. id: maskSourceProxy
  117. input: rootItem.maskSource
  118. }
  119. ShaderEffectSource {
  120. id: cacheItem
  121. anchors.fill: parent
  122. visible: rootItem.cached
  123. smooth: true
  124. sourceItem: shaderItem
  125. live: true
  126. hideSource: visible
  127. }
  128. ShaderEffect {
  129. id: shaderItem
  130. property variant source: sourceProxy.output
  131. property variant maskSource: maskSourceProxy.output
  132. anchors.fill: parent
  133. fragmentShader: invert ? "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/opacitymask_invert.frag" : "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/opacitymask.frag"
  134. }
  135. }