blob: fa1e9b8f1f1eaaa659d015e12fae26753871206f [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) {
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();
Steve Block5baa3a62011-12-20 16:23:08 +000061 ALOGD("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 Guy8f85e802011-12-14 19:23:32 -080082 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
83
Romain Guya1d3c912011-12-13 14:55:06 -080084 glActiveTexture(gTextureUnits[0]);
85 mTextureUnit = 0;
86
Romain Guy8ff6b9e2011-11-09 20:10:18 -080087 mRegionMesh = NULL;
88
89 blend = false;
90 lastSrcMode = GL_ZERO;
91 lastDstMode = GL_ZERO;
92 currentProgram = NULL;
93
94 mInitialized = true;
95}
96
97void Caches::terminate() {
98 if (!mInitialized) return;
99
100 glDeleteBuffers(1, &meshBuffer);
101 mCurrentBuffer = 0;
102
103 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700104 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800105 mRegionMesh = NULL;
106
107 fboCache.clear();
108
109 programCache.clear();
110 currentProgram = NULL;
111
112 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700113}
114
115///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800116// Debug
117///////////////////////////////////////////////////////////////////////////////
118
119void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700120 String8 stringLog;
121 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000122 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700123}
124
125void Caches::dumpMemoryUsage(String8 &log) {
126 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
127 log.appendFormat(" TextureCache %8d / %8d\n",
128 textureCache.getSize(), textureCache.getMaxSize());
129 log.appendFormat(" LayerCache %8d / %8d\n",
130 layerCache.getSize(), layerCache.getMaxSize());
131 log.appendFormat(" GradientCache %8d / %8d\n",
132 gradientCache.getSize(), gradientCache.getMaxSize());
133 log.appendFormat(" PathCache %8d / %8d\n",
134 pathCache.getSize(), pathCache.getMaxSize());
135 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800136 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700137 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800138 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700139 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800140 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700141 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800142 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700143 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800144 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700145 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800146 dropShadowCache.getMaxSize());
147 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
148 const uint32_t size = fontRenderer.getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700149 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800150 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700151 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700152 log.appendFormat(" FboCache %8d / %8d\n",
153 fboCache.getSize(), fboCache.getMaxSize());
154 log.appendFormat(" PatchCache %8d / %8d\n",
155 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800156
157 uint32_t total = 0;
158 total += textureCache.getSize();
159 total += layerCache.getSize();
160 total += gradientCache.getSize();
161 total += pathCache.getSize();
162 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800163 total += roundRectShapeCache.getSize();
164 total += circleShapeCache.getSize();
165 total += ovalShapeCache.getSize();
166 total += rectShapeCache.getSize();
167 total += arcShapeCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800168 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
169 total += fontRenderer.getFontRendererSize(i);
170 }
171
Chet Haase9c1e23b2011-03-24 10:51:31 -0700172 log.appendFormat("Total memory usage:\n");
173 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800174}
175
176///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800177// Memory management
178///////////////////////////////////////////////////////////////////////////////
179
180void Caches::clearGarbage() {
181 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800182 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800183
184 Mutex::Autolock _l(mGarbageLock);
185
Romain Guyada830f2011-01-13 12:13:20 -0800186 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800187 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800188 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800189 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800190 }
Romain Guyada830f2011-01-13 12:13:20 -0800191 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800192}
193
Romain Guyada830f2011-01-13 12:13:20 -0800194void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800195 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800196 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800197}
198
Romain Guybdf76092011-07-18 15:00:43 -0700199void Caches::flush(FlushMode mode) {
200 FLUSH_LOGD("Flushing caches (mode %d)", mode);
201
202 clearGarbage();
203
204 switch (mode) {
205 case kFlushMode_Full:
206 textureCache.clear();
207 patchCache.clear();
208 dropShadowCache.clear();
209 gradientCache.clear();
Romain Guyeca0ca22011-11-04 15:12:29 -0700210 fontRenderer.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700211 // fall through
212 case kFlushMode_Moderate:
Romain Guyeca0ca22011-11-04 15:12:29 -0700213 fontRenderer.flush();
214 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700215 pathCache.clear();
216 roundRectShapeCache.clear();
217 circleShapeCache.clear();
218 ovalShapeCache.clear();
219 rectShapeCache.clear();
220 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700221 // fall through
222 case kFlushMode_Layers:
223 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700224 break;
225 }
226}
227
Romain Guyfe48f652010-11-11 15:36:56 -0800228///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700229// VBO
230///////////////////////////////////////////////////////////////////////////////
231
Romain Guyf3a910b42011-12-12 20:35:21 -0800232bool Caches::bindMeshBuffer() {
233 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700234}
235
Romain Guyf3a910b42011-12-12 20:35:21 -0800236bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700237 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700238 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700239 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800240 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700241 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800242 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700243}
244
Romain Guyf3a910b42011-12-12 20:35:21 -0800245bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700246 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700247 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700248 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800249 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700250 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800251 return false;
252}
253
Romain Guy15bc6432011-12-13 13:11:32 -0800254bool Caches::bindIndicesBuffer(const GLuint buffer) {
255 if (mCurrentIndicesBuffer != buffer) {
256 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
257 mCurrentIndicesBuffer = buffer;
258 return true;
259 }
260 return false;
261}
262
263bool Caches::unbindIndicesBuffer() {
264 if (mCurrentIndicesBuffer) {
265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
266 mCurrentIndicesBuffer = 0;
267 return true;
268 }
269 return false;
270}
271
Romain Guyf3a910b42011-12-12 20:35:21 -0800272void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
273 if (force || vertices != mCurrentPositionPointer) {
274 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
275 mCurrentPositionPointer = vertices;
276 }
277}
278
279void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
280 if (force || vertices != mCurrentTexCoordsPointer) {
281 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
282 mCurrentTexCoordsPointer = vertices;
283 }
284}
285
286void Caches::resetVertexPointers() {
287 mCurrentPositionPointer = this;
288 mCurrentTexCoordsPointer = this;
289}
290
291void Caches::resetTexCoordsVertexPointer() {
292 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700293}
294
Romain Guy15bc6432011-12-13 13:11:32 -0800295void Caches::enableTexCoordsVertexArray() {
296 if (!mTexCoordsArrayEnabled) {
297 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800298 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800299 mTexCoordsArrayEnabled = true;
300 }
301}
302
303void Caches::disbaleTexCoordsVertexArray() {
304 if (mTexCoordsArrayEnabled) {
305 glDisableVertexAttribArray(Program::kBindingTexCoords);
306 mTexCoordsArrayEnabled = false;
307 }
308}
309
Romain Guya1d3c912011-12-13 14:55:06 -0800310void Caches::activeTexture(GLuint textureUnit) {
311 if (mTextureUnit != textureUnit) {
312 glActiveTexture(gTextureUnits[textureUnit]);
313 mTextureUnit = textureUnit;
314 }
315}
316
Romain Guy8f85e802011-12-14 19:23:32 -0800317void Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
318 if (x != mScissorX || y != mScissorY || width != mScissorWidth || height != mScissorHeight) {
319 glScissor(x, y, width, height);
320
321 mScissorX = x;
322 mScissorY = y;
323 mScissorWidth = width;
324 mScissorHeight = height;
325 }
326}
327
Romain Guy82bc7a72012-01-03 14:13:39 -0800328void Caches::resetScissor() {
329 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
330}
331
Romain Guy5b3b3522010-10-27 18:57:51 -0700332TextureVertex* Caches::getRegionMesh() {
333 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
334 if (!mRegionMesh) {
335 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
336
337 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
338 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
339 uint16_t quad = i * 4;
340 int index = i * 6;
341 regionIndices[index ] = quad; // top-left
342 regionIndices[index + 1] = quad + 1; // top-right
343 regionIndices[index + 2] = quad + 2; // bottom-left
344 regionIndices[index + 3] = quad + 2; // bottom-left
345 regionIndices[index + 4] = quad + 1; // top-right
346 regionIndices[index + 5] = quad + 3; // bottom-right
347 }
348
349 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800350 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700351 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
352 regionIndices, GL_STATIC_DRAW);
353
354 delete[] regionIndices;
355 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800356 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700357 }
358
359 return mRegionMesh;
360}
361
Chet Haasedd78cca2010-10-22 18:59:26 -0700362}; // namespace uirenderer
363}; // namespace android