blob: f2108203b9ba61a14cb9466eff9a269d6cd4c0b1 [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 Guybb0acdf2012-03-05 13:44:35 -080023#include "DisplayListRenderer.h"
Romain Guye190aa62010-11-10 19:01:29 -080024#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080025#include "LayerRenderer.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070026
27namespace android {
28
29#ifdef USE_OPENGL_RENDERER
30using namespace uirenderer;
31ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
32#endif
33
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070037// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070042#else
43 #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070047// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy8ff6b9e2011-11-09 20:10:18 -080050Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080051 init();
Romain Guydfa10462012-05-12 16:18:58 -070052 initExtensions();
53 initConstraints();
Romain Guye190aa62010-11-10 19:01:29 -080054
55 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000056 ALOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080057
58#if RENDER_LAYERS_AS_REGIONS
Romain Guy02ccac62011-06-24 13:20:23 -070059 INIT_LOGD("Layers will be composited as regions");
Romain Guy7230a742011-01-10 22:26:16 -080060#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070061}
62
Romain Guy8ff6b9e2011-11-09 20:10:18 -080063void Caches::init() {
64 if (mInitialized) return;
65
66 glGenBuffers(1, &meshBuffer);
67 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
68 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
69
70 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080071 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080072 mCurrentPositionPointer = this;
73 mCurrentTexCoordsPointer = this;
74
Romain Guy15bc6432011-12-13 13:11:32 -080075 mTexCoordsArrayEnabled = false;
76
Romain Guy8f85e802011-12-14 19:23:32 -080077 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
78
Romain Guya1d3c912011-12-13 14:55:06 -080079 glActiveTexture(gTextureUnits[0]);
80 mTextureUnit = 0;
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
Romain Guydfa10462012-05-12 16:18:58 -070092void Caches::initExtensions() {
93 if (extensions.hasDebugMarker()) {
94 eventMark = glInsertEventMarkerEXT;
95 startMark = glPushGroupMarkerEXT;
96 endMark = glPopGroupMarkerEXT;
97 } else {
98 eventMark = eventMarkNull;
99 startMark = startMarkNull;
100 endMark = endMarkNull;
101 }
102
103 if (extensions.hasDebugLabel()) {
104 setLabel = glLabelObjectEXT;
105 getLabel = glGetObjectLabelEXT;
106 } else {
107 setLabel = setLabelNull;
108 getLabel = getLabelNull;
109 }
110}
111
112void Caches::initConstraints() {
113 GLint maxTextureUnits;
114 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
115 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
116 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
117 }
118
119 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
120}
121
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800122void Caches::terminate() {
123 if (!mInitialized) return;
124
125 glDeleteBuffers(1, &meshBuffer);
126 mCurrentBuffer = 0;
127
128 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700129 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800130 mRegionMesh = NULL;
131
132 fboCache.clear();
133
134 programCache.clear();
135 currentProgram = NULL;
136
137 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700138}
139
140///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800141// Debug
142///////////////////////////////////////////////////////////////////////////////
143
144void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700145 String8 stringLog;
146 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000147 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700148}
149
150void Caches::dumpMemoryUsage(String8 &log) {
151 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
152 log.appendFormat(" TextureCache %8d / %8d\n",
153 textureCache.getSize(), textureCache.getMaxSize());
154 log.appendFormat(" LayerCache %8d / %8d\n",
155 layerCache.getSize(), layerCache.getMaxSize());
156 log.appendFormat(" GradientCache %8d / %8d\n",
157 gradientCache.getSize(), gradientCache.getMaxSize());
158 log.appendFormat(" PathCache %8d / %8d\n",
159 pathCache.getSize(), pathCache.getMaxSize());
160 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800161 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700162 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800163 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700164 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800165 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700166 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800167 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700168 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800169 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700170 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800171 dropShadowCache.getMaxSize());
172 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
173 const uint32_t size = fontRenderer.getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700174 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800175 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700176 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700177 log.appendFormat(" FboCache %8d / %8d\n",
178 fboCache.getSize(), fboCache.getMaxSize());
179 log.appendFormat(" PatchCache %8d / %8d\n",
180 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800181
182 uint32_t total = 0;
183 total += textureCache.getSize();
184 total += layerCache.getSize();
185 total += gradientCache.getSize();
186 total += pathCache.getSize();
187 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800188 total += roundRectShapeCache.getSize();
189 total += circleShapeCache.getSize();
190 total += ovalShapeCache.getSize();
191 total += rectShapeCache.getSize();
192 total += arcShapeCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800193 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
194 total += fontRenderer.getFontRendererSize(i);
195 }
196
Chet Haase9c1e23b2011-03-24 10:51:31 -0700197 log.appendFormat("Total memory usage:\n");
198 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800199}
200
201///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800202// Memory management
203///////////////////////////////////////////////////////////////////////////////
204
205void Caches::clearGarbage() {
206 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800207 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800208
209 Mutex::Autolock _l(mGarbageLock);
210
Romain Guyada830f2011-01-13 12:13:20 -0800211 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800212 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800213 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800214 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800215 }
Romain Guyada830f2011-01-13 12:13:20 -0800216 mLayerGarbage.clear();
Romain Guybb0acdf2012-03-05 13:44:35 -0800217
218 count = mDisplayListGarbage.size();
219 for (size_t i = 0; i < count; i++) {
220 DisplayList* displayList = mDisplayListGarbage.itemAt(i);
221 delete displayList;
222 }
223 mDisplayListGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800224}
225
Romain Guyada830f2011-01-13 12:13:20 -0800226void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800227 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800228 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800229}
230
Romain Guybb0acdf2012-03-05 13:44:35 -0800231void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
232 Mutex::Autolock _l(mGarbageLock);
233 mDisplayListGarbage.push(displayList);
234}
235
Romain Guybdf76092011-07-18 15:00:43 -0700236void Caches::flush(FlushMode mode) {
237 FLUSH_LOGD("Flushing caches (mode %d)", mode);
238
239 clearGarbage();
240
241 switch (mode) {
242 case kFlushMode_Full:
243 textureCache.clear();
244 patchCache.clear();
245 dropShadowCache.clear();
246 gradientCache.clear();
Romain Guyeca0ca22011-11-04 15:12:29 -0700247 fontRenderer.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700248 // fall through
249 case kFlushMode_Moderate:
Romain Guyeca0ca22011-11-04 15:12:29 -0700250 fontRenderer.flush();
251 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700252 pathCache.clear();
253 roundRectShapeCache.clear();
254 circleShapeCache.clear();
255 ovalShapeCache.clear();
256 rectShapeCache.clear();
257 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700258 // fall through
259 case kFlushMode_Layers:
260 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700261 break;
262 }
263}
264
Romain Guyfe48f652010-11-11 15:36:56 -0800265///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700266// VBO
267///////////////////////////////////////////////////////////////////////////////
268
Romain Guyf3a910b42011-12-12 20:35:21 -0800269bool Caches::bindMeshBuffer() {
270 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700271}
272
Romain Guyf3a910b42011-12-12 20:35:21 -0800273bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700274 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700275 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700276 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800277 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700278 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800279 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700280}
281
Romain Guyf3a910b42011-12-12 20:35:21 -0800282bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700283 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700284 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700285 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800286 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700287 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800288 return false;
289}
290
Romain Guy15bc6432011-12-13 13:11:32 -0800291bool Caches::bindIndicesBuffer(const GLuint buffer) {
292 if (mCurrentIndicesBuffer != buffer) {
293 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
294 mCurrentIndicesBuffer = buffer;
295 return true;
296 }
297 return false;
298}
299
300bool Caches::unbindIndicesBuffer() {
301 if (mCurrentIndicesBuffer) {
302 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
303 mCurrentIndicesBuffer = 0;
304 return true;
305 }
306 return false;
307}
308
Romain Guyf3a910b42011-12-12 20:35:21 -0800309void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
310 if (force || vertices != mCurrentPositionPointer) {
311 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
312 mCurrentPositionPointer = vertices;
313 }
314}
315
316void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
317 if (force || vertices != mCurrentTexCoordsPointer) {
318 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
319 mCurrentTexCoordsPointer = vertices;
320 }
321}
322
323void Caches::resetVertexPointers() {
324 mCurrentPositionPointer = this;
325 mCurrentTexCoordsPointer = this;
326}
327
328void Caches::resetTexCoordsVertexPointer() {
329 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700330}
331
Romain Guy15bc6432011-12-13 13:11:32 -0800332void Caches::enableTexCoordsVertexArray() {
333 if (!mTexCoordsArrayEnabled) {
334 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800335 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800336 mTexCoordsArrayEnabled = true;
337 }
338}
339
340void Caches::disbaleTexCoordsVertexArray() {
341 if (mTexCoordsArrayEnabled) {
342 glDisableVertexAttribArray(Program::kBindingTexCoords);
343 mTexCoordsArrayEnabled = false;
344 }
345}
346
Romain Guya1d3c912011-12-13 14:55:06 -0800347void Caches::activeTexture(GLuint textureUnit) {
348 if (mTextureUnit != textureUnit) {
349 glActiveTexture(gTextureUnits[textureUnit]);
350 mTextureUnit = textureUnit;
351 }
352}
353
Romain Guy8f85e802011-12-14 19:23:32 -0800354void Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
355 if (x != mScissorX || y != mScissorY || width != mScissorWidth || height != mScissorHeight) {
356 glScissor(x, y, width, height);
357
358 mScissorX = x;
359 mScissorY = y;
360 mScissorWidth = width;
361 mScissorHeight = height;
362 }
363}
364
Romain Guy82bc7a72012-01-03 14:13:39 -0800365void Caches::resetScissor() {
366 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
367}
368
Romain Guy5b3b3522010-10-27 18:57:51 -0700369TextureVertex* Caches::getRegionMesh() {
370 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
371 if (!mRegionMesh) {
372 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
373
374 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
375 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
376 uint16_t quad = i * 4;
377 int index = i * 6;
378 regionIndices[index ] = quad; // top-left
379 regionIndices[index + 1] = quad + 1; // top-right
380 regionIndices[index + 2] = quad + 2; // bottom-left
381 regionIndices[index + 3] = quad + 2; // bottom-left
382 regionIndices[index + 4] = quad + 1; // top-right
383 regionIndices[index + 5] = quad + 3; // bottom-right
384 }
385
386 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800387 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700388 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
389 regionIndices, GL_STATIC_DRAW);
390
391 delete[] regionIndices;
392 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800393 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700394 }
395
396 return mRegionMesh;
397}
398
Chet Haasedd78cca2010-10-22 18:59:26 -0700399}; // namespace uirenderer
400}; // namespace android