blob: 4f5edd5759264e39992911516550c3b1dd4f8060 [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>
20
Chet Haasedd78cca2010-10-22 18:59:26 -070021#include "Caches.h"
Romain Guye190aa62010-11-10 19:01:29 -080022#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080023#include "LayerRenderer.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070024
25namespace android {
26
27#ifdef USE_OPENGL_RENDERER
28using namespace uirenderer;
29ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
30#endif
31
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Constructors/destructor
36///////////////////////////////////////////////////////////////////////////////
37
38Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO),
39 lastDstMode(GL_ZERO), currentProgram(NULL) {
40 GLint maxTextureUnits;
41 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
42 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
43 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
44 }
45
46 glGenBuffers(1, &meshBuffer);
47 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
48 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
49
Romain Guy746b7402010-10-26 16:27:31 -070050 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
51
Romain Guy9bca4792010-10-25 18:42:25 -070052 mCurrentBuffer = meshBuffer;
Romain Guy5b3b3522010-10-27 18:57:51 -070053 mRegionMesh = NULL;
Romain Guye190aa62010-11-10 19:01:29 -080054
55 mDebugLevel = readDebugLevel();
56 LOGD("Enabling debug mode %d", mDebugLevel);
Romain Guy7230a742011-01-10 22:26:16 -080057
58#if RENDER_LAYERS_AS_REGIONS
59 LOGD("Layers will be composited as regions");
60#endif
Chet Haasedd78cca2010-10-22 18:59:26 -070061}
62
Romain Guy5b3b3522010-10-27 18:57:51 -070063Caches::~Caches() {
64 delete[] mRegionMesh;
65}
66
67///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -080068// Debug
69///////////////////////////////////////////////////////////////////////////////
70
71void Caches::dumpMemoryUsage() {
72 LOGD("Current memory usage / total memory usage (bytes):");
73 LOGD(" TextureCache %8d / %8d", textureCache.getSize(), textureCache.getMaxSize());
74 LOGD(" LayerCache %8d / %8d", layerCache.getSize(), layerCache.getMaxSize());
75 LOGD(" GradientCache %8d / %8d", gradientCache.getSize(), gradientCache.getMaxSize());
76 LOGD(" PathCache %8d / %8d", pathCache.getSize(), pathCache.getMaxSize());
Romain Guy01d58e42011-01-19 21:54:02 -080077 LOGD(" CircleShapeCache %8d / %8d",
78 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Romain Guy2fc941e2011-02-03 15:06:05 -080079 LOGD(" OvalShapeCache %8d / %8d",
80 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Romain Guy01d58e42011-01-19 21:54:02 -080081 LOGD(" RoundRectShapeCache %8d / %8d",
82 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Romain Guy2fc941e2011-02-03 15:06:05 -080083 LOGD(" RectShapeCache %8d / %8d",
84 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
85 LOGD(" ArcShapeCache %8d / %8d",
86 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -080087 LOGD(" TextDropShadowCache %8d / %8d", dropShadowCache.getSize(),
88 dropShadowCache.getMaxSize());
89 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
90 const uint32_t size = fontRenderer.getFontRendererSize(i);
91 LOGD(" FontRenderer %d %8d / %8d", i, size, size);
92 }
93 LOGD("Other:");
94 LOGD(" FboCache %8d / %8d", fboCache.getSize(), fboCache.getMaxSize());
95 LOGD(" PatchCache %8d / %8d", patchCache.getSize(), patchCache.getMaxSize());
96
97 uint32_t total = 0;
98 total += textureCache.getSize();
99 total += layerCache.getSize();
100 total += gradientCache.getSize();
101 total += pathCache.getSize();
102 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800103 total += roundRectShapeCache.getSize();
104 total += circleShapeCache.getSize();
105 total += ovalShapeCache.getSize();
106 total += rectShapeCache.getSize();
107 total += arcShapeCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800108 for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
109 total += fontRenderer.getFontRendererSize(i);
110 }
111
112 LOGD("Total memory usage:");
113 LOGD(" %d bytes, %.2f MB", total, total / 1024.0f / 1024.0f);
114 LOGD("\n");
115}
116
117///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800118// Memory management
119///////////////////////////////////////////////////////////////////////////////
120
121void Caches::clearGarbage() {
122 textureCache.clearGarbage();
123 gradientCache.clearGarbage();
124 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800125
126 Mutex::Autolock _l(mGarbageLock);
127
Romain Guyada830f2011-01-13 12:13:20 -0800128 size_t count = mLayerGarbage.size();
Romain Guy57066eb2011-01-12 12:53:32 -0800129 for (size_t i = 0; i < count; i++) {
Romain Guyada830f2011-01-13 12:13:20 -0800130 Layer* layer = mLayerGarbage.itemAt(i);
Romain Guy09b7c912011-02-02 20:28:09 -0800131 LayerRenderer::destroyLayer(layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800132 }
Romain Guyada830f2011-01-13 12:13:20 -0800133 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800134}
135
Romain Guyada830f2011-01-13 12:13:20 -0800136void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800137 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800138 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800139}
140
141///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700142// VBO
143///////////////////////////////////////////////////////////////////////////////
144
Chet Haasedd78cca2010-10-22 18:59:26 -0700145void Caches::bindMeshBuffer() {
146 bindMeshBuffer(meshBuffer);
147}
148
Chet Haasedd78cca2010-10-22 18:59:26 -0700149void Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700150 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700151 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700152 mCurrentBuffer = buffer;
Chet Haasedd78cca2010-10-22 18:59:26 -0700153 }
154}
155
Chet Haasedd78cca2010-10-22 18:59:26 -0700156void Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700157 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700158 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700159 mCurrentBuffer = 0;
Chet Haasedd78cca2010-10-22 18:59:26 -0700160 }
161}
162
Romain Guy5b3b3522010-10-27 18:57:51 -0700163TextureVertex* Caches::getRegionMesh() {
164 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
165 if (!mRegionMesh) {
166 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
167
168 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
169 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
170 uint16_t quad = i * 4;
171 int index = i * 6;
172 regionIndices[index ] = quad; // top-left
173 regionIndices[index + 1] = quad + 1; // top-right
174 regionIndices[index + 2] = quad + 2; // bottom-left
175 regionIndices[index + 3] = quad + 2; // bottom-left
176 regionIndices[index + 4] = quad + 1; // top-right
177 regionIndices[index + 5] = quad + 3; // bottom-right
178 }
179
180 glGenBuffers(1, &mRegionMeshIndices);
181 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
182 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
183 regionIndices, GL_STATIC_DRAW);
184
185 delete[] regionIndices;
186 } else {
187 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
188 }
189
190 return mRegionMesh;
191}
192
Chet Haasedd78cca2010-10-22 18:59:26 -0700193}; // namespace uirenderer
194}; // namespace android