joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [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 | |
| 8 | #ifndef GrTBackendProcessorFactory_DEFINED |
| 9 | #define GrTBackendProcessorFactory_DEFINED |
| 10 | |
| 11 | #include "GrBackendProcessorFactory.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | |
| 13 | /** |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 14 | * Implements GrBackendProcessorFactory for a GrProcessor subclass as a singleton. This can be used |
| 15 | * by most GrProcessor subclasses to implement the GrProcessor::getFactory() method: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 16 | * |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 17 | * const GrBackendProcessorFactory& MyProcessor::getFactory() const { |
| 18 | * return GrTBackendProcessorFactory<MyProcessor>::getInstance(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | * } |
| 20 | * |
| 21 | * Using this class requires that the GrProcessor subclass always produces the same GrGLProcessor |
| 22 | * subclass. Additionally, it adds the following requirements to the GrProcessor and GrGLProcessor |
| 23 | * subclasses: |
| 24 | * |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 25 | * 1. The GrGLProcessor used by GrProcessor subclass MyProcessor must be named or typedef'ed to |
| 26 | * MyProcessor::GLProcessor. |
| 27 | * 2. MyProcessor::GLProcessor must have a static function: |
| 28 | void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder* b) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 29 | * which generates a key that maps 1 to 1 with code variations emitted by |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 30 | * MyProcessor::GLProcessor::emitCode(). |
| 31 | * 3. MyProcessor must have a static function: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 32 | * const char* Name() |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 33 | * which returns a human-readable name for the processor. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 34 | */ |
| 35 | template <class ProcessorClass, class BackEnd, class ProcessorBase, class GLProcessorBase> |
| 36 | class GrTBackendProcessorFactory : public BackEnd { |
| 37 | public: |
| 38 | typedef typename ProcessorClass::GLProcessor GLProcessor; |
| 39 | |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 40 | /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 41 | * described in this class's comment. */ |
| 42 | virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); } |
| 43 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 44 | /** Returns a new instance of the appropriate *GL* implementation class |
| 45 | for the given GrProcessor; caller is responsible for deleting |
| 46 | the object. */ |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 47 | virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE { |
| 48 | return SkNEW_ARGS(GLProcessor, (*this, processor)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** This class is a singleton. This function returns the single instance. */ |
| 52 | static const BackEnd& getInstance() { |
| 53 | static SkAlignedSTStorage<1, GrTBackendProcessorFactory> gInstanceMem; |
| 54 | static const GrTBackendProcessorFactory* gInstance; |
| 55 | if (!gInstance) { |
| 56 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
| 57 | GrTBackendProcessorFactory); |
| 58 | } |
| 59 | return *gInstance; |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | GrTBackendProcessorFactory() {} |
| 64 | }; |
| 65 | |
| 66 | /* |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 67 | * Every processor so far derives from one of the following subclasses of |
| 68 | * GrTBackendProcessorFactory. All of this machinery is necessary to ensure that creatGLInstace is |
| 69 | * typesafe and does not require any casting. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 70 | */ |
| 71 | template <class ProcessorClass> |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 72 | class GrTBackendGeometryProcessorFactory : public GrBackendGeometryProcessorFactory { |
| 73 | public: |
| 74 | typedef typename ProcessorClass::GLProcessor GLProcessor; |
| 75 | |
| 76 | /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as |
| 77 | * described in this class's comment. */ |
| 78 | virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); } |
| 79 | |
| 80 | /** Implemented using GLProcessor::GenKey as described in this class's comment. */ |
| 81 | virtual void getGLProcessorKey(const GrGeometryProcessor& processor, |
| 82 | const GrBatchTracker& bt, |
| 83 | const GrGLCaps& caps, |
| 84 | GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 85 | GLProcessor::GenKey(processor, bt, caps, b); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** Returns a new instance of the appropriate *GL* implementation class |
| 90 | for the given GrProcessor; caller is responsible for deleting |
| 91 | the object. */ |
| 92 | virtual GrGLGeometryProcessor* createGLInstance(const GrGeometryProcessor& gp, |
| 93 | const GrBatchTracker& bt) const SK_OVERRIDE { |
| 94 | return SkNEW_ARGS(GLProcessor, (*this, gp, bt)); |
| 95 | } |
| 96 | |
| 97 | /** This class is a singleton. This function returns the single instance. */ |
| 98 | static const GrBackendGeometryProcessorFactory& getInstance() { |
| 99 | static SkAlignedSTStorage<1, GrTBackendGeometryProcessorFactory> gInstanceMem; |
| 100 | static const GrTBackendGeometryProcessorFactory* gInstance; |
| 101 | if (!gInstance) { |
| 102 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
| 103 | GrTBackendGeometryProcessorFactory); |
| 104 | } |
| 105 | return *gInstance; |
| 106 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 107 | protected: |
| 108 | GrTBackendGeometryProcessorFactory() {} |
| 109 | }; |
| 110 | |
| 111 | template <class ProcessorClass> |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame^] | 112 | class GrTBackendFragmentProcessorFactory : public GrBackendFragmentProcessorFactory { |
| 113 | public: |
| 114 | typedef typename ProcessorClass::GLProcessor GLProcessor; |
| 115 | |
| 116 | /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as |
| 117 | * described in this class's comment. */ |
| 118 | virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); } |
| 119 | |
| 120 | /** Implemented using GLProcessor::GenKey as described in this class's comment. */ |
| 121 | virtual void getGLProcessorKey(const GrFragmentProcessor& processor, |
| 122 | const GrGLCaps& caps, |
| 123 | GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 124 | GLProcessor::GenKey(processor, caps, b); |
| 125 | } |
| 126 | |
| 127 | /** Returns a new instance of the appropriate *GL* implementation class |
| 128 | for the given GrProcessor; caller is responsible for deleting |
| 129 | the object. */ |
| 130 | virtual GrGLFragmentProcessor* createGLInstance(const GrFragmentProcessor& gp) const SK_OVERRIDE { |
| 131 | return SkNEW_ARGS(GLProcessor, (*this, gp)); |
| 132 | } |
| 133 | |
| 134 | /** This class is a singleton. This function returns the single instance. */ |
| 135 | static const GrBackendFragmentProcessorFactory& getInstance() { |
| 136 | static SkAlignedSTStorage<1, GrTBackendFragmentProcessorFactory> gInstanceMem; |
| 137 | static const GrTBackendFragmentProcessorFactory* gInstance; |
| 138 | if (!gInstance) { |
| 139 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
| 140 | GrTBackendFragmentProcessorFactory); |
| 141 | } |
| 142 | return *gInstance; |
| 143 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 144 | protected: |
| 145 | GrTBackendFragmentProcessorFactory() {} |
| 146 | }; |
| 147 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 148 | #endif |