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 #define LOG_TAG "OpenGLRenderer"
18
19 #include "Program.h"
20
21 namespace android {
22 namespace uirenderer {
23
24 ///////////////////////////////////////////////////////////////////////////////
25 // Base program
26 ///////////////////////////////////////////////////////////////////////////////
27
28 // TODO: Program instance should be created from a factory method
Program(const ProgramDescription & description,const char * vertex,const char * fragment)29 Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
30 mInitialized = false;
31 mHasColorUniform = false;
32 mHasSampler = false;
33 mUse = false;
34
35 // No need to cache compiled shaders, rely instead on Android's
36 // persistent shaders cache
37 mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
38 if (mVertexShader) {
39 mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
40 if (mFragmentShader) {
41 mProgramId = glCreateProgram();
42
43 glAttachShader(mProgramId, mVertexShader);
44 glAttachShader(mProgramId, mFragmentShader);
45
46 position = bindAttrib("position", kBindingPosition);
47 if (description.hasTexture || description.hasExternalTexture) {
48 texCoords = bindAttrib("texCoords", kBindingTexCoords);
49 } else {
50 texCoords = -1;
51 }
52
53 glLinkProgram(mProgramId);
54
55 GLint status;
56 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
57 if (status != GL_TRUE) {
58 ALOGE("Error while linking shaders:");
59 GLint infoLen = 0;
60 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
61 if (infoLen > 1) {
62 GLchar log[infoLen];
63 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
64 ALOGE("%s", log);
65 }
66
67 glDetachShader(mProgramId, mVertexShader);
68 glDetachShader(mProgramId, mFragmentShader);
69
70 glDeleteShader(mVertexShader);
71 glDeleteShader(mFragmentShader);
72
73 glDeleteProgram(mProgramId);
74 } else {
75 mInitialized = true;
76 }
77 } else {
78 glDeleteShader(mVertexShader);
79 }
80 }
81
82 if (mInitialized) {
83 transform = addUniform("transform");
84 projection = addUniform("projection");
85 }
86 }
87
~Program()88 Program::~Program() {
89 if (mInitialized) {
90 glDetachShader(mProgramId, mVertexShader);
91 glDetachShader(mProgramId, mFragmentShader);
92
93 glDeleteShader(mVertexShader);
94 glDeleteShader(mFragmentShader);
95
96 glDeleteProgram(mProgramId);
97 }
98 }
99
addAttrib(const char * name)100 int Program::addAttrib(const char* name) {
101 int slot = glGetAttribLocation(mProgramId, name);
102 mAttributes.add(name, slot);
103 return slot;
104 }
105
bindAttrib(const char * name,ShaderBindings bindingSlot)106 int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
107 glBindAttribLocation(mProgramId, bindingSlot, name);
108 mAttributes.add(name, bindingSlot);
109 return bindingSlot;
110 }
111
getAttrib(const char * name)112 int Program::getAttrib(const char* name) {
113 ssize_t index = mAttributes.indexOfKey(name);
114 if (index >= 0) {
115 return mAttributes.valueAt(index);
116 }
117 return addAttrib(name);
118 }
119
addUniform(const char * name)120 int Program::addUniform(const char* name) {
121 int slot = glGetUniformLocation(mProgramId, name);
122 mUniforms.add(name, slot);
123 return slot;
124 }
125
getUniform(const char * name)126 int Program::getUniform(const char* name) {
127 ssize_t index = mUniforms.indexOfKey(name);
128 if (index >= 0) {
129 return mUniforms.valueAt(index);
130 }
131 return addUniform(name);
132 }
133
buildShader(const char * source,GLenum type)134 GLuint Program::buildShader(const char* source, GLenum type) {
135 GLuint shader = glCreateShader(type);
136 glShaderSource(shader, 1, &source, 0);
137 glCompileShader(shader);
138
139 GLint status;
140 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
141 if (status != GL_TRUE) {
142 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
143 // use a fixed size instead
144 GLchar log[512];
145 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
146 ALOGE("Error while compiling shader: %s", log);
147 glDeleteShader(shader);
148 return 0;
149 }
150
151 return shader;
152 }
153
set(const mat4 & projectionMatrix,const mat4 & modelViewMatrix,const mat4 & transformMatrix,bool offset)154 void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
155 const mat4& transformMatrix, bool offset) {
156 mat4 p(projectionMatrix);
157 if (offset) {
158 // offset screenspace xy by an amount that compensates for typical precision
159 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
160 // up and to the left.
161 // This offset value is based on an assumption that some hardware may use as
162 // little as 12.4 precision, so we offset by slightly more than 1/16.
163 p.translate(.065, .065, 0);
164 }
165
166 mat4 t(transformMatrix);
167 t.multiply(modelViewMatrix);
168
169 glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
170 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
171 }
172
setColor(const float r,const float g,const float b,const float a)173 void Program::setColor(const float r, const float g, const float b, const float a) {
174 if (!mHasColorUniform) {
175 mColorUniform = getUniform("color");
176 mHasColorUniform = true;
177 }
178 glUniform4f(mColorUniform, r, g, b, a);
179 }
180
use()181 void Program::use() {
182 glUseProgram(mProgramId);
183 if (texCoords >= 0 && !mHasSampler) {
184 glUniform1i(getUniform("baseSampler"), 0);
185 mHasSampler = true;
186 }
187 mUse = true;
188 }
189
remove()190 void Program::remove() {
191 mUse = false;
192 }
193
194 }; // namespace uirenderer
195 }; // namespace android
196