blob: 0ef846903343192ad1c4826db6d5140d03df8a5e [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
Steve Block5baa3a62011-12-20 16:23:08 +000040 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070041#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) {
Steve Block8564c8d2012-01-05 23:22:43 +000053 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
Chet Haasedd78cca2010-10-22 18:59:26 -070054 }
55
Romain Guy746b7402010-10-26 16:27:31 -070056 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
57
Romain Guy13631f32012-01-30 17:41:55 -080058 if (extensions.hasDebugMarker()) {
59 eventMark = glInsertEventMarkerEXT;
60 startMark = glPushGroupMarkerEXT;
61 endMark = glPopGroupMarkerEXT;
62 } else {
63 eventMark = eventMarkNull;
64 startMark = startMarkNull;
65 endMark = endMarkNull;
66 }
67
Romain Guy8ff6b9e2011-11-09 20:10:18 -080068 init();
Romain Guye190aa62010-11-10 19:01:29 -080069
70 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000071 ALOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080072
73#if RENDER_LAYERS_AS_REGIONS
Romain Guy02ccac62011-06-24 13:20:23 -070074 INIT_LOGD("Layers will be composited as regions");
Romain Guy7230a742011-01-10 22:26:16 -080075#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070076}
77
Romain Guy8ff6b9e2011-11-09 20:10:18 -080078void Caches::init() {
79 if (mInitialized) return;
80
81 glGenBuffers(1, &meshBuffer);
82 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
83 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
84
85 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080086 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080087 mCurrentPositionPointer = this;
88 mCurrentTexCoordsPointer = this;
89
Romain Guy15bc6432011-12-13 13:11:32 -080090 mTexCoordsArrayEnabled = false;
91
Romain Guy8f85e802011-12-14 19:23:32 -080092 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
93
Romain Guya1d3c912011-12-13 14:55:06 -080094 glActiveTexture(gTextureUnits[0]);
95 mTextureUnit = 0;
96
Romain Guy8ff6b9e2011-11-09 20:10:18 -080097 mRegionMesh = NULL;
98
99 blend = false;
100 lastSrcMode = GL_ZERO;
101 lastDstMode = GL_ZERO;
102 currentProgram = NULL;
103
104 mInitialized = true;
105}
106
107void Caches::terminate() {
108 if (!mInitialized) return;
109
110 glDeleteBuffers(1, &meshBuffer);
111 mCurrentBuffer = 0;
112
113 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700114 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800115 mRegionMesh = NULL;
116
117 fboCache.clear();
118
119 programCache.clear();
120 currentProgram = NULL;
121
122 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700123}
124
125///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800126// Debug
127///////////////////////////////////////////////////////////////////////////////
128
129void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700130 String8 stringLog;
131 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000132 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700133}
134
135void Caches::dumpMemoryUsage(String8 &log) {
136 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
137 log.appendFormat(" TextureCache %8d / %8d\n",
138 textureCache.getSize(), textureCache.getMaxSize());
139 log.appendFormat(" LayerCache %8d / %8d\n",
140 layerCache.getSize(), layerCache.getMaxSize());
141 log.appendFormat(" GradientCache %8d / %8d\n",
142 gradientCache.getSize(), gradientCache.getMaxSize());
143 log.appendFormat(" PathCache %8d / %8d\n",
144 pathCache.getSize(), pathCache.getMaxSize());
145 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800146 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700147 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800148 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700149 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800150 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700151 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800152 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700153 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800154 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700155 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800156 dropShadowCache.getMaxSize());
157 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
158 const uint32_t size = fontRenderer.getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700159 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800160 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700161 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700162 log.appendFormat(" FboCache %8d / %8d\n",
163 fboCache.getSize(), fboCache.getMaxSize());
164 log.appendFormat(" PatchCache %8d / %8d\n",
165 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800166
167 uint32_t total = 0;
168 total += textureCache.getSize();
169 total += layerCache.getSize();
170 total += gradientCache.getSize();
171 total += pathCache.getSize();
172 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800173 total += roundRectShapeCache.getSize();
174 total += circleShapeCache.getSize();
175 total += ovalShapeCache.getSize();
176 total += rectShapeCache.getSize();
177 total += arcShapeCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800178 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
179 total += fontRenderer.getFontRendererSize(i);
180 }
181
Chet Haase9c1e23b2011-03-24 10:51:31 -0700182 log.appendFormat("Total memory usage:\n");
183 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800184}
185
186///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800187// Memory management
188///////////////////////////////////////////////////////////////////////////////
189
190void Caches::clearGarbage() {
191 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800192 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800193
194 Mutex::Autolock _l(mGarbageLock);
195
Romain Guyada830f2011-01-13 12:13:20 -0800196 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800197 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800198 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800199 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800200 }
Romain Guyada830f2011-01-13 12:13:20 -0800201 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800202}
203
Romain Guyada830f2011-01-13 12:13:20 -0800204void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800205 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800206 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800207}
208
Romain Guybdf76092011-07-18 15:00:43 -0700209void Caches::flush(FlushMode mode) {
210 FLUSH_LOGD("Flushing caches (mode %d)", mode);
211
212 clearGarbage();
213
214 switch (mode) {
215 case kFlushMode_Full:
216 textureCache.clear();
217 patchCache.clear();
218 dropShadowCache.clear();
219 gradientCache.clear();
Romain Guyeca0ca22011-11-04 15:12:29 -0700220 fontRenderer.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700221 // fall through
222 case kFlushMode_Moderate:
Romain Guyeca0ca22011-11-04 15:12:29 -0700223 fontRenderer.flush();
224 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700225 pathCache.clear();
226 roundRectShapeCache.clear();
227 circleShapeCache.clear();
228 ovalShapeCache.clear();
229 rectShapeCache.clear();
230 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700231 // fall through
232 case kFlushMode_Layers:
233 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700234 break;
235 }
236}
237
Romain Guyfe48f652010-11-11 15:36:56 -0800238///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700239// VBO
240///////////////////////////////////////////////////////////////////////////////
241
Romain Guyf3a910b42011-12-12 20:35:21 -0800242bool Caches::bindMeshBuffer() {
243 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700244}
245
Romain Guyf3a910b42011-12-12 20:35:21 -0800246bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700247 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700248 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700249 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800250 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700251 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800252 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700253}
254
Romain Guyf3a910b42011-12-12 20:35:21 -0800255bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700256 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700257 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700258 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800259 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700260 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800261 return false;
262}
263
Romain Guy15bc6432011-12-13 13:11:32 -0800264bool Caches::bindIndicesBuffer(const GLuint buffer) {
265 if (mCurrentIndicesBuffer != buffer) {
266 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
267 mCurrentIndicesBuffer = buffer;
268 return true;
269 }
270 return false;
271}
272
273bool Caches::unbindIndicesBuffer() {
274 if (mCurrentIndicesBuffer) {
275 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
276 mCurrentIndicesBuffer = 0;
277 return true;
278 }
279 return false;
280}
281
Romain Guyf3a910b42011-12-12 20:35:21 -0800282void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
283 if (force || vertices != mCurrentPositionPointer) {
284 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
285 mCurrentPositionPointer = vertices;
286 }
287}
288
289void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
290 if (force || vertices != mCurrentTexCoordsPointer) {
291 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
292 mCurrentTexCoordsPointer = vertices;
293 }
294}
295
296void Caches::resetVertexPointers() {
297 mCurrentPositionPointer = this;
298 mCurrentTexCoordsPointer = this;
299}
300
301void Caches::resetTexCoordsVertexPointer() {
302 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700303}
304
Romain Guy15bc6432011-12-13 13:11:32 -0800305void Caches::enableTexCoordsVertexArray() {
306 if (!mTexCoordsArrayEnabled) {
307 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800308 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800309 mTexCoordsArrayEnabled = true;
310 }
311}
312
313void Caches::disbaleTexCoordsVertexArray() {
314 if (mTexCoordsArrayEnabled) {
315 glDisableVertexAttribArray(Program::kBindingTexCoords);
316 mTexCoordsArrayEnabled = false;
317 }
318}
319
Romain Guya1d3c912011-12-13 14:55:06 -0800320void Caches::activeTexture(GLuint textureUnit) {
321 if (mTextureUnit != textureUnit) {
322 glActiveTexture(gTextureUnits[textureUnit]);
323 mTextureUnit = textureUnit;
324 }
325}
326
Romain Guy8f85e802011-12-14 19:23:32 -0800327void Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
328 if (x != mScissorX || y != mScissorY || width != mScissorWidth || height != mScissorHeight) {
329 glScissor(x, y, width, height);
330
331 mScissorX = x;
332 mScissorY = y;
333 mScissorWidth = width;
334 mScissorHeight = height;
335 }
336}
337
Romain Guy82bc7a72012-01-03 14:13:39 -0800338void Caches::resetScissor() {
339 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
340}
341
Romain Guy5b3b3522010-10-27 18:57:51 -0700342TextureVertex* Caches::getRegionMesh() {
343 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
344 if (!mRegionMesh) {
345 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
346
347 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
348 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
349 uint16_t quad = i * 4;
350 int index = i * 6;
351 regionIndices[index ] = quad; // top-left
352 regionIndices[index + 1] = quad + 1; // top-right
353 regionIndices[index + 2] = quad + 2; // bottom-left
354 regionIndices[index + 3] = quad + 2; // bottom-left
355 regionIndices[index + 4] = quad + 1; // top-right
356 regionIndices[index + 5] = quad + 3; // bottom-right
357 }
358
359 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800360 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700361 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
362 regionIndices, GL_STATIC_DRAW);
363
364 delete[] regionIndices;
365 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800366 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700367 }
368
369 return mRegionMesh;
370}
371
Chet Haasedd78cca2010-10-22 18:59:26 -0700372}; // namespace uirenderer
373}; // namespace android