GrPaint.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2011 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 GrPaint_DEFINED
  8. #define GrPaint_DEFINED
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/core/SkRegion.h"
  12. #include "src/core/SkTLazy.h"
  13. #include "src/gpu/GrColor.h"
  14. #include "src/gpu/GrFragmentProcessor.h"
  15. class GrTextureProxy;
  16. class GrXPFactory;
  17. /**
  18. * The paint describes how color and coverage are computed at each pixel by GrContext draw
  19. * functions and the how color is blended with the destination pixel.
  20. *
  21. * The paint allows installation of custom color and coverage stages. New types of stages are
  22. * created by subclassing GrProcessor.
  23. *
  24. * The primitive color computation starts with the color specified by setColor(). This color is the
  25. * input to the first color stage. Each color stage feeds its output to the next color stage.
  26. *
  27. * Fractional pixel coverage follows a similar flow. The GrGeometryProcessor (specified elsewhere)
  28. * provides the initial coverage which is passed to the first coverage fragment processor, which
  29. * feeds its output to next coverage fragment processor.
  30. *
  31. * setXPFactory is used to control blending between the output color and dest. It also implements
  32. * the application of fractional coverage from the coverage pipeline.
  33. */
  34. class GrPaint {
  35. public:
  36. GrPaint() = default;
  37. ~GrPaint() = default;
  38. static GrPaint Clone(const GrPaint& src) { return GrPaint(src); }
  39. /**
  40. * The initial color of the drawn primitive. Defaults to solid white.
  41. */
  42. void setColor4f(const SkPMColor4f& color) { fColor = color; }
  43. const SkPMColor4f& getColor4f() const { return fColor; }
  44. void setXPFactory(const GrXPFactory* xpFactory) {
  45. fXPFactory = xpFactory;
  46. fTrivial &= !SkToBool(xpFactory);
  47. }
  48. void setPorterDuffXPFactory(SkBlendMode mode);
  49. void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false);
  50. /**
  51. * Appends an additional color processor to the color computation.
  52. */
  53. void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
  54. SkASSERT(fp);
  55. fColorFragmentProcessors.push_back(std::move(fp));
  56. fTrivial = false;
  57. }
  58. /**
  59. * Appends an additional coverage processor to the coverage computation.
  60. */
  61. void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
  62. SkASSERT(fp);
  63. fCoverageFragmentProcessors.push_back(std::move(fp));
  64. fTrivial = false;
  65. }
  66. /**
  67. * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
  68. * to the src space position to compute texture coordinates.
  69. */
  70. void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
  71. void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
  72. void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
  73. void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
  74. int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
  75. int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
  76. int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
  77. this->numCoverageFragmentProcessors(); }
  78. const GrXPFactory* getXPFactory() const { return fXPFactory; }
  79. GrFragmentProcessor* getColorFragmentProcessor(int i) const {
  80. return fColorFragmentProcessors[i].get();
  81. }
  82. GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
  83. return fCoverageFragmentProcessors[i].get();
  84. }
  85. /**
  86. * Returns true if the paint's output color will be constant after blending. If the result is
  87. * true, constantColor will be updated to contain the constant color. Note that we can conflate
  88. * coverage and color, so the actual values written to pixels with partial coverage may still
  89. * not seem constant, even if this function returns true.
  90. */
  91. bool isConstantBlendedColor(SkPMColor4f* constantColor) const;
  92. /**
  93. * A trivial paint is one that uses src-over and has no fragment processors.
  94. * It may have variable sRGB settings.
  95. **/
  96. bool isTrivial() const { return fTrivial; }
  97. friend void assert_alive(GrPaint& p) {
  98. SkASSERT(p.fAlive);
  99. }
  100. private:
  101. // Since paint copying is expensive if there are fragment processors, we require going through
  102. // the Clone() method.
  103. GrPaint(const GrPaint&);
  104. GrPaint& operator=(const GrPaint&) = delete;
  105. friend class GrProcessorSet;
  106. const GrXPFactory* fXPFactory = nullptr;
  107. SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors;
  108. SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors;
  109. bool fTrivial = true;
  110. SkPMColor4f fColor = SK_PMColor4fWHITE;
  111. SkDEBUGCODE(bool fAlive = true;) // Set false after moved from.
  112. };
  113. #endif