Program.h revision 3e263fac8c9c0e0fb242186b514a7af8efb40961
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HWUI_PROGRAM_H
18#define ANDROID_HWUI_PROGRAM_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <utils/KeyedVector.h>
24
25#include "Matrix.h"
26
27namespace android {
28namespace uirenderer {
29
30/**
31 * A program holds a vertex and a fragment shader. It offers several utility
32 * methods to query attributes and uniforms.
33 */
34class Program {
35public:
36    enum ShaderBindings {
37        kBindingPosition,
38        kBindingTexCoords
39    };
40
41    /**
42     * Creates a new program with the specified vertex and fragment
43     * shaders sources.
44     */
45    Program(const char* vertex, const char* fragment);
46    virtual ~Program();
47
48    /**
49     * Binds this program to the GL context.
50     */
51    virtual void use();
52
53    /**
54     * Marks this program as unused. This will not unbind
55     * the program from the GL context.
56     */
57    virtual void remove();
58
59    /**
60     * Returns the OpenGL name of the specified attribute.
61     */
62    int getAttrib(const char* name);
63
64    /**
65     * Returns the OpenGL name of the specified uniform.
66     */
67    int getUniform(const char* name);
68
69    /**
70     * Indicates whether this program is currently in use with
71     * the GL context.
72     */
73    inline bool isInUse() const {
74        return mUse;
75    }
76
77    /**
78     * Indicates whether this program was correctly compiled and linked.
79     */
80    inline bool isInitialized() const {
81        return mInitialized;
82    }
83
84    /**
85     * Binds the program with the specified projection, modelView and
86     * transform matrices.
87     */
88    void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
89             const mat4& transformMatrix, bool offset = false);
90
91    /**
92     * Sets the color associated with this shader.
93     */
94    void setColor(const float r, const float g, const float b, const float a);
95
96    /**
97     * Name of the position attribute.
98     */
99    int position;
100
101    /**
102     * Name of the transform uniform.
103     */
104    int transform;
105
106protected:
107    /**
108     * Adds an attribute with the specified name.
109     *
110     * @return The OpenGL name of the attribute.
111     */
112    int addAttrib(const char* name);
113
114    /**
115     * Binds the specified attribute name to the specified slot.
116     */
117    int bindAttrib(const char* name, ShaderBindings bindingSlot);
118
119    /**
120     * Adds a uniform with the specified name.
121     *
122     * @return The OpenGL name of the uniform.
123     */
124    int addUniform(const char* name);
125
126private:
127    /**
128     * Compiles the specified shader of the specified type.
129     *
130     * @return The name of the compiled shader.
131     */
132    GLuint buildShader(const char* source, GLenum type);
133
134    // Name of the OpenGL program and shaders
135    GLuint mProgramId;
136    GLuint mVertexShader;
137    GLuint mFragmentShader;
138
139    // Keeps track of attributes and uniforms slots
140    KeyedVector<const char*, int> mAttributes;
141    KeyedVector<const char*, int> mUniforms;
142
143    bool mUse;
144    bool mInitialized;
145
146    bool mHasColorUniform;
147    int mColorUniform;
148}; // class Program
149
150}; // namespace uirenderer
151}; // namespace android
152
153#endif // ANDROID_HWUI_PROGRAM_H
154