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