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