blob: 44004c023faf934e2b16f52dd0b6bbf69a9423ad [file] [log] [blame]
bsalomon6251d172014-10-15 10:50:36 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrProcessor.h"
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060012#include "src/gpu/ops/GrOp.h"
bsalomon6251d172014-10-15 10:50:36 -070013
14class GrCoordTransform;
egdaniel64c47282015-11-13 06:54:19 -080015class GrGLSLFragmentProcessor;
Chris Dalton1c548942018-05-22 13:09:48 -060016class GrPaint;
bsalomona624bf32016-09-20 09:12:47 -070017class GrPipeline;
joshualitteb2a6762014-12-04 11:35:33 -080018class GrProcessorKeyBuilder;
Brian Salomon94efbf52016-11-29 13:43:05 -050019class GrShaderCaps;
Brian Osmance425512017-03-22 14:37:50 -040020class GrSwizzle;
bsalomon6251d172014-10-15 10:50:36 -070021
Ethan Nicholasf7b88202017-09-18 14:10:39 -040022/** Provides custom fragment shader code. Fragment processors receive an input color (half4) and
bsalomon6251d172014-10-15 10:50:36 -070023 produce an output color. They may reference textures and uniforms. They may use
24 GrCoordTransforms to receive a transformation of the local coordinates that map from local space
25 to the fragment being processed.
26 */
Brian Salomone782f842018-07-31 13:53:11 -040027class GrFragmentProcessor : public GrProcessor {
bsalomon6251d172014-10-15 10:50:36 -070028public:
Brian Salomone782f842018-07-31 13:53:11 -040029 class TextureSampler;
30
bsalomon87ba62e2015-09-22 06:41:59 -070031 /**
32 * In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to
33 * only consider the input color's alpha. However, there is a competing desire to have reusable
34 * GrFragmentProcessor subclasses that can be used in other scenarios where the entire input
35 * color is considered. This function exists to filter the input color and pass it to a FP. It
36 * does so by returning a parent FP that multiplies the passed in FPs output by the parent's
37 * input alpha. The passed in FP will not receive an input color.
38 */
Mike Reed28eaed22018-02-01 11:24:53 -050039 static std::unique_ptr<GrFragmentProcessor> MulChildByInputAlpha(
40 std::unique_ptr<GrFragmentProcessor> child);
41
42 /**
43 * Like MulChildByInputAlpha(), but reverses the sense of src and dst. In this case, return
44 * the input modulated by the child's alpha. The passed in FP will not receive an input color.
45 *
46 * output = input * child.a
47 */
48 static std::unique_ptr<GrFragmentProcessor> MulInputByChildAlpha(
49 std::unique_ptr<GrFragmentProcessor> child);
bsalomonf1b7a1d2015-09-28 06:26:28 -070050
51 /**
Brian Salomon22af73f2017-01-26 11:25:12 -050052 * This assumes that the input color to the returned processor will be unpremul and that the
53 * passed processor (which becomes the returned processor's child) produces a premul output.
54 * The result of the returned processor is a premul of its input color modulated by the child
55 * processor's premul output.
bsalomonf1b7a1d2015-09-28 06:26:28 -070056 */
Brian Salomonaff329b2017-08-11 09:40:37 -040057 static std::unique_ptr<GrFragmentProcessor> MakeInputPremulAndMulByOutput(
58 std::unique_ptr<GrFragmentProcessor>);
bsalomonf1b7a1d2015-09-28 06:26:28 -070059
60 /**
bsalomone25eea42015-09-29 06:38:55 -070061 * Returns a parent fragment processor that adopts the passed fragment processor as a child.
62 * The parent will ignore its input color and instead feed the passed in color as input to the
63 * child.
bsalomonf1b7a1d2015-09-28 06:26:28 -070064 */
Brian Salomonaff329b2017-08-11 09:40:37 -040065 static std::unique_ptr<GrFragmentProcessor> OverrideInput(std::unique_ptr<GrFragmentProcessor>,
Brian Salomonc0d79e52019-04-10 15:02:11 -040066 const SkPMColor4f&,
67 bool useUniform = true);
bsalomon87ba62e2015-09-22 06:41:59 -070068
bsalomone25eea42015-09-29 06:38:55 -070069 /**
dvonbeckc526da92016-07-20 11:20:30 -070070 * Returns a fragment processor that premuls the input before calling the passed in fragment
71 * processor.
72 */
Brian Salomonaff329b2017-08-11 09:40:37 -040073 static std::unique_ptr<GrFragmentProcessor> PremulInput(std::unique_ptr<GrFragmentProcessor>);
dvonbeckc526da92016-07-20 11:20:30 -070074
75 /**
Brian Osmance425512017-03-22 14:37:50 -040076 * Returns a fragment processor that calls the passed in fragment processor, and then swizzles
77 * the output.
78 */
Brian Salomonaff329b2017-08-11 09:40:37 -040079 static std::unique_ptr<GrFragmentProcessor> SwizzleOutput(std::unique_ptr<GrFragmentProcessor>,
80 const GrSwizzle&);
Brian Osmance425512017-03-22 14:37:50 -040081
82 /**
bsalomone25eea42015-09-29 06:38:55 -070083 * Returns a fragment processor that runs the passed in array of fragment processors in a
84 * series. The original input is passed to the first, the first's output is passed to the
85 * second, etc. The output of the returned processor is the output of the last processor of the
86 * series.
bungeman06ca8ec2016-06-09 08:01:03 -070087 *
88 * The array elements with be moved.
bsalomone25eea42015-09-29 06:38:55 -070089 */
Brian Salomonaff329b2017-08-11 09:40:37 -040090 static std::unique_ptr<GrFragmentProcessor> RunInSeries(std::unique_ptr<GrFragmentProcessor>*,
91 int cnt);
bsalomonac856c92015-08-27 06:30:17 -070092
Brian Salomon0e05a822017-07-25 09:43:22 -040093 /**
94 * Makes a copy of this fragment processor that draws equivalently to the original.
Brian Salomon96271cd2017-07-31 16:27:23 -040095 * If the processor has child processors they are cloned as well.
Brian Salomon0e05a822017-07-25 09:43:22 -040096 */
Brian Salomonaff329b2017-08-11 09:40:37 -040097 virtual std::unique_ptr<GrFragmentProcessor> clone() const = 0;
Brian Salomon0e05a822017-07-25 09:43:22 -040098
egdaniel57d3b032015-11-13 11:57:27 -080099 GrGLSLFragmentProcessor* createGLSLInstance() const;
joshualitteb2a6762014-12-04 11:35:33 -0800100
Brian Salomon94efbf52016-11-29 13:43:05 -0500101 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
egdaniel57d3b032015-11-13 11:57:27 -0800102 this->onGetGLSLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -0700103 for (int i = 0; i < fChildProcessors.count(); ++i) {
egdaniel57d3b032015-11-13 11:57:27 -0800104 fChildProcessors[i]->getGLSLProcessorKey(caps, b);
wangyix4b3050b2015-08-04 07:59:37 -0700105 }
106 }
107
Brian Salomone782f842018-07-31 13:53:11 -0400108 int numTextureSamplers() const { return fTextureSamplerCnt; }
109 const TextureSampler& textureSampler(int i) const;
110
bsalomona624bf32016-09-20 09:12:47 -0700111 int numCoordTransforms() const { return fCoordTransforms.count(); }
bsalomon6251d172014-10-15 10:50:36 -0700112
113 /** Returns the coordinate transformation at index. index must be valid according to
114 numTransforms(). */
115 const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
116
joshualittabb52a12015-01-13 15:02:10 -0800117 const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
118 return fCoordTransforms;
119 }
120
wangyix4b3050b2015-08-04 07:59:37 -0700121 int numChildProcessors() const { return fChildProcessors.count(); }
122
bsalomonac856c92015-08-27 06:30:17 -0700123 const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
wangyix4b3050b2015-08-04 07:59:37 -0700124
Robert Phillips9bee2e52017-05-29 12:37:20 -0400125 bool instantiate(GrResourceProvider*) const;
126
joshualitt290c09b2014-12-19 13:45:20 -0800127 /** Do any of the coordtransforms for this processor require local coords? */
Brian Salomon587e08f2017-01-27 10:59:27 -0500128 bool usesLocalCoords() const { return SkToBool(fFlags & kUsesLocalCoords_Flag); }
joshualitt290c09b2014-12-19 13:45:20 -0800129
Brian Salomon587e08f2017-01-27 10:59:27 -0500130 /**
Brian Salomonf3b995b2017-02-15 10:22:23 -0500131 * A GrDrawOp may premultiply its antialiasing coverage into its GrGeometryProcessor's color
132 * output under the following scenario:
133 * * all the color fragment processors report true to this query,
134 * * all the coverage fragment processors report true to this query,
135 * * the blend mode arithmetic allows for it it.
136 * To be compatible a fragment processor's output must be a modulation of its input color or
137 * alpha with a computed premultiplied color or alpha that is in 0..1 range. The computed color
138 * or alpha that is modulated against the input cannot depend on the input's alpha. The computed
139 * value cannot depend on the input's color channels unless it unpremultiplies the input color
140 * channels by the input alpha.
Brian Salomon587e08f2017-01-27 10:59:27 -0500141 */
Brian Salomonf3b995b2017-02-15 10:22:23 -0500142 bool compatibleWithCoverageAsAlpha() const {
143 return SkToBool(fFlags & kCompatibleWithCoverageAsAlpha_OptimizationFlag);
144 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500145
146 /**
147 * If this is true then all opaque input colors to the processor produce opaque output colors.
148 */
149 bool preservesOpaqueInput() const {
150 return SkToBool(fFlags & kPreservesOpaqueInput_OptimizationFlag);
151 }
152
153 /**
154 * Tests whether given a constant input color the processor produces a constant output color
155 * (for all fragments). If true outputColor will contain the constant color produces for
156 * inputColor.
157 */
Brian Osman1d5b5982018-10-01 13:41:39 -0400158 bool hasConstantOutputForConstantInput(SkPMColor4f inputColor, SkPMColor4f* outputColor) const {
Brian Salomon587e08f2017-01-27 10:59:27 -0500159 if (fFlags & kConstantOutputForConstantInput_OptimizationFlag) {
160 *outputColor = this->constantOutputForConstantInput(inputColor);
161 return true;
162 }
163 return false;
164 }
165 bool hasConstantOutputForConstantInput() const {
166 return SkToBool(fFlags & kConstantOutputForConstantInput_OptimizationFlag);
167 }
dvonbeck9b03e7b2016-08-01 11:01:56 -0700168
joshualitteb2a6762014-12-04 11:35:33 -0800169 /** Returns true if this and other processor conservatively draw identically. It can only return
170 true when the two processor are of the same subclass (i.e. they return the same object from
bsalomon6251d172014-10-15 10:50:36 -0700171 from getFactory()).
172
joshualitteb2a6762014-12-04 11:35:33 -0800173 A return value of true from isEqual() should not be used to test whether the processor would
egdaniel57d3b032015-11-13 11:57:27 -0800174 generate the same shader code. To test for identical code generation use getGLSLProcessorKey
175 */
bsalomon7312ff82016-09-12 08:55:38 -0700176 bool isEqual(const GrFragmentProcessor& that) const;
bsalomon6251d172014-10-15 10:50:36 -0700177
joshualitt56995b52014-12-11 15:44:02 -0800178 /**
bsalomona624bf32016-09-20 09:12:47 -0700179 * Pre-order traversal of a FP hierarchy, or of the forest of FPs in a GrPipeline. In the latter
180 * case the tree rooted at each FP in the GrPipeline is visited successively.
bsalomonb58a2b42016-09-26 06:55:02 -0700181 */
bsalomona624bf32016-09-20 09:12:47 -0700182 class Iter : public SkNoncopyable {
183 public:
184 explicit Iter(const GrFragmentProcessor* fp) { fFPStack.push_back(fp); }
185 explicit Iter(const GrPipeline& pipeline);
Chris Dalton1c548942018-05-22 13:09:48 -0600186 explicit Iter(const GrPaint&);
bsalomona624bf32016-09-20 09:12:47 -0700187 const GrFragmentProcessor* next();
188
189 private:
190 SkSTArray<4, const GrFragmentProcessor*, true> fFPStack;
191 };
192
193 /**
bsalomonb58a2b42016-09-26 06:55:02 -0700194 * Iterates over all the Ts owned by a GrFragmentProcessor and its children or over all the Ts
195 * owned by the forest of GrFragmentProcessors in a GrPipeline. FPs are visited in the same
196 * order as Iter and each of an FP's Ts are visited in order.
bsalomona624bf32016-09-20 09:12:47 -0700197 */
Brian Salomone782f842018-07-31 13:53:11 -0400198 template <typename T, int (GrFragmentProcessor::*COUNT)() const,
199 const T& (GrFragmentProcessor::*GET)(int)const>
bsalomonb58a2b42016-09-26 06:55:02 -0700200 class FPItemIter : public SkNoncopyable {
bsalomona624bf32016-09-20 09:12:47 -0700201 public:
bsalomonb58a2b42016-09-26 06:55:02 -0700202 explicit FPItemIter(const GrFragmentProcessor* fp)
203 : fCurrFP(nullptr)
204 , fCTIdx(0)
205 , fFPIter(fp) {
206 fCurrFP = fFPIter.next();
207 }
208 explicit FPItemIter(const GrPipeline& pipeline)
bsalomona624bf32016-09-20 09:12:47 -0700209 : fCurrFP(nullptr)
210 , fCTIdx(0)
211 , fFPIter(pipeline) {
212 fCurrFP = fFPIter.next();
213 }
bsalomonb58a2b42016-09-26 06:55:02 -0700214
215 const T* next() {
216 if (!fCurrFP) {
217 return nullptr;
218 }
219 while (fCTIdx == (fCurrFP->*COUNT)()) {
220 fCTIdx = 0;
221 fCurrFP = fFPIter.next();
222 if (!fCurrFP) {
223 return nullptr;
224 }
225 }
226 return &(fCurrFP->*GET)(fCTIdx++);
227 }
bsalomona624bf32016-09-20 09:12:47 -0700228
229 private:
230 const GrFragmentProcessor* fCurrFP;
231 int fCTIdx;
232 GrFragmentProcessor::Iter fFPIter;
233 };
234
bsalomonb58a2b42016-09-26 06:55:02 -0700235 using CoordTransformIter = FPItemIter<GrCoordTransform,
bsalomonb58a2b42016-09-26 06:55:02 -0700236 &GrFragmentProcessor::numCoordTransforms,
237 &GrFragmentProcessor::coordTransform>;
238
Brian Salomon0bbecb22016-11-17 11:38:22 -0500239 using TextureAccessIter = FPItemIter<TextureSampler,
Brian Salomone782f842018-07-31 13:53:11 -0400240 &GrFragmentProcessor::numTextureSamplers,
241 &GrFragmentProcessor::textureSampler>;
bsalomonb58a2b42016-09-26 06:55:02 -0700242
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600243 void visitProxies(const GrOp::VisitProxyFunc& func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400244
bsalomon6251d172014-10-15 10:50:36 -0700245protected:
Brian Salomon587e08f2017-01-27 10:59:27 -0500246 enum OptimizationFlags : uint32_t {
247 kNone_OptimizationFlags,
Brian Salomonf3b995b2017-02-15 10:22:23 -0500248 kCompatibleWithCoverageAsAlpha_OptimizationFlag = 0x1,
Brian Salomon587e08f2017-01-27 10:59:27 -0500249 kPreservesOpaqueInput_OptimizationFlag = 0x2,
250 kConstantOutputForConstantInput_OptimizationFlag = 0x4,
Brian Salomonf3b995b2017-02-15 10:22:23 -0500251 kAll_OptimizationFlags = kCompatibleWithCoverageAsAlpha_OptimizationFlag |
Brian Salomon587e08f2017-01-27 10:59:27 -0500252 kPreservesOpaqueInput_OptimizationFlag |
253 kConstantOutputForConstantInput_OptimizationFlag
254 };
255 GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags)
256
Brian Salomon6cd51b52017-07-26 19:07:15 -0400257 /**
258 * Can be used as a helper to decide which fragment processor OptimizationFlags should be set.
259 * This assumes that the subclass output color will be a modulation of the input color with a
260 * value read from a texture of the passed config and that the texture contains premultiplied
261 * color or alpha values that are in range.
Michael Ludwig257a03d2018-12-13 14:07:07 -0500262 *
263 * Since there are multiple ways in which a sampler may have its coordinates clamped or wrapped,
264 * callers must determine on their own if the sampling uses a decal strategy in any way, in
265 * which case the texture may become transparent regardless of the pixel config.
Brian Salomon6cd51b52017-07-26 19:07:15 -0400266 */
Michael Ludwig257a03d2018-12-13 14:07:07 -0500267 static OptimizationFlags ModulateForSamplerOptFlags(GrPixelConfig config, bool samplingDecal) {
268 if (samplingDecal) {
269 return kCompatibleWithCoverageAsAlpha_OptimizationFlag;
270 } else {
271 return ModulateForClampedSamplerOptFlags(config);
272 }
273 }
274
275 // As above, but callers should somehow ensure or assert their sampler still uses clamping
276 static OptimizationFlags ModulateForClampedSamplerOptFlags(GrPixelConfig config) {
Brian Salomon6cd51b52017-07-26 19:07:15 -0400277 if (GrPixelConfigIsOpaque(config)) {
278 return kCompatibleWithCoverageAsAlpha_OptimizationFlag |
279 kPreservesOpaqueInput_OptimizationFlag;
280 } else {
281 return kCompatibleWithCoverageAsAlpha_OptimizationFlag;
282 }
283 }
284
Ethan Nicholasabff9562017-10-09 10:54:08 -0400285 GrFragmentProcessor(ClassID classID, OptimizationFlags optimizationFlags)
Robert Phillips39667382019-04-17 16:03:30 -0400286 : INHERITED(classID)
287 , fFlags(optimizationFlags) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500288 SkASSERT((fFlags & ~kAll_OptimizationFlags) == 0);
289 }
290
291 OptimizationFlags optimizationFlags() const {
292 return static_cast<OptimizationFlags>(kAll_OptimizationFlags & fFlags);
293 }
294
Brian Salomonc0d79e52019-04-10 15:02:11 -0400295 /** Useful when you can't call fp->optimizationFlags() on a base class object from a subclass.*/
296 static OptimizationFlags ProcessorOptimizationFlags(const GrFragmentProcessor* fp) {
297 return fp->optimizationFlags();
298 }
299
Brian Salomon587e08f2017-01-27 10:59:27 -0500300 /**
301 * This allows one subclass to access another subclass's implementation of
302 * constantOutputForConstantInput. It must only be called when
303 * hasConstantOutputForConstantInput() is known to be true.
304 */
Brian Osman1d5b5982018-10-01 13:41:39 -0400305 static SkPMColor4f ConstantOutputForConstantInput(const GrFragmentProcessor& fp,
306 const SkPMColor4f& input) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500307 SkASSERT(fp.hasConstantOutputForConstantInput());
308 return fp.constantOutputForConstantInput(input);
309 }
310
bsalomon6251d172014-10-15 10:50:36 -0700311 /**
312 * Fragment Processor subclasses call this from their constructor to register coordinate
bsalomonde258cd2014-10-15 19:06:21 -0700313 * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
314 * in their FS code. The matrix expresses a transformation from local space. For a given
315 * fragment the matrix will be applied to the local coordinate that maps to the fragment.
316 *
317 * When the transformation has perspective, the transformed coordinates will have
mtklein790d74f2015-08-19 11:05:39 -0700318 * 3 components. Otherwise they'll have 2.
bsalomonde258cd2014-10-15 19:06:21 -0700319 *
320 * This must only be called from the constructor because GrProcessors are immutable. The
321 * processor subclass manages the lifetime of the transformations (this function only stores a
mtklein790d74f2015-08-19 11:05:39 -0700322 * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
bsalomonde258cd2014-10-15 19:06:21 -0700323 *
324 * A processor subclass that has multiple methods of construction should always add its coord
325 * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
326 * compares transforms and will assume they line up across the two processor instances.
bsalomon6251d172014-10-15 10:50:36 -0700327 */
328 void addCoordTransform(const GrCoordTransform*);
329
330 /**
wangyix58d890b2015-08-12 09:40:47 -0700331 * FragmentProcessor subclasses call this from their constructor to register any child
wangyix93ab2542015-08-19 08:23:12 -0700332 * FragmentProcessors they have. This must be called AFTER all texture accesses and coord
333 * transforms have been added.
wangyix4b3050b2015-08-04 07:59:37 -0700334 * This is for processors whose shader code will be composed of nested processors whose output
335 * colors will be combined somehow to produce its output color. Registering these child
wangyix58d890b2015-08-12 09:40:47 -0700336 * processors will allow the ProgramBuilder to automatically handle their transformed coords and
337 * texture accesses and mangle their uniform and output color names.
wangyix4b3050b2015-08-04 07:59:37 -0700338 */
Brian Salomonaff329b2017-08-11 09:40:37 -0400339 int registerChildProcessor(std::unique_ptr<GrFragmentProcessor> child);
wangyix4b3050b2015-08-04 07:59:37 -0700340
Brian Salomone782f842018-07-31 13:53:11 -0400341 void setTextureSamplerCnt(int cnt) {
342 SkASSERT(cnt >= 0);
343 fTextureSamplerCnt = cnt;
344 }
345
346 /**
347 * Helper for implementing onTextureSampler(). E.g.:
348 * return IthTexureSampler(i, fMyFirstSampler, fMySecondSampler, fMyThirdSampler);
349 */
350 template <typename... Args>
351 static const TextureSampler& IthTextureSampler(int i, const TextureSampler& samp0,
352 const Args&... samps) {
353 return (0 == i) ? samp0 : IthTextureSampler(i - 1, samps...);
354 }
355 inline static const TextureSampler& IthTextureSampler(int i);
356
bsalomon6251d172014-10-15 10:50:36 -0700357private:
Brian Osman1d5b5982018-10-01 13:41:39 -0400358 virtual SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& /* inputColor */) const {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400359 SK_ABORT("Subclass must override this if advertising this optimization.");
Brian Osmanf28e55d2018-10-03 16:35:54 -0400360 return SK_PMColor4fTRANSPARENT;
Brian Salomon587e08f2017-01-27 10:59:27 -0500361 }
362
wangyixb1daa862015-08-18 11:29:31 -0700363 /** Returns a new instance of the appropriate *GL* implementation class
364 for the given GrFragmentProcessor; caller is responsible for deleting
365 the object. */
egdaniel57d3b032015-11-13 11:57:27 -0800366 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0;
wangyixb1daa862015-08-18 11:29:31 -0700367
wangyix4b3050b2015-08-04 07:59:37 -0700368 /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
Brian Salomon94efbf52016-11-29 13:43:05 -0500369 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
wangyix4b3050b2015-08-04 07:59:37 -0700370
bsalomonde258cd2014-10-15 19:06:21 -0700371 /**
372 * Subclass implements this to support isEqual(). It will only be called if it is known that
373 * the two processors are of the same subclass (i.e. they return the same object from
374 * getFactory()). The processor subclass should not compare its coord transforms as that will
375 * be performed automatically in the non-virtual isEqual().
376 */
377 virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
378
Brian Salomone782f842018-07-31 13:53:11 -0400379 virtual const TextureSampler& onTextureSampler(int) const { return IthTextureSampler(0); }
380
bsalomonde258cd2014-10-15 19:06:21 -0700381 bool hasSameTransforms(const GrFragmentProcessor&) const;
bsalomon6251d172014-10-15 10:50:36 -0700382
Brian Salomon587e08f2017-01-27 10:59:27 -0500383 enum PrivateFlags {
384 kFirstPrivateFlag = kAll_OptimizationFlags + 1,
385 kUsesLocalCoords_Flag = kFirstPrivateFlag,
Brian Salomon587e08f2017-01-27 10:59:27 -0500386 };
387
388 mutable uint32_t fFlags = 0;
wangyix58d890b2015-08-12 09:40:47 -0700389
Brian Salomone782f842018-07-31 13:53:11 -0400390 int fTextureSamplerCnt = 0;
391
bsalomona624bf32016-09-20 09:12:47 -0700392 SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
393
Brian Salomonaff329b2017-08-11 09:40:37 -0400394 SkSTArray<1, std::unique_ptr<GrFragmentProcessor>, true> fChildProcessors;
bsalomon6251d172014-10-15 10:50:36 -0700395
Brian Salomone782f842018-07-31 13:53:11 -0400396 typedef GrProcessor INHERITED;
bsalomon6251d172014-10-15 10:50:36 -0700397};
398
Brian Salomone782f842018-07-31 13:53:11 -0400399/**
400 * Used to represent a texture that is required by a GrFragmentProcessor. It holds a GrTextureProxy
401 * along with an associated GrSamplerState. TextureSamplers don't perform any coord manipulation to
402 * account for texture origin.
403 */
404class GrFragmentProcessor::TextureSampler {
405public:
406 TextureSampler() = default;
407
408 /**
409 * This copy constructor is used by GrFragmentProcessor::clone() implementations. The copy
410 * always takes a new ref on the texture proxy as the new fragment processor will not yet be
411 * in pending execution state.
412 */
413 explicit TextureSampler(const TextureSampler& that)
Robert Phillipsb5204762019-06-19 14:12:13 -0400414 : fProxy(that.fProxy)
Brian Salomone782f842018-07-31 13:53:11 -0400415 , fSamplerState(that.fSamplerState) {}
416
417 TextureSampler(sk_sp<GrTextureProxy>, const GrSamplerState&);
418
419 explicit TextureSampler(sk_sp<GrTextureProxy>,
420 GrSamplerState::Filter = GrSamplerState::Filter::kNearest,
421 GrSamplerState::WrapMode wrapXAndY = GrSamplerState::WrapMode::kClamp);
422
423 TextureSampler& operator=(const TextureSampler&) = delete;
424
425 void reset(sk_sp<GrTextureProxy>, const GrSamplerState&);
426 void reset(sk_sp<GrTextureProxy>,
427 GrSamplerState::Filter = GrSamplerState::Filter::kNearest,
428 GrSamplerState::WrapMode wrapXAndY = GrSamplerState::WrapMode::kClamp);
429
430 bool operator==(const TextureSampler& that) const {
431 return this->proxy()->underlyingUniqueID() == that.proxy()->underlyingUniqueID() &&
432 fSamplerState == that.fSamplerState;
433 }
434
435 bool operator!=(const TextureSampler& other) const { return !(*this == other); }
436
437 // 'instantiate' should only ever be called at flush time.
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400438 // TODO: this can go away once explicit allocation has stuck
Brian Salomone782f842018-07-31 13:53:11 -0400439 bool instantiate(GrResourceProvider* resourceProvider) const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400440 return fProxy->isInstantiated();
Brian Salomone782f842018-07-31 13:53:11 -0400441 }
442
443 // 'peekTexture' should only ever be called after a successful 'instantiate' call
444 GrTexture* peekTexture() const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400445 SkASSERT(fProxy->isInstantiated());
446 return fProxy->peekTexture();
Brian Salomone782f842018-07-31 13:53:11 -0400447 }
448
Robert Phillipsb5204762019-06-19 14:12:13 -0400449 GrTextureProxy* proxy() const { return fProxy.get(); }
Brian Salomone782f842018-07-31 13:53:11 -0400450 const GrSamplerState& samplerState() const { return fSamplerState; }
451
Robert Phillipsb5204762019-06-19 14:12:13 -0400452 bool isInitialized() const { return SkToBool(fProxy.get()); }
Brian Salomone782f842018-07-31 13:53:11 -0400453
454private:
Robert Phillipsb5204762019-06-19 14:12:13 -0400455 sk_sp<GrTextureProxy> fProxy;
456 GrSamplerState fSamplerState;
Brian Salomone782f842018-07-31 13:53:11 -0400457};
458
459//////////////////////////////////////////////////////////////////////////////
460
461const GrFragmentProcessor::TextureSampler& GrFragmentProcessor::IthTextureSampler(int i) {
462 SK_ABORT("Illegal texture sampler index");
463 static const TextureSampler kBogus;
464 return kBogus;
465}
466
Brian Salomon587e08f2017-01-27 10:59:27 -0500467GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags)
468
bsalomon6251d172014-10-15 10:50:36 -0700469#endif