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 | 841a6b5 | 2014-12-04 06:00:41 -0800 | [diff] [blame] | 44 | |
| 45 | /** Implemented using GLProcessor::GenKey as described in this class's comment. */ |
| 46 | virtual void getGLProcessorKey(const GrProcessor& processor, |
| 47 | const GrGLCaps& caps, |
| 48 | GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 49 | GLProcessor::GenKey(processor, caps, b); |
| 50 | } |
| 51 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 52 | /** Returns a new instance of the appropriate *GL* implementation class |
| 53 | for the given GrProcessor; caller is responsible for deleting |
| 54 | the object. */ |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 55 | virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE { |
| 56 | return SkNEW_ARGS(GLProcessor, (*this, processor)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** This class is a singleton. This function returns the single instance. */ |
| 60 | static const BackEnd& getInstance() { |
| 61 | static SkAlignedSTStorage<1, GrTBackendProcessorFactory> gInstanceMem; |
| 62 | static const GrTBackendProcessorFactory* gInstance; |
| 63 | if (!gInstance) { |
| 64 | gInstance = SkNEW_PLACEMENT(gInstanceMem.get(), |
| 65 | GrTBackendProcessorFactory); |
| 66 | } |
| 67 | return *gInstance; |
| 68 | } |
| 69 | |
| 70 | protected: |
| 71 | GrTBackendProcessorFactory() {} |
| 72 | }; |
| 73 | |
| 74 | /* |
bsalomon | b762cb5 | 2014-10-15 11:25:21 -0700 | [diff] [blame] | 75 | * Every processor so far derives from one of the following subclasses of |
| 76 | * GrTBackendProcessorFactory. All of this machinery is necessary to ensure that creatGLInstace is |
| 77 | * typesafe and does not require any casting. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 78 | */ |
| 79 | template <class ProcessorClass> |
joshualitt | 841a6b5 | 2014-12-04 06:00:41 -0800 | [diff] [blame] | 80 | class GrTBackendGeometryProcessorFactory |
| 81 | : public GrTBackendProcessorFactory<ProcessorClass, |
| 82 | GrBackendGeometryProcessorFactory, |
| 83 | GrGeometryProcessor, |
| 84 | GrGLGeometryProcessor> { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | protected: |
| 86 | GrTBackendGeometryProcessorFactory() {} |
| 87 | }; |
| 88 | |
| 89 | template <class ProcessorClass> |
joshualitt | 841a6b5 | 2014-12-04 06:00:41 -0800 | [diff] [blame] | 90 | class GrTBackendFragmentProcessorFactory |
| 91 | : public GrTBackendProcessorFactory<ProcessorClass, |
| 92 | GrBackendFragmentProcessorFactory, |
| 93 | GrFragmentProcessor, |
| 94 | GrGLFragmentProcessor> { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 95 | protected: |
| 96 | GrTBackendFragmentProcessorFactory() {} |
| 97 | }; |
| 98 | |
joshualitt | 841a6b5 | 2014-12-04 06:00:41 -0800 | [diff] [blame] | 99 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 100 | #endif |