bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrFragmentProcessor_DEFINED |
| 9 | #define GrFragmentProcessor_DEFINED |
| 10 | |
| 11 | #include "GrProcessor.h" |
| 12 | |
| 13 | class GrCoordTransform; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | class GrGLSLFragmentProcessor; |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 15 | class GrInvariantOutput; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 16 | class GrPipeline; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 17 | class GrProcessorKeyBuilder; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 18 | class GrShaderCaps; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 19 | class GrSwizzle; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 20 | |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 21 | /** Provides custom fragment shader code. Fragment processors receive an input color (float4) and |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 22 | produce an output color. They may reference textures and uniforms. They may use |
| 23 | GrCoordTransforms to receive a transformation of the local coordinates that map from local space |
| 24 | to the fragment being processed. |
| 25 | */ |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 26 | class GrFragmentProcessor : public GrResourceIOProcessor, public GrProgramElement { |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 27 | public: |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 28 | /** |
| 29 | * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to |
| 30 | * only consider the input color's alpha. However, there is a competing desire to have reusable |
| 31 | * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input |
| 32 | * color is considered. This function exists to filter the input color and pass it to a FP. It |
| 33 | * does so by returning a parent FP that multiplies the passed in FPs output by the parent's |
| 34 | * input alpha. The passed in FP will not receive an input color. |
| 35 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 36 | static sk_sp<GrFragmentProcessor> MulOutputByInputAlpha(sk_sp<GrFragmentProcessor>); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 39 | * This assumes that the input color to the returned processor will be unpremul and that the |
| 40 | * passed processor (which becomes the returned processor's child) produces a premul output. |
| 41 | * The result of the returned processor is a premul of its input color modulated by the child |
| 42 | * processor's premul output. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 43 | */ |
Brian Salomon | 22af73f | 2017-01-26 11:25:12 -0500 | [diff] [blame] | 44 | static sk_sp<GrFragmentProcessor> MakeInputPremulAndMulByOutput(sk_sp<GrFragmentProcessor>); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 47 | * Returns a parent fragment processor that adopts the passed fragment processor as a child. |
| 48 | * The parent will ignore its input color and instead feed the passed in color as input to the |
| 49 | * child. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 50 | */ |
brianosman | 4cea3b9 | 2016-09-08 09:33:50 -0700 | [diff] [blame] | 51 | static sk_sp<GrFragmentProcessor> OverrideInput(sk_sp<GrFragmentProcessor>, GrColor4f); |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 52 | |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 53 | /** |
dvonbeck | c526da9 | 2016-07-20 11:20:30 -0700 | [diff] [blame] | 54 | * Returns a fragment processor that premuls the input before calling the passed in fragment |
| 55 | * processor. |
| 56 | */ |
| 57 | static sk_sp<GrFragmentProcessor> PremulInput(sk_sp<GrFragmentProcessor>); |
| 58 | |
| 59 | /** |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 60 | * Returns a fragment processor that calls the passed in fragment processor, and then premuls |
| 61 | * the output. |
| 62 | */ |
| 63 | static sk_sp<GrFragmentProcessor> PremulOutput(sk_sp<GrFragmentProcessor>); |
| 64 | |
| 65 | /** |
| 66 | * Returns a fragment processor that calls the passed in fragment processor, and then unpremuls |
| 67 | * the output. |
| 68 | */ |
| 69 | static sk_sp<GrFragmentProcessor> UnpremulOutput(sk_sp<GrFragmentProcessor>); |
| 70 | |
| 71 | /** |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 72 | * Returns a fragment processor that calls the passed in fragment processor, and then swizzles |
| 73 | * the output. |
| 74 | */ |
| 75 | static sk_sp<GrFragmentProcessor> SwizzleOutput(sk_sp<GrFragmentProcessor>, const GrSwizzle&); |
| 76 | |
| 77 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 78 | * Returns a fragment processor that runs the passed in array of fragment processors in a |
| 79 | * series. The original input is passed to the first, the first's output is passed to the |
| 80 | * second, etc. The output of the returned processor is the output of the last processor of the |
| 81 | * series. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 82 | * |
| 83 | * The array elements with be moved. |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 84 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 85 | static sk_sp<GrFragmentProcessor> RunInSeries(sk_sp<GrFragmentProcessor>*, int cnt); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 86 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 87 | ~GrFragmentProcessor() override; |
| 88 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 89 | /** |
| 90 | * Makes a copy of this fragment processor that draws equivalently to the original. |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame^] | 91 | * If the processor has child processors they are cloned as well. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 92 | */ |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame^] | 93 | virtual sk_sp<GrFragmentProcessor> clone() const = 0; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 94 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 95 | GrGLSLFragmentProcessor* createGLSLInstance() const; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 96 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 97 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 98 | this->onGetGLSLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 99 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 100 | fChildProcessors[i]->getGLSLProcessorKey(caps, b); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 104 | int numCoordTransforms() const { return fCoordTransforms.count(); } |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 105 | |
| 106 | /** Returns the coordinate transformation at index. index must be valid according to |
| 107 | numTransforms(). */ |
| 108 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 109 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 110 | const SkTArray<const GrCoordTransform*, true>& coordTransforms() const { |
| 111 | return fCoordTransforms; |
| 112 | } |
| 113 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 114 | int numChildProcessors() const { return fChildProcessors.count(); } |
| 115 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 116 | const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 117 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 118 | bool instantiate(GrResourceProvider*) const; |
| 119 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 120 | /** Do any of the coordtransforms for this processor require local coords? */ |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 121 | bool usesLocalCoords() const { return SkToBool(fFlags & kUsesLocalCoords_Flag); } |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 122 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 123 | /** |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 124 | * A GrDrawOp may premultiply its antialiasing coverage into its GrGeometryProcessor's color |
| 125 | * output under the following scenario: |
| 126 | * * all the color fragment processors report true to this query, |
| 127 | * * all the coverage fragment processors report true to this query, |
| 128 | * * the blend mode arithmetic allows for it it. |
| 129 | * To be compatible a fragment processor's output must be a modulation of its input color or |
| 130 | * alpha with a computed premultiplied color or alpha that is in 0..1 range. The computed color |
| 131 | * or alpha that is modulated against the input cannot depend on the input's alpha. The computed |
| 132 | * value cannot depend on the input's color channels unless it unpremultiplies the input color |
| 133 | * channels by the input alpha. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 134 | */ |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 135 | bool compatibleWithCoverageAsAlpha() const { |
| 136 | return SkToBool(fFlags & kCompatibleWithCoverageAsAlpha_OptimizationFlag); |
| 137 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * If this is true then all opaque input colors to the processor produce opaque output colors. |
| 141 | */ |
| 142 | bool preservesOpaqueInput() const { |
| 143 | return SkToBool(fFlags & kPreservesOpaqueInput_OptimizationFlag); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Tests whether given a constant input color the processor produces a constant output color |
| 148 | * (for all fragments). If true outputColor will contain the constant color produces for |
| 149 | * inputColor. |
| 150 | */ |
| 151 | bool hasConstantOutputForConstantInput(GrColor4f inputColor, GrColor4f* outputColor) const { |
| 152 | if (fFlags & kConstantOutputForConstantInput_OptimizationFlag) { |
| 153 | *outputColor = this->constantOutputForConstantInput(inputColor); |
| 154 | return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | bool hasConstantOutputForConstantInput() const { |
| 159 | return SkToBool(fFlags & kConstantOutputForConstantInput_OptimizationFlag); |
| 160 | } |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 161 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 162 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 163 | true when the two processor are of the same subclass (i.e. they return the same object from |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 164 | from getFactory()). |
| 165 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 166 | A return value of true from isEqual() should not be used to test whether the processor would |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 167 | generate the same shader code. To test for identical code generation use getGLSLProcessorKey |
| 168 | */ |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 169 | bool isEqual(const GrFragmentProcessor& that) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 170 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 171 | /** |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 172 | * Pre-order traversal of a FP hierarchy, or of the forest of FPs in a GrPipeline. In the latter |
| 173 | * case the tree rooted at each FP in the GrPipeline is visited successively. |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 174 | */ |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 175 | class Iter : public SkNoncopyable { |
| 176 | public: |
| 177 | explicit Iter(const GrFragmentProcessor* fp) { fFPStack.push_back(fp); } |
| 178 | explicit Iter(const GrPipeline& pipeline); |
| 179 | const GrFragmentProcessor* next(); |
| 180 | |
| 181 | private: |
| 182 | SkSTArray<4, const GrFragmentProcessor*, true> fFPStack; |
| 183 | }; |
| 184 | |
| 185 | /** |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 186 | * Iterates over all the Ts owned by a GrFragmentProcessor and its children or over all the Ts |
| 187 | * owned by the forest of GrFragmentProcessors in a GrPipeline. FPs are visited in the same |
| 188 | * order as Iter and each of an FP's Ts are visited in order. |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 189 | */ |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 190 | template <typename T, typename BASE, |
| 191 | int (BASE::*COUNT)() const, |
| 192 | const T& (BASE::*GET)(int) const> |
| 193 | class FPItemIter : public SkNoncopyable { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 194 | public: |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 195 | explicit FPItemIter(const GrFragmentProcessor* fp) |
| 196 | : fCurrFP(nullptr) |
| 197 | , fCTIdx(0) |
| 198 | , fFPIter(fp) { |
| 199 | fCurrFP = fFPIter.next(); |
| 200 | } |
| 201 | explicit FPItemIter(const GrPipeline& pipeline) |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 202 | : fCurrFP(nullptr) |
| 203 | , fCTIdx(0) |
| 204 | , fFPIter(pipeline) { |
| 205 | fCurrFP = fFPIter.next(); |
| 206 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 207 | |
| 208 | const T* next() { |
| 209 | if (!fCurrFP) { |
| 210 | return nullptr; |
| 211 | } |
| 212 | while (fCTIdx == (fCurrFP->*COUNT)()) { |
| 213 | fCTIdx = 0; |
| 214 | fCurrFP = fFPIter.next(); |
| 215 | if (!fCurrFP) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | } |
| 219 | return &(fCurrFP->*GET)(fCTIdx++); |
| 220 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 221 | |
| 222 | private: |
| 223 | const GrFragmentProcessor* fCurrFP; |
| 224 | int fCTIdx; |
| 225 | GrFragmentProcessor::Iter fFPIter; |
| 226 | }; |
| 227 | |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 228 | using CoordTransformIter = FPItemIter<GrCoordTransform, |
| 229 | GrFragmentProcessor, |
| 230 | &GrFragmentProcessor::numCoordTransforms, |
| 231 | &GrFragmentProcessor::coordTransform>; |
| 232 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 233 | using TextureAccessIter = FPItemIter<TextureSampler, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 234 | GrResourceIOProcessor, |
| 235 | &GrResourceIOProcessor::numTextureSamplers, |
| 236 | &GrResourceIOProcessor::textureSampler>; |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 237 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 238 | protected: |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 239 | enum OptimizationFlags : uint32_t { |
| 240 | kNone_OptimizationFlags, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 241 | kCompatibleWithCoverageAsAlpha_OptimizationFlag = 0x1, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 242 | kPreservesOpaqueInput_OptimizationFlag = 0x2, |
| 243 | kConstantOutputForConstantInput_OptimizationFlag = 0x4, |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 244 | kAll_OptimizationFlags = kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 245 | kPreservesOpaqueInput_OptimizationFlag | |
| 246 | kConstantOutputForConstantInput_OptimizationFlag |
| 247 | }; |
| 248 | GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags) |
| 249 | |
Brian Salomon | 6cd51b5 | 2017-07-26 19:07:15 -0400 | [diff] [blame] | 250 | /** |
| 251 | * Can be used as a helper to decide which fragment processor OptimizationFlags should be set. |
| 252 | * This assumes that the subclass output color will be a modulation of the input color with a |
| 253 | * value read from a texture of the passed config and that the texture contains premultiplied |
| 254 | * color or alpha values that are in range. |
| 255 | */ |
| 256 | static OptimizationFlags ModulateByConfigOptimizationFlags(GrPixelConfig config) { |
| 257 | if (GrPixelConfigIsOpaque(config)) { |
| 258 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
| 259 | kPreservesOpaqueInput_OptimizationFlag; |
| 260 | } else { |
| 261 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 262 | } |
| 263 | } |
| 264 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 265 | GrFragmentProcessor(OptimizationFlags optimizationFlags) : fFlags(optimizationFlags) { |
| 266 | SkASSERT((fFlags & ~kAll_OptimizationFlags) == 0); |
| 267 | } |
| 268 | |
| 269 | OptimizationFlags optimizationFlags() const { |
| 270 | return static_cast<OptimizationFlags>(kAll_OptimizationFlags & fFlags); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * This allows one subclass to access another subclass's implementation of |
| 275 | * constantOutputForConstantInput. It must only be called when |
| 276 | * hasConstantOutputForConstantInput() is known to be true. |
| 277 | */ |
| 278 | static GrColor4f ConstantOutputForConstantInput(const GrFragmentProcessor& fp, |
| 279 | GrColor4f input) { |
| 280 | SkASSERT(fp.hasConstantOutputForConstantInput()); |
| 281 | return fp.constantOutputForConstantInput(input); |
| 282 | } |
| 283 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 284 | /** |
| 285 | * Fragment Processor subclasses call this from their constructor to register coordinate |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 286 | * transformations. Coord transforms provide a mechanism for a processor to receive coordinates |
| 287 | * in their FS code. The matrix expresses a transformation from local space. For a given |
| 288 | * fragment the matrix will be applied to the local coordinate that maps to the fragment. |
| 289 | * |
| 290 | * When the transformation has perspective, the transformed coordinates will have |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 291 | * 3 components. Otherwise they'll have 2. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 292 | * |
| 293 | * This must only be called from the constructor because GrProcessors are immutable. The |
| 294 | * processor subclass manages the lifetime of the transformations (this function only stores a |
mtklein | 790d74f | 2015-08-19 11:05:39 -0700 | [diff] [blame] | 295 | * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass. |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 296 | * |
| 297 | * A processor subclass that has multiple methods of construction should always add its coord |
| 298 | * transforms in a consistent order. The non-virtual implementation of isEqual() automatically |
| 299 | * compares transforms and will assume they line up across the two processor instances. |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 300 | */ |
| 301 | void addCoordTransform(const GrCoordTransform*); |
| 302 | |
| 303 | /** |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 304 | * FragmentProcessor subclasses call this from their constructor to register any child |
wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 305 | * FragmentProcessors they have. This must be called AFTER all texture accesses and coord |
| 306 | * transforms have been added. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 307 | * This is for processors whose shader code will be composed of nested processors whose output |
| 308 | * colors will be combined somehow to produce its output color. Registering these child |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 309 | * processors will allow the ProgramBuilder to automatically handle their transformed coords and |
| 310 | * texture accesses and mangle their uniform and output color names. |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 311 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 312 | int registerChildProcessor(sk_sp<GrFragmentProcessor> child); |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 313 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 314 | private: |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 315 | void addPendingIOs() const override { GrResourceIOProcessor::addPendingIOs(); } |
| 316 | void removeRefs() const override { GrResourceIOProcessor::removeRefs(); } |
| 317 | void pendingIOComplete() const override { GrResourceIOProcessor::pendingIOComplete(); } |
| 318 | |
bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 319 | void notifyRefCntIsZero() const final; |
| 320 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 321 | virtual GrColor4f constantOutputForConstantInput(GrColor4f /* inputColor */) const { |
| 322 | SkFAIL("Subclass must override this if advertising this optimization."); |
| 323 | return GrColor4f::TransparentBlack(); |
| 324 | } |
| 325 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 326 | /** Returns a new instance of the appropriate *GL* implementation class |
| 327 | for the given GrFragmentProcessor; caller is responsible for deleting |
| 328 | the object. */ |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 329 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 330 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 331 | /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 332 | virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 333 | |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Subclass implements this to support isEqual(). It will only be called if it is known that |
| 336 | * the two processors are of the same subclass (i.e. they return the same object from |
| 337 | * getFactory()). The processor subclass should not compare its coord transforms as that will |
| 338 | * be performed automatically in the non-virtual isEqual(). |
| 339 | */ |
| 340 | virtual bool onIsEqual(const GrFragmentProcessor&) const = 0; |
| 341 | |
| 342 | bool hasSameTransforms(const GrFragmentProcessor&) const; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 343 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 344 | enum PrivateFlags { |
| 345 | kFirstPrivateFlag = kAll_OptimizationFlags + 1, |
| 346 | kUsesLocalCoords_Flag = kFirstPrivateFlag, |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | mutable uint32_t fFlags = 0; |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 350 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 351 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
| 352 | |
wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 353 | /** |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 354 | * This is not SkSTArray<1, sk_sp<GrFragmentProcessor>> because this class holds strong |
| 355 | * references until notifyRefCntIsZero and then it holds pending executions. |
| 356 | */ |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 357 | SkSTArray<1, GrFragmentProcessor*, true> fChildProcessors; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 358 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 359 | typedef GrResourceIOProcessor INHERITED; |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 360 | }; |
| 361 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 362 | GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags) |
| 363 | |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 364 | #endif |