bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +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 | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 8 | #ifndef GrBackendEffectFactory_DEFINED |
| 9 | #define GrBackendEffectFactory_DEFINED |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 10 | |
| 11 | #include "GrTypes.h" |
bsalomon@google.com | 8e520fc | 2012-05-18 20:06:45 +0000 | [diff] [blame] | 12 | #include "SkTemplates.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 13 | #include "SkThread_platform.h" |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 14 | #include "GrNoncopyable.h" |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 16 | /** Given a GrEffect of a particular type, creates the corresponding graphics-backend-specific |
| 17 | effect object. Also tracks equivalence of shaders generated via a key. Each factory instance |
| 18 | is assigned a generation ID at construction. The ID of the return of GrEffect::getFactory() |
| 19 | is used as a type identifier. Thus a GrEffect subclass must return a singleton from |
| 20 | getFactory(). GrEffect subclasses should use the derived class GrTBackendEffectFactory that is |
| 21 | templated on the GrEffect subclass as their factory object. It requires that the GrEffect |
| 22 | subclass has a nested class (or typedef) GLEffect which is its GL implementation and a subclass |
| 23 | of GrGLEffect. |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 26 | class GrEffect; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 27 | class GrEffectStage; |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 28 | class GrGLEffect; |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 29 | class GrGLCaps; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 31 | class GrBackendEffectFactory : public GrNoncopyable { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 32 | public: |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 33 | typedef uint32_t EffectKey; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 34 | enum { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 35 | kNoEffectKey = 0, |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 36 | kEffectKeyBits = 12, |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Some aspects of the generated code may be determined by the particular textures that are |
| 39 | * associated with the effect. These manipulations are performed by GrGLShaderBuilder beyond |
| 40 | * GrGLEffects' control. So there is a dedicated part of the key which is combined |
| 41 | * automatically with the bits produced by GrGLEffect::GenKey(). |
| 42 | */ |
| 43 | kTextureKeyBits = 6 |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 46 | virtual EffectKey glEffectKey(const GrEffectStage&, const GrGLCaps&) const = 0; |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 47 | virtual GrGLEffect* createGLInstance(const GrEffect&) const = 0; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 48 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 49 | bool operator ==(const GrBackendEffectFactory& b) const { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 50 | return fEffectClassID == b.fEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 51 | } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 52 | bool operator !=(const GrBackendEffectFactory& b) const { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 53 | return !(*this == b); |
| 54 | } |
| 55 | |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 56 | virtual const char* name() const = 0; |
| 57 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 58 | protected: |
| 59 | enum { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 60 | kIllegalEffectClassID = 0, |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 63 | GrBackendEffectFactory() { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 64 | fEffectClassID = kIllegalEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 65 | } |
| 66 | |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 67 | static EffectKey GenID() { |
robertphillips@google.com | 4187a2f | 2012-11-05 01:31:44 +0000 | [diff] [blame] | 68 | GR_DEBUGCODE(static const int32_t kClassIDBits = 8 * sizeof(EffectKey) - |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 69 | kTextureKeyBits - |
skia.committer@gmail.com | 1aa90cf | 2012-11-06 13:18:25 +0000 | [diff] [blame] | 70 | kEffectKeyBits); |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 71 | // fCurrEffectClassID has been initialized to kIllegalEffectClassID. The |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 72 | // atomic inc returns the old value not the incremented value. So we add |
| 73 | // 1 to the returned value. |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 74 | int32_t id = sk_atomic_inc(&fCurrEffectClassID) + 1; |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 75 | GrAssert(id < (1 << kClassIDBits)); |
| 76 | return static_cast<EffectKey>(id); |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 77 | } |
| 78 | |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 79 | EffectKey fEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 80 | |
| 81 | private: |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 82 | static int32_t fCurrEffectClassID; |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 85 | #endif |