| 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 GrBackendProcessorFactory_DEFINED |
| 9 #define GrBackendEffectFactory_DEFINED | 9 #define GrBackendProcessorFactory_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 class GrGLEffect; | 17 class GrGLProcessor; |
| 18 class GrGLCaps; | 18 class GrGLCaps; |
| 19 class GrEffect; | 19 class GrProcessor; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Used by effects to build their keys. It incorporates each per-effect key into
a larger shader key. | 22 * Used by effects to build their keys. It incorporates each per-processor key i
nto a larger shader |
| 23 * key. |
| 23 */ | 24 */ |
| 24 class GrEffectKeyBuilder { | 25 class GrProcessorKeyBuilder { |
| 25 public: | 26 public: |
| 26 GrEffectKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCoun
t(0) { | 27 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fC
ount(0) { |
| 27 SkASSERT(0 == fData->count() % sizeof(uint32_t)); | 28 SkASSERT(0 == fData->count() % sizeof(uint32_t)); |
| 28 } | 29 } |
| 29 | 30 |
| 30 void add32(uint32_t v) { | 31 void add32(uint32_t v) { |
| 31 ++fCount; | 32 ++fCount; |
| 32 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); | 33 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); |
| 33 } | 34 } |
| 34 | 35 |
| 35 /** Inserts count uint32_ts into the key. The returned pointer is only valid
until the next | 36 /** Inserts count uint32_ts into the key. The returned pointer is only valid
until the next |
| 36 add*() call. */ | 37 add*() call. */ |
| 37 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { | 38 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { |
| 38 SkASSERT(count > 0); | 39 SkASSERT(count > 0); |
| 39 fCount += count; | 40 fCount += count; |
| 40 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); | 41 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); |
| 41 } | 42 } |
| 42 | 43 |
| 43 size_t size() const { return sizeof(uint32_t) * fCount; } | 44 size_t size() const { return sizeof(uint32_t) * fCount; } |
| 44 | 45 |
| 45 private: | 46 private: |
| 46 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. | 47 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. |
| 47 int fCount; // number of uint32_ts added to fData by the
effect. | 48 int fCount; // number of uint32_ts added to fData by the
effect. |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 /** | 51 /** |
| 51 * This class is used to pass the key that was created for a GrGLEffect back to
it | 52 * This class is used to pass the key that was created for a GrGLProcessor back
to it |
| 52 * when it emits code. It may allow the emit step to skip calculations that were | 53 * when it emits code. It may allow the emit step to skip calculations that were |
| 53 * performed when computing the key. | 54 * performed when computing the key. |
| 54 */ | 55 */ |
| 55 class GrEffectKey { | 56 class GrProcessorKey { |
| 56 public: | 57 public: |
| 57 GrEffectKey(const uint32_t* key, int count) : fKey(key), fCount(count) { | 58 GrProcessorKey(const uint32_t* key, int count) : fKey(key), fCount(count) { |
| 58 SkASSERT(0 == reinterpret_cast<intptr_t>(key) % sizeof(uint32_t)); | 59 SkASSERT(0 == reinterpret_cast<intptr_t>(key) % sizeof(uint32_t)); |
| 59 } | 60 } |
| 60 | 61 |
| 61 /** Gets the uint32_t values that the effect inserted into the key. */ | 62 /** Gets the uint32_t values that the effect inserted into the key. */ |
| 62 uint32_t get32(int index) const { | 63 uint32_t get32(int index) const { |
| 63 SkASSERT(index >=0 && index < fCount); | 64 SkASSERT(index >=0 && index < fCount); |
| 64 return fKey[index]; | 65 return fKey[index]; |
| 65 } | 66 } |
| 66 | 67 |
| 67 /** Gets the number of uint32_t values that the effect inserted into the key
. */ | 68 /** Gets the number of uint32_t values that the effect inserted into the key
. */ |
| 68 int count32() const { return fCount; } | 69 int count32() const { return fCount; } |
| 69 | 70 |
| 70 private: | 71 private: |
| 71 const uint32_t* fKey; // unowned ptr into the larger key. | 72 const uint32_t* fKey; // unowned ptr into the larger key. |
| 72 int fCount; // number of uint32_ts inserted by the effec
t into its key. | 73 int fCount; // number of uint32_ts inserted by the effec
t into its key. |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 /** | 76 /** |
| 76 * Given a GrEffect of a particular type, creates the corresponding graphics-bac
kend-specific | 77 * Given a GrProcessor of a particular type, creates the corresponding graphics-
backend-specific |
| 77 * effect object. It also tracks equivalence of shaders generated via a key. The
factory for an | 78 * effect object. It also tracks equivalence of shaders generated via a key. The
factory for an |
| 78 * effect is accessed via GrEffect::getFactory(). Each factory instance is assig
ned an ID at | 79 * effect is accessed via GrProcessor::getFactory(). Each factory instance is as
signed an ID at |
| 79 * construction. The ID of GrEffect::getFactory() is used as a type identifier.
Thus, a GrEffect | 80 * construction. The ID of GrProcessor::getFactory() is used as a type identifie
r. Thus, a |
| 80 * subclass must always return the same object from getFactory() and that factor
y object must be | 81 * GrProcessor subclass must always return the same object from getFactory() and
that factory object |
| 81 * unique to the GrEffect subclass (and unique from any further derived subclass
es). | 82 * must be unique to the GrProcessor subclass (and unique from any further deriv
ed subclasses). |
| 82 * | 83 * |
| 83 * Rather than subclassing this class themselves, it is recommended that GrEffec
t authors use | 84 * Rather than subclassing this class themselves, it is recommended that GrProce
ssor authors use |
| 84 * the templated subclass GrTBackendEffectFactory by writing their getFactory()
method as: | 85 * the templated subclass GrTBackendEffectFactory by writing their getFactory()
method as: |
| 85 * | 86 * |
| 86 * const GrBackendEffectFactory& MyEffect::getFactory() const { | 87 * const GrBackendEffectFactory& MyEffect::getFactory() const { |
| 87 * return GrTBackendEffectFactory<MyEffect>::getInstance(); | 88 * return GrTBackendEffectFactory<MyEffect>::getInstance(); |
| 88 * } | 89 * } |
| 89 * | 90 * |
| 90 * Using GrTBackendEffectFactory places a few constraints on the effect. See tha
t class's comments. | 91 * Using GrTBackendEffectFactory places a few constraints on the effect. See tha
t class's comments. |
| 91 */ | 92 */ |
| 92 class GrBackendEffectFactory : SkNoncopyable { | 93 class GrBackendProcessorFactory : SkNoncopyable { |
| 93 public: | 94 public: |
| 94 /** | 95 /** |
| 95 * Generates an effect's key. The key is based on the aspects of the GrEffec
t object's | 96 * Generates an effect's key. The key is based on the aspects of the GrProce
ssor object's |
| 96 * configuration that affect GLSL code generation. Two GrEffect instances th
at would cause | 97 * configuration that affect GLSL code generation. Two GrProcessor instances
that would cause |
| 97 * this->createGLInstance()->emitCode() to produce different code must produ
ce different keys. | 98 * this->createGLInstance()->emitCode() to produce different code must produ
ce different keys. |
| 98 */ | 99 */ |
| 99 virtual void getGLEffectKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBui
lder*) const = 0; | 100 virtual void getGLProcessorKey(const GrProcessor&, const GrGLCaps&, |
| 100 | 101 GrProcessorKeyBuilder*) const = 0; |
| 101 /** | |
| 102 * Creates a GrGLEffect instance that is used both to generate code for the
GrEffect in a GLSL | |
| 103 * program and to manage updating uniforms for the program when it is used. | |
| 104 */ | |
| 105 virtual GrGLEffect* createGLInstance(const GrEffect&) const = 0; | |
| 106 | 102 |
| 107 /** | 103 /** |
| 108 * Produces a human-reable name for the effect. | 104 * Produces a human-reable name for the effect. |
| 109 */ | 105 */ |
| 110 virtual const char* name() const = 0; | 106 virtual const char* name() const = 0; |
| 111 | 107 |
| 112 /** | 108 /** |
| 113 * A unique value for every instance of this factory. It is automatically in
corporated into the | 109 * A unique value for every instance of this factory. It is automatically in
corporated into the |
| 114 * effect's key. This allows keys generated by getGLEffectKey() to only be u
nique within a | 110 * effect's key. This allows keys generated by getGLProcessorKey() to only b
e unique within a |
| 115 * GrEffect subclass and not necessarily across subclasses. | 111 * GrProcessor subclass and not necessarily across subclasses. |
| 116 */ | 112 */ |
| 117 uint32_t effectClassID() const { return fEffectClassID; } | 113 uint32_t effectClassID() const { return fEffectClassID; } |
| 118 | 114 |
| 119 protected: | 115 protected: |
| 120 GrBackendEffectFactory() : fEffectClassID(GenID()) {} | 116 GrBackendProcessorFactory() : fEffectClassID(GenID()) {} |
| 121 virtual ~GrBackendEffectFactory() {} | 117 virtual ~GrBackendProcessorFactory() {} |
| 122 | 118 |
| 123 private: | 119 private: |
| 124 enum { | 120 enum { |
| 125 kIllegalEffectClassID = 0, | 121 kIllegalEffectClassID = 0, |
| 126 }; | 122 }; |
| 127 | 123 |
| 128 static uint32_t GenID() { | 124 static uint32_t GenID() { |
| 129 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The | 125 // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
| 130 // atomic inc returns the old value not the incremented value. So we add | 126 // atomic inc returns the old value not the incremented value. So we add |
| 131 // 1 to the returned value. | 127 // 1 to the returned value. |
| 132 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrEffectClassID))
+ 1; | 128 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&fCurrEffectClassID))
+ 1; |
| 133 if (!id) { | 129 if (!id) { |
| 134 SkFAIL("This should never wrap as it should only be called once for
each GrEffect " | 130 SkFAIL("This should never wrap as it should only be called once for
each GrProcessor " |
| 135 "subclass."); | 131 "subclass."); |
| 136 } | 132 } |
| 137 return id; | 133 return id; |
| 138 } | 134 } |
| 139 | 135 |
| 140 const uint32_t fEffectClassID; | 136 const uint32_t fEffectClassID; |
| 141 static int32_t fCurrEffectClassID; | 137 static int32_t fCurrEffectClassID; |
| 142 }; | 138 }; |
| 143 | 139 |
| 140 class GrFragmentProcessor; |
| 141 class GrGeometryProcessor; |
| 142 class GrGLFragmentProcessor; |
| 143 class GrGLGeometryProcessor; |
| 144 |
| 145 /** |
| 146 * Backend processor factory cannot actually create anything, it is up to subcla
sses to implement |
| 147 * a create binding which matches Gr to GL in a type safe way |
| 148 */ |
| 149 |
| 150 class GrBackendFragmentProcessorFactory : public GrBackendProcessorFactory { |
| 151 public: |
| 152 /** |
| 153 * Creates a GrGLProcessor instance that is used both to generate code for t
he GrProcessor in a |
| 154 * GLSL program and to manage updating uniforms for the program when it is u
sed. |
| 155 */ |
| 156 virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor&)
const = 0; |
| 157 }; |
| 158 |
| 159 class GrBackendGeometryProcessorFactory : public GrBackendProcessorFactory { |
| 160 public: |
| 161 /** |
| 162 * Creates a GrGLProcessor instance that is used both to generate code for t
he GrProcessor in a |
| 163 * GLSL program and to manage updating uniforms for the program when it is u
sed. |
| 164 */ |
| 165 virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor&)
const = 0; |
| 166 }; |
| 167 |
| 144 #endif | 168 #endif |
| OLD | NEW |