blob: a86fe38d92ea40367c9f78009e44579bc4a358ed [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
joshualitt841a6b52014-12-04 06:00:41 -080044
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
joshualittb0a8a372014-09-23 09:50:21 -070052 /** Returns a new instance of the appropriate *GL* implementation class
53 for the given GrProcessor; caller is responsible for deleting
54 the object. */
bsalomonb762cb52014-10-15 11:25:21 -070055 virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE {
56 return SkNEW_ARGS(GLProcessor, (*this, processor));
joshualittb0a8a372014-09-23 09:50:21 -070057 }
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
70protected:
71 GrTBackendProcessorFactory() {}
72};
73
74/*
bsalomonb762cb52014-10-15 11:25:21 -070075 * 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.
joshualittb0a8a372014-09-23 09:50:21 -070078 */
79template <class ProcessorClass>
joshualitt841a6b52014-12-04 06:00:41 -080080class GrTBackendGeometryProcessorFactory
81 : public GrTBackendProcessorFactory<ProcessorClass,
82 GrBackendGeometryProcessorFactory,
83 GrGeometryProcessor,
84 GrGLGeometryProcessor> {
joshualittb0a8a372014-09-23 09:50:21 -070085protected:
86 GrTBackendGeometryProcessorFactory() {}
87};
88
89template <class ProcessorClass>
joshualitt841a6b52014-12-04 06:00:41 -080090class GrTBackendFragmentProcessorFactory
91 : public GrTBackendProcessorFactory<ProcessorClass,
92 GrBackendFragmentProcessorFactory,
93 GrFragmentProcessor,
94 GrGLFragmentProcessor> {
joshualittb0a8a372014-09-23 09:50:21 -070095protected:
96 GrTBackendFragmentProcessorFactory() {}
97};
98
joshualitt841a6b52014-12-04 06:00:41 -080099
joshualittb0a8a372014-09-23 09:50:21 -0700100#endif