blob: edd12093f474d8daabca0c7b182fea65fde98141 [file] [log] [blame]
Romain Guy5cbbce52010-06-27 22:59:20 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_PROGRAM_H
18#define ANDROID_HWUI_PROGRAM_H
Romain Guy5cbbce52010-06-27 22:59:20 -070019
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <utils/KeyedVector.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070024
Romain Guy0b9db912010-07-09 18:53:25 -070025#include "Matrix.h"
26
Romain Guy5cbbce52010-06-27 22:59:20 -070027namespace 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 */
Romain Guy889f8d12010-07-29 14:37:42 -070034class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -070035public:
36 /**
37 * Creates a new program with the specified vertex and fragment
38 * shaders sources.
39 */
40 Program(const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -070041 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -070042
43 /**
44 * Binds this program to the GL context.
45 */
Romain Guy6926c722010-07-12 20:20:03 -070046 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -070047
Romain Guy260e1022010-07-12 14:41:06 -070048 /**
49 * Marks this program as unused. This will not unbind
50 * the program from the GL context.
51 */
Romain Guy6926c722010-07-12 20:20:03 -070052 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -070053
54 /**
Romain Guyac670c02010-07-27 17:39:27 -070055 * Returns the OpenGL name of the specified attribute.
56 */
57 int getAttrib(const char* name);
58
59 /**
60 * Returns the OpenGL name of the specified uniform.
61 */
62 int getUniform(const char* name);
63
64 /**
Romain Guy260e1022010-07-12 14:41:06 -070065 * Indicates whether this program is currently in use with
66 * the GL context.
67 */
68 inline bool isInUse() const {
69 return mUse;
70 }
71
Romain Guy889f8d12010-07-29 14:37:42 -070072 /**
Romain Guy67f27952010-12-07 20:09:23 -080073 * Indicates whether this program was correctly compiled and linked.
74 */
75 inline bool isInitialized() const {
76 return mInitialized;
77 }
78
79 /**
Romain Guy889f8d12010-07-29 14:37:42 -070080 * Binds the program with the specified projection, modelView and
81 * transform matrices.
82 */
83 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -070084 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -070085
86 /**
Romain Guy707b2f72010-10-11 16:34:59 -070087 * Sets the color associated with this shader.
88 */
89 void setColor(const float r, const float g, const float b, const float a);
90
91 /**
Romain Guy889f8d12010-07-29 14:37:42 -070092 * Name of the position attribute.
93 */
94 int position;
95
96 /**
Romain Guy889f8d12010-07-29 14:37:42 -070097 * Name of the transform uniform.
98 */
99 int transform;
100
Romain Guy5cbbce52010-06-27 22:59:20 -0700101protected:
102 /**
103 * Adds an attribute with the specified name.
104 *
105 * @return The OpenGL name of the attribute.
106 */
107 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700108
109 /**
110 * Adds a uniform with the specified name.
111 *
112 * @return The OpenGL name of the uniform.
113 */
114 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700115
116private:
117 /**
118 * Compiles the specified shader of the specified type.
119 *
120 * @return The name of the compiled shader.
121 */
122 GLuint buildShader(const char* source, GLenum type);
123
124 // Name of the OpenGL program
Romain Guy05bbde72011-12-09 12:55:37 -0800125 GLuint mProgramId;
Romain Guy5cbbce52010-06-27 22:59:20 -0700126
127 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800128 KeyedVector<const char*, int> mAttributes;
129 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700130
131 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800132 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800133
134 bool mHasColorUniform;
135 int mColorUniform;
Romain Guy5cbbce52010-06-27 22:59:20 -0700136}; // class Program
137
Romain Guy5cbbce52010-06-27 22:59:20 -0700138}; // namespace uirenderer
139}; // namespace android
140
Romain Guy5b3b3522010-10-27 18:57:51 -0700141#endif // ANDROID_HWUI_PROGRAM_H