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 27 namespace android { 28 namespace 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 */ 34 class Program { 35 public: 36 /** 37 * Creates a new program with the specified vertex and fragment 38 * shaders sources. 39 */ 40 Program(const char* vertex, const char* fragment); 41 virtual ~Program(); 42 43 /** 44 * Binds this program to the GL context. 45 */ 46 virtual void use(); 47 48 /** 49 * Marks this program as unused. This will not unbind 50 * the program from the GL context. 51 */ 52 virtual void remove(); 53 54 /** 55 * 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 /** 65 * Indicates whether this program is currently in use with 66 * the GL context. 67 */ 68 inline bool isInUse() const { 69 return mUse; 70 } 71 72 /** 73 * Indicates whether this program was correctly compiled and linked. 74 */ 75 inline bool isInitialized() const { 76 return mInitialized; 77 } 78 79 /** 80 * Binds the program with the specified projection, modelView and 81 * transform matrices. 82 */ 83 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix, 84 const mat4& transformMatrix, bool offset = false); 85 86 /** 87 * 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 /** 92 * Name of the position attribute. 93 */ 94 int position; 95 96 /** 97 * Name of the transform uniform. 98 */ 99 int transform; 100 101 protected: 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); 108 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); 115 116 private: 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 125 GLuint id; 126 127 // Name of the shaders 128 GLuint vertexShader; 129 GLuint fragmentShader; 130 131 // Keeps track of attributes and uniforms slots 132 KeyedVector<const char*, int> attributes; 133 KeyedVector<const char*, int> uniforms; 134 135 bool mUse; 136 bool mInitialized; 137 }; // class Program 138 139 }; // namespace uirenderer 140 }; // namespace android 141 142 #endif // ANDROID_HWUI_PROGRAM_H 143