blob: 123695ab4a8584aaf042376345a5922b80a73166 [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) {
Chet Haasedd78cca2010-10-22 18:59:26 -070051 GLint maxTextureUnits;
52 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
53 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Steve Block8564c8d2012-01-05 23:22:43 +000054 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
Chet Haasedd78cca2010-10-22 18:59:26 -070055 }
56
Romain Guy746b7402010-10-26 16:27:31 -070057 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
58
Romain Guy13631f32012-01-30 17:41:55 -080059 if (extensions.hasDebugMarker()) {
60 eventMark = glInsertEventMarkerEXT;
61 startMark = glPushGroupMarkerEXT;
62 endMark = glPopGroupMarkerEXT;
63 } else {
64 eventMark = eventMarkNull;
65 startMark = startMarkNull;
66 endMark = endMarkNull;
67 }
68
Romain Guy8ff6b9e2011-11-09 20:10:18 -080069 init();
Romain Guye190aa62010-11-10 19:01:29 -080070
71 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000072 ALOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080073
74#if RENDER_LAYERS_AS_REGIONS
Romain Guy02ccac62011-06-24 13:20:23 -070075 INIT_LOGD("Layers will be composited as regions");
Romain Guy7230a742011-01-10 22:26:16 -080076#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070077}
78
Romain Guy8ff6b9e2011-11-09 20:10:18 -080079void Caches::init() {
80 if (mInitialized) return;
81
82 glGenBuffers(1, &meshBuffer);
83 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
84 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
85
86 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080087 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080088 mCurrentPositionPointer = this;
89 mCurrentTexCoordsPointer = this;
90
Romain Guy15bc6432011-12-13 13:11:32 -080091 mTexCoordsArrayEnabled = false;
92
Romain Guy8f85e802011-12-14 19:23:32 -080093 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
94
Romain Guya1d3c912011-12-13 14:55:06 -080095 glActiveTexture(gTextureUnits[0]);
96 mTextureUnit = 0;
97
Romain Guy8ff6b9e2011-11-09 20:10:18 -080098 mRegionMesh = NULL;
99
100 blend = false;
101 lastSrcMode = GL_ZERO;
102 lastDstMode = GL_ZERO;
103 currentProgram = NULL;
104
105 mInitialized = true;
106}
107
108void Caches::terminate() {
109 if (!mInitialized) return;
110
111 glDeleteBuffers(1, &meshBuffer);
112 mCurrentBuffer = 0;
113
114 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700115 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800116 mRegionMesh = NULL;
117
118 fboCache.clear();
119
120 programCache.clear();
121 currentProgram = NULL;
122
123 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700124}
125
126///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800127// Debug
128///////////////////////////////////////////////////////////////////////////////
129
130void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700131 String8 stringLog;
132 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000133 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700134}
135
136void Caches::dumpMemoryUsage(String8 &log) {
137 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
138 log.appendFormat(" TextureCache %8d / %8d\n",
139 textureCache.getSize(), textureCache.getMaxSize());
140 log.appendFormat(" LayerCache %8d / %8d\n",
141 layerCache.getSize(), layerCache.getMaxSize());
142 log.appendFormat(" GradientCache %8d / %8d\n",
143 gradientCache.getSize(), gradientCache.getMaxSize());
144 log.appendFormat(" PathCache %8d / %8d\n",
145 pathCache.getSize(), pathCache.getMaxSize());
146 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800147 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700148 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800149 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700150 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800151 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700152 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800153 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700154 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800155 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700156 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800157 dropShadowCache.getMaxSize());
158 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
159 const uint32_t size = fontRenderer.getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700160 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800161 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700162 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700163 log.appendFormat(" FboCache %8d / %8d\n",
164 fboCache.getSize(), fboCache.getMaxSize());
165 log.appendFormat(" PatchCache %8d / %8d\n",
166 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800167
168 uint32_t total = 0;
169 total += textureCache.getSize();
170 total += layerCache.getSize();
171 total += gradientCache.getSize();
172 total += pathCache.getSize();
173 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800174 total += roundRectShapeCache.getSize();
175 total += circleShapeCache.getSize();
176 total += ovalShapeCache.getSize();
177 total += rectShapeCache.getSize();
178 total += arcShapeCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800179 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
180 total += fontRenderer.getFontRendererSize(i);
181 }
182
Chet Haase9c1e23b2011-03-24 10:51:31 -0700183 log.appendFormat("Total memory usage:\n");
184 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800185}
186
187///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800188// Memory management
189///////////////////////////////////////////////////////////////////////////////
190
191void Caches::clearGarbage() {
192 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800193 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800194
195 Mutex::Autolock _l(mGarbageLock);
196
Romain Guyada830f2011-01-13 12:13:20 -0800197 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800198 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800199 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800200 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800201 }
Romain Guyada830f2011-01-13 12:13:20 -0800202 mLayerGarbage.clear();
Romain Guybb0acdf2012-03-05 13:44:35 -0800203
204 count = mDisplayListGarbage.size();
205 for (size_t i = 0; i < count; i++) {
206 DisplayList* displayList = mDisplayListGarbage.itemAt(i);
207 delete displayList;
208 }
209 mDisplayListGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800210}
211
Romain Guyada830f2011-01-13 12:13:20 -0800212void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800213 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800214 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800215}
216
Romain Guybb0acdf2012-03-05 13:44:35 -0800217void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
218 Mutex::Autolock _l(mGarbageLock);
219 mDisplayListGarbage.push(displayList);
220}
221
Romain Guybdf76092011-07-18 15:00:43 -0700222void Caches::flush(FlushMode mode) {
223 FLUSH_LOGD("Flushing caches (mode %d)", mode);
224
225 clearGarbage();
226
227 switch (mode) {
228 case kFlushMode_Full:
229 textureCache.clear();
230 patchCache.clear();
231 dropShadowCache.clear();
232 gradientCache.clear();
Romain Guyeca0ca22011-11-04 15:12:29 -0700233 fontRenderer.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700234 // fall through
235 case kFlushMode_Moderate:
Romain Guyeca0ca22011-11-04 15:12:29 -0700236 fontRenderer.flush();
237 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700238 pathCache.clear();
239 roundRectShapeCache.clear();
240 circleShapeCache.clear();
241 ovalShapeCache.clear();
242 rectShapeCache.clear();
243 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700244 // fall through
245 case kFlushMode_Layers:
246 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700247 break;
248 }
249}
250
Romain Guyfe48f652010-11-11 15:36:56 -0800251///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700252// VBO
253///////////////////////////////////////////////////////////////////////////////
254
Romain Guyf3a910b42011-12-12 20:35:21 -0800255bool Caches::bindMeshBuffer() {
256 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700257}
258
Romain Guyf3a910b42011-12-12 20:35:21 -0800259bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700260 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700261 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700262 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800263 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700264 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800265 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700266}
267
Romain Guyf3a910b42011-12-12 20:35:21 -0800268bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700269 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700270 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700271 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800272 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700273 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800274 return false;
275}
276
Romain Guy15bc6432011-12-13 13:11:32 -0800277bool Caches::bindIndicesBuffer(const GLuint buffer) {
278 if (mCurrentIndicesBuffer != buffer) {
279 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
280 mCurrentIndicesBuffer = buffer;
281 return true;
282 }
283 return false;
284}
285
286bool Caches::unbindIndicesBuffer() {
287 if (mCurrentIndicesBuffer) {
288 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
289 mCurrentIndicesBuffer = 0;
290 return true;
291 }
292 return false;
293}
294
Romain Guyf3a910b42011-12-12 20:35:21 -0800295void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
296 if (force || vertices != mCurrentPositionPointer) {
297 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
298 mCurrentPositionPointer = vertices;
299 }
300}
301
302void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
303 if (force || vertices != mCurrentTexCoordsPointer) {
304 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
305 mCurrentTexCoordsPointer = vertices;
306 }
307}
308
309void Caches::resetVertexPointers() {
310 mCurrentPositionPointer = this;
311 mCurrentTexCoordsPointer = this;
312}
313
314void Caches::resetTexCoordsVertexPointer() {
315 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700316}
317
Romain Guy15bc6432011-12-13 13:11:32 -0800318void Caches::enableTexCoordsVertexArray() {
319 if (!mTexCoordsArrayEnabled) {
320 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800321 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800322 mTexCoordsArrayEnabled = true;
323 }
324}
325
326void Caches::disbaleTexCoordsVertexArray() {
327 if (mTexCoordsArrayEnabled) {
328 glDisableVertexAttribArray(Program::kBindingTexCoords);
329 mTexCoordsArrayEnabled = false;
330 }
331}
332
Romain Guya1d3c912011-12-13 14:55:06 -0800333void Caches::activeTexture(GLuint textureUnit) {
334 if (mTextureUnit != textureUnit) {
335 glActiveTexture(gTextureUnits[textureUnit]);
336 mTextureUnit = textureUnit;
337 }
338}
339
Romain Guy8f85e802011-12-14 19:23:32 -0800340void Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
341 if (x != mScissorX || y != mScissorY || width != mScissorWidth || height != mScissorHeight) {
342 glScissor(x, y, width, height);
343
344 mScissorX = x;
345 mScissorY = y;
346 mScissorWidth = width;
347 mScissorHeight = height;
348 }
349}
350
Romain Guy82bc7a72012-01-03 14:13:39 -0800351void Caches::resetScissor() {
352 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
353}
354
Romain Guy5b3b3522010-10-27 18:57:51 -0700355TextureVertex* Caches::getRegionMesh() {
356 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
357 if (!mRegionMesh) {
358 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
359
360 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
361 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
362 uint16_t quad = i * 4;
363 int index = i * 6;
364 regionIndices[index ] = quad; // top-left
365 regionIndices[index + 1] = quad + 1; // top-right
366 regionIndices[index + 2] = quad + 2; // bottom-left
367 regionIndices[index + 3] = quad + 2; // bottom-left
368 regionIndices[index + 4] = quad + 1; // top-right
369 regionIndices[index + 5] = quad + 3; // bottom-right
370 }
371
372 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800373 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700374 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
375 regionIndices, GL_STATIC_DRAW);
376
377 delete[] regionIndices;
378 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800379 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700380 }
381
382 return mRegionMesh;
383}
384
Chet Haasedd78cca2010-10-22 18:59:26 -0700385}; // namespace uirenderer
386}; // namespace android