1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef GrPaint_DEFINED 11 #define GrPaint_DEFINED 12 13 #include "GrColor.h" 14 #include "GrFragmentProcessor.h" 15 #include "SkBlendMode.h" 16 #include "SkRefCnt.h" 17 #include "SkRegion.h" 18 #include "SkTLazy.h" 19 20 class GrTextureProxy; 21 class GrXPFactory; 22 23 /** 24 * The paint describes how color and coverage are computed at each pixel by GrContext draw 25 * functions and the how color is blended with the destination pixel. 26 * 27 * The paint allows installation of custom color and coverage stages. New types of stages are 28 * created by subclassing GrProcessor. 29 * 30 * The primitive color computation starts with the color specified by setColor(). This color is the 31 * input to the first color stage. Each color stage feeds its output to the next color stage. 32 * 33 * Fractional pixel coverage follows a similar flow. The GrGeometryProcessor (specified elsewhere) 34 * provides the initial coverage which is passed to the first coverage fragment processor, which 35 * feeds its output to next coverage fragment processor. 36 * 37 * setXPFactory is used to control blending between the output color and dest. It also implements 38 * the application of fractional coverage from the coverage pipeline. 39 */ 40 class GrPaint { 41 public: 42 GrPaint() = default; 43 ~GrPaint() = default; 44 Clone(const GrPaint & src)45 static GrPaint Clone(const GrPaint& src) { return GrPaint(src); } 46 47 /** 48 * The initial color of the drawn primitive. Defaults to solid white. 49 */ setColor4f(const GrColor4f & color)50 void setColor4f(const GrColor4f& color) { fColor = color; } getColor4f()51 const GrColor4f& getColor4f() const { return fColor; } 52 53 /** 54 * Legacy getter, until all code handles 4f directly. 55 */ getColor()56 GrColor getColor() const { return fColor.toGrColor(); } 57 58 /** 59 * Should shader output conversion from linear to sRGB be disabled. 60 * Only relevant if the destination is sRGB. Defaults to false. 61 */ setDisableOutputConversionToSRGB(bool srgb)62 void setDisableOutputConversionToSRGB(bool srgb) { fDisableOutputConversionToSRGB = srgb; } getDisableOutputConversionToSRGB()63 bool getDisableOutputConversionToSRGB() const { return fDisableOutputConversionToSRGB; } 64 65 /** 66 * Should sRGB inputs be allowed to perform sRGB to linear conversion. With this flag 67 * set to false, sRGB textures will be treated as linear (including filtering). 68 */ setAllowSRGBInputs(bool allowSRGBInputs)69 void setAllowSRGBInputs(bool allowSRGBInputs) { fAllowSRGBInputs = allowSRGBInputs; } getAllowSRGBInputs()70 bool getAllowSRGBInputs() const { return fAllowSRGBInputs; } 71 72 /** 73 * Should rendering be gamma-correct, end-to-end. Causes sRGB render targets to behave 74 * as such (with linear blending), and sRGB inputs to be filtered and decoded correctly. 75 */ setGammaCorrect(bool gammaCorrect)76 void setGammaCorrect(bool gammaCorrect) { 77 this->setDisableOutputConversionToSRGB(!gammaCorrect); 78 this->setAllowSRGBInputs(gammaCorrect); 79 } 80 setXPFactory(const GrXPFactory * xpFactory)81 void setXPFactory(const GrXPFactory* xpFactory) { 82 fXPFactory = xpFactory; 83 fTrivial &= !SkToBool(xpFactory); 84 } 85 86 void setPorterDuffXPFactory(SkBlendMode mode); 87 88 void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false); 89 90 /** 91 * Appends an additional color processor to the color computation. 92 */ addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp)93 void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) { 94 SkASSERT(fp); 95 fColorFragmentProcessors.push_back(std::move(fp)); 96 fTrivial = false; 97 } 98 99 /** 100 * Appends an additional coverage processor to the coverage computation. 101 */ addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp)102 void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) { 103 SkASSERT(fp); 104 fCoverageFragmentProcessors.push_back(std::move(fp)); 105 fTrivial = false; 106 } 107 108 /** 109 * Helpers for adding color or coverage effects that sample a texture. The matrix is applied 110 * to the src space position to compute texture coordinates. 111 */ 112 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&); 113 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&); 114 115 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&); 116 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&); 117 numColorFragmentProcessors()118 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } numCoverageFragmentProcessors()119 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } numTotalFragmentProcessors()120 int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + 121 this->numCoverageFragmentProcessors(); } 122 getXPFactory()123 const GrXPFactory* getXPFactory() const { return fXPFactory; } 124 getColorFragmentProcessor(int i)125 GrFragmentProcessor* getColorFragmentProcessor(int i) const { 126 return fColorFragmentProcessors[i].get(); 127 } getCoverageFragmentProcessor(int i)128 GrFragmentProcessor* getCoverageFragmentProcessor(int i) const { 129 return fCoverageFragmentProcessors[i].get(); 130 } 131 132 /** 133 * Returns true if the paint's output color will be constant after blending. If the result is 134 * true, constantColor will be updated to contain the constant color. Note that we can conflate 135 * coverage and color, so the actual values written to pixels with partial coverage may still 136 * not seem constant, even if this function returns true. 137 */ 138 bool isConstantBlendedColor(GrColor* constantColor) const; 139 140 /** 141 * A trivial paint is one that uses src-over and has no fragment processors. 142 * It may have variable sRGB settings. 143 **/ isTrivial()144 bool isTrivial() const { return fTrivial; } 145 146 private: 147 // Since paint copying is expensive if there are fragment processors, we require going through 148 // the Clone() method. 149 GrPaint(const GrPaint&); 150 GrPaint& operator=(const GrPaint&) = delete; 151 152 friend class GrProcessorSet; 153 154 const GrXPFactory* fXPFactory = nullptr; 155 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors; 156 SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors; 157 bool fDisableOutputConversionToSRGB = false; 158 bool fAllowSRGBInputs = false; 159 bool fTrivial = true; 160 GrColor4f fColor = GrColor4f::OpaqueWhite(); 161 }; 162 163 #endif 164