Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrBackendEffectFactory_DEFINED | 8 #ifndef GrBackendEffectFactory_DEFINED |
| 9 #define GrBackendEffectFactory_DEFINED | 9 #define GrBackendEffectFactory_DEFINED |
| 10 | 10 |
| 11 #include "GrTypes.h" | 11 #include "GrTypes.h" |
| 12 #include "SkTemplates.h" | 12 #include "SkTemplates.h" |
| 13 #include "SkThread.h" | 13 #include "SkThread.h" |
| 14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
| 15 #include "SkTArray.h" | 15 #include "SkTArray.h" |
| 16 | 16 |
| 17 /** Given a GrEffect of a particular type, creates the corresponding graphics-ba ckend-specific | |
| 18 effect object. Also tracks equivalence of shaders generated via a key. Each factory instance | |
| 19 is assigned a generation ID at construction. The ID of the return of GrEffec t::getFactory() | |
| 20 is used as a type identifier. Thus a GrEffect subclass must return a singlet on from | |
| 21 getFactory(). GrEffect subclasses should use the derived class GrTBackendEff ectFactory that is | |
| 22 templated on the GrEffect subclass as their factory object. It requires that the GrEffect | |
| 23 subclass has a nested class (or typedef) GLEffect which is its GL implementa tion and a subclass | |
| 24 of GrGLEffect. | |
| 25 */ | |
| 26 | |
| 27 class GrGLEffect; | 17 class GrGLEffect; |
| 28 class GrGLCaps; | 18 class GrGLCaps; |
| 29 class GrDrawEffect; | 19 class GrDrawEffect; |
| 30 | 20 |
| 31 /** | 21 /** |
| 32 * Used by effects to build their keys. It incorpates each per-effect key into a larger shader key. | 22 * Used by effects to build their keys. It incorporates each per-effect key into a larger shader key. |
| 33 */ | 23 */ |
| 34 class GrEffectKeyBuilder { | 24 class GrEffectKeyBuilder { |
| 35 public: | 25 public: |
| 36 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun t(0) { | 26 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun t(0) { |
| 37 SkASSERT(0 == fData->count() % sizeof(uint32_t)); | 27 SkASSERT(0 == fData->count() % sizeof(uint32_t)); |
| 38 } | 28 } |
| 39 | 29 |
| 40 void add32(uint32_t v) { | 30 void add32(uint32_t v) { |
| 41 ++fCount; | 31 ++fCount; |
| 42 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); | 32 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); |
| 43 } | 33 } |
| 44 | 34 |
| 35 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next | |
| 36 add*() call. */ | |
| 37 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { | |
| 38 SkASSERT(count > 0); | |
| 39 fCount += count; | |
| 40 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); | |
| 41 } | |
| 42 | |
| 45 size_t size() const { return sizeof(uint32_t) * fCount; } | 43 size_t size() const { return sizeof(uint32_t) * fCount; } |
| 46 | 44 |
| 47 private: | 45 private: |
| 48 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. | 46 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. |
| 49 int fCount; // number of uint32_ts added to fData by the effect. | 47 int fCount; // number of uint32_ts added to fData by the effect. |
| 50 }; | 48 }; |
| 51 | 49 |
| 50 /** | |
| 51 * Given a GrEffect of a particular type, creates the corresponding graphics-bac kend-specific | |
| 52 * effect object. Also tracks equivalence of shaders generated via a key. Each f actory instance | |
| 53 * is assigned an ID at construction. The ID of the return of GrEffect::getFacto ry() is used as a | |
| 54 * type identifier. Thus, a GrEffect subclass must always return the same object from getFactory() | |
| 55 * and that factory object must be unique to the GrEffect subclass (and unique f rom any further | |
| 56 * derived subclasses). | |
| 57 * | |
|
robertphillips
2014/07/11 17:24:58
This sentence seems a bit garbled
bsalomon
2014/07/11 17:52:56
Done.
| |
| 58 * It is recommended that GrEffect subclasses the derived class GrTBackendEffect Factory by | |
| 59 * writing their getFactory() method as: | |
| 60 * | |
| 61 * const GrBackendEffectFactory& MyEffect::getFactory() const { | |
| 62 * return GrTBackendEffectFactory<MyEffect>::getInstance(); | |
| 63 * } | |
| 64 * | |
| 65 * Using GrTBackendEffectFactory places a few constraints on the effect. See tha t class's comments. | |
| 66 */ | |
| 52 class GrBackendEffectFactory : SkNoncopyable { | 67 class GrBackendEffectFactory : SkNoncopyable { |
| 53 public: | 68 public: |
| 54 typedef uint32_t EffectKey; | 69 typedef uint32_t EffectKey; |
| 55 | 70 |
| 56 virtual bool getGLEffectKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKe yBuilder*) const = 0; | 71 /** |
| 72 * Generates an effect's key. The key is based on the aspects of the GrEffec t object's | |
| 73 * configuration that affect GLSL code generation. Two GrEffect instances th at would cause | |
| 74 * this->createGLInstance()->emitCode() to produce different code must produ ce different keys. | |
| 75 */ | |
| 76 virtual void getGLEffectKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKe yBuilder*) const = 0; | |
| 77 | |
| 78 /** | |
| 79 * Creates a GrGLEffect instance that is used both to generate code for the GrEffect in a GLSL | |
| 80 * program and to manage updating uniforms for the program when it is used. | |
| 81 */ | |
| 57 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; | 82 virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0; |
| 58 | 83 |
| 59 bool operator ==(const GrBackendEffectFactory& b) const { | 84 /** |
| 60 return fEffectClassID == b.fEffectClassID; | 85 * Produces a human-reable name for the effect. |
| 61 } | 86 */ |
| 62 bool operator !=(const GrBackendEffectFactory& b) const { | |
| 63 return !(*this == b); | |
| 64 } | |
| 65 | |
| 66 virtual const char* name() const = 0; | 87 virtual const char* name() const = 0; |
| 67 | 88 |
| 89 /** | |
| 90 * A unique value for every instance of this factory. It is automatically in corporated into the | |
| 91 * effect's key. This allows keys generated by getGLEffectKey() to only be u nique within a | |
| 92 * GrEffect subclass and not necessarily across subclasses. | |
| 93 */ | |
| 94 uint32_t effectClassID() const { return fEffectClassID; } | |
| 95 | |
| 68 protected: | 96 protected: |
| 97 GrBackendEffectFactory() : fEffectClassID(GenID()) {} | |
| 98 virtual ~GrBackendEffectFactory() {} | |
| 99 | |
| 100 private: | |
| 69 enum { | 101 enum { |
| 70 kIllegalEffectClassID = 0, | 102 kIllegalEffectClassID = 0, |
| 71 }; | 103 }; |
| 72 | 104 |
| 73 GrBackendEffectFactory() { | 105 static uint32_t GenID() { |
| 74 fEffectClassID = kIllegalEffectClassID; | |
| 75 } | |
| 76 virtual ~GrBackendEffectFactory() {} | |
| 77 | |
| 78 static int32_t GenID() { | |
| 79 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The | 106 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
| 80 // atomic inc returns the old value not the incremented value. So we add | 107 // atomic inc returns the old value not the incremented value. So we add |
| 81 // 1 to the returned value. | 108 // 1 to the returned value. |
| 82 int32_t id = sk_atomic_inc(&fCurrEffectClassID) + 1; | 109 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrEffectClassID)) + 1; |
|
robertphillips
2014/07/11 17:24:58
Can this even happen ?
bsalomon
2014/07/11 17:52:56
After 4 billion calls, yeah :)
| |
| 110 if (!id) { | |
|
robertphillips
2014/07/11 17:24:58
This error message seems misleading
bsalomon
2014/07/11 17:52:56
Done.
| |
| 111 SkFAIL("This should only be called once per GrEffect subclass."); | |
| 112 } | |
| 83 return id; | 113 return id; |
| 84 } | 114 } |
| 85 | 115 |
|
robertphillips
2014/07/11 17:24:58
const?
bsalomon
2014/07/11 17:52:56
Done.
| |
| 86 int32_t fEffectClassID; | 116 uint32_t fEffectClassID; |
| 87 | |
| 88 private: | |
| 89 static int32_t fCurrEffectClassID; | 117 static int32_t fCurrEffectClassID; |
| 90 }; | 118 }; |
| 91 | 119 |
| 92 #endif | 120 #endif |
| OLD | NEW |