blob: 5981662ad8fa7532cd8b69b14afcc489c914c03e [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 /**
73 * Binds the program with the specified projection, modelView and
74 * transform matrices.
75 */
76 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
77 const mat4& transformMatrix);
78
79 /**
Romain Guy707b2f72010-10-11 16:34:59 -070080 * Sets the color associated with this shader.
81 */
82 void setColor(const float r, const float g, const float b, const float a);
83
84 /**
Romain Guy889f8d12010-07-29 14:37:42 -070085 * Name of the position attribute.
86 */
87 int position;
88
89 /**
Romain Guy889f8d12010-07-29 14:37:42 -070090 * Name of the transform uniform.
91 */
92 int transform;
93
Romain Guy5cbbce52010-06-27 22:59:20 -070094protected:
95 /**
96 * Adds an attribute with the specified name.
97 *
98 * @return The OpenGL name of the attribute.
99 */
100 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700101
102 /**
103 * Adds a uniform with the specified name.
104 *
105 * @return The OpenGL name of the uniform.
106 */
107 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700108
109private:
110 /**
111 * Compiles the specified shader of the specified type.
112 *
113 * @return The name of the compiled shader.
114 */
115 GLuint buildShader(const char* source, GLenum type);
116
117 // Name of the OpenGL program
118 GLuint id;
119
120 // Name of the shaders
121 GLuint vertexShader;
122 GLuint fragmentShader;
123
124 // Keeps track of attributes and uniforms slots
125 KeyedVector<const char*, int> attributes;
126 KeyedVector<const char*, int> uniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700127
128 bool mUse;
Romain Guy5cbbce52010-06-27 22:59:20 -0700129}; // class Program
130
Romain Guy5cbbce52010-06-27 22:59:20 -0700131}; // namespace uirenderer
132}; // namespace android
133
Romain Guy5b3b3522010-10-27 18:57:51 -0700134#endif // ANDROID_HWUI_PROGRAM_H