Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 #include "GLUtils.h"
     16 #include <stdlib.h>
     17 #include <sys/time.h>
     18 
     19 #include <android/asset_manager_jni.h>
     20 
     21 #define LOG_TAG "PTS_OPENGL"
     22 #define LOG_NDEBUG 0
     23 #include <utils/Log.h>
     24 
     25 static JNIEnv* sEnv = NULL;
     26 static jobject sAssetManager = NULL;
     27 
     28 void GLUtils::setEnvAndAssetManager(JNIEnv* env, jobject assetManager) {
     29     sEnv = env;
     30     sAssetManager = assetManager;
     31 }
     32 
     33 static AAsset* loadAsset(const char* path) {
     34     AAssetManager* nativeManager = AAssetManager_fromJava(sEnv, sAssetManager);
     35     if (nativeManager == NULL) {
     36         return NULL;
     37     }
     38     return AAssetManager_open(nativeManager, path, AASSET_MODE_UNKNOWN);;
     39 }
     40 
     41 char* GLUtils::openTextFile(const char* path) {
     42     AAsset* asset = loadAsset(path);
     43     if (asset == NULL) {
     44         ALOGE("Couldn't load %s", path);
     45         return NULL;
     46     }
     47     off_t length = AAsset_getLength(asset);
     48     char* buffer = new char[length + 1];
     49     int num = AAsset_read(asset, buffer, length);
     50     AAsset_close(asset);
     51     if (num != length) {
     52         ALOGE("Couldn't read %s", path);
     53         delete[] buffer;
     54         return NULL;
     55     }
     56     buffer[length] = '\0';
     57     return buffer;
     58 }
     59 
     60 GLuint GLUtils::loadTexture(const char* path) {
     61     GLuint textureId = 0;
     62     jclass activityClass = sEnv->FindClass("com/android/pts/opengl/reference/GLGameActivity");
     63     if (activityClass == NULL) {
     64         ALOGE("Couldn't find activity class");
     65         return -1;
     66     }
     67     jmethodID loadTexture = sEnv->GetStaticMethodID(activityClass, "loadTexture",
     68             "(Landroid/content/res/AssetManager;Ljava/lang/String;)I");
     69     if (loadTexture == NULL) {
     70         ALOGE("Couldn't find loadTexture method");
     71         return -1;
     72     }
     73     jstring pathStr = sEnv->NewStringUTF(path);
     74     textureId = sEnv->CallStaticIntMethod(activityClass, loadTexture, sAssetManager, pathStr);
     75     sEnv->DeleteLocalRef(pathStr);
     76     return textureId;
     77 }
     78 
     79 static int readInt(char* b) {
     80     return (((int) b[0]) << 24) | (((int) b[1]) << 16) | (((int) b[2]) << 8) | ((int) b[3]);
     81 }
     82 
     83 static float readFloat(char* b) {
     84     union {
     85         int input;
     86         float output;
     87     } data;
     88     data.input = readInt(b);
     89     return data.output;
     90 }
     91 
     92 Mesh* GLUtils::loadMesh(const char* path) {
     93     char* buffer = openTextFile(path);
     94     if (buffer == NULL) {
     95         return NULL;
     96     }
     97     int index = 0;
     98     int numVertices = readInt(buffer + index);
     99     index += 4;
    100     float* vertices = new float[numVertices * 3];
    101     float* normals = new float[numVertices * 3];
    102     float* texCoords = new float[numVertices * 2];
    103     for (int i = 0; i < numVertices; i++) {
    104         // Vertices
    105         int vIndex = i * 3;
    106         vertices[vIndex + 0] = readFloat(buffer + index);
    107         index += 4;
    108         vertices[vIndex + 1] = readFloat(buffer + index);
    109         index += 4;
    110         vertices[vIndex + 2] = readFloat(buffer + index);
    111         index += 4;
    112         // Normals
    113         normals[vIndex + 0] = readFloat(buffer + index);
    114         index += 4;
    115         normals[vIndex + 1] = readFloat(buffer + index);
    116         index += 4;
    117         normals[vIndex + 2] = readFloat(buffer + index);
    118         index += 4;
    119         // Texture Coordinates
    120         int tIndex = i * 2;
    121         texCoords[tIndex + 0] = readFloat(buffer + index);
    122         index += 4;
    123         texCoords[tIndex + 1] = readFloat(buffer + index);
    124         index += 4;
    125     }
    126     return new Mesh(vertices, normals, texCoords, numVertices);
    127 }
    128 
    129 // Loads the given source code as a shader of the given type.
    130 static GLuint loadShader(GLenum shaderType, const char** source) {
    131     GLuint shader = glCreateShader(shaderType);
    132     if (shader) {
    133         glShaderSource(shader, 1, source, NULL);
    134         glCompileShader(shader);
    135         GLint compiled = 0;
    136         glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
    137         if (!compiled) {
    138             GLint infoLen = 0;
    139             glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
    140             if (infoLen > 0) {
    141                 char* infoLog = (char*) malloc(sizeof(char) * infoLen);
    142                 glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
    143                 ALOGE("Error compiling shader:\n%s\n", infoLog);
    144                 free(infoLog);
    145             }
    146             glDeleteShader(shader);
    147             shader = 0;
    148         }
    149     }
    150     return shader;
    151 }
    152 
    153 GLuint GLUtils::createProgram(const char** vertexSource, const char** fragmentSource) {
    154     GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
    155     if (!vertexShader) {
    156         return 0;
    157     }
    158 
    159     GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
    160     if (!fragmentShader) {
    161         return 0;
    162     }
    163 
    164     GLuint program = glCreateProgram();
    165     if (program) {
    166         glAttachShader(program, vertexShader);
    167         glAttachShader(program, fragmentShader);
    168 
    169         GLint linkStatus;
    170         glLinkProgram(program);
    171         glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
    172 
    173         if (!linkStatus) {
    174             GLint infoLen = 0;
    175             glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
    176             if (infoLen > 0) {
    177                 char* infoLog = (char*) malloc(sizeof(char) * infoLen);
    178                 glGetProgramInfoLog(program, infoLen, NULL, infoLog);
    179                 ALOGE("Error linking program:\n%s\n", infoLog);
    180                 free(infoLog);
    181             }
    182             glDeleteProgram(program);
    183             program = 0;
    184         }
    185     }
    186     return program;
    187 }
    188 
    189 double GLUtils::currentTimeMillis() {
    190     struct timeval tv;
    191     gettimeofday(&tv, (struct timezone *) NULL);
    192     return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
    193 }
    194 
    195 // Rounds a number up to the smallest power of 2 that is greater than or equal to x.
    196 int GLUtils::roundUpToSmallestPowerOf2(int x) {
    197     if (x < 0) {
    198         return 0;
    199     }
    200     --x;
    201     x |= x >> 1;
    202     x |= x >> 2;
    203     x |= x >> 4;
    204     x |= x >> 8;
    205     x |= x >> 16;
    206     return x + 1;
    207 }
    208 
    209 GLuint GLUtils::genTexture(int texWidth, int texHeight, int fill) {
    210     GLuint textureId = 0;
    211     int w = roundUpToSmallestPowerOf2(texWidth);
    212     int h = roundUpToSmallestPowerOf2(texHeight);
    213     uint32_t* m = new uint32_t[w * h];
    214     if (m != NULL) {
    215         uint32_t* d = m;
    216         for (int y = 0; y < h; y++) {
    217             for (int x = 0; x < w; x++) {
    218                 if (fill == RANDOM_FILL) {
    219                     *d = 0xff000000 | ((y & 0xff) << 16) | ((x & 0xff) << 8) | ((x + y) & 0xff);
    220                 } else {
    221                     *d = 0xff000000 | fill;
    222                 }
    223                 d++;
    224             }
    225         }
    226         glGenTextures(1, &textureId);
    227         glBindTexture(GL_TEXTURE_2D, textureId);
    228         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, m);
    229         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    230         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    231         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    232         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    233     }
    234     delete[] m;
    235     return textureId;
    236 }
    237