Caches.cpp revision ff78583d8a73ca35ce65b5d2592570ff6fb9901b
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>(), mExtensions(Extensions::getInstance()), mInitialized(false) {
51    init();
52    initFont();
53    initConstraints();
54    initProperties();
55    initExtensions();
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    mCurrentPositionStride = 0;
72    mCurrentTexCoordsPointer = this;
73
74    mTexCoordsArrayEnabled = false;
75
76    glDisable(GL_SCISSOR_TEST);
77    scissorEnabled = false;
78    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
79
80    glActiveTexture(gTextureUnits[0]);
81    mTextureUnit = 0;
82
83    mRegionMesh = NULL;
84
85    blend = false;
86    lastSrcMode = GL_ZERO;
87    lastDstMode = GL_ZERO;
88    currentProgram = NULL;
89
90    mFunctorsCount = 0;
91
92    debugLayersUpdates = false;
93    debugOverdraw = false;
94    debugStencilClip = kStencilHide;
95
96    mInitialized = true;
97}
98
99void Caches::initFont() {
100    fontRenderer = GammaFontRenderer::createRenderer();
101}
102
103void Caches::initExtensions() {
104    if (mExtensions.hasDebugMarker()) {
105        eventMark = glInsertEventMarkerEXT;
106
107        startMark = glPushGroupMarkerEXT;
108        endMark = glPopGroupMarkerEXT;
109    } else {
110        eventMark = eventMarkNull;
111        startMark = startMarkNull;
112        endMark = endMarkNull;
113    }
114
115    if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
116        setLabel = glLabelObjectEXT;
117        getLabel = glGetObjectLabelEXT;
118    } else {
119        setLabel = setLabelNull;
120        getLabel = getLabelNull;
121    }
122}
123
124void Caches::initConstraints() {
125    GLint maxTextureUnits;
126    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
127    if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
128        ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
129    }
130
131    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
132}
133
134bool Caches::initProperties() {
135    bool prevDebugLayersUpdates = debugLayersUpdates;
136    bool prevDebugOverdraw = debugOverdraw;
137    StencilClipDebug prevDebugStencilClip = debugStencilClip;
138
139    char property[PROPERTY_VALUE_MAX];
140    if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
141        INIT_LOGD("  Layers updates debug enabled: %s", property);
142        debugLayersUpdates = !strcmp(property, "true");
143    } else {
144        debugLayersUpdates = false;
145    }
146
147    if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
148        INIT_LOGD("  Overdraw debug enabled: %s", property);
149        debugOverdraw = !strcmp(property, "true");
150    } else {
151        debugOverdraw = false;
152    }
153
154    // See Properties.h for valid values
155    if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
156        INIT_LOGD("  Stencil clip debug enabled: %s", property);
157        if (!strcmp(property, "hide")) {
158            debugStencilClip = kStencilHide;
159        } else if (!strcmp(property, "highlight")) {
160            debugStencilClip = kStencilShowHighlight;
161        } else if (!strcmp(property, "region")) {
162            debugStencilClip = kStencilShowRegion;
163        }
164    } else {
165        debugStencilClip = kStencilHide;
166    }
167
168    if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
169        drawDeferDisabled = !strcasecmp(property, "true");
170        INIT_LOGD("  Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
171    } else {
172        INIT_LOGD("  Draw defer enabled");
173    }
174
175    if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
176        drawReorderDisabled = !strcasecmp(property, "true");
177        INIT_LOGD("  Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
178    } else {
179        INIT_LOGD("  Draw reorder enabled");
180    }
181
182    return (prevDebugLayersUpdates != debugLayersUpdates) ||
183            (prevDebugOverdraw != debugOverdraw) ||
184            (prevDebugStencilClip != debugStencilClip);
185}
186
187void Caches::terminate() {
188    if (!mInitialized) return;
189
190    glDeleteBuffers(1, &meshBuffer);
191    mCurrentBuffer = 0;
192
193    glDeleteBuffers(1, &mRegionMeshIndices);
194    delete[] mRegionMesh;
195    mRegionMesh = NULL;
196
197    fboCache.clear();
198
199    programCache.clear();
200    currentProgram = NULL;
201
202    mInitialized = false;
203}
204
205///////////////////////////////////////////////////////////////////////////////
206// Debug
207///////////////////////////////////////////////////////////////////////////////
208
209void Caches::dumpMemoryUsage() {
210    String8 stringLog;
211    dumpMemoryUsage(stringLog);
212    ALOGD("%s", stringLog.string());
213}
214
215void Caches::dumpMemoryUsage(String8 &log) {
216    log.appendFormat("Current memory usage / total memory usage (bytes):\n");
217    log.appendFormat("  TextureCache         %8d / %8d\n",
218            textureCache.getSize(), textureCache.getMaxSize());
219    log.appendFormat("  LayerCache           %8d / %8d\n",
220            layerCache.getSize(), layerCache.getMaxSize());
221    log.appendFormat("  RenderBufferCache    %8d / %8d\n",
222            renderBufferCache.getSize(), renderBufferCache.getMaxSize());
223    log.appendFormat("  GradientCache        %8d / %8d\n",
224            gradientCache.getSize(), gradientCache.getMaxSize());
225    log.appendFormat("  PathCache            %8d / %8d\n",
226            pathCache.getSize(), pathCache.getMaxSize());
227    log.appendFormat("  CircleShapeCache     %8d / %8d\n",
228            circleShapeCache.getSize(), circleShapeCache.getMaxSize());
229    log.appendFormat("  OvalShapeCache       %8d / %8d\n",
230            ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
231    log.appendFormat("  RoundRectShapeCache  %8d / %8d\n",
232            roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
233    log.appendFormat("  RectShapeCache       %8d / %8d\n",
234            rectShapeCache.getSize(), rectShapeCache.getMaxSize());
235    log.appendFormat("  ArcShapeCache        %8d / %8d\n",
236            arcShapeCache.getSize(), arcShapeCache.getMaxSize());
237    log.appendFormat("  TextDropShadowCache  %8d / %8d\n", dropShadowCache.getSize(),
238            dropShadowCache.getMaxSize());
239    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
240        const uint32_t size = fontRenderer->getFontRendererSize(i);
241        log.appendFormat("  FontRenderer %d       %8d / %8d\n", i, size, size);
242    }
243    log.appendFormat("Other:\n");
244    log.appendFormat("  FboCache             %8d / %8d\n",
245            fboCache.getSize(), fboCache.getMaxSize());
246    log.appendFormat("  PatchCache           %8d / %8d\n",
247            patchCache.getSize(), patchCache.getMaxSize());
248
249    uint32_t total = 0;
250    total += textureCache.getSize();
251    total += layerCache.getSize();
252    total += renderBufferCache.getSize();
253    total += gradientCache.getSize();
254    total += pathCache.getSize();
255    total += dropShadowCache.getSize();
256    total += roundRectShapeCache.getSize();
257    total += circleShapeCache.getSize();
258    total += ovalShapeCache.getSize();
259    total += rectShapeCache.getSize();
260    total += arcShapeCache.getSize();
261    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
262        total += fontRenderer->getFontRendererSize(i);
263    }
264
265    log.appendFormat("Total memory usage:\n");
266    log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
267}
268
269///////////////////////////////////////////////////////////////////////////////
270// Memory management
271///////////////////////////////////////////////////////////////////////////////
272
273void Caches::clearGarbage() {
274    textureCache.clearGarbage();
275    pathCache.clearGarbage();
276
277    Vector<DisplayList*> displayLists;
278    Vector<Layer*> layers;
279
280    { // scope for the lock
281        Mutex::Autolock _l(mGarbageLock);
282        displayLists = mDisplayListGarbage;
283        layers = mLayerGarbage;
284        mDisplayListGarbage.clear();
285        mLayerGarbage.clear();
286    }
287
288    size_t count = displayLists.size();
289    for (size_t i = 0; i < count; i++) {
290        DisplayList* displayList = displayLists.itemAt(i);
291        delete displayList;
292    }
293
294    count = layers.size();
295    for (size_t i = 0; i < count; i++) {
296        Layer* layer = layers.itemAt(i);
297        delete layer;
298    }
299    layers.clear();
300}
301
302void Caches::deleteLayerDeferred(Layer* layer) {
303    Mutex::Autolock _l(mGarbageLock);
304    mLayerGarbage.push(layer);
305}
306
307void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
308    Mutex::Autolock _l(mGarbageLock);
309    mDisplayListGarbage.push(displayList);
310}
311
312void Caches::flush(FlushMode mode) {
313    FLUSH_LOGD("Flushing caches (mode %d)", mode);
314
315    switch (mode) {
316        case kFlushMode_Full:
317            textureCache.clear();
318            patchCache.clear();
319            dropShadowCache.clear();
320            gradientCache.clear();
321            fontRenderer->clear();
322            dither.clear();
323            // fall through
324        case kFlushMode_Moderate:
325            fontRenderer->flush();
326            textureCache.flush();
327            pathCache.clear();
328            roundRectShapeCache.clear();
329            circleShapeCache.clear();
330            ovalShapeCache.clear();
331            rectShapeCache.clear();
332            arcShapeCache.clear();
333            // fall through
334        case kFlushMode_Layers:
335            layerCache.clear();
336            renderBufferCache.clear();
337            break;
338    }
339
340    clearGarbage();
341}
342
343///////////////////////////////////////////////////////////////////////////////
344// VBO
345///////////////////////////////////////////////////////////////////////////////
346
347bool Caches::bindMeshBuffer() {
348    return bindMeshBuffer(meshBuffer);
349}
350
351bool Caches::bindMeshBuffer(const GLuint buffer) {
352    if (mCurrentBuffer != buffer) {
353        glBindBuffer(GL_ARRAY_BUFFER, buffer);
354        mCurrentBuffer = buffer;
355        return true;
356    }
357    return false;
358}
359
360bool Caches::unbindMeshBuffer() {
361    if (mCurrentBuffer) {
362        glBindBuffer(GL_ARRAY_BUFFER, 0);
363        mCurrentBuffer = 0;
364        return true;
365    }
366    return false;
367}
368
369bool Caches::bindIndicesBuffer(const GLuint buffer) {
370    if (mCurrentIndicesBuffer != buffer) {
371        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
372        mCurrentIndicesBuffer = buffer;
373        return true;
374    }
375    return false;
376}
377
378bool Caches::unbindIndicesBuffer() {
379    if (mCurrentIndicesBuffer) {
380        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
381        mCurrentIndicesBuffer = 0;
382        return true;
383    }
384    return false;
385}
386
387///////////////////////////////////////////////////////////////////////////////
388// Meshes and textures
389///////////////////////////////////////////////////////////////////////////////
390
391void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
392    if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
393        GLuint slot = currentProgram->position;
394        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
395        mCurrentPositionPointer = vertices;
396        mCurrentPositionStride = stride;
397    }
398}
399
400void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
401    if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
402        GLuint slot = currentProgram->texCoords;
403        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
404        mCurrentTexCoordsPointer = vertices;
405        mCurrentTexCoordsStride = stride;
406    }
407}
408
409void Caches::resetVertexPointers() {
410    mCurrentPositionPointer = this;
411    mCurrentTexCoordsPointer = this;
412}
413
414void Caches::resetTexCoordsVertexPointer() {
415    mCurrentTexCoordsPointer = this;
416}
417
418void Caches::enableTexCoordsVertexArray() {
419    if (!mTexCoordsArrayEnabled) {
420        glEnableVertexAttribArray(Program::kBindingTexCoords);
421        mCurrentTexCoordsPointer = this;
422        mTexCoordsArrayEnabled = true;
423    }
424}
425
426void Caches::disableTexCoordsVertexArray() {
427    if (mTexCoordsArrayEnabled) {
428        glDisableVertexAttribArray(Program::kBindingTexCoords);
429        mTexCoordsArrayEnabled = false;
430    }
431}
432
433void Caches::activeTexture(GLuint textureUnit) {
434    if (mTextureUnit != textureUnit) {
435        glActiveTexture(gTextureUnits[textureUnit]);
436        mTextureUnit = textureUnit;
437    }
438}
439
440///////////////////////////////////////////////////////////////////////////////
441// Scissor
442///////////////////////////////////////////////////////////////////////////////
443
444bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
445    if (scissorEnabled && (x != mScissorX || y != mScissorY ||
446            width != mScissorWidth || height != mScissorHeight)) {
447
448        if (x < 0) {
449            width += x;
450            x = 0;
451        }
452        if (y < 0) {
453            height += y;
454            y = 0;
455        }
456        if (width < 0) {
457            width = 0;
458        }
459        if (height < 0) {
460            height = 0;
461        }
462        glScissor(x, y, width, height);
463
464        mScissorX = x;
465        mScissorY = y;
466        mScissorWidth = width;
467        mScissorHeight = height;
468
469        return true;
470    }
471    return false;
472}
473
474bool Caches::enableScissor() {
475    if (!scissorEnabled) {
476        glEnable(GL_SCISSOR_TEST);
477        scissorEnabled = true;
478        resetScissor();
479        return true;
480    }
481    return false;
482}
483
484bool Caches::disableScissor() {
485    if (scissorEnabled) {
486        glDisable(GL_SCISSOR_TEST);
487        scissorEnabled = false;
488        return true;
489    }
490    return false;
491}
492
493void Caches::setScissorEnabled(bool enabled) {
494    if (scissorEnabled != enabled) {
495        if (enabled) glEnable(GL_SCISSOR_TEST);
496        else glDisable(GL_SCISSOR_TEST);
497        scissorEnabled = enabled;
498    }
499}
500
501void Caches::resetScissor() {
502    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
503}
504
505///////////////////////////////////////////////////////////////////////////////
506// Tiling
507///////////////////////////////////////////////////////////////////////////////
508
509void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
510    if (mExtensions.hasTiledRendering() && !debugOverdraw) {
511        glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
512    }
513}
514
515void Caches::endTiling() {
516    if (mExtensions.hasTiledRendering() && !debugOverdraw) {
517        glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
518    }
519}
520
521bool Caches::hasRegisteredFunctors() {
522    return mFunctorsCount > 0;
523}
524
525void Caches::registerFunctors(uint32_t functorCount) {
526    mFunctorsCount += functorCount;
527}
528
529void Caches::unregisterFunctors(uint32_t functorCount) {
530    if (functorCount > mFunctorsCount) {
531        mFunctorsCount = 0;
532    } else {
533        mFunctorsCount -= functorCount;
534    }
535}
536
537///////////////////////////////////////////////////////////////////////////////
538// Regions
539///////////////////////////////////////////////////////////////////////////////
540
541TextureVertex* Caches::getRegionMesh() {
542    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
543    if (!mRegionMesh) {
544        mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
545
546        uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
547        for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
548            uint16_t quad = i * 4;
549            int index = i * 6;
550            regionIndices[index    ] = quad;       // top-left
551            regionIndices[index + 1] = quad + 1;   // top-right
552            regionIndices[index + 2] = quad + 2;   // bottom-left
553            regionIndices[index + 3] = quad + 2;   // bottom-left
554            regionIndices[index + 4] = quad + 1;   // top-right
555            regionIndices[index + 5] = quad + 3;   // bottom-right
556        }
557
558        glGenBuffers(1, &mRegionMeshIndices);
559        bindIndicesBuffer(mRegionMeshIndices);
560        glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
561                regionIndices, GL_STATIC_DRAW);
562
563        delete[] regionIndices;
564    } else {
565        bindIndicesBuffer(mRegionMeshIndices);
566    }
567
568    return mRegionMesh;
569}
570
571}; // namespace uirenderer
572}; // namespace android
573