Caches.cpp revision 01d58e43ede5ca98cbebdd166f9b0c545032c01b
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
21#include "Caches.h"
22#include "Properties.h"
23
24namespace android {
25
26#ifdef USE_OPENGL_RENDERER
27using namespace uirenderer;
28ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
29#endif
30
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Constructors/destructor
35///////////////////////////////////////////////////////////////////////////////
36
37Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO),
38        lastDstMode(GL_ZERO), currentProgram(NULL) {
39    GLint maxTextureUnits;
40    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
41    if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
42        LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
43    }
44
45    glGenBuffers(1, &meshBuffer);
46    glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
47    glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
48
49    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
50
51    mCurrentBuffer = meshBuffer;
52    mRegionMesh = NULL;
53
54    mDebugLevel = readDebugLevel();
55    LOGD("Enabling debug mode %d", mDebugLevel);
56
57#if RENDER_LAYERS_AS_REGIONS
58    LOGD("Layers will be composited as regions");
59#endif
60}
61
62Caches::~Caches() {
63    delete[] mRegionMesh;
64}
65
66///////////////////////////////////////////////////////////////////////////////
67// Debug
68///////////////////////////////////////////////////////////////////////////////
69
70void Caches::dumpMemoryUsage() {
71    LOGD("Current memory usage / total memory usage (bytes):");
72    LOGD("  TextureCache         %8d / %8d", textureCache.getSize(), textureCache.getMaxSize());
73    LOGD("  LayerCache           %8d / %8d", layerCache.getSize(), layerCache.getMaxSize());
74    LOGD("  GradientCache        %8d / %8d", gradientCache.getSize(), gradientCache.getMaxSize());
75    LOGD("  PathCache            %8d / %8d", pathCache.getSize(), pathCache.getMaxSize());
76    LOGD("  CircleShapeCache     %8d / %8d",
77            circleShapeCache.getSize(), circleShapeCache.getMaxSize());
78    LOGD("  RoundRectShapeCache  %8d / %8d",
79            roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
80    LOGD("  TextDropShadowCache  %8d / %8d", dropShadowCache.getSize(),
81            dropShadowCache.getMaxSize());
82    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
83        const uint32_t size = fontRenderer.getFontRendererSize(i);
84        LOGD("  FontRenderer %d       %8d / %8d", i, size, size);
85    }
86    LOGD("Other:");
87    LOGD("  FboCache             %8d / %8d", fboCache.getSize(), fboCache.getMaxSize());
88    LOGD("  PatchCache           %8d / %8d", patchCache.getSize(), patchCache.getMaxSize());
89
90    uint32_t total = 0;
91    total += textureCache.getSize();
92    total += layerCache.getSize();
93    total += gradientCache.getSize();
94    total += pathCache.getSize();
95    total += dropShadowCache.getSize();
96    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
97        total += fontRenderer.getFontRendererSize(i);
98    }
99
100    LOGD("Total memory usage:");
101    LOGD("  %d bytes, %.2f MB", total, total / 1024.0f / 1024.0f);
102    LOGD("\n");
103}
104
105///////////////////////////////////////////////////////////////////////////////
106// Memory management
107///////////////////////////////////////////////////////////////////////////////
108
109void Caches::clearGarbage() {
110    textureCache.clearGarbage();
111    gradientCache.clearGarbage();
112    pathCache.clearGarbage();
113
114    Mutex::Autolock _l(mGarbageLock);
115
116    size_t count = mLayerGarbage.size();
117    for (size_t i = 0; i < count; i++) {
118        Layer* layer = mLayerGarbage.itemAt(i);
119        if (layer) {
120            if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo);
121            if (layer->texture) glDeleteTextures(1, &layer->texture);
122
123            delete layer;
124        }
125    }
126    mLayerGarbage.clear();
127}
128
129void Caches::deleteLayerDeferred(Layer* layer) {
130    Mutex::Autolock _l(mGarbageLock);
131    mLayerGarbage.push(layer);
132}
133
134///////////////////////////////////////////////////////////////////////////////
135// VBO
136///////////////////////////////////////////////////////////////////////////////
137
138void Caches::bindMeshBuffer() {
139    bindMeshBuffer(meshBuffer);
140}
141
142void Caches::bindMeshBuffer(const GLuint buffer) {
143    if (mCurrentBuffer != buffer) {
144        glBindBuffer(GL_ARRAY_BUFFER, buffer);
145        mCurrentBuffer = buffer;
146    }
147}
148
149void Caches::unbindMeshBuffer() {
150    if (mCurrentBuffer) {
151        glBindBuffer(GL_ARRAY_BUFFER, 0);
152        mCurrentBuffer = 0;
153    }
154}
155
156TextureVertex* Caches::getRegionMesh() {
157    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
158    if (!mRegionMesh) {
159        mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
160
161        uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
162        for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
163            uint16_t quad = i * 4;
164            int index = i * 6;
165            regionIndices[index    ] = quad;       // top-left
166            regionIndices[index + 1] = quad + 1;   // top-right
167            regionIndices[index + 2] = quad + 2;   // bottom-left
168            regionIndices[index + 3] = quad + 2;   // bottom-left
169            regionIndices[index + 4] = quad + 1;   // top-right
170            regionIndices[index + 5] = quad + 3;   // bottom-right
171        }
172
173        glGenBuffers(1, &mRegionMeshIndices);
174        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
175        glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
176                regionIndices, GL_STATIC_DRAW);
177
178        delete[] regionIndices;
179    } else {
180        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
181    }
182
183    return mRegionMesh;
184}
185
186}; // namespace uirenderer
187}; // namespace android
188