• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
85 }
86 
~Program()87 Program::~Program() {
88     if (mInitialized) {
89         glDetachShader(mProgramId, mVertexShader);
90         glDetachShader(mProgramId, mFragmentShader);
91 
92         glDeleteShader(mVertexShader);
93         glDeleteShader(mFragmentShader);
94 
95         glDeleteProgram(mProgramId);
96     }
97 }
98 
addAttrib(const char * name)99 int Program::addAttrib(const char* name) {
100     int slot = glGetAttribLocation(mProgramId, name);
101     mAttributes.add(name, slot);
102     return slot;
103 }
104 
bindAttrib(const char * name,ShaderBindings bindingSlot)105 int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
106     glBindAttribLocation(mProgramId, bindingSlot, name);
107     mAttributes.add(name, bindingSlot);
108     return bindingSlot;
109 }
110 
getAttrib(const char * name)111 int Program::getAttrib(const char* name) {
112     ssize_t index = mAttributes.indexOfKey(name);
113     if (index >= 0) {
114         return mAttributes.valueAt(index);
115     }
116     return addAttrib(name);
117 }
118 
addUniform(const char * name)119 int Program::addUniform(const char* name) {
120     int slot = glGetUniformLocation(mProgramId, name);
121     mUniforms.add(name, slot);
122     return slot;
123 }
124 
getUniform(const char * name)125 int Program::getUniform(const char* name) {
126     ssize_t index = mUniforms.indexOfKey(name);
127     if (index >= 0) {
128         return mUniforms.valueAt(index);
129     }
130     return addUniform(name);
131 }
132 
buildShader(const char * source,GLenum type)133 GLuint Program::buildShader(const char* source, GLenum type) {
134     GLuint shader = glCreateShader(type);
135     glShaderSource(shader, 1, &source, 0);
136     glCompileShader(shader);
137 
138     GLint status;
139     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
140     if (status != GL_TRUE) {
141         // Some drivers return wrong values for GL_INFO_LOG_LENGTH
142         // use a fixed size instead
143         GLchar log[512];
144         glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
145         ALOGE("Error while compiling shader: %s", log);
146         glDeleteShader(shader);
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     mat4 t(projectionMatrix);
156     if (offset) {
157         // offset screenspace xy by an amount that compensates for typical precision
158         // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
159         // up and to the left.
160         // This offset value is based on an assumption that some hardware may use as
161         // little as 12.4 precision, so we offset by slightly more than 1/16.
162         t.translate(.375, .375, 0);
163     }
164     t.multiply(transformMatrix);
165     t.multiply(modelViewMatrix);
166 
167     glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
168 }
169 
setColor(const float r,const float g,const float b,const float a)170 void Program::setColor(const float r, const float g, const float b, const float a) {
171     if (!mHasColorUniform) {
172         mColorUniform = getUniform("color");
173         mHasColorUniform = true;
174     }
175     glUniform4f(mColorUniform, r, g, b, a);
176 }
177 
use()178 void Program::use() {
179     glUseProgram(mProgramId);
180     if (texCoords >= 0 && !mHasSampler) {
181         glUniform1i(getUniform("sampler"), 0);
182         mHasSampler = true;
183     }
184     mUse = true;
185 }
186 
remove()187 void Program::remove() {
188     mUse = false;
189 }
190 
191 }; // namespace uirenderer
192 }; // namespace android
193