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 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 8 | #ifndef GrEffect_DEFINED |
| 9 | #define GrEffect_DEFINED |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 11 | #include "GrColor.h" |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 12 | #include "GrEffectUnitTest.h" |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 13 | #include "GrNoncopyable.h" |
| 14 | #include "GrRefCnt.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 15 | #include "GrTexture.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" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 19 | class GrBackendEffectFactory; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 20 | class GrContext; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 21 | class GrEffect; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 22 | class SkString; |
| 23 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 24 | /** |
| 25 | * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue |
| 26 | * new draw operations separately from ownership within a deferred drawing queue. When the |
| 27 | * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled |
| 28 | * in service of later draws. However, the deferred draw queue may still own direct references to |
| 29 | * the underlying GrEffect. |
| 30 | */ |
| 31 | class GrEffectRef : public SkRefCnt { |
| 32 | public: |
| 33 | SK_DECLARE_INST_COUNT(GrEffectRef); |
| 34 | |
| 35 | GrEffect* get() { return fEffect; } |
| 36 | const GrEffect* get() const { return fEffect; } |
| 37 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 38 | const GrEffect* operator-> () { return fEffect; } |
| 39 | const GrEffect* operator-> () const { return fEffect; } |
| 40 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 41 | void* operator new(size_t size); |
| 42 | void operator delete(void* target); |
| 43 | |
| 44 | private: |
bsalomon@google.com | 64287c5 | 2013-01-16 15:25:55 +0000 | [diff] [blame] | 45 | friend class GrEffect; // to construct these |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 46 | |
| 47 | explicit GrEffectRef(GrEffect* effect); |
| 48 | |
| 49 | virtual ~GrEffectRef(); |
| 50 | |
| 51 | GrEffect* fEffect; |
| 52 | |
| 53 | typedef SkRefCnt INHERITED; |
| 54 | }; |
| 55 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 56 | /** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the |
| 57 | Ganesh shading pipeline. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 58 | Subclasses must have a function that produces a human-readable name: |
| 59 | static const char* Name(); |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 60 | GrEffect 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] | 61 | |
| 62 | GrEffect subclass objects should be created by factory functions that return GrEffectRef. |
| 63 | There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static |
| 64 | member function of a GrEffect subclass. |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 65 | |
| 66 | Because almost no code should ever handle a GrEffect outside of a GrEffectRef, we privately |
| 67 | inherit from GrRefCnt to help prevent accidental direct ref'ing/unref'ing of effects. |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 68 | */ |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 69 | class GrEffect : private GrRefCnt { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 70 | public: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 71 | SK_DECLARE_INST_COUNT(GrEffect) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 73 | virtual ~GrEffect(); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Flags for getConstantColorComponents. They are defined so that the bit order reflects the |
| 77 | * GrColor shift order. |
| 78 | */ |
| 79 | enum ValidComponentFlags { |
| 80 | kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8), |
| 81 | kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8), |
| 82 | kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8), |
| 83 | kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8), |
| 84 | |
| 85 | kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag | |
| 86 | kB_ValidComponentFlag | kA_ValidComponentFlag) |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * This function is used to perform optimizations. When called the color and validFlags params |
| 91 | * indicate whether the input components to this effect in the FS will have known values. The |
| 92 | * function updates both params to indicate known values of its output. A component of the color |
| 93 | * param only has meaning if the corresponding bit in validFlags is set. |
| 94 | */ |
| 95 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 96 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 97 | /** This object, besides creating back-end-specific helper objects, is used for run-time-type- |
| 98 | identification. The factory should be an instance of templated class, |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 99 | GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 100 | a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created |
| 101 | by the factory. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 102 | |
| 103 | Example: |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 104 | class MyCustomEffect : public GrEffect { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 105 | ... |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 106 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 107 | return GrTBackendEffectFactory<MyCustomEffect>::getInstance(); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 108 | } |
| 109 | ... |
| 110 | }; |
| 111 | */ |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 112 | virtual const GrBackendEffectFactory& getFactory() const = 0; |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 113 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 114 | /** Returns true if this and other effect conservatively draw identically. It can only return |
| 115 | true when the two effects are of the same subclass (i.e. they return the same object from |
| 116 | from getFactory()). |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 118 | A return value of true from isEqual() should not be used to test whether the effects would |
| 119 | generate the same shader code. To test for identical code generation use the EffectKey |
| 120 | computed by the GrBackendEffectFactory: |
| 121 | effectA.getFactory().glEffectKey(effectA) == effectB.getFactory().glEffectKey(effectB). |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 122 | */ |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 123 | bool isEqual(const GrEffectRef& other) const { |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 124 | return this->isEqual(*other.get()); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 125 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 126 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 127 | /** Human-meaningful string to identify this effect; may be embedded |
| 128 | in generated shader code. */ |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 129 | const char* name() const; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 131 | int numTextures() const { return fTextureAccesses.count(); } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 132 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 133 | /** Returns the access pattern for the texture at index. index must be valid according to |
| 134 | numTextures(). */ |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 135 | const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 136 | |
| 137 | /** Shortcut for textureAccess(index).texture(); */ |
| 138 | GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 139 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame^] | 140 | |
| 141 | int numVertexAttribs() const { return fVertexAttribTypes.count(); } |
| 142 | |
| 143 | GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; } |
| 144 | |
| 145 | static const int kMaxVertexAttribs = 2; |
| 146 | |
| 147 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 148 | /** Useful for effects that want to insert a texture matrix that is implied by the texture |
| 149 | dimensions */ |
| 150 | static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { |
| 151 | GrAssert(NULL != texture); |
| 152 | SkMatrix mat; |
| 153 | mat.setIDiv(texture->width(), texture->height()); |
| 154 | return mat; |
| 155 | } |
| 156 | |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 157 | void* operator new(size_t size); |
| 158 | void operator delete(void* target); |
| 159 | |
bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 160 | /** These functions are used when recording effects into a deferred drawing queue. The inc call |
| 161 | keeps the effect alive outside of GrEffectRef while allowing any resources owned by the |
| 162 | effect to be returned to the cache for reuse. The dec call must balance the inc call. */ |
| 163 | void incDeferredRefCounts() const { |
| 164 | this->ref(); |
| 165 | int count = fTextureAccesses.count(); |
| 166 | for (int t = 0; t < count; ++t) { |
| 167 | fTextureAccesses[t]->getTexture()->incDeferredRefCount(); |
| 168 | } |
| 169 | } |
| 170 | void decDeferredRefCounts() const { |
| 171 | int count = fTextureAccesses.count(); |
| 172 | for (int t = 0; t < count; ++t) { |
| 173 | fTextureAccesses[t]->getTexture()->decDeferredRefCount(); |
| 174 | } |
| 175 | this->unref(); |
| 176 | } |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 178 | protected: |
| 179 | /** |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame^] | 180 | * Subclasses call this from their constructor to register GrTextureAccesses. The effect |
| 181 | * subclass manages the lifetime of the accesses (this function only stores a pointer). This |
| 182 | * must only be called from the constructor because GrEffects are immutable. |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 183 | */ |
| 184 | void addTextureAccess(const GrTextureAccess* textureAccess); |
| 185 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame^] | 186 | /** |
| 187 | * Subclasses call this from their constructor to register vertex attributes (at most |
| 188 | * kMaxVertexAttribs). This must only be called from the constructor because GrEffects are |
| 189 | * immutable. |
| 190 | */ |
| 191 | void addVertexAttrib(GrSLType type); |
| 192 | |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 193 | GrEffect() : fEffectRef(NULL) {}; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 194 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 195 | /** This should be called by GrEffect subclass factories. See the comment on AutoEffectUnref for |
| 196 | an example factory function. */ |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 197 | static GrEffectRef* CreateEffectRef(GrEffect* effect) { |
| 198 | if (NULL == effect->fEffectRef) { |
| 199 | effect->fEffectRef = SkNEW_ARGS(GrEffectRef, (effect)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 200 | } else { |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 201 | effect->fEffectRef->ref(); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 202 | } |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 203 | return effect->fEffectRef; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 204 | } |
| 205 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 206 | static const GrEffectRef* CreateEffectRef(const GrEffect* effect) { |
| 207 | return CreateEffectRef(const_cast<GrEffect*>(effect)); |
| 208 | } |
| 209 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 210 | /** Helper used in subclass factory functions to unref the effect after it has been wrapped in a |
| 211 | GrEffectRef. E.g.: |
| 212 | |
| 213 | class EffectSubclass : public GrEffect { |
| 214 | public: |
| 215 | GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) { |
| 216 | AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param2, ...))); |
| 217 | return CreateEffectRef(effect); |
| 218 | } |
| 219 | */ |
| 220 | class AutoEffectUnref { |
| 221 | public: |
| 222 | AutoEffectUnref(GrEffect* effect) : fEffect(effect) { } |
bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 223 | ~AutoEffectUnref() { fEffect->unref(); } |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 224 | operator GrEffect*() { return fEffect; } |
| 225 | private: |
| 226 | GrEffect* fEffect; |
| 227 | }; |
| 228 | |
| 229 | /** Helper for getting the GrEffect out of a GrEffectRef and down-casting to a GrEffect subclass |
| 230 | */ |
| 231 | template <typename T> |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 232 | static const T& CastEffect(const GrEffect& effectRef) { |
| 233 | return *static_cast<const T*>(&effectRef); |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 234 | } |
| 235 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 236 | private: |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 237 | bool isEqual(const GrEffect& other) const { |
| 238 | if (&this->getFactory() != &other.getFactory()) { |
| 239 | return false; |
| 240 | } |
| 241 | bool result = this->onIsEqual(other); |
| 242 | #if GR_DEBUG |
| 243 | if (result) { |
| 244 | GrAssert(this->numTextures() == other.numTextures()); |
| 245 | for (int i = 0; i < this->numTextures(); ++i) { |
| 246 | GrAssert(*fTextureAccesses[i] == *other.fTextureAccesses[i]); |
| 247 | } |
| 248 | } |
| 249 | #endif |
| 250 | return result; |
| 251 | } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 252 | |
| 253 | /** 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] | 254 | the two effects are of the same subclass (i.e. they return the same object from |
| 255 | getFactory()).*/ |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 256 | virtual bool onIsEqual(const GrEffect& other) const = 0; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 257 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 258 | void EffectRefDestroyed() { fEffectRef = NULL; } |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 259 | |
bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 260 | friend class GrEffectRef; // to call EffectRefDestroyed() |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 261 | friend class GrEffectStage; // to rewrap GrEffect in GrEffectRef when restoring an effect-stage |
bsalomon@google.com | 838f6e1 | 2013-01-23 21:37:01 +0000 | [diff] [blame] | 262 | // from deferred state, to call isEqual on naked GrEffects, and |
| 263 | // to inc/dec deferred ref counts. |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 264 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame^] | 265 | SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; |
| 266 | SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes; |
| 267 | GrEffectRef* fEffectRef; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 268 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 269 | typedef GrRefCnt INHERITED; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 272 | inline GrEffectRef::GrEffectRef(GrEffect* effect) { |
| 273 | GrAssert(NULL != effect); |
| 274 | effect->ref(); |
| 275 | fEffect = effect; |
| 276 | } |
| 277 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 278 | #endif |