2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrTBackendProcessorFactory_DEFINED
9 #define GrTBackendProcessorFactory_DEFINED
11 #include "GrBackendProcessorFactory.h"
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:
17 * const GrBackendProcessorFactory& MyProcessor::getFactory() const {
18 * return GrTBackendProcessorFactory<MyProcessor>::getInstance();
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
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)
29 * which generates a key that maps 1 to 1 with code variations emitted by
30 * MyProcessor::GLProcessor::emitCode().
31 * 3. MyProcessor must have a static function:
33 * which returns a human-readable name for the processor.
35 template <class ProcessorClass, class BackEnd, class ProcessorBase, class GLProcessorBase>
36 class GrTBackendProcessorFactory : public BackEnd {
38 typedef typename ProcessorClass::GLProcessor GLProcessor;
40 /** Returns a human-readable name for the processor. Implemented using GLProcessor::Name as
41 * described in this class's comment. */
42 virtual const char* name() const SK_OVERRIDE { return ProcessorClass::Name(); }
45 /** Implemented using GLProcessor::GenKey as described in this class's comment. */
46 virtual void getGLProcessorKey(const GrProcessor& processor,
48 GrProcessorKeyBuilder* b) const SK_OVERRIDE {
49 GLProcessor::GenKey(processor, caps, b);
52 /** Returns a new instance of the appropriate *GL* implementation class
53 for the given GrProcessor; caller is responsible for deleting
55 virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE {
56 return SkNEW_ARGS(GLProcessor, (*this, processor));
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;
64 gInstance = SkNEW_PLACEMENT(gInstanceMem.get(),
65 GrTBackendProcessorFactory);
71 GrTBackendProcessorFactory() {}
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.
79 template <class ProcessorClass>
80 class GrTBackendGeometryProcessorFactory
81 : public GrTBackendProcessorFactory<ProcessorClass,
82 GrBackendGeometryProcessorFactory,
84 GrGLGeometryProcessor> {
86 GrTBackendGeometryProcessorFactory() {}
89 template <class ProcessorClass>
90 class GrTBackendFragmentProcessorFactory
91 : public GrTBackendProcessorFactory<ProcessorClass,
92 GrBackendFragmentProcessorFactory,
94 GrGLFragmentProcessor> {
96 GrTBackendFragmentProcessorFactory() {}