blob: fde4f966c7b13f96030c9ed1113a7debf19edd07 [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
Romain Guyc15008e2010-11-10 11:59:15 -080019#include <utils/Log.h>
20
Chet Haasedd78cca2010-10-22 18:59:26 -070021#include "Caches.h"
Romain Guye190aa62010-11-10 19:01:29 -080022#include "Properties.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070023
24namespace android {
25
26#ifdef USE_OPENGL_RENDERER
27using namespace uirenderer;
28ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
29#endif
30
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Constructors/destructor
35///////////////////////////////////////////////////////////////////////////////
36
37Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO),
38 lastDstMode(GL_ZERO), currentProgram(NULL) {
39 GLint maxTextureUnits;
40 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
41 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
42 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
43 }
44
45 glGenBuffers(1, &meshBuffer);
46 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
47 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
48
Romain Guy746b7402010-10-26 16:27:31 -070049 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
50
Romain Guy9bca4792010-10-25 18:42:25 -070051 mCurrentBuffer = meshBuffer;
Romain Guy5b3b3522010-10-27 18:57:51 -070052 mRegionMesh = NULL;
Romain Guye190aa62010-11-10 19:01:29 -080053
54 mDebugLevel = readDebugLevel();
55 LOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080056
57#if RENDER_LAYERS_AS_REGIONS
58 LOGD("Layers will be composited as regions");
59#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070060}
61
Romain Guy5b3b3522010-10-27 18:57:51 -070062Caches::~Caches() {
63 delete[] mRegionMesh;
64}
65
66///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -080067// Debug
68///////////////////////////////////////////////////////////////////////////////
69
70void Caches::dumpMemoryUsage() {
71 LOGD("Current memory usage / total memory usage (bytes):");
72 LOGD(" TextureCache %8d / %8d", textureCache.getSize(), textureCache.getMaxSize());
73 LOGD(" LayerCache %8d / %8d", layerCache.getSize(), layerCache.getMaxSize());
74 LOGD(" GradientCache %8d / %8d", gradientCache.getSize(), gradientCache.getMaxSize());
75 LOGD(" PathCache %8d / %8d", pathCache.getSize(), pathCache.getMaxSize());
76 LOGD(" TextDropShadowCache %8d / %8d", dropShadowCache.getSize(),
77 dropShadowCache.getMaxSize());
78 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
79 const uint32_t size = fontRenderer.getFontRendererSize(i);
80 LOGD(" FontRenderer %d %8d / %8d", i, size, size);
81 }
82 LOGD("Other:");
83 LOGD(" FboCache %8d / %8d", fboCache.getSize(), fboCache.getMaxSize());
84 LOGD(" PatchCache %8d / %8d", patchCache.getSize(), patchCache.getMaxSize());
85
86 uint32_t total = 0;
87 total += textureCache.getSize();
88 total += layerCache.getSize();
89 total += gradientCache.getSize();
90 total += pathCache.getSize();
91 total += dropShadowCache.getSize();
92 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
93 total += fontRenderer.getFontRendererSize(i);
94 }
95
96 LOGD("Total memory usage:");
97 LOGD(" %d bytes, %.2f MB", total, total / 1024.0f / 1024.0f);
98 LOGD("\n");
99}
100
101///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800102// Memory management
103///////////////////////////////////////////////////////////////////////////////
104
105void Caches::clearGarbage() {
106 textureCache.clearGarbage();
107 gradientCache.clearGarbage();
108 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800109
110 Mutex::Autolock _l(mGarbageLock);
111
Romain Guyada830f2011-01-13 12:13:20 -0800112 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800113 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800114 Layer* layer = mLayerGarbage.itemAt(i);
115 if (layer) {
116 if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
117 if (layer->texture) glDeleteTextures(1, &layer->texture);
Romain Guy57066eb2011-01-12 12:53:32 -0800118
Romain Guyada830f2011-01-13 12:13:20 -0800119 delete layer;
120 }
Romain Guy57066eb2011-01-12 12:53:32 -0800121 }
Romain Guyada830f2011-01-13 12:13:20 -0800122 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800123}
124
Romain Guyada830f2011-01-13 12:13:20 -0800125void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800126 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800127 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800128}
129
130///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700131// VBO
132///////////////////////////////////////////////////////////////////////////////
133
Chet Haasedd78cca2010-10-22 18:59:26 -0700134void Caches::bindMeshBuffer() {
135 bindMeshBuffer(meshBuffer);
136}
137
Chet Haasedd78cca2010-10-22 18:59:26 -0700138void Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700139 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700140 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700141 mCurrentBuffer = buffer;
Chet Haasedd78cca2010-10-22 18:59:26 -0700142 }
143}
144
Chet Haasedd78cca2010-10-22 18:59:26 -0700145void Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700146 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700147 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700148 mCurrentBuffer = 0;
Chet Haasedd78cca2010-10-22 18:59:26 -0700149 }
150}
151
Romain Guy5b3b3522010-10-27 18:57:51 -0700152TextureVertex* Caches::getRegionMesh() {
153 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
154 if (!mRegionMesh) {
155 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
156
157 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
158 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
159 uint16_t quad = i * 4;
160 int index = i * 6;
161 regionIndices[index ] = quad; // top-left
162 regionIndices[index + 1] = quad + 1; // top-right
163 regionIndices[index + 2] = quad + 2; // bottom-left
164 regionIndices[index + 3] = quad + 2; // bottom-left
165 regionIndices[index + 4] = quad + 1; // top-right
166 regionIndices[index + 5] = quad + 3; // bottom-right
167 }
168
169 glGenBuffers(1, &mRegionMeshIndices);
170 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
171 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
172 regionIndices, GL_STATIC_DRAW);
173
174 delete[] regionIndices;
175 } else {
176 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
177 }
178
179 return mRegionMesh;
180}
181
Chet Haasedd78cca2010-10-22 18:59:26 -0700182}; // namespace uirenderer
183}; // namespace android