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 GrGLSLProgramDataManager_DEFINED
9 #define GrGLSLProgramDataManager_DEFINED
10 
11 #include "SkTypes.h"
12 
13 class SkMatrix;
14 
15 /** Manages the resources used by a shader program.
16  * The resources are objects the program uses to communicate with the
17  * application code.
18  */
19 class GrGLSLProgramDataManager : SkNoncopyable {
20 public:
21     // Opaque handle to a resource
22     class ShaderResourceHandle {
23     public:
ShaderResourceHandle(int value)24         ShaderResourceHandle(int value)
25             : fValue(value) {
26             SkASSERT(this->isValid());
27         }
28 
ShaderResourceHandle()29         ShaderResourceHandle()
30             : fValue(kInvalid_ShaderResourceHandle) {
31         }
32 
33         bool operator==(const ShaderResourceHandle& other) const { return other.fValue == fValue; }
isValid()34         bool isValid() const { return kInvalid_ShaderResourceHandle != fValue; }
toIndex()35         int toIndex() const { SkASSERT(this->isValid()); return fValue; }
36 
37     private:
38         static const int kInvalid_ShaderResourceHandle = -1;
39         int fValue;
40     };
41 
42     typedef ShaderResourceHandle UniformHandle;
43 
~GrGLSLProgramDataManager()44     virtual ~GrGLSLProgramDataManager() {}
45 
46     /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
47      *  array of uniforms. arrayCount must be <= the array count of the uniform.
48      */
49     virtual void set1f(UniformHandle, float v0) const = 0;
50     virtual void set1fv(UniformHandle, int arrayCount, const float v[]) const = 0;
51     virtual void set2f(UniformHandle, float, float) const = 0;
52     virtual void set2fv(UniformHandle, int arrayCount, const float v[]) const = 0;
53     virtual void set3f(UniformHandle, float, float, float) const = 0;
54     virtual void set3fv(UniformHandle, int arrayCount, const float v[]) const = 0;
55     virtual void set4f(UniformHandle, float, float, float, float) const = 0;
56     virtual void set4fv(UniformHandle, int arrayCount, const float v[]) const = 0;
57     // matrices are column-major, the first three upload a single matrix, the latter three upload
58     // arrayCount matrices into a uniform array.
59     virtual void setMatrix3f(UniformHandle, const float matrix[]) const = 0;
60     virtual void setMatrix4f(UniformHandle, const float matrix[]) const = 0;
61     virtual void setMatrix3fv(UniformHandle, int arrayCount, const float matrices[]) const = 0;
62     virtual void setMatrix4fv(UniformHandle, int arrayCount, const float matrices[]) const = 0;
63 
64     // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
65     void setSkMatrix(UniformHandle, const SkMatrix&) const;
66 
67     // for nvpr only
68     typedef ShaderResourceHandle VaryingHandle;
69     virtual void setPathFragmentInputTransform(VaryingHandle u, int components,
70                                                const SkMatrix& matrix) const = 0;
71 
72 protected:
GrGLSLProgramDataManager()73     GrGLSLProgramDataManager() {}
74 
75 private:
76 
77     typedef SkNoncopyable INHERITED;
78 };
79 
80 #endif
81