blob: 98b5d6cb7e89239a18576a1fe6544507a304d82d [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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"
joshualittb0a8a372014-09-23 09:50:21 -070012
13/**
bsalomonb762cb52014-10-15 11:25:21 -070014 * Implements GrBackendProcessorFactory for a GrProcessor subclass as a singleton. This can be used
15 * by most GrProcessor subclasses to implement the GrProcessor::getFactory() method:
joshualittb0a8a372014-09-23 09:50:21 -070016 *
bsalomonb762cb52014-10-15 11:25:21 -070017 * const GrBackendProcessorFactory& MyProcessor::getFactory() const {
18 * return GrTBackendProcessorFactory<MyProcessor>::getInstance();
joshualittb0a8a372014-09-23 09:50:21 -070019 * }
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 *
bsalomonb762cb52014-10-15 11:25:21 -070025 * 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)
joshualittb0a8a372014-09-23 09:50:21 -070029 * which generates a key that maps 1 to 1 with code variations emitted by
bsalomonb762cb52014-10-15 11:25:21 -070030 * MyProcessor::GLProcessor::emitCode().
31 * 3. MyProcessor must have a static function:
joshualittb0a8a372014-09-23 09:50:21 -070032 * const char* Name()
bsalomonb762cb52014-10-15 11:25:21 -070033 * which returns a human-readable name for the processor.
joshualittb0a8a372014-09-23 09:50:21 -070034 */
35template <class ProcessorClass, class BackEnd, class ProcessorBase, class GLProcessorBase>
36class GrTBackendProcessorFactory : public BackEnd {
37public:
38 typedef typename ProcessorClass::GLProcessor GLProcessor;
39
bsalomonb762cb52014-10-15 11:25:21 -070040 /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as
joshualittb0a8a372014-09-23 09:50:21 -070041 * described in this class's comment. */
42 virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); }
43
joshualittb0a8a372014-09-23 09:50:21 -070044 /** Returns a new instance of the appropriate *GL* implementation class
45 for the given GrProcessor; caller is responsible for deleting
46 the object. */
bsalomonb762cb52014-10-15 11:25:21 -070047 virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE {
48 return SkNEW_ARGS(GLProcessor, (*this, processor));
joshualittb0a8a372014-09-23 09:50:21 -070049 }
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
62protected:
63 GrTBackendProcessorFactory() {}
64};
65
66/*
bsalomonb762cb52014-10-15 11:25:21 -070067 * 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.
joshualittb0a8a372014-09-23 09:50:21 -070070 */
71template <class ProcessorClass>
joshualitt87f48d92014-12-04 10:41:40 -080072class GrTBackendGeometryProcessorFactory : public GrBackendGeometryProcessorFactory {
73public:
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 }
joshualittb0a8a372014-09-23 09:50:21 -0700107protected:
108 GrTBackendGeometryProcessorFactory() {}
109};
110
111template <class ProcessorClass>
joshualitt87f48d92014-12-04 10:41:40 -0800112class GrTBackendFragmentProcessorFactory : public GrBackendFragmentProcessorFactory {
113public:
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 }
joshualittb0a8a372014-09-23 09:50:21 -0700144protected:
145 GrTBackendFragmentProcessorFactory() {}
146};
147
joshualittb0a8a372014-09-23 09:50:21 -0700148#endif