epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 10 | #ifndef GrPaint_DEFINED |
| 11 | #define GrPaint_DEFINED |
| 12 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 13 | #include "GrColor.h" |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 14 | #include "GrColorSpaceXform.h" |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 15 | #include "GrFragmentProcessor.h" |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 16 | #include "SkBlendMode.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 17 | #include "SkRefCnt.h" |
egdaniel | b197b8f | 2015-02-17 07:34:43 -0800 | [diff] [blame] | 18 | #include "SkRegion.h" |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 19 | #include "SkTLazy.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 20 | |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 21 | class GrTextureProxy; |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 22 | class GrXPFactory; |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 23 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 24 | /** |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 25 | * The paint describes how color and coverage are computed at each pixel by GrContext draw |
| 26 | * functions and the how color is blended with the destination pixel. |
| 27 | * |
| 28 | * The paint allows installation of custom color and coverage stages. New types of stages are |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 29 | * created by subclassing GrProcessor. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 30 | * |
| 31 | * The primitive color computation starts with the color specified by setColor(). This color is the |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 32 | * input to the first color stage. Each color stage feeds its output to the next color stage. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 33 | * |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 34 | * Fractional pixel coverage follows a similar flow. The GrGeometryProcessor (specified elsewhere) |
| 35 | * provides the initial coverage which is passed to the first coverage fragment processor, which |
| 36 | * feeds its output to next coverage fragment processor. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 37 | * |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 38 | * setXPFactory is used to control blending between the output color and dest. It also implements |
| 39 | * the application of fractional coverage from the coverage pipeline. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 40 | */ |
| 41 | class GrPaint { |
| 42 | public: |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 43 | GrPaint() = default; |
| 44 | explicit GrPaint(const GrPaint&) = default; |
| 45 | ~GrPaint() = default; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 47 | /** |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 48 | * The initial color of the drawn primitive. Defaults to solid white. |
| 49 | */ |
brianosman | a4535a3 | 2016-06-24 12:50:19 -0700 | [diff] [blame] | 50 | void setColor4f(const GrColor4f& color) { fColor = color; } |
| 51 | const GrColor4f& getColor4f() const { return fColor; } |
| 52 | |
| 53 | /** |
| 54 | * Legacy getter, until all code handles 4f directly. |
| 55 | */ |
| 56 | GrColor getColor() const { return fColor.toGrColor(); } |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
brianosman | 64d094d | 2016-03-25 06:01:59 -0700 | [diff] [blame] | 59 | * Should shader output conversion from linear to sRGB be disabled. |
| 60 | * Only relevant if the destination is sRGB. Defaults to false. |
| 61 | */ |
| 62 | void setDisableOutputConversionToSRGB(bool srgb) { fDisableOutputConversionToSRGB = srgb; } |
| 63 | bool getDisableOutputConversionToSRGB() const { return fDisableOutputConversionToSRGB; } |
| 64 | |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 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 | */ |
| 69 | void setAllowSRGBInputs(bool allowSRGBInputs) { fAllowSRGBInputs = allowSRGBInputs; } |
| 70 | bool getAllowSRGBInputs() const { return fAllowSRGBInputs; } |
| 71 | |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 72 | /** |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 73 | * Does one of the fragment processors need a field of distance vectors to the nearest edge? |
| 74 | */ |
| 75 | bool usesDistanceVectorField() const { return fUsesDistanceVectorField; } |
| 76 | |
| 77 | /** |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 78 | * Should rendering be gamma-correct, end-to-end. Causes sRGB render targets to behave |
| 79 | * as such (with linear blending), and sRGB inputs to be filtered and decoded correctly. |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 80 | */ |
| 81 | void setGammaCorrect(bool gammaCorrect) { |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 82 | this->setDisableOutputConversionToSRGB(!gammaCorrect); |
| 83 | this->setAllowSRGBInputs(gammaCorrect); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 86 | void setXPFactory(const GrXPFactory* xpFactory) { |
| 87 | fXPFactory = xpFactory; |
| 88 | fTrivial &= !SkToBool(xpFactory); |
| 89 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 90 | |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 91 | void setPorterDuffXPFactory(SkBlendMode mode); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 92 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 93 | void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 94 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 95 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 96 | * Appends an additional color processor to the color computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 97 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 98 | void addColorFragmentProcessor(sk_sp<GrFragmentProcessor> fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 99 | SkASSERT(fp); |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 100 | fUsesDistanceVectorField |= fp->usesDistanceVectorField(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 101 | fColorFragmentProcessors.push_back(std::move(fp)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 102 | fTrivial = false; |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 103 | } |
| 104 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 105 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 106 | * Appends an additional coverage processor to the coverage computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 107 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 108 | void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 109 | SkASSERT(fp); |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 110 | fUsesDistanceVectorField |= fp->usesDistanceVectorField(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 111 | fCoverageFragmentProcessors.push_back(std::move(fp)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 112 | fTrivial = false; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 113 | } |
| 114 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Helpers for adding color or coverage effects that sample a texture. The matrix is applied |
| 117 | * to the src space position to compute texture coordinates. |
| 118 | */ |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 119 | void addColorTextureProcessor(GrResourceProvider*, sk_sp<GrTextureProxy>, |
| 120 | sk_sp<GrColorSpaceXform>, const SkMatrix&); |
| 121 | void addColorTextureProcessor(GrResourceProvider*, sk_sp<GrTextureProxy>, |
| 122 | sk_sp<GrColorSpaceXform>, const SkMatrix&, |
| 123 | const GrSamplerParams&); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 124 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 125 | void addCoverageTextureProcessor(GrResourceProvider*, sk_sp<GrTextureProxy>, const SkMatrix&); |
| 126 | void addCoverageTextureProcessor(GrResourceProvider*, sk_sp<GrTextureProxy>, |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 127 | const SkMatrix&, const GrSamplerParams&); |
| 128 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 129 | int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } |
| 130 | int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } |
| 131 | int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + |
| 132 | this->numCoverageFragmentProcessors(); } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 133 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 134 | const GrXPFactory* getXPFactory() const { return fXPFactory; } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 135 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 136 | GrFragmentProcessor* getColorFragmentProcessor(int i) const { |
| 137 | return fColorFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 138 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 139 | GrFragmentProcessor* getCoverageFragmentProcessor(int i) const { |
| 140 | return fCoverageFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 141 | } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 143 | /** |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 144 | * Returns true if the paint's output color will be constant after blending. If the result is |
| 145 | * true, constantColor will be updated to contain the constant color. Note that we can conflate |
| 146 | * coverage and color, so the actual values written to pixels with partial coverage may still |
| 147 | * not seem constant, even if this function returns true. |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 148 | */ |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 149 | bool isConstantBlendedColor(GrColor* constantColor) const; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 150 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 151 | /** |
| 152 | * A trivial paint is one that uses src-over and has no fragment processors. |
| 153 | * It may have variable sRGB settings. |
| 154 | **/ |
| 155 | bool isTrivial() const { return fTrivial; } |
| 156 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 157 | private: |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 158 | template <bool> class MoveOrImpl; |
| 159 | |
| 160 | public: |
| 161 | /** |
| 162 | * A temporary instance of this class can be used to select between moving an existing paint or |
| 163 | * a temporary copy of an existing paint into a call site. MoveOrClone(paint, false) is a rvalue |
| 164 | * reference to paint while MoveOrClone(paint, true) is a rvalue reference to a copy of paint. |
| 165 | */ |
| 166 | using MoveOrClone = MoveOrImpl<true>; |
| 167 | |
| 168 | /** |
| 169 | * A temporary instance of this class can be used to select between moving an existing or a |
| 170 | * newly default constructed paint into a call site. MoveOrNew(paint, false) is a rvalue |
| 171 | * reference to paint while MoveOrNew(paint, true) is a rvalue reference to a default paint. |
| 172 | */ |
| 173 | using MoveOrNew = MoveOrImpl<false>; |
| 174 | |
| 175 | private: |
| 176 | GrPaint& operator=(const GrPaint&) = delete; |
| 177 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 178 | friend class GrProcessorSet; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 179 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 180 | const GrXPFactory* fXPFactory = nullptr; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 181 | SkSTArray<4, sk_sp<GrFragmentProcessor>> fColorFragmentProcessors; |
| 182 | SkSTArray<2, sk_sp<GrFragmentProcessor>> fCoverageFragmentProcessors; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 183 | bool fDisableOutputConversionToSRGB = false; |
| 184 | bool fAllowSRGBInputs = false; |
| 185 | bool fUsesDistanceVectorField = false; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 186 | bool fTrivial = true; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 187 | GrColor4f fColor = GrColor4f::OpaqueWhite(); |
| 188 | }; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 189 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 190 | /** This is the implementation of MoveOrCopy and MoveOrNew. */ |
| 191 | template <bool COPY_IF_NEW> |
| 192 | class GrPaint::MoveOrImpl { |
| 193 | public: |
| 194 | MoveOrImpl(GrPaint& paint, bool newPaint) { |
| 195 | if (newPaint) { |
| 196 | if (COPY_IF_NEW) { |
| 197 | fStorage.init(paint); |
| 198 | } else { |
| 199 | fStorage.init(); |
| 200 | }; |
| 201 | fPaint = fStorage.get(); |
| 202 | } else { |
| 203 | fPaint = &paint; |
| 204 | } |
| 205 | } |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 206 | |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 207 | operator GrPaint&&() && { return std::move(*fPaint); } |
| 208 | GrPaint& paint() { return *fPaint; } |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | SkTLazy<GrPaint> fStorage; |
| 212 | GrPaint* fPaint; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | #endif |