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 #include <utils/Trace.h>
18 
19 #include "Program.h"
20 #include "Vertex.h"
21 
22 namespace android {
23 namespace uirenderer {
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 // Base program
27 ///////////////////////////////////////////////////////////////////////////////
28 
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             bindAttrib("position", kBindingPosition);
47             if (description.hasTexture || description.hasExternalTexture) {
48                 texCoords = bindAttrib("texCoords", kBindingTexCoords);
49             } else {
50                 texCoords = -1;
51             }
52 
53             ATRACE_BEGIN("linkProgram");
54             glLinkProgram(mProgramId);
55             ATRACE_END();
56 
57             GLint status;
58             glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
59             if (status != GL_TRUE) {
60                 GLint infoLen = 0;
61                 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
62                 if (infoLen > 1) {
63                     GLchar log[infoLen];
64                     glGetProgramInfoLog(mProgramId, infoLen, nullptr, &log[0]);
65                     ALOGE("%s", log);
66                 }
67                 LOG_ALWAYS_FATAL("Error while linking shaders");
68             } else {
69                 mInitialized = true;
70             }
71         } else {
72             glDeleteShader(mVertexShader);
73         }
74     }
75 
76     if (mInitialized) {
77         transform = addUniform("transform");
78         projection = addUniform("projection");
79     }
80 }
81 
~Program()82 Program::~Program() {
83     if (mInitialized) {
84         // This would ideally happen after linking the program
85         // but Tegra drivers, especially when perfhud is enabled,
86         // sometimes crash if we do so
87         glDetachShader(mProgramId, mVertexShader);
88         glDetachShader(mProgramId, mFragmentShader);
89 
90         glDeleteShader(mVertexShader);
91         glDeleteShader(mFragmentShader);
92 
93         glDeleteProgram(mProgramId);
94     }
95 }
96 
addAttrib(const char * name)97 int Program::addAttrib(const char* name) {
98     int slot = glGetAttribLocation(mProgramId, name);
99     mAttributes.add(name, slot);
100     return slot;
101 }
102 
bindAttrib(const char * name,ShaderBindings bindingSlot)103 int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
104     glBindAttribLocation(mProgramId, bindingSlot, name);
105     mAttributes.add(name, bindingSlot);
106     return bindingSlot;
107 }
108 
getAttrib(const char * name)109 int Program::getAttrib(const char* name) {
110     ssize_t index = mAttributes.indexOfKey(name);
111     if (index >= 0) {
112         return mAttributes.valueAt(index);
113     }
114     return addAttrib(name);
115 }
116 
addUniform(const char * name)117 int Program::addUniform(const char* name) {
118     int slot = glGetUniformLocation(mProgramId, name);
119     mUniforms.add(name, slot);
120     return slot;
121 }
122 
getUniform(const char * name)123 int Program::getUniform(const char* name) {
124     ssize_t index = mUniforms.indexOfKey(name);
125     if (index >= 0) {
126         return mUniforms.valueAt(index);
127     }
128     return addUniform(name);
129 }
130 
buildShader(const char * source,GLenum type)131 GLuint Program::buildShader(const char* source, GLenum type) {
132     ATRACE_NAME("Build GL Shader");
133 
134     GLuint shader = glCreateShader(type);
135     glShaderSource(shader, 1, &source, nullptr);
136     glCompileShader(shader);
137 
138     GLint status;
139     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
140     if (status != GL_TRUE) {
141         ALOGE("Error while compiling this shader:\n===\n%s\n===", source);
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), nullptr, &log[0]);
146         LOG_ALWAYS_FATAL("Shader info log: %s", log);
147         return 0;
148     }
149 
150     return shader;
151 }
152 
set(const mat4 & projectionMatrix,const mat4 & modelViewMatrix,const mat4 & transformMatrix,bool offset)153 void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
154                   const mat4& transformMatrix, bool offset) {
155     if (projectionMatrix != mProjection || offset != mOffset) {
156         if (CC_LIKELY(!offset)) {
157             glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]);
158         } else {
159             mat4 p(projectionMatrix);
160             // offset screenspace xy by an amount that compensates for typical precision
161             // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
162             // up and to the left.
163             // This offset value is based on an assumption that some hardware may use as
164             // little as 12.4 precision, so we offset by slightly more than 1/16.
165             p.translate(Vertex::GeometryFudgeFactor(), Vertex::GeometryFudgeFactor());
166             glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
167         }
168         mProjection = projectionMatrix;
169         mOffset = offset;
170     }
171 
172     mat4 t(transformMatrix);
173     t.multiply(modelViewMatrix);
174     glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
175 }
176 
setColor(FloatColor color)177 void Program::setColor(FloatColor color) {
178     if (!mHasColorUniform) {
179         mColorUniform = getUniform("color");
180         mHasColorUniform = true;
181     }
182     glUniform4f(mColorUniform, color.r, color.g, color.b, color.a);
183 }
184 
use()185 void Program::use() {
186     glUseProgram(mProgramId);
187     if (texCoords >= 0 && !mHasSampler) {
188         glUniform1i(getUniform("baseSampler"), 0);
189         mHasSampler = true;
190     }
191     mUse = true;
192 }
193 
remove()194 void Program::remove() {
195     mUse = false;
196 }
197 
198 };  // namespace uirenderer
199 };  // namespace android
200