Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef GrBackendEffectFactory_DEFINED |
michael@0 | 9 | #define GrBackendEffectFactory_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "GrTypes.h" |
michael@0 | 12 | #include "SkTemplates.h" |
michael@0 | 13 | #include "SkThread.h" |
michael@0 | 14 | #include "SkTypes.h" |
michael@0 | 15 | |
michael@0 | 16 | /** Given a GrEffect of a particular type, creates the corresponding graphics-backend-specific |
michael@0 | 17 | effect object. Also tracks equivalence of shaders generated via a key. Each factory instance |
michael@0 | 18 | is assigned a generation ID at construction. The ID of the return of GrEffect::getFactory() |
michael@0 | 19 | is used as a type identifier. Thus a GrEffect subclass must return a singleton from |
michael@0 | 20 | getFactory(). GrEffect subclasses should use the derived class GrTBackendEffectFactory that is |
michael@0 | 21 | templated on the GrEffect subclass as their factory object. It requires that the GrEffect |
michael@0 | 22 | subclass has a nested class (or typedef) GLEffect which is its GL implementation and a subclass |
michael@0 | 23 | of GrGLEffect. |
michael@0 | 24 | */ |
michael@0 | 25 | |
michael@0 | 26 | class GrEffectRef; |
michael@0 | 27 | class GrGLEffect; |
michael@0 | 28 | class GrGLCaps; |
michael@0 | 29 | class GrDrawEffect; |
michael@0 | 30 | |
michael@0 | 31 | class GrBackendEffectFactory : public SkNoncopyable { |
michael@0 | 32 | public: |
michael@0 | 33 | typedef uint32_t EffectKey; |
michael@0 | 34 | enum { |
michael@0 | 35 | kNoEffectKey = 0, |
michael@0 | 36 | kEffectKeyBits = 10, |
michael@0 | 37 | /** |
michael@0 | 38 | * The framework automatically includes coord transforms and texture accesses in their |
michael@0 | 39 | * effect's EffectKey, so effects don't need to account for them in GenKey(). |
michael@0 | 40 | */ |
michael@0 | 41 | kTextureKeyBits = 4, |
michael@0 | 42 | kTransformKeyBits = 6, |
michael@0 | 43 | kAttribKeyBits = 6, |
michael@0 | 44 | kClassIDBits = 6 |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | virtual EffectKey glEffectKey(const GrDrawEffect&, const GrGLCaps&) const = 0; |
michael@0 | 48 | virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; |
michael@0 | 49 | |
michael@0 | 50 | bool operator ==(const GrBackendEffectFactory& b) const { |
michael@0 | 51 | return fEffectClassID == b.fEffectClassID; |
michael@0 | 52 | } |
michael@0 | 53 | bool operator !=(const GrBackendEffectFactory& b) const { |
michael@0 | 54 | return !(*this == b); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | virtual const char* name() const = 0; |
michael@0 | 58 | |
michael@0 | 59 | static EffectKey GetTransformKey(EffectKey key) { |
michael@0 | 60 | return key >> (kEffectKeyBits + kTextureKeyBits) & ((1U << kTransformKeyBits) - 1); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | protected: |
michael@0 | 64 | enum { |
michael@0 | 65 | kIllegalEffectClassID = 0, |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | GrBackendEffectFactory() { |
michael@0 | 69 | fEffectClassID = kIllegalEffectClassID; |
michael@0 | 70 | } |
michael@0 | 71 | virtual ~GrBackendEffectFactory() {} |
michael@0 | 72 | |
michael@0 | 73 | static EffectKey GenID() { |
michael@0 | 74 | SkDEBUGCODE(static const int32_t kClassIDBits = 8 * sizeof(EffectKey) - |
michael@0 | 75 | kTextureKeyBits - kEffectKeyBits - kAttribKeyBits); |
michael@0 | 76 | // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
michael@0 | 77 | // atomic inc returns the old value not the incremented value. So we add |
michael@0 | 78 | // 1 to the returned value. |
michael@0 | 79 | int32_t id = sk_atomic_inc(&fCurrEffectClassID) + 1; |
michael@0 | 80 | SkASSERT(id < (1 << kClassIDBits)); |
michael@0 | 81 | return static_cast<EffectKey>(id); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | EffectKey fEffectClassID; |
michael@0 | 85 | |
michael@0 | 86 | private: |
michael@0 | 87 | static int32_t fCurrEffectClassID; |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | #endif |