Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLProgramDataManager.h
1 /*
2  * Copyright 2012 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 GrGLProgramDataManager_DEFINED
9 #define GrGLProgramDataManager_DEFINED
10
11 #include "gl/GrGLShaderVar.h"
12 #include "gl/GrGLSL.h"
13 #include "GrAllocator.h"
14
15 #include "SkTArray.h"
16
17 class GrGpuGL;
18 class SkMatrix;
19 class GrGLProgram;
20 class GrGLShaderBuilder;
21
22 /** Manages the resources used by a shader program.
23  * The resources are objects the program uses to communicate with the
24  * application code.
25  */
26 class GrGLProgramDataManager : public SkRefCnt {
27 public:
28     // Opaque handle to a uniform
29     class UniformHandle {
30     public:
31         /** Creates a reference to an unifrom of a GrGLShaderBuilder.
32          * The ref can be used to set the uniform with corresponding the GrGLProgramDataManager.*/
33         static UniformHandle CreateFromUniformIndex(int i);
34
35         bool isValid() const { return -1 != fValue; }
36
37         bool operator==(const UniformHandle& other) const { return other.fValue == fValue; }
38
39         UniformHandle()
40             : fValue(-1) {
41         }
42
43     private:
44         UniformHandle(int value)
45             : fValue(value) {
46             SkASSERT(isValid());
47         }
48
49         int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; }
50         int toShaderBuilderIndex() const { return toProgramDataIndex(); }
51
52         int fValue;
53         friend class GrGLProgramDataManager; // For accessing toProgramDataIndex().
54         friend class GrGLShaderBuilder; // For accessing toShaderBuilderIndex().
55     };
56
57     GrGLProgramDataManager(GrGpuGL*, GrGLProgram*, const GrGLShaderBuilder&);
58
59     /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
60      *  array of uniforms. arrayCount must be <= the array count of the uniform.
61      */
62     void setSampler(UniformHandle, GrGLint texUnit) const;
63     void set1f(UniformHandle, GrGLfloat v0) const;
64     void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
65     void set2f(UniformHandle, GrGLfloat, GrGLfloat) const;
66     void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
67     void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const;
68     void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
69     void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const;
70     void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
71     // matrices are column-major, the first three upload a single matrix, the latter three upload
72     // arrayCount matrices into a uniform array.
73     void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const;
74     void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const;
75     void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
76     void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
77
78     // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
79     void setSkMatrix(UniformHandle, const SkMatrix&) const;
80
81 private:
82     enum {
83         kUnusedUniform = -1,
84     };
85
86     struct Uniform {
87         GrGLint     fVSLocation;
88         GrGLint     fFSLocation;
89         SkDEBUGCODE(
90             GrSLType    fType;
91             int         fArrayCount;
92         );
93     };
94
95     SkTArray<Uniform, true> fUniforms;
96     GrGpuGL* fGpu;
97
98     typedef SkRefCnt INHERITED;
99 };
100
101 #endif