blob: 258ced03899dfe18744c0c4c34ab34c79e4aa83b [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 Guyb1d0a4e2012-07-13 18:25:35 -070052 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070053 initExtensions();
54 initConstraints();
Romain Guye190aa62010-11-10 19:01:29 -080055
56 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000057 ALOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080058
59#if RENDER_LAYERS_AS_REGIONS
Romain Guy02ccac62011-06-24 13:20:23 -070060 INIT_LOGD("Layers will be composited as regions");
Romain Guy7230a742011-01-10 22:26:16 -080061#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070062}
63
Romain Guy8ff6b9e2011-11-09 20:10:18 -080064void Caches::init() {
65 if (mInitialized) return;
66
67 glGenBuffers(1, &meshBuffer);
68 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
69 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
70
71 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080072 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080073 mCurrentPositionPointer = this;
74 mCurrentTexCoordsPointer = this;
75
Romain Guy15bc6432011-12-13 13:11:32 -080076 mTexCoordsArrayEnabled = false;
77
Romain Guyb1d0a4e2012-07-13 18:25:35 -070078 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070079 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080080 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
81
Romain Guya1d3c912011-12-13 14:55:06 -080082 glActiveTexture(gTextureUnits[0]);
83 mTextureUnit = 0;
84
Romain Guy8ff6b9e2011-11-09 20:10:18 -080085 mRegionMesh = NULL;
86
87 blend = false;
88 lastSrcMode = GL_ZERO;
89 lastDstMode = GL_ZERO;
90 currentProgram = NULL;
91
92 mInitialized = true;
93}
94
Romain Guyb1d0a4e2012-07-13 18:25:35 -070095void Caches::initFont() {
96 fontRenderer = GammaFontRenderer::createRenderer();
97}
98
Romain Guydfa10462012-05-12 16:18:58 -070099void Caches::initExtensions() {
100 if (extensions.hasDebugMarker()) {
101 eventMark = glInsertEventMarkerEXT;
102 startMark = glPushGroupMarkerEXT;
103 endMark = glPopGroupMarkerEXT;
104 } else {
105 eventMark = eventMarkNull;
106 startMark = startMarkNull;
107 endMark = endMarkNull;
108 }
109
110 if (extensions.hasDebugLabel()) {
111 setLabel = glLabelObjectEXT;
112 getLabel = glGetObjectLabelEXT;
113 } else {
114 setLabel = setLabelNull;
115 getLabel = getLabelNull;
116 }
117}
118
119void Caches::initConstraints() {
120 GLint maxTextureUnits;
121 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
122 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
123 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
124 }
125
126 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
127}
128
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800129void Caches::terminate() {
130 if (!mInitialized) return;
131
132 glDeleteBuffers(1, &meshBuffer);
133 mCurrentBuffer = 0;
134
135 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700136 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800137 mRegionMesh = NULL;
138
139 fboCache.clear();
140
141 programCache.clear();
142 currentProgram = NULL;
143
144 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700145}
146
147///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800148// Debug
149///////////////////////////////////////////////////////////////////////////////
150
151void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700152 String8 stringLog;
153 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000154 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700155}
156
157void Caches::dumpMemoryUsage(String8 &log) {
158 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
159 log.appendFormat(" TextureCache %8d / %8d\n",
160 textureCache.getSize(), textureCache.getMaxSize());
161 log.appendFormat(" LayerCache %8d / %8d\n",
162 layerCache.getSize(), layerCache.getMaxSize());
163 log.appendFormat(" GradientCache %8d / %8d\n",
164 gradientCache.getSize(), gradientCache.getMaxSize());
165 log.appendFormat(" PathCache %8d / %8d\n",
166 pathCache.getSize(), pathCache.getMaxSize());
167 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800168 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700169 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800170 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700171 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800172 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700173 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800174 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700175 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800176 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700177 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800178 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700179 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
180 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700181 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800182 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700183 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700184 log.appendFormat(" FboCache %8d / %8d\n",
185 fboCache.getSize(), fboCache.getMaxSize());
186 log.appendFormat(" PatchCache %8d / %8d\n",
187 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800188
189 uint32_t total = 0;
190 total += textureCache.getSize();
191 total += layerCache.getSize();
192 total += gradientCache.getSize();
193 total += pathCache.getSize();
194 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800195 total += roundRectShapeCache.getSize();
196 total += circleShapeCache.getSize();
197 total += ovalShapeCache.getSize();
198 total += rectShapeCache.getSize();
199 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700200 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
201 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800202 }
203
Chet Haase9c1e23b2011-03-24 10:51:31 -0700204 log.appendFormat("Total memory usage:\n");
205 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800206}
207
208///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800209// Memory management
210///////////////////////////////////////////////////////////////////////////////
211
212void Caches::clearGarbage() {
213 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800214 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800215
216 Mutex::Autolock _l(mGarbageLock);
217
Romain Guyada830f2011-01-13 12:13:20 -0800218 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800219 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800220 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800221 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800222 }
Romain Guyada830f2011-01-13 12:13:20 -0800223 mLayerGarbage.clear();
Romain Guybb0acdf2012-03-05 13:44:35 -0800224
225 count = mDisplayListGarbage.size();
226 for (size_t i = 0; i < count; i++) {
227 DisplayList* displayList = mDisplayListGarbage.itemAt(i);
228 delete displayList;
229 }
230 mDisplayListGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800231}
232
Romain Guyada830f2011-01-13 12:13:20 -0800233void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800234 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800235 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800236}
237
Romain Guybb0acdf2012-03-05 13:44:35 -0800238void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
239 Mutex::Autolock _l(mGarbageLock);
240 mDisplayListGarbage.push(displayList);
241}
242
Romain Guybdf76092011-07-18 15:00:43 -0700243void Caches::flush(FlushMode mode) {
244 FLUSH_LOGD("Flushing caches (mode %d)", mode);
245
246 clearGarbage();
247
248 switch (mode) {
249 case kFlushMode_Full:
250 textureCache.clear();
251 patchCache.clear();
252 dropShadowCache.clear();
253 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700254 fontRenderer->clear();
Romain Guybdf76092011-07-18 15:00:43 -0700255 // fall through
256 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700257 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700258 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700259 pathCache.clear();
260 roundRectShapeCache.clear();
261 circleShapeCache.clear();
262 ovalShapeCache.clear();
263 rectShapeCache.clear();
264 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700265 // fall through
266 case kFlushMode_Layers:
267 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700268 break;
269 }
270}
271
Romain Guyfe48f652010-11-11 15:36:56 -0800272///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700273// VBO
274///////////////////////////////////////////////////////////////////////////////
275
Romain Guyf3a910b42011-12-12 20:35:21 -0800276bool Caches::bindMeshBuffer() {
277 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700278}
279
Romain Guyf3a910b42011-12-12 20:35:21 -0800280bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700281 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700282 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700283 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800284 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700285 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800286 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700287}
288
Romain Guyf3a910b42011-12-12 20:35:21 -0800289bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700290 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700291 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700292 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800293 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700294 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800295 return false;
296}
297
Romain Guy15bc6432011-12-13 13:11:32 -0800298bool Caches::bindIndicesBuffer(const GLuint buffer) {
299 if (mCurrentIndicesBuffer != buffer) {
300 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
301 mCurrentIndicesBuffer = buffer;
302 return true;
303 }
304 return false;
305}
306
307bool Caches::unbindIndicesBuffer() {
308 if (mCurrentIndicesBuffer) {
309 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
310 mCurrentIndicesBuffer = 0;
311 return true;
312 }
313 return false;
314}
315
Romain Guyf3a910b42011-12-12 20:35:21 -0800316void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
317 if (force || vertices != mCurrentPositionPointer) {
318 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
319 mCurrentPositionPointer = vertices;
320 }
321}
322
323void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
324 if (force || vertices != mCurrentTexCoordsPointer) {
325 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
326 mCurrentTexCoordsPointer = vertices;
327 }
328}
329
330void Caches::resetVertexPointers() {
331 mCurrentPositionPointer = this;
332 mCurrentTexCoordsPointer = this;
333}
334
335void Caches::resetTexCoordsVertexPointer() {
336 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700337}
338
Romain Guy15bc6432011-12-13 13:11:32 -0800339void Caches::enableTexCoordsVertexArray() {
340 if (!mTexCoordsArrayEnabled) {
341 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800342 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800343 mTexCoordsArrayEnabled = true;
344 }
345}
346
347void Caches::disbaleTexCoordsVertexArray() {
348 if (mTexCoordsArrayEnabled) {
349 glDisableVertexAttribArray(Program::kBindingTexCoords);
350 mTexCoordsArrayEnabled = false;
351 }
352}
353
Romain Guya1d3c912011-12-13 14:55:06 -0800354void Caches::activeTexture(GLuint textureUnit) {
355 if (mTextureUnit != textureUnit) {
356 glActiveTexture(gTextureUnits[textureUnit]);
357 mTextureUnit = textureUnit;
358 }
359}
360
Romain Guy8a4ac612012-07-17 17:32:48 -0700361bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700362 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
363 width != mScissorWidth || height != mScissorHeight)) {
364
Romain Guy8f85e802011-12-14 19:23:32 -0800365 glScissor(x, y, width, height);
366
367 mScissorX = x;
368 mScissorY = y;
369 mScissorWidth = width;
370 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700371
372 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800373 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700374 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800375}
376
Romain Guy8a4ac612012-07-17 17:32:48 -0700377bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700378 if (!scissorEnabled) {
379 glEnable(GL_SCISSOR_TEST);
380 scissorEnabled = true;
Romain Guy8a4ac612012-07-17 17:32:48 -0700381 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700382 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700383 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700384}
385
Romain Guy8a4ac612012-07-17 17:32:48 -0700386bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700387 if (scissorEnabled) {
388 glDisable(GL_SCISSOR_TEST);
389 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700390 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700391 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700392 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700393}
394
395void Caches::setScissorEnabled(bool enabled) {
396 if (scissorEnabled != enabled) {
397 if (enabled) glEnable(GL_SCISSOR_TEST);
398 else glDisable(GL_SCISSOR_TEST);
399 scissorEnabled = enabled;
400 }
401}
402
Romain Guy82bc7a72012-01-03 14:13:39 -0800403void Caches::resetScissor() {
404 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
405}
406
Romain Guy5b3b3522010-10-27 18:57:51 -0700407TextureVertex* Caches::getRegionMesh() {
408 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
409 if (!mRegionMesh) {
410 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
411
412 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
413 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
414 uint16_t quad = i * 4;
415 int index = i * 6;
416 regionIndices[index ] = quad; // top-left
417 regionIndices[index + 1] = quad + 1; // top-right
418 regionIndices[index + 2] = quad + 2; // bottom-left
419 regionIndices[index + 3] = quad + 2; // bottom-left
420 regionIndices[index + 4] = quad + 1; // top-right
421 regionIndices[index + 5] = quad + 3; // bottom-right
422 }
423
424 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800425 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700426 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
427 regionIndices, GL_STATIC_DRAW);
428
429 delete[] regionIndices;
430 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800431 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700432 }
433
434 return mRegionMesh;
435}
436
Chet Haasedd78cca2010-10-22 18:59:26 -0700437}; // namespace uirenderer
438}; // namespace android