• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrGLSLGeometryProcessor_DEFINED
9 #define GrGLSLGeometryProcessor_DEFINED
10 
11 #include "GrGLSLPrimitiveProcessor.h"
12 
13 class GrGLSLGPBuilder;
14 
15 /**
16  * If a GL effect needs a GrGLFullShaderBuilder* object to emit vertex code, then it must inherit
17  * from this class. Since paths don't have vertices, this class is only meant to be used internally
18  * by skia, for special cases.
19  */
20 class GrGLSLGeometryProcessor : public GrGLSLPrimitiveProcessor {
21 public:
22     /* Any general emit code goes in the base class emitCode.  Subclasses override onEmitCode */
23     void emitCode(EmitArgs&) final;
24 
25 protected:
26     // A helper which subclasses can use to upload coord transform matrices in setData().
27     void setTransformDataHelper(const SkMatrix& localMatrix,
28                                 const GrGLSLProgramDataManager& pdman,
29                                 FPCoordTransformIter*);
30 
31     // Emit transformed local coords from the vertex shader as a uniform matrix and varying per
32     // coord-transform. localCoordsVar must be a 2- or 3-component vector. If it is 3 then it is
33     // assumed to be a 2D homogeneous coordinate.
34     void emitTransforms(GrGLSLVertexBuilder*,
35                         GrGLSLVaryingHandler*,
36                         GrGLSLUniformHandler*,
37                         const GrShaderVar& localCoordsVar,
38                         const SkMatrix& localMatrix,
39                         FPCoordTransformHandler*);
40 
41     // Version of above that assumes identity for the local matrix.
emitTransforms(GrGLSLVertexBuilder * vb,GrGLSLVaryingHandler * varyingHandler,GrGLSLUniformHandler * uniformHandler,const GrShaderVar & localCoordsVar,FPCoordTransformHandler * handler)42     void emitTransforms(GrGLSLVertexBuilder* vb,
43                         GrGLSLVaryingHandler* varyingHandler,
44                         GrGLSLUniformHandler* uniformHandler,
45                         const GrShaderVar& localCoordsVar,
46                         FPCoordTransformHandler* handler) {
47         this->emitTransforms(vb, varyingHandler, uniformHandler, localCoordsVar, SkMatrix::I(),
48                              handler);
49     }
50 
51     struct GrGPArgs {
52         // Used to specify the output variable used by the GP to store its device position. It can
53         // either be a float2 or a float3 (in order to handle perspective). The subclass sets this
54         // in its onEmitCode().
55         GrShaderVar fPositionVar;
56     };
57 
58     // Helpers for adding code to write the transformed vertex position. The first simple version
59     // just writes a variable named by 'posName' into the position output variable with the
60     // assumption that the position is 2D. The second version transforms the input position by a
61     // view matrix and the output variable is 2D or 3D depending on whether the view matrix is
62     // perspective. Both versions declare the output position variable and will set
63     // GrGPArgs::fPositionVar.
64     void writeOutputPosition(GrGLSLVertexBuilder*, GrGPArgs*, const char* posName);
65     void writeOutputPosition(GrGLSLVertexBuilder*,
66                              GrGLSLUniformHandler* uniformHandler,
67                              GrGPArgs*,
68                              const char* posName,
69                              const SkMatrix& mat,
70                              UniformHandle* viewMatrixUniform);
71 
ComputePosKey(const SkMatrix & mat)72     static uint32_t ComputePosKey(const SkMatrix& mat) {
73         if (mat.isIdentity()) {
74             return 0x0;
75         } else if (!mat.hasPerspective()) {
76             return 0x01;
77         } else {
78             return 0x02;
79         }
80     }
81 
82 private:
83     virtual void onEmitCode(EmitArgs&, GrGPArgs*) = 0;
84 
85     struct TransformUniform {
86         UniformHandle  fHandle;
87         SkMatrix       fCurrentValue = SkMatrix::InvalidMatrix();
88     };
89 
90     SkTArray<TransformUniform, true> fInstalledTransforms;
91 
92     typedef GrGLSLPrimitiveProcessor INHERITED;
93 };
94 
95 #endif
96