Program.cpp revision 67f27952c1bcb2230beef9b5ca0bf42edad436a9
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
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Base program
26///////////////////////////////////////////////////////////////////////////////
27
28Program::Program(const char* vertex, const char* fragment) {
29    mInitialized = false;
30
31    vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
32    if (vertexShader) {
33
34        fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
35        if (fragmentShader) {
36
37            id = glCreateProgram();
38            glAttachShader(id, vertexShader);
39            glAttachShader(id, fragmentShader);
40            glLinkProgram(id);
41
42            GLint status;
43            glGetProgramiv(id, GL_LINK_STATUS, &status);
44            if (status != GL_TRUE) {
45                LOGE("Error while linking shaders:");
46                GLint infoLen = 0;
47                glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen);
48                if (infoLen > 1) {
49                    GLchar log[infoLen];
50                    glGetProgramInfoLog(id, infoLen, 0, &log[0]);
51                    LOGE("%s", log);
52                }
53                glDeleteShader(vertexShader);
54                glDeleteShader(fragmentShader);
55                glDeleteProgram(id);
56            } else {
57                mInitialized = true;
58            }
59        }
60    }
61
62    mUse = false;
63
64    if (mInitialized) {
65        position = addAttrib("position");
66        transform = addUniform("transform");
67    }
68}
69
70Program::~Program() {
71    if (mInitialized) {
72        glDeleteShader(vertexShader);
73        glDeleteShader(fragmentShader);
74        glDeleteProgram(id);
75    }
76}
77
78int Program::addAttrib(const char* name) {
79    int slot = glGetAttribLocation(id, name);
80    attributes.add(name, slot);
81    return slot;
82}
83
84int Program::getAttrib(const char* name) {
85    ssize_t index = attributes.indexOfKey(name);
86    if (index >= 0) {
87        return attributes.valueAt(index);
88    }
89    return addAttrib(name);
90}
91
92int Program::addUniform(const char* name) {
93    int slot = glGetUniformLocation(id, name);
94    uniforms.add(name, slot);
95    return slot;
96}
97
98int Program::getUniform(const char* name) {
99    ssize_t index = uniforms.indexOfKey(name);
100    if (index >= 0) {
101        return uniforms.valueAt(index);
102    }
103    return addUniform(name);
104}
105
106GLuint Program::buildShader(const char* source, GLenum type) {
107    GLuint shader = glCreateShader(type);
108    glShaderSource(shader, 1, &source, 0);
109    glCompileShader(shader);
110
111    GLint status;
112    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
113    if (status != GL_TRUE) {
114        // Some drivers return wrong values for GL_INFO_LOG_LENGTH
115        // use a fixed size instead
116        GLchar log[512];
117        glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
118        LOGE("Error while compiling shader: %s", log);
119        glDeleteShader(shader);
120        return 0;
121    }
122
123    return shader;
124}
125
126void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
127        const mat4& transformMatrix) {
128    mat4 t(projectionMatrix);
129    t.multiply(transformMatrix);
130    t.multiply(modelViewMatrix);
131
132    glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
133}
134
135void Program::setColor(const float r, const float g, const float b, const float a) {
136    glUniform4f(getUniform("color"), r, g, b, a);
137}
138
139void Program::use() {
140    glUseProgram(id);
141    mUse = true;
142
143    glEnableVertexAttribArray(position);
144}
145
146void Program::remove() {
147    mUse = false;
148
149    glDisableVertexAttribArray(position);
150}
151
152}; // namespace uirenderer
153}; // namespace android
154