0fd1ae3c32f757b45d814f41c7d7e7a14d03148b
[platform/upstream/libSkiaSharp.git] / src / gpu / glsl / GrGLSLFragmentProcessor.h
1 /*
2  * Copyright 2013 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 GrGLSLFragmentProcessor_DEFINED
9 #define GrGLSLFragmentProcessor_DEFINED
10
11 #include "glsl/GrGLSLProcessorTypes.h"
12 #include "glsl/GrGLSLProgramDataManager.h"
13 #include "glsl/GrGLSLTextureSampler.h"
14
15 class GrProcessor;
16 class GrProcessorKeyBuilder;
17 class GrGLSLFPBuilder;
18 class GrGLSLFragmentBuilder;
19 class GrGLSLCaps;
20
21 class GrGLSLFragmentProcessor {
22 public:
23     GrGLSLFragmentProcessor() {}
24
25     virtual ~GrGLSLFragmentProcessor() {
26         for (int i = 0; i < fChildProcessors.count(); ++i) {
27             delete fChildProcessors[i];
28         }
29     }
30
31     typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
32     typedef GrGLSLTextureSampler::TextureSamplerArray TextureSamplerArray;
33
34     /** Called when the program stage should insert its code into the shaders. The code in each
35         shader will be in its own block ({}) and so locally scoped names will not collide across
36         stages.
37
38         @param builder      Interface used to emit code in the shaders.
39         @param processor    The processor that generated this program stage.
40         @param key          The key that was computed by GenKey() from the generating GrProcessor.
41         @param outputColor  A predefined vec4 in the FS in which the stage should place its output
42                             color (or coverage).
43         @param inputColor   A vec4 that holds the input color to the stage in the FS. This may be
44                             nullptr in which case the implied input is solid white (all ones).
45                             TODO: Better system for communicating optimization info (e.g. input
46                             color is solid white, trans black, known to be opaque, etc.) that allows
47                             the processor to communicate back similar known info about its output.
48         @param samplers     Contains one entry for each GrTextureAccess of the GrProcessor. These
49                             can be passed to the builder to emit texture reads in the generated
50                             code.
51      */
52
53     struct EmitArgs {
54         EmitArgs(GrGLSLFPBuilder* builder,
55                  GrGLSLFragmentBuilder* fragBuilder,
56                  const GrGLSLCaps* caps,
57                  const GrFragmentProcessor& fp,
58                  const char* outputColor,
59                  const char* inputColor,
60                  const GrGLSLTransformedCoordsArray& coords,
61                  const TextureSamplerArray& samplers)
62             : fBuilder(builder)
63             , fFragBuilder(fragBuilder)
64             , fGLSLCaps(caps)
65             , fFp(fp)
66             , fOutputColor(outputColor)
67             , fInputColor(inputColor)
68             , fCoords(coords)
69             , fSamplers(samplers) {}
70         GrGLSLFPBuilder* fBuilder;
71         GrGLSLFragmentBuilder* fFragBuilder;
72         const GrGLSLCaps* fGLSLCaps;
73         const GrFragmentProcessor& fFp;
74         const char* fOutputColor;
75         const char* fInputColor;
76         const GrGLSLTransformedCoordsArray& fCoords;
77         const TextureSamplerArray& fSamplers;
78     };
79
80     virtual void emitCode(EmitArgs&) = 0;
81
82     void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor);
83
84     static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*) {}
85
86     int numChildProcessors() const { return fChildProcessors.count(); }
87
88     GrGLSLFragmentProcessor* childProcessor(int index) const {
89         return fChildProcessors[index];
90     }
91
92     /** Will emit the code of a child proc in its own scope. Pass in the parent's EmitArgs and
93      *  emitChild will automatically extract the coords and samplers of that child and pass them
94      *  on to the child's emitCode(). Also, any uniforms or functions emitted by the child will
95      *  have their names mangled to prevent redefinitions. The output color name is also mangled
96      *  therefore in an in/out param. It will be declared in mangled form by emitChild(). It is
97      *  legal to pass nullptr as inputColor, since all fragment processors are required to work
98      *  without an input color.
99      */
100     void emitChild(int childIndex, const char* inputColor, SkString* outputColor,
101                    EmitArgs& parentArgs);
102
103     /** Variation that uses the parent's output color variable to hold the child's output.*/
104     void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs);
105
106 protected:
107     /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProcessor that produces
108     the same stage key; this function reads data from a GrFragmentProcessor and uploads any
109     uniform variables required by the shaders created in emitCode(). The GrFragmentProcessor
110     parameter is guaranteed to be of the same type that created this GrGLSLFragmentProcessor and
111     to have an identical processor key as the one that created this GrGLSLFragmentProcessor.  */
112     // TODO update this to pass in GrFragmentProcessor
113     virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) {}
114
115 private:
116     void internalEmitChild(int, const char*, const char*, EmitArgs&);
117
118     SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors;
119
120     friend class GrFragmentProcessor;
121 };
122
123 #endif