Caches.cpp revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
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 "Caches.h" 20 21namespace android { 22 23#ifdef USE_OPENGL_RENDERER 24using namespace uirenderer; 25ANDROID_SINGLETON_STATIC_INSTANCE(Caches); 26#endif 27 28namespace uirenderer { 29 30/////////////////////////////////////////////////////////////////////////////// 31// Constructors/destructor 32/////////////////////////////////////////////////////////////////////////////// 33 34Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO), 35 lastDstMode(GL_ZERO), currentProgram(NULL) { 36 GLint maxTextureUnits; 37 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); 38 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { 39 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); 40 } 41 42 glGenBuffers(1, &meshBuffer); 43 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); 44 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); 45 46 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); 47 48 mCurrentBuffer = meshBuffer; 49 mRegionMesh = NULL; 50} 51 52Caches::~Caches() { 53 delete[] mRegionMesh; 54} 55 56/////////////////////////////////////////////////////////////////////////////// 57// VBO 58/////////////////////////////////////////////////////////////////////////////// 59 60void Caches::bindMeshBuffer() { 61 bindMeshBuffer(meshBuffer); 62} 63 64void Caches::bindMeshBuffer(const GLuint buffer) { 65 if (mCurrentBuffer != buffer) { 66 glBindBuffer(GL_ARRAY_BUFFER, buffer); 67 mCurrentBuffer = buffer; 68 } 69} 70 71void Caches::unbindMeshBuffer() { 72 if (mCurrentBuffer) { 73 glBindBuffer(GL_ARRAY_BUFFER, 0); 74 mCurrentBuffer = 0; 75 } 76} 77 78TextureVertex* Caches::getRegionMesh() { 79 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region 80 if (!mRegionMesh) { 81 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; 82 83 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; 84 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { 85 uint16_t quad = i * 4; 86 int index = i * 6; 87 regionIndices[index ] = quad; // top-left 88 regionIndices[index + 1] = quad + 1; // top-right 89 regionIndices[index + 2] = quad + 2; // bottom-left 90 regionIndices[index + 3] = quad + 2; // bottom-left 91 regionIndices[index + 4] = quad + 1; // top-right 92 regionIndices[index + 5] = quad + 3; // bottom-right 93 } 94 95 glGenBuffers(1, &mRegionMeshIndices); 96 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); 97 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), 98 regionIndices, GL_STATIC_DRAW); 99 100 delete[] regionIndices; 101 } else { 102 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); 103 } 104 105 return mRegionMesh; 106} 107 108}; // namespace uirenderer 109}; // namespace android 110