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" |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 14 | #include "GrFragmentProcessor.h" |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 15 | #include "SkBlendMode.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 16 | #include "SkRefCnt.h" |
egdaniel | b197b8f | 2015-02-17 07:34:43 -0800 | [diff] [blame] | 17 | #include "SkRegion.h" |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 18 | #include "SkTLazy.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 19 | |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 20 | class GrTextureProxy; |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 21 | class GrXPFactory; |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 22 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 23 | /** |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 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 |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | * created by subclassing GrProcessor. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 29 | * |
| 30 | * 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] | 31 | * 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] | 32 | * |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 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. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 36 | * |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 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. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 39 | */ |
| 40 | class GrPaint { |
| 41 | public: |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 42 | GrPaint() = default; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 43 | ~GrPaint() = default; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 44 | |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 45 | static GrPaint Clone(const GrPaint& src) { return GrPaint(src); } |
| 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 | /** |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 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. |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 75 | */ |
| 76 | void setGammaCorrect(bool gammaCorrect) { |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 77 | this->setDisableOutputConversionToSRGB(!gammaCorrect); |
| 78 | this->setAllowSRGBInputs(gammaCorrect); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 81 | void setXPFactory(const GrXPFactory* xpFactory) { |
| 82 | fXPFactory = xpFactory; |
| 83 | fTrivial &= !SkToBool(xpFactory); |
| 84 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 85 | |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 86 | void setPorterDuffXPFactory(SkBlendMode mode); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 87 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 88 | void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 89 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 90 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 91 | * Appends an additional color processor to the color computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 92 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 93 | void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 94 | SkASSERT(fp); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 95 | fColorFragmentProcessors.push_back(std::move(fp)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 96 | fTrivial = false; |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 99 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 100 | * Appends an additional coverage processor to the coverage computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 101 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 102 | void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 103 | SkASSERT(fp); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 104 | fCoverageFragmentProcessors.push_back(std::move(fp)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 105 | fTrivial = false; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 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 | */ |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 112 | void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&); |
| 113 | void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 114 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 115 | void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 116 | void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 117 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 118 | int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } |
| 119 | int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } |
| 120 | int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + |
| 121 | this->numCoverageFragmentProcessors(); } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 122 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 123 | const GrXPFactory* getXPFactory() const { return fXPFactory; } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 124 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 125 | GrFragmentProcessor* getColorFragmentProcessor(int i) const { |
| 126 | return fColorFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 127 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 128 | GrFragmentProcessor* getCoverageFragmentProcessor(int i) const { |
| 129 | return fCoverageFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 130 | } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 131 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 132 | /** |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 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. |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 137 | */ |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 138 | bool isConstantBlendedColor(GrColor* constantColor) const; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 139 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 140 | /** |
| 141 | * A trivial paint is one that uses src-over and has no fragment processors. |
| 142 | * It may have variable sRGB settings. |
| 143 | **/ |
| 144 | bool isTrivial() const { return fTrivial; } |
| 145 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 146 | private: |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 147 | // Since paint copying is expensive if there are fragment processors, we require going through |
| 148 | // the Clone() method. |
| 149 | GrPaint(const GrPaint&); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 150 | GrPaint& operator=(const GrPaint&) = delete; |
| 151 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 152 | friend class GrProcessorSet; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 153 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 154 | const GrXPFactory* fXPFactory = nullptr; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 155 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors; |
| 156 | SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 157 | bool fDisableOutputConversionToSRGB = false; |
| 158 | bool fAllowSRGBInputs = false; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 159 | bool fTrivial = true; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 160 | GrColor4f fColor = GrColor4f::OpaqueWhite(); |
| 161 | }; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 162 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 163 | #endif |