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