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