blob: 27039dd0431132cd5f55a1c263e46a75ade7ad14 [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>
Chet Haase9c1e23b2011-03-24 10:51:31 -070020#include <utils/String8.h>
Romain Guyc15008e2010-11-10 11:59:15 -080021
Chet Haasedd78cca2010-10-22 18:59:26 -070022#include "Caches.h"
Romain Guye190aa62010-11-10 19:01:29 -080023#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080024#include "LayerRenderer.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070025
26namespace android {
27
28#ifdef USE_OPENGL_RENDERER
29using namespace uirenderer;
30ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
31#endif
32
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070036// Macros
37///////////////////////////////////////////////////////////////////////////////
38
39#if DEBUG_CACHE_FLUSH
40 #define FLUSH_LOGD(...) LOGD(__VA_ARGS__)
41#else
42 #define FLUSH_LOGD(...)
43#endif
44
45///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070046// Constructors/destructor
47///////////////////////////////////////////////////////////////////////////////
48
Romain Guy8ff6b9e2011-11-09 20:10:18 -080049Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
Chet Haasedd78cca2010-10-22 18:59:26 -070050 GLint maxTextureUnits;
51 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
52 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
53 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
54 }
55
Romain Guy746b7402010-10-26 16:27:31 -070056 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
57
Romain Guy8ff6b9e2011-11-09 20:10:18 -080058 init();
Romain Guye190aa62010-11-10 19:01:29 -080059
60 mDebugLevel = readDebugLevel();
61 LOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080062
63#if RENDER_LAYERS_AS_REGIONS
Romain Guy02ccac62011-06-24 13:20:23 -070064 INIT_LOGD("Layers will be composited as regions");
Romain Guy7230a742011-01-10 22:26:16 -080065#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070066}
67
Romain Guy8ff6b9e2011-11-09 20:10:18 -080068void Caches::init() {
69 if (mInitialized) return;
70
71 glGenBuffers(1, &meshBuffer);
72 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
73 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
74
75 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080076 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080077 mCurrentPositionPointer = this;
78 mCurrentTexCoordsPointer = this;
79
Romain Guy15bc6432011-12-13 13:11:32 -080080 mTexCoordsArrayEnabled = false;
81
Romain Guy8ff6b9e2011-11-09 20:10:18 -080082 mRegionMesh = NULL;
83
84 blend = false;
85 lastSrcMode = GL_ZERO;
86 lastDstMode = GL_ZERO;
87 currentProgram = NULL;
88
89 mInitialized = true;
90}
91
92void Caches::terminate() {
93 if (!mInitialized) return;
94
95 glDeleteBuffers(1, &meshBuffer);
96 mCurrentBuffer = 0;
97
98 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -070099 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800100 mRegionMesh = NULL;
101
102 fboCache.clear();
103
104 programCache.clear();
105 currentProgram = NULL;
106
107 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700108}
109
110///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800111// Debug
112///////////////////////////////////////////////////////////////////////////////
113
114void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700115 String8 stringLog;
116 dumpMemoryUsage(stringLog);
117 LOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700118}
119
120void Caches::dumpMemoryUsage(String8 &log) {
121 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
122 log.appendFormat(" TextureCache %8d / %8d\n",
123 textureCache.getSize(), textureCache.getMaxSize());
124 log.appendFormat(" LayerCache %8d / %8d\n",
125 layerCache.getSize(), layerCache.getMaxSize());
126 log.appendFormat(" GradientCache %8d / %8d\n",
127 gradientCache.getSize(), gradientCache.getMaxSize());
128 log.appendFormat(" PathCache %8d / %8d\n",
129 pathCache.getSize(), pathCache.getMaxSize());
130 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800131 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700132 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800133 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700134 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800135 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700136 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800137 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700138 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800139 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700140 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800141 dropShadowCache.getMaxSize());
142 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
143 const uint32_t size = fontRenderer.getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700144 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800145 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700146 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700147 log.appendFormat(" FboCache %8d / %8d\n",
148 fboCache.getSize(), fboCache.getMaxSize());
149 log.appendFormat(" PatchCache %8d / %8d\n",
150 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800151
152 uint32_t total = 0;
153 total += textureCache.getSize();
154 total += layerCache.getSize();
155 total += gradientCache.getSize();
156 total += pathCache.getSize();
157 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800158 total += roundRectShapeCache.getSize();
159 total += circleShapeCache.getSize();
160 total += ovalShapeCache.getSize();
161 total += rectShapeCache.getSize();
162 total += arcShapeCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800163 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
164 total += fontRenderer.getFontRendererSize(i);
165 }
166
Chet Haase9c1e23b2011-03-24 10:51:31 -0700167 log.appendFormat("Total memory usage:\n");
168 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800169}
170
171///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800172// Memory management
173///////////////////////////////////////////////////////////////////////////////
174
175void Caches::clearGarbage() {
176 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800177 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800178
179 Mutex::Autolock _l(mGarbageLock);
180
Romain Guyada830f2011-01-13 12:13:20 -0800181 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800182 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800183 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800184 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800185 }
Romain Guyada830f2011-01-13 12:13:20 -0800186 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800187}
188
Romain Guyada830f2011-01-13 12:13:20 -0800189void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800190 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800191 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800192}
193
Romain Guybdf76092011-07-18 15:00:43 -0700194void Caches::flush(FlushMode mode) {
195 FLUSH_LOGD("Flushing caches (mode %d)", mode);
196
197 clearGarbage();
198
199 switch (mode) {
200 case kFlushMode_Full:
201 textureCache.clear();
202 patchCache.clear();
203 dropShadowCache.clear();
204 gradientCache.clear();
Romain Guyeca0ca22011-11-04 15:12:29 -0700205 fontRenderer.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700206 // fall through
207 case kFlushMode_Moderate:
Romain Guyeca0ca22011-11-04 15:12:29 -0700208 fontRenderer.flush();
209 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700210 pathCache.clear();
211 roundRectShapeCache.clear();
212 circleShapeCache.clear();
213 ovalShapeCache.clear();
214 rectShapeCache.clear();
215 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700216 // fall through
217 case kFlushMode_Layers:
218 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700219 break;
220 }
221}
222
Romain Guyfe48f652010-11-11 15:36:56 -0800223///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700224// VBO
225///////////////////////////////////////////////////////////////////////////////
226
Romain Guyf3a910b42011-12-12 20:35:21 -0800227bool Caches::bindMeshBuffer() {
228 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700229}
230
Romain Guyf3a910b42011-12-12 20:35:21 -0800231bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700232 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700233 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700234 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800235 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700236 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800237 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700238}
239
Romain Guyf3a910b42011-12-12 20:35:21 -0800240bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700241 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700242 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700243 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800244 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700245 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800246 return false;
247}
248
Romain Guy15bc6432011-12-13 13:11:32 -0800249bool Caches::bindIndicesBuffer(const GLuint buffer) {
250 if (mCurrentIndicesBuffer != buffer) {
251 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
252 mCurrentIndicesBuffer = buffer;
253 return true;
254 }
255 return false;
256}
257
258bool Caches::unbindIndicesBuffer() {
259 if (mCurrentIndicesBuffer) {
260 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
261 mCurrentIndicesBuffer = 0;
262 return true;
263 }
264 return false;
265}
266
Romain Guyf3a910b42011-12-12 20:35:21 -0800267void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
268 if (force || vertices != mCurrentPositionPointer) {
269 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
270 mCurrentPositionPointer = vertices;
271 }
272}
273
274void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
275 if (force || vertices != mCurrentTexCoordsPointer) {
276 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
277 mCurrentTexCoordsPointer = vertices;
278 }
279}
280
281void Caches::resetVertexPointers() {
282 mCurrentPositionPointer = this;
283 mCurrentTexCoordsPointer = this;
284}
285
286void Caches::resetTexCoordsVertexPointer() {
287 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700288}
289
Romain Guy15bc6432011-12-13 13:11:32 -0800290void Caches::enableTexCoordsVertexArray() {
291 if (!mTexCoordsArrayEnabled) {
292 glEnableVertexAttribArray(Program::kBindingTexCoords);
293 mTexCoordsArrayEnabled = true;
294 }
295}
296
297void Caches::disbaleTexCoordsVertexArray() {
298 if (mTexCoordsArrayEnabled) {
299 glDisableVertexAttribArray(Program::kBindingTexCoords);
300 mTexCoordsArrayEnabled = false;
301 }
302}
303
Romain Guy5b3b3522010-10-27 18:57:51 -0700304TextureVertex* Caches::getRegionMesh() {
305 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
306 if (!mRegionMesh) {
307 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
308
309 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
310 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
311 uint16_t quad = i * 4;
312 int index = i * 6;
313 regionIndices[index ] = quad; // top-left
314 regionIndices[index + 1] = quad + 1; // top-right
315 regionIndices[index + 2] = quad + 2; // bottom-left
316 regionIndices[index + 3] = quad + 2; // bottom-left
317 regionIndices[index + 4] = quad + 1; // top-right
318 regionIndices[index + 5] = quad + 3; // bottom-right
319 }
320
321 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800322 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700323 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
324 regionIndices, GL_STATIC_DRAW);
325
326 delete[] regionIndices;
327 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800328 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700329 }
330
331 return mRegionMesh;
332}
333
Chet Haasedd78cca2010-10-22 18:59:26 -0700334}; // namespace uirenderer
335}; // namespace android