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" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 15 | #include "effects/GrPorterDuffXferProcessor.h" |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 16 | #include "GrFragmentProcessor.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 17 | |
egdaniel | b197b8f | 2015-02-17 07:34:43 -0800 | [diff] [blame] | 18 | #include "SkRegion.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 19 | #include "SkXfermode.h" |
| 20 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 21 | /** |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 22 | * The paint describes how color and coverage are computed at each pixel by GrContext draw |
| 23 | * functions and the how color is blended with the destination pixel. |
| 24 | * |
| 25 | * 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] | 26 | * created by subclassing GrProcessor. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 27 | * |
| 28 | * 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] | 29 | * 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] | 30 | * |
| 31 | * Fractional pixel coverage follows a similar flow. The coverage is initially the value specified |
| 32 | * by setCoverage(). This is input to the first coverage stage. Coverage stages are chained |
| 33 | * together in the same manner as color stages. The output of the last stage is modulated by any |
| 34 | * fractional coverage produced by anti-aliasing. This last step produces the final coverage, C. |
| 35 | * |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 36 | * setXPFactory is used to control blending between the output color and dest. It also implements |
| 37 | * the application of fractional coverage from the coverage pipeline. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 38 | */ |
| 39 | class GrPaint { |
| 40 | public: |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 41 | GrPaint(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 43 | GrPaint(const GrPaint& paint) { *this = paint; } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 44 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 45 | ~GrPaint() { this->resetFragmentProcessors(); } |
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 | */ |
| 50 | void setColor(GrColor color) { fColor = color; } |
| 51 | GrColor getColor() const { return fColor; } |
| 52 | |
| 53 | /** |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 54 | * Should primitives be anti-aliased or not. Defaults to false. |
| 55 | */ |
| 56 | void setAntiAlias(bool aa) { fAntiAlias = aa; } |
| 57 | bool isAntiAlias() const { return fAntiAlias; } |
| 58 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 59 | const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) { |
| 60 | fXPFactory.reset(SkRef(xpFactory)); |
| 61 | return xpFactory; |
| 62 | } |
| 63 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 64 | void setPorterDuffXPFactory(SkXfermode::Mode mode) { |
| 65 | fXPFactory.reset(GrPorterDuffXPFactory::Create(mode)); |
| 66 | } |
| 67 | |
egdaniel | b197b8f | 2015-02-17 07:34:43 -0800 | [diff] [blame] | 68 | void setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage = false); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 69 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 70 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 71 | * Appends an additional color processor to the color computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 72 | */ |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 73 | const GrFragmentProcessor* addColorFragmentProcessor(const GrFragmentProcessor* fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 74 | SkASSERT(fp); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 75 | fColorFragmentProcessors.push_back(SkRef(fp)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 76 | return fp; |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 77 | } |
| 78 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 79 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | * Appends an additional coverage processor to the coverage computation. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 81 | */ |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 82 | const GrFragmentProcessor* addCoverageFragmentProcessor(const GrFragmentProcessor* fp) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | SkASSERT(fp); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 84 | fCoverageFragmentProcessors.push_back(SkRef(fp)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | return fp; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 86 | } |
| 87 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Helpers for adding color or coverage effects that sample a texture. The matrix is applied |
| 90 | * to the src space position to compute texture coordinates. |
| 91 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 92 | void addColorTextureProcessor(GrTexture*, const SkMatrix&); |
| 93 | void addCoverageTextureProcessor(GrTexture*, const SkMatrix&); |
| 94 | void addColorTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&); |
| 95 | void addCoverageTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&); |
tomhudson@google.com | f13f588 | 2012-06-25 17:27:28 +0000 | [diff] [blame] | 96 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 97 | int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } |
| 98 | int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } |
| 99 | int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + |
| 100 | this->numCoverageFragmentProcessors(); } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 101 | |
joshualitt | 2fdeda0 | 2015-01-22 07:11:44 -0800 | [diff] [blame] | 102 | const GrXPFactory* getXPFactory() const { |
| 103 | if (!fXPFactory) { |
| 104 | fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode)); |
| 105 | } |
| 106 | return fXPFactory.get(); |
| 107 | } |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 108 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 109 | const GrFragmentProcessor* getColorFragmentProcessor(int i) const { |
| 110 | return fColorFragmentProcessors[i]; |
| 111 | } |
| 112 | const GrFragmentProcessor* getCoverageFragmentProcessor(int i) const { |
| 113 | return fCoverageFragmentProcessors[i]; |
| 114 | } |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 115 | |
bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 116 | GrPaint& operator=(const GrPaint& paint) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 117 | fAntiAlias = paint.fAntiAlias; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 118 | |
| 119 | fColor = paint.fColor; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 120 | this->resetFragmentProcessors(); |
| 121 | fColorFragmentProcessors = paint.fColorFragmentProcessors; |
| 122 | fCoverageFragmentProcessors = paint.fCoverageFragmentProcessors; |
| 123 | for (int i = 0; i < fColorFragmentProcessors.count(); ++i) { |
| 124 | fColorFragmentProcessors[i]->ref(); |
| 125 | } |
| 126 | for (int i = 0; i < fCoverageFragmentProcessors.count(); ++i) { |
| 127 | fCoverageFragmentProcessors[i]->ref(); |
| 128 | } |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 129 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 130 | fXPFactory.reset(SkRef(paint.getXPFactory())); |
| 131 | |
bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 132 | return *this; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 135 | /** |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 136 | * Returns true if the paint's output color will be constant after blending. If the result is |
| 137 | * true, constantColor will be updated to contain the constant color. Note that we can conflate |
| 138 | * coverage and color, so the actual values written to pixels with partial coverage may still |
| 139 | * not seem constant, even if this function returns true. |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 140 | */ |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 141 | bool isConstantBlendedColor(GrColor* constantColor) const; |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 142 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 143 | private: |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 144 | void resetFragmentProcessors() { |
| 145 | for (int i = 0; i < fColorFragmentProcessors.count(); ++i) { |
| 146 | fColorFragmentProcessors[i]->unref(); |
| 147 | } |
| 148 | for (int i = 0; i < fCoverageFragmentProcessors.count(); ++i) { |
| 149 | fCoverageFragmentProcessors[i]->unref(); |
| 150 | } |
| 151 | fColorFragmentProcessors.reset(); |
| 152 | fCoverageFragmentProcessors.reset(); |
| 153 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 154 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 155 | mutable SkAutoTUnref<const GrXPFactory> fXPFactory; |
| 156 | SkSTArray<4, const GrFragmentProcessor*, true> fColorFragmentProcessors; |
| 157 | SkSTArray<2, const GrFragmentProcessor*, true> fCoverageFragmentProcessors; |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 158 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 159 | bool fAntiAlias; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 160 | |
| 161 | GrColor fColor; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | #endif |