GrBlend.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrBlend_DEFINED
  8. #define GrBlend_DEFINED
  9. #include "include/core/SkTypes.h"
  10. /**
  11. * Equations for alpha-blending.
  12. */
  13. enum GrBlendEquation {
  14. // Basic blend equations.
  15. kAdd_GrBlendEquation, //<! Cs*S + Cd*D
  16. kSubtract_GrBlendEquation, //<! Cs*S - Cd*D
  17. kReverseSubtract_GrBlendEquation, //<! Cd*D - Cs*S
  18. // Advanced blend equations. These are described in the SVG and PDF specs.
  19. kScreen_GrBlendEquation,
  20. kOverlay_GrBlendEquation,
  21. kDarken_GrBlendEquation,
  22. kLighten_GrBlendEquation,
  23. kColorDodge_GrBlendEquation,
  24. kColorBurn_GrBlendEquation,
  25. kHardLight_GrBlendEquation,
  26. kSoftLight_GrBlendEquation,
  27. kDifference_GrBlendEquation,
  28. kExclusion_GrBlendEquation,
  29. kMultiply_GrBlendEquation,
  30. kHSLHue_GrBlendEquation,
  31. kHSLSaturation_GrBlendEquation,
  32. kHSLColor_GrBlendEquation,
  33. kHSLLuminosity_GrBlendEquation,
  34. kIllegal_GrBlendEquation,
  35. kFirstAdvancedGrBlendEquation = kScreen_GrBlendEquation,
  36. kLast_GrBlendEquation = kIllegal_GrBlendEquation,
  37. };
  38. static const int kGrBlendEquationCnt = kLast_GrBlendEquation + 1;
  39. /**
  40. * Coefficients for alpha-blending.
  41. */
  42. enum GrBlendCoeff {
  43. kZero_GrBlendCoeff, //<! 0
  44. kOne_GrBlendCoeff, //<! 1
  45. kSC_GrBlendCoeff, //<! src color
  46. kISC_GrBlendCoeff, //<! one minus src color
  47. kDC_GrBlendCoeff, //<! dst color
  48. kIDC_GrBlendCoeff, //<! one minus dst color
  49. kSA_GrBlendCoeff, //<! src alpha
  50. kISA_GrBlendCoeff, //<! one minus src alpha
  51. kDA_GrBlendCoeff, //<! dst alpha
  52. kIDA_GrBlendCoeff, //<! one minus dst alpha
  53. kConstC_GrBlendCoeff, //<! constant color
  54. kIConstC_GrBlendCoeff, //<! one minus constant color
  55. kConstA_GrBlendCoeff, //<! constant color alpha
  56. kIConstA_GrBlendCoeff, //<! one minus constant color alpha
  57. kS2C_GrBlendCoeff,
  58. kIS2C_GrBlendCoeff,
  59. kS2A_GrBlendCoeff,
  60. kIS2A_GrBlendCoeff,
  61. kIllegal_GrBlendCoeff,
  62. kLast_GrBlendCoeff = kIllegal_GrBlendCoeff,
  63. };
  64. static const int kGrBlendCoeffCnt = kLast_GrBlendCoeff + 1;
  65. static constexpr bool GrBlendCoeffRefsSrc(const GrBlendCoeff coeff) {
  66. return kSC_GrBlendCoeff == coeff || kISC_GrBlendCoeff == coeff || kSA_GrBlendCoeff == coeff ||
  67. kISA_GrBlendCoeff == coeff;
  68. }
  69. static constexpr bool GrBlendCoeffRefsDst(const GrBlendCoeff coeff) {
  70. return kDC_GrBlendCoeff == coeff || kIDC_GrBlendCoeff == coeff || kDA_GrBlendCoeff == coeff ||
  71. kIDA_GrBlendCoeff == coeff;
  72. }
  73. static constexpr bool GrBlendCoeffRefsSrc2(const GrBlendCoeff coeff) {
  74. return kS2C_GrBlendCoeff == coeff || kIS2C_GrBlendCoeff == coeff ||
  75. kS2A_GrBlendCoeff == coeff || kIS2A_GrBlendCoeff == coeff;
  76. }
  77. static constexpr bool GrBlendCoeffsUseSrcColor(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
  78. return kZero_GrBlendCoeff != srcCoeff || GrBlendCoeffRefsSrc(dstCoeff);
  79. }
  80. static constexpr bool GrBlendCoeffsUseDstColor(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
  81. return GrBlendCoeffRefsDst(srcCoeff) || kZero_GrBlendCoeff != dstCoeff;
  82. }
  83. static constexpr bool GrBlendEquationIsAdvanced(GrBlendEquation equation) {
  84. return equation >= kFirstAdvancedGrBlendEquation
  85. && equation != kIllegal_GrBlendEquation;
  86. }
  87. static constexpr bool GrBlendModifiesDst(GrBlendEquation equation, GrBlendCoeff srcCoeff,
  88. GrBlendCoeff dstCoeff) {
  89. return (kAdd_GrBlendEquation != equation && kReverseSubtract_GrBlendEquation != equation) ||
  90. kZero_GrBlendCoeff != srcCoeff || kOne_GrBlendCoeff != dstCoeff;
  91. }
  92. /**
  93. * Advanced blend equations can always tweak alpha for coverage. (See GrCustomXfermode.cpp)
  94. *
  95. * For "add" and "reverse subtract" the blend equation with f=coverage is:
  96. *
  97. * D' = f * (S * srcCoeff + D * dstCoeff) + (1-f) * D
  98. * = f * S * srcCoeff + D * (f * dstCoeff + (1 - f))
  99. *
  100. * (Let srcCoeff be negative for reverse subtract.) We can tweak alpha for coverage when the
  101. * following relationship holds:
  102. *
  103. * (f*S) * srcCoeff' + D * dstCoeff' == f * S * srcCoeff + D * (f * dstCoeff + (1 - f))
  104. *
  105. * (Where srcCoeff' and dstCoeff' have any reference to S pre-multiplied by f.)
  106. *
  107. * It's easy to see this works for the src term as long as srcCoeff' == srcCoeff (meaning srcCoeff
  108. * does not reference S). For the dst term, this will work as long as the following is true:
  109. *|
  110. * dstCoeff' == f * dstCoeff + (1 - f)
  111. * dstCoeff' == 1 - f * (1 - dstCoeff)
  112. *
  113. * By inspection we can see this will work as long as dstCoeff has a 1, and any other term in
  114. * dstCoeff references S.
  115. *
  116. * Moreover, if the blend doesn't modify the dst at all then it is ok to arbitrarily modify the src
  117. * color so folding in coverage is allowed.
  118. */
  119. static constexpr bool GrBlendAllowsCoverageAsAlpha(GrBlendEquation equation,
  120. GrBlendCoeff srcCoeff,
  121. GrBlendCoeff dstCoeff) {
  122. return GrBlendEquationIsAdvanced(equation) ||
  123. !GrBlendModifiesDst(equation, srcCoeff, dstCoeff) ||
  124. ((kAdd_GrBlendEquation == equation || kReverseSubtract_GrBlendEquation == equation) &&
  125. !GrBlendCoeffRefsSrc(srcCoeff) &&
  126. (kOne_GrBlendCoeff == dstCoeff || kISC_GrBlendCoeff == dstCoeff ||
  127. kISA_GrBlendCoeff == dstCoeff));
  128. }
  129. #endif