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 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 58 | void setXPFactory(const GrXPFactory* xpFactory) { |
| 59 | fXPFactory = xpFactory; |
| 60 | fTrivial &= !SkToBool(xpFactory); |
| 61 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 62 | |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 63 | void setPorterDuffXPFactory(SkBlendMode mode); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 64 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 65 | void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 66 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 67 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 68 | * Appends an additional color processor to the color computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 69 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 70 | void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 71 | SkASSERT(fp); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 72 | fColorFragmentProcessors.push_back(std::move(fp)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 73 | fTrivial = false; |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 74 | } |
| 75 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 76 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | * Appends an additional coverage processor to the coverage computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 78 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 79 | void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | SkASSERT(fp); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 81 | fCoverageFragmentProcessors.push_back(std::move(fp)); |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 82 | fTrivial = false; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 83 | } |
| 84 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Helpers for adding color or coverage effects that sample a texture. The matrix is applied |
| 87 | * to the src space position to compute texture coordinates. |
| 88 | */ |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 89 | void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&); |
| 90 | void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 91 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 92 | void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 93 | void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 94 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 95 | int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } |
| 96 | int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } |
| 97 | int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + |
| 98 | this->numCoverageFragmentProcessors(); } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 99 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 100 | const GrXPFactory* getXPFactory() const { return fXPFactory; } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 101 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 102 | GrFragmentProcessor* getColorFragmentProcessor(int i) const { |
| 103 | return fColorFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 104 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 105 | GrFragmentProcessor* getCoverageFragmentProcessor(int i) const { |
| 106 | return fCoverageFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 107 | } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 109 | /** |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 110 | * Returns true if the paint's output color will be constant after blending. If the result is |
| 111 | * true, constantColor will be updated to contain the constant color. Note that we can conflate |
| 112 | * coverage and color, so the actual values written to pixels with partial coverage may still |
| 113 | * not seem constant, even if this function returns true. |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 114 | */ |
Brian Salomon | 94cce4c | 2017-02-21 16:32:20 -0500 | [diff] [blame] | 115 | bool isConstantBlendedColor(GrColor* constantColor) const; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 116 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 117 | /** |
| 118 | * A trivial paint is one that uses src-over and has no fragment processors. |
| 119 | * It may have variable sRGB settings. |
| 120 | **/ |
| 121 | bool isTrivial() const { return fTrivial; } |
| 122 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 123 | private: |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 124 | // Since paint copying is expensive if there are fragment processors, we require going through |
| 125 | // the Clone() method. |
| 126 | GrPaint(const GrPaint&); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 127 | GrPaint& operator=(const GrPaint&) = delete; |
| 128 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 129 | friend class GrProcessorSet; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 130 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 131 | const GrXPFactory* fXPFactory = nullptr; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 132 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors; |
| 133 | SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 134 | bool fTrivial = true; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 135 | GrColor4f fColor = GrColor4f::OpaqueWhite(); |
| 136 | }; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 137 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 138 | #endif |