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