• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 SkNormalSourcePriv_DEFINED
9 #define SkNormalSourcePriv_DEFINED
10 
11 #if SK_SUPPORT_GPU
12 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 
15 /* GLSLFragmentProcessors for NormalSourceImpls must sub-class this class and override onEmitCode,
16  * and setNormalData calls, as well as all other calls FPs normally override, except for the 2
17  * defined in this superclass.
18  * This class exists to intercept emitCode calls and emit <0, 0, 1> if the FP requires a distance
19  * vector but the GP doesn't provide it. onSetData calls need to be intercepted too because
20  * uniform handlers will be invalid in subclasses where onEmitCode isn't called.
21  * We don't need to adjust the key here since the use of a given GP (through its class ID already in
22  * the key), will determine what code gets emitted here.
23  */
24 class GLSLNormalFP : public GrGLSLFragmentProcessor {
25 public:
GLSLNormalFP()26     GLSLNormalFP()
27         : fDidIntercept(false) {}
28 
emitCode(EmitArgs & args)29     void emitCode(EmitArgs& args) final override {
30         if (args.fFp.usesDistanceVectorField() && !args.fGpImplementsDistanceVector) {
31             GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
32             fragBuilder->codeAppendf("// GLSLNormalFP intercepted emitCode call, GP does not "
33                                              "implement required distance vector feature\n");
34             fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor);
35 
36             fDidIntercept = true;
37         } else {
38             this->onEmitCode(args);
39         }
40     }
41 
onSetData(const GrGLSLProgramDataManager & pdman,const GrProcessor & proc)42     void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) final override {
43         if (!fDidIntercept) {
44             this->setNormalData(pdman, proc);
45         }
46     }
47 
48 protected:
49     virtual void onEmitCode(EmitArgs& args) = 0;
50     virtual void setNormalData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) = 0;
51 
52 private:
53     bool fDidIntercept;
54 };
55 #endif
56 
57 #endif
58