Caches.cpp revision 09b7c91de73b59aa3f679b3ae3ba299f82ec9f8a
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#include "LayerRenderer.h"
24
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
50    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
51
52    mCurrentBuffer = meshBuffer;
53    mRegionMesh = NULL;
54
55    mDebugLevel = readDebugLevel();
56    LOGD("Enabling debug mode %d", mDebugLevel);
57
58#if RENDER_LAYERS_AS_REGIONS
59    LOGD("Layers will be composited as regions");
60#endif
61}
62
63Caches::~Caches() {
64    delete[] mRegionMesh;
65}
66
67///////////////////////////////////////////////////////////////////////////////
68// 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());
77    LOGD("  CircleShapeCache     %8d / %8d",
78            circleShapeCache.getSize(), circleShapeCache.getMaxSize());
79    LOGD("  RoundRectShapeCache  %8d / %8d",
80            roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
81    LOGD("  TextDropShadowCache  %8d / %8d", dropShadowCache.getSize(),
82            dropShadowCache.getMaxSize());
83    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
84        const uint32_t size = fontRenderer.getFontRendererSize(i);
85        LOGD("  FontRenderer %d       %8d / %8d", i, size, size);
86    }
87    LOGD("Other:");
88    LOGD("  FboCache             %8d / %8d", fboCache.getSize(), fboCache.getMaxSize());
89    LOGD("  PatchCache           %8d / %8d", patchCache.getSize(), patchCache.getMaxSize());
90
91    uint32_t total = 0;
92    total += textureCache.getSize();
93    total += layerCache.getSize();
94    total += gradientCache.getSize();
95    total += pathCache.getSize();
96    total += dropShadowCache.getSize();
97    for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) {
98        total += fontRenderer.getFontRendererSize(i);
99    }
100
101    LOGD("Total memory usage:");
102    LOGD("  %d bytes, %.2f MB", total, total / 1024.0f / 1024.0f);
103    LOGD("\n");
104}
105
106///////////////////////////////////////////////////////////////////////////////
107// Memory management
108///////////////////////////////////////////////////////////////////////////////
109
110void Caches::clearGarbage() {
111    textureCache.clearGarbage();
112    gradientCache.clearGarbage();
113    pathCache.clearGarbage();
114
115    Mutex::Autolock _l(mGarbageLock);
116
117    size_t count = mLayerGarbage.size();
118    for (size_t i = 0; i < count; i++) {
119        Layer* layer = mLayerGarbage.itemAt(i);
120        LayerRenderer::destroyLayer(layer);
121    }
122    mLayerGarbage.clear();
123}
124
125void Caches::deleteLayerDeferred(Layer* layer) {
126    Mutex::Autolock _l(mGarbageLock);
127    mLayerGarbage.push(layer);
128}
129
130///////////////////////////////////////////////////////////////////////////////
131// VBO
132///////////////////////////////////////////////////////////////////////////////
133
134void Caches::bindMeshBuffer() {
135    bindMeshBuffer(meshBuffer);
136}
137
138void Caches::bindMeshBuffer(const GLuint buffer) {
139    if (mCurrentBuffer != buffer) {
140        glBindBuffer(GL_ARRAY_BUFFER, buffer);
141        mCurrentBuffer = buffer;
142    }
143}
144
145void Caches::unbindMeshBuffer() {
146    if (mCurrentBuffer) {
147        glBindBuffer(GL_ARRAY_BUFFER, 0);
148        mCurrentBuffer = 0;
149    }
150}
151
152TextureVertex* Caches::getRegionMesh() {
153    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
154    if (!mRegionMesh) {
155        mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
156
157        uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
158        for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
159            uint16_t quad = i * 4;
160            int index = i * 6;
161            regionIndices[index    ] = quad;       // top-left
162            regionIndices[index + 1] = quad + 1;   // top-right
163            regionIndices[index + 2] = quad + 2;   // bottom-left
164            regionIndices[index + 3] = quad + 2;   // bottom-left
165            regionIndices[index + 4] = quad + 1;   // top-right
166            regionIndices[index + 5] = quad + 3;   // bottom-right
167        }
168
169        glGenBuffers(1, &mRegionMeshIndices);
170        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
171        glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
172                regionIndices, GL_STATIC_DRAW);
173
174        delete[] regionIndices;
175    } else {
176        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices);
177    }
178
179    return mRegionMesh;
180}
181
182}; // namespace uirenderer
183}; // namespace android
184