blob: 93c5b346fd41bca2dcf4fedd901cd7d639ea3274 [file] [log] [blame]
Chet Haasedd78cca2010-10-22 18:59:26 -07001/*
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
Romain Guy746b7402010-10-26 16:27:31 -070046 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
47
Romain Guy9bca4792010-10-25 18:42:25 -070048 mCurrentBuffer = meshBuffer;
Romain Guy5b3b3522010-10-27 18:57:51 -070049 mRegionMesh = NULL;
Chet Haasedd78cca2010-10-22 18:59:26 -070050}
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052Caches::~Caches() {
53 delete[] mRegionMesh;
54}
55
56///////////////////////////////////////////////////////////////////////////////
57// VBO
58///////////////////////////////////////////////////////////////////////////////
59
Chet Haasedd78cca2010-10-22 18:59:26 -070060void Caches::bindMeshBuffer() {
61 bindMeshBuffer(meshBuffer);
62}
63
Chet Haasedd78cca2010-10-22 18:59:26 -070064void Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -070065 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -070066 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -070067 mCurrentBuffer = buffer;
Chet Haasedd78cca2010-10-22 18:59:26 -070068 }
69}
70
Chet Haasedd78cca2010-10-22 18:59:26 -070071void Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -070072 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -070073 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -070074 mCurrentBuffer = 0;
Chet Haasedd78cca2010-10-22 18:59:26 -070075 }
76}
77
Romain Guy5b3b3522010-10-27 18:57:51 -070078TextureVertex* 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
Chet Haasedd78cca2010-10-22 18:59:26 -0700108}; // namespace uirenderer
109}; // namespace android