Caches.cpp revision 4285de3c8bc77856a2df98894f74bb7333f1d348
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 "DisplayListRenderer.h"
24#include "Properties.h"
25#include "LayerRenderer.h"
26
27namespace android {
28
29#ifdef USE_OPENGL_RENDERER
30using namespace uirenderer;
31ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
32#endif
33
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
41    #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
42#else
43    #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
47// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
50Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
51    init();
52    initFont();
53    initExtensions();
54    initConstraints();
55    initProperties();
56
57    mDebugLevel = readDebugLevel();
58    ALOGD("Enabling debug mode %d", mDebugLevel);
59}
60
61void Caches::init() {
62    if (mInitialized) return;
63
64    glGenBuffers(1, &meshBuffer);
65    glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
66    glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
67
68    mCurrentBuffer = meshBuffer;
69    mCurrentIndicesBuffer = 0;
70    mCurrentPositionPointer = this;
71    mCurrentTexCoordsPointer = this;
72
73    mTexCoordsArrayEnabled = false;
74
75    glDisable(GL_SCISSOR_TEST);
76    scissorEnabled = false;
77    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
78
79    glActiveTexture(gTextureUnits[0]);
80    mTextureUnit = 0;
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::initFont() {
93    fontRenderer = GammaFontRenderer::createRenderer();
94}
95
96void Caches::initExtensions() {
97    if (extensions.hasDebugMarker()) {
98        eventMark = glInsertEventMarkerEXT;
99        startMark = glPushGroupMarkerEXT;
100        endMark = glPopGroupMarkerEXT;
101    } else {
102        eventMark = eventMarkNull;
103        startMark = startMarkNull;
104        endMark = endMarkNull;
105    }
106
107    if (extensions.hasDebugLabel()) {
108        setLabel = glLabelObjectEXT;
109        getLabel = glGetObjectLabelEXT;
110    } else {
111        setLabel = setLabelNull;
112        getLabel = getLabelNull;
113    }
114}
115
116void Caches::initConstraints() {
117    GLint maxTextureUnits;
118    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
119    if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
120        ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
121    }
122
123    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
124}
125
126void Caches::initProperties() {
127    char property[PROPERTY_VALUE_MAX];
128    if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
129        INIT_LOGD("  Layers updates debug enabled: %s", property);
130        debugLayersUpdates = !strcmp(property, "true");
131    } else {
132        debugLayersUpdates = false;
133    }
134
135    if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
136        INIT_LOGD("  Overdraw debug enabled: %s", property);
137        debugOverdraw = !strcmp(property, "true");
138    } else {
139        debugOverdraw = false;
140    }
141}
142
143void Caches::terminate() {
144    if (!mInitialized) return;
145
146    glDeleteBuffers(1, &meshBuffer);
147    mCurrentBuffer = 0;
148
149    glDeleteBuffers(1, &mRegionMeshIndices);
150    delete[] mRegionMesh;
151    mRegionMesh = NULL;
152
153    fboCache.clear();
154
155    programCache.clear();
156    currentProgram = NULL;
157
158    mInitialized = false;
159}
160
161///////////////////////////////////////////////////////////////////////////////
162// Debug
163///////////////////////////////////////////////////////////////////////////////
164
165void Caches::dumpMemoryUsage() {
166    String8 stringLog;
167    dumpMemoryUsage(stringLog);
168    ALOGD("%s", stringLog.string());
169}
170
171void Caches::dumpMemoryUsage(String8 &log) {
172    log.appendFormat("Current memory usage / total memory usage (bytes):\n");
173    log.appendFormat("  TextureCache         %8d / %8d\n",
174            textureCache.getSize(), textureCache.getMaxSize());
175    log.appendFormat("  LayerCache           %8d / %8d\n",
176            layerCache.getSize(), layerCache.getMaxSize());
177    log.appendFormat("  GradientCache        %8d / %8d\n",
178            gradientCache.getSize(), gradientCache.getMaxSize());
179    log.appendFormat("  PathCache            %8d / %8d\n",
180            pathCache.getSize(), pathCache.getMaxSize());
181    log.appendFormat("  CircleShapeCache     %8d / %8d\n",
182            circleShapeCache.getSize(), circleShapeCache.getMaxSize());
183    log.appendFormat("  OvalShapeCache       %8d / %8d\n",
184            ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
185    log.appendFormat("  RoundRectShapeCache  %8d / %8d\n",
186            roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
187    log.appendFormat("  RectShapeCache       %8d / %8d\n",
188            rectShapeCache.getSize(), rectShapeCache.getMaxSize());
189    log.appendFormat("  ArcShapeCache        %8d / %8d\n",
190            arcShapeCache.getSize(), arcShapeCache.getMaxSize());
191    log.appendFormat("  TextDropShadowCache  %8d / %8d\n", dropShadowCache.getSize(),
192            dropShadowCache.getMaxSize());
193    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
194        const uint32_t size = fontRenderer->getFontRendererSize(i);
195        log.appendFormat("  FontRenderer %d       %8d / %8d\n", i, size, size);
196    }
197    log.appendFormat("Other:\n");
198    log.appendFormat("  FboCache             %8d / %8d\n",
199            fboCache.getSize(), fboCache.getMaxSize());
200    log.appendFormat("  PatchCache           %8d / %8d\n",
201            patchCache.getSize(), patchCache.getMaxSize());
202
203    uint32_t total = 0;
204    total += textureCache.getSize();
205    total += layerCache.getSize();
206    total += gradientCache.getSize();
207    total += pathCache.getSize();
208    total += dropShadowCache.getSize();
209    total += roundRectShapeCache.getSize();
210    total += circleShapeCache.getSize();
211    total += ovalShapeCache.getSize();
212    total += rectShapeCache.getSize();
213    total += arcShapeCache.getSize();
214    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
215        total += fontRenderer->getFontRendererSize(i);
216    }
217
218    log.appendFormat("Total memory usage:\n");
219    log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
220}
221
222///////////////////////////////////////////////////////////////////////////////
223// Memory management
224///////////////////////////////////////////////////////////////////////////////
225
226void Caches::clearGarbage() {
227    textureCache.clearGarbage();
228    pathCache.clearGarbage();
229
230    Mutex::Autolock _l(mGarbageLock);
231
232    size_t count = mLayerGarbage.size();
233    for (size_t i = 0; i < count; i++) {
234        Layer* layer = mLayerGarbage.itemAt(i);
235        LayerRenderer::destroyLayer(layer);
236    }
237    mLayerGarbage.clear();
238
239    count = mDisplayListGarbage.size();
240    for (size_t i = 0; i < count; i++) {
241        DisplayList* displayList = mDisplayListGarbage.itemAt(i);
242        delete displayList;
243    }
244    mDisplayListGarbage.clear();
245}
246
247void Caches::deleteLayerDeferred(Layer* layer) {
248    Mutex::Autolock _l(mGarbageLock);
249    mLayerGarbage.push(layer);
250}
251
252void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
253    Mutex::Autolock _l(mGarbageLock);
254    mDisplayListGarbage.push(displayList);
255}
256
257void Caches::flush(FlushMode mode) {
258    FLUSH_LOGD("Flushing caches (mode %d)", mode);
259
260    clearGarbage();
261
262    switch (mode) {
263        case kFlushMode_Full:
264            textureCache.clear();
265            patchCache.clear();
266            dropShadowCache.clear();
267            gradientCache.clear();
268            fontRenderer->clear();
269            dither.clear();
270            // fall through
271        case kFlushMode_Moderate:
272            fontRenderer->flush();
273            textureCache.flush();
274            pathCache.clear();
275            roundRectShapeCache.clear();
276            circleShapeCache.clear();
277            ovalShapeCache.clear();
278            rectShapeCache.clear();
279            arcShapeCache.clear();
280            // fall through
281        case kFlushMode_Layers:
282            layerCache.clear();
283            break;
284    }
285}
286
287///////////////////////////////////////////////////////////////////////////////
288// VBO
289///////////////////////////////////////////////////////////////////////////////
290
291bool Caches::bindMeshBuffer() {
292    return bindMeshBuffer(meshBuffer);
293}
294
295bool Caches::bindMeshBuffer(const GLuint buffer) {
296    if (mCurrentBuffer != buffer) {
297        glBindBuffer(GL_ARRAY_BUFFER, buffer);
298        mCurrentBuffer = buffer;
299        return true;
300    }
301    return false;
302}
303
304bool Caches::unbindMeshBuffer() {
305    if (mCurrentBuffer) {
306        glBindBuffer(GL_ARRAY_BUFFER, 0);
307        mCurrentBuffer = 0;
308        return true;
309    }
310    return false;
311}
312
313bool Caches::bindIndicesBuffer(const GLuint buffer) {
314    if (mCurrentIndicesBuffer != buffer) {
315        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
316        mCurrentIndicesBuffer = buffer;
317        return true;
318    }
319    return false;
320}
321
322bool Caches::unbindIndicesBuffer() {
323    if (mCurrentIndicesBuffer) {
324        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
325        mCurrentIndicesBuffer = 0;
326        return true;
327    }
328    return false;
329}
330
331///////////////////////////////////////////////////////////////////////////////
332// Meshes and textures
333///////////////////////////////////////////////////////////////////////////////
334
335void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
336    if (force || vertices != mCurrentPositionPointer) {
337        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
338        mCurrentPositionPointer = vertices;
339    }
340}
341
342void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
343    if (force || vertices != mCurrentTexCoordsPointer) {
344        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
345        mCurrentTexCoordsPointer = vertices;
346    }
347}
348
349void Caches::resetVertexPointers() {
350    mCurrentPositionPointer = this;
351    mCurrentTexCoordsPointer = this;
352}
353
354void Caches::resetTexCoordsVertexPointer() {
355    mCurrentTexCoordsPointer = this;
356}
357
358void Caches::enableTexCoordsVertexArray() {
359    if (!mTexCoordsArrayEnabled) {
360        glEnableVertexAttribArray(Program::kBindingTexCoords);
361        mCurrentTexCoordsPointer = this;
362        mTexCoordsArrayEnabled = true;
363    }
364}
365
366void Caches::disbaleTexCoordsVertexArray() {
367    if (mTexCoordsArrayEnabled) {
368        glDisableVertexAttribArray(Program::kBindingTexCoords);
369        mTexCoordsArrayEnabled = false;
370    }
371}
372
373void Caches::activeTexture(GLuint textureUnit) {
374    if (mTextureUnit != textureUnit) {
375        glActiveTexture(gTextureUnits[textureUnit]);
376        mTextureUnit = textureUnit;
377    }
378}
379
380///////////////////////////////////////////////////////////////////////////////
381// Scissor
382///////////////////////////////////////////////////////////////////////////////
383
384bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
385    if (scissorEnabled && (x != mScissorX || y != mScissorY ||
386            width != mScissorWidth || height != mScissorHeight)) {
387
388        if (x < 0) x = 0;
389        if (y < 0) y = 0;
390
391        glScissor(x, y, width, height);
392
393        mScissorX = x;
394        mScissorY = y;
395        mScissorWidth = width;
396        mScissorHeight = height;
397
398        return true;
399    }
400    return false;
401}
402
403bool Caches::enableScissor() {
404    if (!scissorEnabled) {
405        glEnable(GL_SCISSOR_TEST);
406        scissorEnabled = true;
407        return true;
408    }
409    return false;
410}
411
412bool Caches::disableScissor() {
413    if (scissorEnabled) {
414        glDisable(GL_SCISSOR_TEST);
415        scissorEnabled = false;
416        return true;
417    }
418    return false;
419}
420
421void Caches::setScissorEnabled(bool enabled) {
422    if (scissorEnabled != enabled) {
423        if (enabled) glEnable(GL_SCISSOR_TEST);
424        else glDisable(GL_SCISSOR_TEST);
425        scissorEnabled = enabled;
426    }
427}
428
429void Caches::resetScissor() {
430    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
431}
432
433///////////////////////////////////////////////////////////////////////////////
434// Tiling
435///////////////////////////////////////////////////////////////////////////////
436
437void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool opaque) {
438    if (extensions.hasTiledRendering() && !debugOverdraw) {
439        glStartTilingQCOM(x, y, width, height, (opaque ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
440    }
441}
442
443void Caches::endTiling() {
444    if (extensions.hasTiledRendering() && !debugOverdraw) {
445        glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
446    }
447}
448
449///////////////////////////////////////////////////////////////////////////////
450// Regions
451///////////////////////////////////////////////////////////////////////////////
452
453TextureVertex* Caches::getRegionMesh() {
454    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
455    if (!mRegionMesh) {
456        mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
457
458        uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
459        for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
460            uint16_t quad = i * 4;
461            int index = i * 6;
462            regionIndices[index    ] = quad;       // top-left
463            regionIndices[index + 1] = quad + 1;   // top-right
464            regionIndices[index + 2] = quad + 2;   // bottom-left
465            regionIndices[index + 3] = quad + 2;   // bottom-left
466            regionIndices[index + 4] = quad + 1;   // top-right
467            regionIndices[index + 5] = quad + 3;   // bottom-right
468        }
469
470        glGenBuffers(1, &mRegionMeshIndices);
471        bindIndicesBuffer(mRegionMeshIndices);
472        glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
473                regionIndices, GL_STATIC_DRAW);
474
475        delete[] regionIndices;
476    } else {
477        bindIndicesBuffer(mRegionMeshIndices);
478    }
479
480    return mRegionMesh;
481}
482
483}; // namespace uirenderer
484}; // namespace android
485