Caches.cpp revision 15bc6437f8b4cf10dba55c7638d349e7b9563f4f
1/*
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
19#include <utils/Log.h>
20#include <utils/String8.h>
21
22#include "Caches.h"
23#include "Properties.h"
24#include "LayerRenderer.h"
25
26namespace android {
27
28#ifdef USE_OPENGL_RENDERER
29using namespace uirenderer;
30ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
31#endif
32
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Macros
37///////////////////////////////////////////////////////////////////////////////
38
39#if DEBUG_CACHE_FLUSH
40    #define FLUSH_LOGD(...) LOGD(__VA_ARGS__)
41#else
42    #define FLUSH_LOGD(...)
43#endif
44
45///////////////////////////////////////////////////////////////////////////////
46// Constructors/destructor
47///////////////////////////////////////////////////////////////////////////////
48
49Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
50    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
56    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
57
58    init();
59
60    mDebugLevel = readDebugLevel();
61    LOGD("Enabling debug mode %d", mDebugLevel);
62
63#if RENDER_LAYERS_AS_REGIONS
64    INIT_LOGD("Layers will be composited as regions");
65#endif
66}
67
68void 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;
76    mCurrentIndicesBuffer = 0;
77    mCurrentPositionPointer = this;
78    mCurrentTexCoordsPointer = this;
79
80    mTexCoordsArrayEnabled = false;
81
82    mRegionMesh = NULL;
83
84    blend = false;
85    lastSrcMode = GL_ZERO;
86    lastDstMode = GL_ZERO;
87    currentProgram = NULL;
88
89    mInitialized = true;
90}
91
92void Caches::terminate() {
93    if (!mInitialized) return;
94
95    glDeleteBuffers(1, &meshBuffer);
96    mCurrentBuffer = 0;
97
98    glDeleteBuffers(1, &mRegionMeshIndices);
99    delete[] mRegionMesh;
100    mRegionMesh = NULL;
101
102    fboCache.clear();
103
104    programCache.clear();
105    currentProgram = NULL;
106
107    mInitialized = false;
108}
109
110///////////////////////////////////////////////////////////////////////////////
111// Debug
112///////////////////////////////////////////////////////////////////////////////
113
114void Caches::dumpMemoryUsage() {
115    String8 stringLog;
116    dumpMemoryUsage(stringLog);
117    LOGD("%s", stringLog.string());
118}
119
120void Caches::dumpMemoryUsage(String8 &log) {
121    log.appendFormat("Current memory usage / total memory usage (bytes):\n");
122    log.appendFormat("  TextureCache         %8d / %8d\n",
123            textureCache.getSize(), textureCache.getMaxSize());
124    log.appendFormat("  LayerCache           %8d / %8d\n",
125            layerCache.getSize(), layerCache.getMaxSize());
126    log.appendFormat("  GradientCache        %8d / %8d\n",
127            gradientCache.getSize(), gradientCache.getMaxSize());
128    log.appendFormat("  PathCache            %8d / %8d\n",
129            pathCache.getSize(), pathCache.getMaxSize());
130    log.appendFormat("  CircleShapeCache     %8d / %8d\n",
131            circleShapeCache.getSize(), circleShapeCache.getMaxSize());
132    log.appendFormat("  OvalShapeCache       %8d / %8d\n",
133            ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
134    log.appendFormat("  RoundRectShapeCache  %8d / %8d\n",
135            roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
136    log.appendFormat("  RectShapeCache       %8d / %8d\n",
137            rectShapeCache.getSize(), rectShapeCache.getMaxSize());
138    log.appendFormat("  ArcShapeCache        %8d / %8d\n",
139            arcShapeCache.getSize(), arcShapeCache.getMaxSize());
140    log.appendFormat("  TextDropShadowCache  %8d / %8d\n", dropShadowCache.getSize(),
141            dropShadowCache.getMaxSize());
142    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
143        const uint32_t size = fontRenderer.getFontRendererSize(i);
144        log.appendFormat("  FontRenderer %d       %8d / %8d\n", i, size, size);
145    }
146    log.appendFormat("Other:\n");
147    log.appendFormat("  FboCache             %8d / %8d\n",
148            fboCache.getSize(), fboCache.getMaxSize());
149    log.appendFormat("  PatchCache           %8d / %8d\n",
150            patchCache.getSize(), patchCache.getMaxSize());
151
152    uint32_t total = 0;
153    total += textureCache.getSize();
154    total += layerCache.getSize();
155    total += gradientCache.getSize();
156    total += pathCache.getSize();
157    total += dropShadowCache.getSize();
158    total += roundRectShapeCache.getSize();
159    total += circleShapeCache.getSize();
160    total += ovalShapeCache.getSize();
161    total += rectShapeCache.getSize();
162    total += arcShapeCache.getSize();
163    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
164        total += fontRenderer.getFontRendererSize(i);
165    }
166
167    log.appendFormat("Total memory usage:\n");
168    log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
169}
170
171///////////////////////////////////////////////////////////////////////////////
172// Memory management
173///////////////////////////////////////////////////////////////////////////////
174
175void Caches::clearGarbage() {
176    textureCache.clearGarbage();
177    pathCache.clearGarbage();
178
179    Mutex::Autolock _l(mGarbageLock);
180
181    size_t count = mLayerGarbage.size();
182    for (size_t i = 0; i < count; i++) {
183        Layer* layer = mLayerGarbage.itemAt(i);
184        LayerRenderer::destroyLayer(layer);
185    }
186    mLayerGarbage.clear();
187}
188
189void Caches::deleteLayerDeferred(Layer* layer) {
190    Mutex::Autolock _l(mGarbageLock);
191    mLayerGarbage.push(layer);
192}
193
194void Caches::flush(FlushMode mode) {
195    FLUSH_LOGD("Flushing caches (mode %d)", mode);
196
197    clearGarbage();
198
199    switch (mode) {
200        case kFlushMode_Full:
201            textureCache.clear();
202            patchCache.clear();
203            dropShadowCache.clear();
204            gradientCache.clear();
205            fontRenderer.clear();
206            // fall through
207        case kFlushMode_Moderate:
208            fontRenderer.flush();
209            textureCache.flush();
210            pathCache.clear();
211            roundRectShapeCache.clear();
212            circleShapeCache.clear();
213            ovalShapeCache.clear();
214            rectShapeCache.clear();
215            arcShapeCache.clear();
216            // fall through
217        case kFlushMode_Layers:
218            layerCache.clear();
219            break;
220    }
221}
222
223///////////////////////////////////////////////////////////////////////////////
224// VBO
225///////////////////////////////////////////////////////////////////////////////
226
227bool Caches::bindMeshBuffer() {
228    return bindMeshBuffer(meshBuffer);
229}
230
231bool Caches::bindMeshBuffer(const GLuint buffer) {
232    if (mCurrentBuffer != buffer) {
233        glBindBuffer(GL_ARRAY_BUFFER, buffer);
234        mCurrentBuffer = buffer;
235        return true;
236    }
237    return false;
238}
239
240bool Caches::unbindMeshBuffer() {
241    if (mCurrentBuffer) {
242        glBindBuffer(GL_ARRAY_BUFFER, 0);
243        mCurrentBuffer = 0;
244        return true;
245    }
246    return false;
247}
248
249bool Caches::bindIndicesBuffer(const GLuint buffer) {
250    if (mCurrentIndicesBuffer != buffer) {
251        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
252        mCurrentIndicesBuffer = buffer;
253        return true;
254    }
255    return false;
256}
257
258bool Caches::unbindIndicesBuffer() {
259    if (mCurrentIndicesBuffer) {
260        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
261        mCurrentIndicesBuffer = 0;
262        return true;
263    }
264    return false;
265}
266
267void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
268    if (force || vertices != mCurrentPositionPointer) {
269        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
270        mCurrentPositionPointer = vertices;
271    }
272}
273
274void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
275    if (force || vertices != mCurrentTexCoordsPointer) {
276        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
277        mCurrentTexCoordsPointer = vertices;
278    }
279}
280
281void Caches::resetVertexPointers() {
282    mCurrentPositionPointer = this;
283    mCurrentTexCoordsPointer = this;
284}
285
286void Caches::resetTexCoordsVertexPointer() {
287    mCurrentTexCoordsPointer = this;
288}
289
290void Caches::enableTexCoordsVertexArray() {
291    if (!mTexCoordsArrayEnabled) {
292        glEnableVertexAttribArray(Program::kBindingTexCoords);
293        mTexCoordsArrayEnabled = true;
294    }
295}
296
297void Caches::disbaleTexCoordsVertexArray() {
298    if (mTexCoordsArrayEnabled) {
299        glDisableVertexAttribArray(Program::kBindingTexCoords);
300        mTexCoordsArrayEnabled = false;
301    }
302}
303
304TextureVertex* Caches::getRegionMesh() {
305    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
306    if (!mRegionMesh) {
307        mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
308
309        uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
310        for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
311            uint16_t quad = i * 4;
312            int index = i * 6;
313            regionIndices[index    ] = quad;       // top-left
314            regionIndices[index + 1] = quad + 1;   // top-right
315            regionIndices[index + 2] = quad + 2;   // bottom-left
316            regionIndices[index + 3] = quad + 2;   // bottom-left
317            regionIndices[index + 4] = quad + 1;   // top-right
318            regionIndices[index + 5] = quad + 3;   // bottom-right
319        }
320
321        glGenBuffers(1, &mRegionMeshIndices);
322        bindIndicesBuffer(mRegionMeshIndices);
323        glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
324                regionIndices, GL_STATIC_DRAW);
325
326        delete[] regionIndices;
327    } else {
328        bindIndicesBuffer(mRegionMeshIndices);
329    }
330
331    return mRegionMesh;
332}
333
334}; // namespace uirenderer
335}; // namespace android
336