Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrTBackendProcessorFactory.h
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"
12
13 /**
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:
16  *
17  * const GrBackendProcessorFactory& MyProcessor::getFactory() const {
18  *     return GrTBackendProcessorFactory<MyProcessor>::getInstance();
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  *
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:
32  *      const char* Name()
33  *    which returns a human-readable name for the processor.
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
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(); }
43
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
52     /** Returns a new instance of the appropriate *GL* implementation class
53         for the given GrProcessor; caller is responsible for deleting
54         the object. */
55     virtual GLProcessorBase* createGLInstance(const ProcessorBase& processor) const SK_OVERRIDE {
56         return SkNEW_ARGS(GLProcessor, (*this, processor));
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 /*
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.
78  */
79 template <class ProcessorClass>
80 class GrTBackendGeometryProcessorFactory
81         : public GrTBackendProcessorFactory<ProcessorClass,
82                                             GrBackendGeometryProcessorFactory,
83                                             GrGeometryProcessor,
84                                             GrGLGeometryProcessor> {
85 protected:
86     GrTBackendGeometryProcessorFactory() {}
87 };
88
89 template <class ProcessorClass>
90 class GrTBackendFragmentProcessorFactory
91         : public GrTBackendProcessorFactory<ProcessorClass,
92                                            GrBackendFragmentProcessorFactory,
93                                            GrFragmentProcessor,
94                                            GrGLFragmentProcessor> {
95 protected:
96     GrTBackendFragmentProcessorFactory() {}
97 };
98
99
100 #endif