tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 8 | #ifndef GrProcessor_DEFINED |
| 9 | #define GrProcessor_DEFINED |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 10 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrBackendProcessorFactory.h" |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 12 | #include "GrColor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | #include "GrProcessorUnitTest.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 14 | #include "GrProgramElement.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 15 | #include "GrShaderVar.h" |
bsalomon@google.com | 047696c | 2012-09-11 13:29:29 +0000 | [diff] [blame] | 16 | #include "GrTextureAccess.h" |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 17 | #include "GrTypesPriv.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 18 | #include "SkString.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 19 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 20 | class GrBackendProcessorFactory; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 21 | class GrContext; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 22 | class GrCoordTransform; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 23 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 24 | /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the |
| 25 | Ganesh shading pipeline. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 26 | Subclasses must have a function that produces a human-readable name: |
| 27 | static const char* Name(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | GrProcessor objects *must* be immutable: after being constructed, their fields may not change. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 29 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 30 | Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 31 | effect must reach 0 before the thread terminates and the pool is destroyed. To create a static |
| 32 | effect use the macro GR_CREATE_STATIC_EFFECT declared below. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 33 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 34 | class GrProcessor : public GrProgramElement { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 35 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 36 | SK_DECLARE_INST_COUNT(GrProcessor) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 37 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 38 | virtual ~GrProcessor(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 39 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 40 | struct InvariantOutput{ |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 41 | InvariantOutput() : fColor(0), fValidFlags(0), fIsSingleComponent(false), |
| 42 | fNonMulStageFound(false) {} |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 43 | |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 44 | void mulByUnknownOpaqueColor() { |
| 45 | if (this->isOpaque()) { |
| 46 | fValidFlags = kA_GrColorComponentFlag; |
| 47 | fIsSingleComponent = false; |
| 48 | } else { |
| 49 | // Since the current state is not opaque we no longer care if the color being |
| 50 | // multiplied is opaque. |
| 51 | this->mulByUnknownColor(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void mulByUnknownColor() { |
| 56 | if (this->hasZeroAlpha()) { |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 57 | this->internalSetToTransparentBlack(); |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 58 | } else { |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 59 | this->internalSetToUnknown(); |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | void mulByUnknownAlpha() { |
| 64 | if (this->hasZeroAlpha()) { |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 65 | this->internalSetToTransparentBlack(); |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 66 | } else { |
| 67 | // We don't need to change fIsSingleComponent in this case |
| 68 | fValidFlags = 0; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void invalidateComponents(uint8_t invalidateFlags) { |
| 73 | fValidFlags &= ~invalidateFlags; |
| 74 | fIsSingleComponent = false; |
| 75 | } |
| 76 | |
| 77 | void setToTransparentBlack() { |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 78 | this->internalSetToTransparentBlack(); |
| 79 | fNonMulStageFound = true; |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void setToOther(uint8_t validFlags, GrColor color) { |
| 83 | fValidFlags = validFlags; |
| 84 | fColor = color; |
| 85 | fIsSingleComponent = false; |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 86 | fNonMulStageFound = true; |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void setToUnknown() { |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 90 | this->internalSetToUnknown(); |
| 91 | fNonMulStageFound= true; |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 94 | bool isOpaque() const { |
| 95 | return ((fValidFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(fColor)); |
| 96 | } |
| 97 | |
| 98 | bool isSolidWhite() const { |
| 99 | return (fValidFlags == kRGBA_GrColorComponentFlags && 0xFFFFFFFF == fColor); |
| 100 | } |
| 101 | |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 102 | GrColor color() const { return fColor; } |
| 103 | uint8_t validFlags() const { return fValidFlags; } |
| 104 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 105 | /** |
| 106 | * If isSingleComponent is true, then the flag values for r, g, b, and a must all be the |
| 107 | * same. If the flags are all set then all color components must be equal. |
| 108 | */ |
| 109 | SkDEBUGCODE(void validate() const;) |
| 110 | |
| 111 | private: |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 112 | void internalSetToTransparentBlack() { |
| 113 | fValidFlags = kRGBA_GrColorComponentFlags; |
| 114 | fColor = 0; |
| 115 | fIsSingleComponent = true; |
| 116 | } |
| 117 | |
| 118 | void internalSetToUnknown() { |
| 119 | fValidFlags = 0; |
| 120 | fIsSingleComponent = false; |
| 121 | } |
| 122 | |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 123 | bool hasZeroAlpha() const { |
| 124 | return ((fValidFlags & kA_GrColorComponentFlag) && 0 == GrColorUnpackA(fColor)); |
| 125 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 126 | |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 127 | SkDEBUGCODE(bool colorComponentsAllEqual() const;) |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 128 | /** |
| 129 | * If alpha is valid, check that any valid R,G,B values are <= A |
| 130 | */ |
| 131 | SkDEBUGCODE(bool validPreMulColor() const;) |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 132 | |
| 133 | // Friended class that have "controller" code which loop over stages calling |
| 134 | // computeInvarianteOutput(). These controllers may need to manually adjust the internal |
| 135 | // members of InvariantOutput |
| 136 | friend class GrDrawState; |
| 137 | friend class GrOptDrawState; |
| 138 | friend class GrPaint; |
| 139 | |
| 140 | GrColor fColor; |
| 141 | uint32_t fValidFlags; |
| 142 | bool fIsSingleComponent; |
egdaniel | ab84fae | 2014-10-14 06:48:46 -0700 | [diff] [blame^] | 143 | bool fNonMulStageFound; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 146 | /** |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 147 | * This function is used to perform optimizations. When called the invarientOuput param |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 148 | * indicate whether the input components to this effect in the FS will have known values. |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 149 | * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent |
| 150 | * member indicates whether the input will be 1 or 4 bytes. The function updates the members of |
| 151 | * inout to indicate known values of its output. A component of the color member only has |
| 152 | * meaning if the corresponding bit in validFlags is set. |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 153 | */ |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 154 | void computeInvariantOutput(InvariantOutput* inout) const { |
| 155 | this->onComputeInvariantOutput(inout); |
| 156 | #ifdef SK_DEBUG |
| 157 | inout->validate(); |
| 158 | #endif |
| 159 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 160 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 161 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 162 | identification. The factory should be an instance of templated class, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 163 | GrTBackendEffectFactory. It is templated on the subclass of GrProcessor. The subclass must |
| 164 | have a nested type (or typedef) named GLProcessor which will be the subclass of |
| 165 | GrGLProcessor created by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 166 | |
| 167 | Example: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 168 | class MyCustomEffect : public GrProcessor { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 169 | ... |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 170 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 171 | return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 172 | } |
| 173 | ... |
| 174 | }; |
| 175 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 176 | virtual const GrBackendProcessorFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 178 | /** Returns true if this and other effect conservatively draw identically. It can only return |
| 179 | true when the two effects are of the same subclass (i.e. they return the same object from |
| 180 | from getFactory()). |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 181 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 182 | A return value of true from isEqual() should not be used to test whether the effects would |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 183 | generate the same shader code. To test for identical code generation use the effects' keys |
| 184 | computed by the GrBackendEffectFactory. |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 185 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 186 | bool isEqual(const GrProcessor& other) const { |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 187 | if (&this->getFactory() != &other.getFactory()) { |
| 188 | return false; |
| 189 | } |
| 190 | bool result = this->onIsEqual(other); |
| 191 | #ifdef SK_DEBUG |
| 192 | if (result) { |
| 193 | this->assertEquality(other); |
| 194 | } |
| 195 | #endif |
| 196 | return result; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 197 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 198 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 199 | /** Human-meaningful string to identify this effect; may be embedded |
| 200 | in generated shader code. */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 201 | const char* name() const; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 203 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 204 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 205 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 206 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 207 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 208 | |
| 209 | /** Shortcut for textureAccess(index).texture(); */ |
| 210 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 211 | |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 212 | /** Will this effect read the fragment position? */ |
| 213 | bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 214 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 215 | void* operator new(size_t size); |
| 216 | void operator delete(void* target); |
| 217 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 218 | void* operator new(size_t size, void* placement) { |
| 219 | return ::operator new(size, placement); |
| 220 | } |
| 221 | void operator delete(void* target, void* placement) { |
| 222 | ::operator delete(target, placement); |
| 223 | } |
| 224 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 225 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 226 | * Helper for down-casting to a GrProcessor subclass |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 227 | */ |
| 228 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 229 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 230 | protected: |
| 231 | /** |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 232 | * Subclasses call this from their constructor to register GrTextureAccesses. The effect |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 233 | * subclass manages the lifetime of the accesses (this function only stores a pointer). The |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 234 | * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be |
| 235 | * called from the constructor because GrProcessors are immutable. |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 236 | */ |
| 237 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 238 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 239 | GrProcessor() |
| 240 | : fWillReadFragmentPosition(false) {} |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * If the effect will generate a backend-specific effect that will read the fragment position |
| 244 | * in the FS then it must call this method from its constructor. Otherwise, the request to |
| 245 | * access the fragment position will be denied. |
| 246 | */ |
| 247 | void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 248 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 249 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 250 | SkDEBUGCODE(void assertEquality(const GrProcessor& other) const;) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 251 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 252 | /** Subclass implements this to support isEqual(). It will only be called if it is known that |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 253 | the two effects are of the same subclass (i.e. they return the same object from |
| 254 | getFactory()).*/ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 255 | virtual bool onIsEqual(const GrProcessor& other) const = 0; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 256 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Subclass implements this to support getConstantColorComponents(...). |
| 259 | */ |
| 260 | virtual void onComputeInvariantOutput(InvariantOutput* inout) const = 0; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 261 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 262 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 263 | bool fWillReadFragmentPosition; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 264 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 265 | typedef GrProgramElement INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 268 | class GrFragmentProcessor : public GrProcessor { |
| 269 | public: |
| 270 | GrFragmentProcessor() |
| 271 | : INHERITED() |
| 272 | , fWillReadDstColor(false) |
| 273 | , fWillUseInputColor(true) {} |
| 274 | |
| 275 | virtual const GrBackendFragmentProcessorFactory& getFactory() const = 0; |
| 276 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 277 | int numTransforms() const { return fCoordTransforms.count(); } |
| 278 | |
| 279 | /** Returns the coordinate transformation at index. index must be valid according to |
| 280 | numTransforms(). */ |
| 281 | const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; } |
| 282 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | /** Will this effect read the destination pixel value? */ |
| 284 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 285 | |
| 286 | /** Will this effect read the source color value? */ |
| 287 | bool willUseInputColor() const { return fWillUseInputColor; } |
| 288 | |
| 289 | protected: |
| 290 | /** |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 291 | * Fragment Processor subclasses call this from their constructor to register coordinate |
| 292 | * transformations. The processor subclass manages the lifetime of the transformations (this |
| 293 | * function only stores a pointer). The GrCoordTransform is typically a member field of the |
| 294 | * GrProcessor subclass. When the matrix has perspective, the transformed coordinates will have |
| 295 | * 3 components. Otherwise they'll have 2. This must only be called from the constructor because |
| 296 | * GrProcessors are immutable. |
| 297 | */ |
| 298 | void addCoordTransform(const GrCoordTransform*); |
| 299 | |
| 300 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 301 | * If the effect subclass will read the destination pixel value then it must call this function |
| 302 | * from its constructor. Otherwise, when its generated backend-specific effect class attempts |
| 303 | * to generate code that reads the destination pixel it will fail. |
| 304 | */ |
| 305 | void setWillReadDstColor() { fWillReadDstColor = true; } |
| 306 | |
| 307 | /** |
| 308 | * If the effect will generate a result that does not depend on the input color value then it |
| 309 | * must call this function from its constructor. Otherwise, when its generated backend-specific |
| 310 | * code might fail during variable binding due to unused variables. |
| 311 | */ |
| 312 | void setWillNotUseInputColor() { fWillUseInputColor = false; } |
| 313 | |
| 314 | private: |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 315 | SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 316 | bool fWillReadDstColor; |
| 317 | bool fWillUseInputColor; |
| 318 | |
| 319 | typedef GrProcessor INHERITED; |
| 320 | }; |
| 321 | |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 322 | /** |
| 323 | * This creates an effect outside of the effect memory pool. The effect's destructor will be called |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 324 | * at global destruction time. NAME will be the name of the created GrProcessor. |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 325 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 326 | #define GR_CREATE_STATIC_FRAGMENT_PROCESSOR(NAME, EFFECT_CLASS, ARGS) \ |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 327 | static SkAlignedSStorage<sizeof(EFFECT_CLASS)> g_##NAME##_Storage; \ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 328 | static GrFragmentProcessor* \ |
| 329 | NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EFFECT_CLASS, ARGS); \ |
| 330 | static SkAutoTDestroy<GrFragmentProcessor> NAME##_ad(NAME); |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 331 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 332 | #endif |