1 /*
2  * Copyright 2015 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 GrGLSLXferProcessor_DEFINED
9 #define GrGLSLXferProcessor_DEFINED
10 
11 #include "glsl/GrGLSLProgramDataManager.h"
12 #include "glsl/GrGLSLUniformHandler.h"
13 
14 class GrXferProcessor;
15 class GrGLSLXPBuilder;
16 class GrGLSLXPFragmentBuilder;
17 class GrShaderCaps;
18 
19 class GrGLSLXferProcessor {
20 public:
GrGLSLXferProcessor()21     GrGLSLXferProcessor() {}
~GrGLSLXferProcessor()22     virtual ~GrGLSLXferProcessor() {}
23 
24     using SamplerHandle        = GrGLSLUniformHandler::SamplerHandle;
25     using ImageStorageHandle   = GrGLSLUniformHandler::ImageStorageHandle;
26 
27     struct EmitArgs {
EmitArgsEmitArgs28         EmitArgs(GrGLSLXPFragmentBuilder* fragBuilder,
29                  GrGLSLUniformHandler* uniformHandler,
30                  const GrShaderCaps* caps,
31                  const GrXferProcessor& xp,
32                  const char* inputColor,
33                  const char* inputCoverage,
34                  const char* outputPrimary,
35                  const char* outputSecondary,
36                  const SamplerHandle* texSamplers,
37                  const SamplerHandle* bufferSamplers,
38                  const ImageStorageHandle* imageStorages)
39                 : fXPFragBuilder(fragBuilder)
40                 , fUniformHandler(uniformHandler)
41                 , fShaderCaps(caps)
42                 , fXP(xp)
43                 , fInputColor(inputColor)
44                 , fInputCoverage(inputCoverage)
45                 , fOutputPrimary(outputPrimary)
46                 , fOutputSecondary(outputSecondary)
47                 , fTexSamplers(texSamplers)
48                 , fBufferSamplers(bufferSamplers)
49                 , fImageStorages(imageStorages) {}
50 
51         GrGLSLXPFragmentBuilder* fXPFragBuilder;
52         GrGLSLUniformHandler* fUniformHandler;
53         const GrShaderCaps* fShaderCaps;
54         const GrXferProcessor& fXP;
55         const char* fInputColor;
56         const char* fInputCoverage;
57         const char* fOutputPrimary;
58         const char* fOutputSecondary;
59         const SamplerHandle* fTexSamplers;
60         const SamplerHandle* fBufferSamplers;
61         const ImageStorageHandle* fImageStorages;
62     };
63     /**
64      * This is similar to emitCode() in the base class, except it takes a full shader builder.
65      * This allows the effect subclass to emit vertex code.
66      */
67     void emitCode(const EmitArgs&);
68 
69     /** A GrGLSLXferProcessor instance can be reused with any GrGLSLXferProcessor that produces
70         the same stage key; this function reads data from a GrGLSLXferProcessor and uploads any
71         uniform variables required  by the shaders created in emitCode(). The GrXferProcessor
72         parameter is guaranteed to be of the same type that created this GrGLSLXferProcessor and
73         to have an identical processor key as the one that created this GrGLSLXferProcessor. This
74         function calls onSetData on the subclass of GrGLSLXferProcessor
75      */
76     void setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp);
77 
78 protected:
79     static void DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
80                                           const char* srcCoverage,
81                                           const char* dstColor,
82                                           const char* outColor,
83                                           const char* outColorSecondary,
84                                           const GrXferProcessor& proc);
85 
86 private:
87     /**
88      * Called by emitCode() when the XP will not be performing a dst read. This method is
89      * responsible for both blending and coverage. A subclass only needs to implement this method if
90      * it can construct a GrXferProcessor that will not read the dst color.
91      */
emitOutputsForBlendState(const EmitArgs &)92     virtual void emitOutputsForBlendState(const EmitArgs&) {
93         SkFAIL("emitOutputsForBlendState not implemented.");
94     }
95 
96     /**
97      * Called by emitCode() when the XP will perform a dst read. This method only needs to supply
98      * the blending logic. The base class applies coverage. A subclass only needs to implement this
99      * method if it can construct a GrXferProcessor that reads the dst color.
100      */
emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder *,GrGLSLUniformHandler *,const char * srcColor,const char * srcCoverage,const char * dstColor,const char * outColor,const char * outColorSecondary,const GrXferProcessor &)101     virtual void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder*,
102                                          GrGLSLUniformHandler*,
103                                          const char* srcColor,
104                                          const char* srcCoverage,
105                                          const char* dstColor,
106                                          const char* outColor,
107                                          const char* outColorSecondary,
108                                          const GrXferProcessor&) {
109         SkFAIL("emitBlendCodeForDstRead not implemented.");
110     }
111 
112     virtual void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) = 0;
113 
114     GrGLSLProgramDataManager::UniformHandle fDstTopLeftUni;
115     GrGLSLProgramDataManager::UniformHandle fDstScaleUni;
116 };
117 #endif
118