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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkBlendMode.h" |
| 14 | #include "include/core/SkRefCnt.h" |
| 15 | #include "include/core/SkRegion.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkTLazy.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrFragmentProcessor.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 Salomon | 078e8fa | 2019-11-22 04:10:18 +0000 | [diff] [blame^] | 84 | void addColorTextureProcessor(sk_sp<GrTextureProxy>, GrColorType srcColorType, const SkMatrix&); |
| 85 | void addColorTextureProcessor(sk_sp<GrTextureProxy>, GrColorType srcColorType, const SkMatrix&, |
| 86 | const GrSamplerState&); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 87 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 88 | int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } |
| 89 | int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } |
| 90 | int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + |
| 91 | this->numCoverageFragmentProcessors(); } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 92 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 93 | const GrXPFactory* getXPFactory() const { return fXPFactory; } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 94 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 95 | GrFragmentProcessor* getColorFragmentProcessor(int i) const { |
| 96 | return fColorFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 97 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 98 | GrFragmentProcessor* getCoverageFragmentProcessor(int i) const { |
| 99 | return fCoverageFragmentProcessors[i].get(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 100 | } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 102 | /** |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 103 | * Returns true if the paint's output color will be constant after blending. If the result is |
| 104 | * true, constantColor will be updated to contain the constant color. Note that we can conflate |
| 105 | * coverage and color, so the actual values written to pixels with partial coverage may still |
| 106 | * not seem constant, even if this function returns true. |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 107 | */ |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 108 | bool isConstantBlendedColor(SkPMColor4f* constantColor) const; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 109 | |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 110 | /** |
| 111 | * A trivial paint is one that uses src-over and has no fragment processors. |
| 112 | * It may have variable sRGB settings. |
| 113 | **/ |
| 114 | bool isTrivial() const { return fTrivial; } |
| 115 | |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 116 | friend void assert_alive(GrPaint& p) { |
| 117 | SkASSERT(p.fAlive); |
| 118 | } |
| 119 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 120 | private: |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 121 | // Since paint copying is expensive if there are fragment processors, we require going through |
| 122 | // the Clone() method. |
| 123 | GrPaint(const GrPaint&); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 124 | GrPaint& operator=(const GrPaint&) = delete; |
| 125 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 126 | friend class GrProcessorSet; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 127 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 128 | const GrXPFactory* fXPFactory = nullptr; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 129 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors; |
| 130 | SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors; |
Brian Salomon | 6d4b65e | 2017-05-03 17:06:09 -0400 | [diff] [blame] | 131 | bool fTrivial = true; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 132 | SkPMColor4f fColor = SK_PMColor4fWHITE; |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 133 | SkDEBUGCODE(bool fAlive = true;) // Set false after moved from. |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 134 | }; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 135 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 136 | #endif |