Caches.cpp revision 44eb2c00861098dd3e2950d923646814b4cc57c2
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 "Caches.h"
20
21#include "DisplayListRenderer.h"
22#include "GammaFontRenderer.h"
23#include "LayerRenderer.h"
24#include "Properties.h"
25#include "renderstate/RenderState.h"
26#include "ShadowTessellator.h"
27
28#include <utils/Log.h>
29#include <utils/String8.h>
30
31namespace android {
32namespace uirenderer {
33
34Caches* Caches::sInstance = nullptr;
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(RenderState& renderState)
51        : patchCache(renderState)
52        , dither(*this)
53        , mRenderState(&renderState)
54        , mExtensions(Extensions::getInstance())
55        , mInitialized(false) {
56    INIT_LOGD("Creating OpenGL renderer caches");
57    init();
58    initFont();
59    initConstraints();
60    initProperties();
61    initStaticProperties();
62    initExtensions();
63    initTempProperties();
64
65    mDebugLevel = readDebugLevel();
66    ALOGD_IF(mDebugLevel != kDebugDisabled,
67            "Enabling debug mode %d", mDebugLevel);
68}
69
70bool Caches::init() {
71    if (mInitialized) return false;
72
73    ATRACE_NAME("Caches::init");
74
75
76    mRegionMesh = nullptr;
77    currentProgram = nullptr;
78
79    mFunctorsCount = 0;
80
81    debugLayersUpdates = false;
82    debugOverdraw = false;
83    debugStencilClip = kStencilHide;
84
85    patchCache.init();
86
87    mInitialized = true;
88
89    mPixelBufferState = new PixelBufferState();
90    mTextureState = new TextureState();
91
92    return true;
93}
94
95void Caches::initFont() {
96    fontRenderer = GammaFontRenderer::createRenderer();
97}
98
99void Caches::initExtensions() {
100    if (mExtensions.hasDebugMarker()) {
101        eventMark = glInsertEventMarkerEXT;
102
103        startMark = glPushGroupMarkerEXT;
104        endMark = glPopGroupMarkerEXT;
105    } else {
106        eventMark = eventMarkNull;
107        startMark = startMarkNull;
108        endMark = endMarkNull;
109    }
110
111    if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
112        setLabel = glLabelObjectEXT;
113        getLabel = glGetObjectLabelEXT;
114    } else {
115        setLabel = setLabelNull;
116        getLabel = getLabelNull;
117    }
118}
119
120void Caches::initConstraints() {
121    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
122}
123
124void Caches::initStaticProperties() {
125    gpuPixelBuffersEnabled = false;
126
127    // OpenGL ES 3.0+ specific features
128    if (mExtensions.hasPixelBufferObjects()) {
129        char property[PROPERTY_VALUE_MAX];
130        if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
131            gpuPixelBuffersEnabled = !strcmp(property, "true");
132        }
133    }
134}
135
136bool Caches::initProperties() {
137    bool prevDebugLayersUpdates = debugLayersUpdates;
138    bool prevDebugOverdraw = debugOverdraw;
139    StencilClipDebug prevDebugStencilClip = debugStencilClip;
140
141    char property[PROPERTY_VALUE_MAX];
142    if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, nullptr) > 0) {
143        INIT_LOGD("  Layers updates debug enabled: %s", property);
144        debugLayersUpdates = !strcmp(property, "true");
145    } else {
146        debugLayersUpdates = false;
147    }
148
149    debugOverdraw = false;
150    if (property_get(PROPERTY_DEBUG_OVERDRAW, property, nullptr) > 0) {
151        INIT_LOGD("  Overdraw debug enabled: %s", property);
152        if (!strcmp(property, "show")) {
153            debugOverdraw = true;
154            mOverdrawDebugColorSet = kColorSet_Default;
155        } else if (!strcmp(property, "show_deuteranomaly")) {
156            debugOverdraw = true;
157            mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
158        }
159    }
160
161    // See Properties.h for valid values
162    if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, nullptr) > 0) {
163        INIT_LOGD("  Stencil clip debug enabled: %s", property);
164        if (!strcmp(property, "hide")) {
165            debugStencilClip = kStencilHide;
166        } else if (!strcmp(property, "highlight")) {
167            debugStencilClip = kStencilShowHighlight;
168        } else if (!strcmp(property, "region")) {
169            debugStencilClip = kStencilShowRegion;
170        }
171    } else {
172        debugStencilClip = kStencilHide;
173    }
174
175    if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
176        drawDeferDisabled = !strcasecmp(property, "true");
177        INIT_LOGD("  Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
178    } else {
179        drawDeferDisabled = false;
180        INIT_LOGD("  Draw defer enabled");
181    }
182
183    if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
184        drawReorderDisabled = !strcasecmp(property, "true");
185        INIT_LOGD("  Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
186    } else {
187        drawReorderDisabled = false;
188        INIT_LOGD("  Draw reorder enabled");
189    }
190
191    return (prevDebugLayersUpdates != debugLayersUpdates) ||
192            (prevDebugOverdraw != debugOverdraw) ||
193            (prevDebugStencilClip != debugStencilClip);
194}
195
196void Caches::terminate() {
197    if (!mInitialized) return;
198    mRegionMesh.release();
199
200    fboCache.clear();
201
202    programCache.clear();
203    currentProgram = nullptr;
204
205    patchCache.clear();
206
207    clearGarbage();
208
209    delete mPixelBufferState;
210    mPixelBufferState = nullptr;
211    delete mTextureState;
212    mTextureState = nullptr;
213    mInitialized = false;
214}
215
216///////////////////////////////////////////////////////////////////////////////
217// Debug
218///////////////////////////////////////////////////////////////////////////////
219
220uint32_t Caches::getOverdrawColor(uint32_t amount) const {
221    static uint32_t sOverdrawColors[2][4] = {
222            { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
223            { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
224    };
225    if (amount < 1) amount = 1;
226    if (amount > 4) amount = 4;
227    return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
228}
229
230void Caches::dumpMemoryUsage() {
231    String8 stringLog;
232    dumpMemoryUsage(stringLog);
233    ALOGD("%s", stringLog.string());
234}
235
236void Caches::dumpMemoryUsage(String8 &log) {
237    uint32_t total = 0;
238    log.appendFormat("Current memory usage / total memory usage (bytes):\n");
239    log.appendFormat("  TextureCache         %8d / %8d\n",
240            textureCache.getSize(), textureCache.getMaxSize());
241    log.appendFormat("  LayerCache           %8d / %8d (numLayers = %zu)\n",
242            layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
243    if (mRenderState) {
244        int memused = 0;
245        for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
246                it != mRenderState->mActiveLayers.end(); it++) {
247            const Layer* layer = *it;
248            log.appendFormat("    Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
249                    layer->getWidth(), layer->getHeight(),
250                    layer->isTextureLayer(), layer->getTexture(),
251                    layer->getFbo(), layer->getStrongCount());
252            memused += layer->getWidth() * layer->getHeight() * 4;
253        }
254        log.appendFormat("  Layers total   %8d (numLayers = %zu)\n",
255                memused, mRenderState->mActiveLayers.size());
256        total += memused;
257    }
258    log.appendFormat("  RenderBufferCache    %8d / %8d\n",
259            renderBufferCache.getSize(), renderBufferCache.getMaxSize());
260    log.appendFormat("  GradientCache        %8d / %8d\n",
261            gradientCache.getSize(), gradientCache.getMaxSize());
262    log.appendFormat("  PathCache            %8d / %8d\n",
263            pathCache.getSize(), pathCache.getMaxSize());
264    log.appendFormat("  TessellationCache    %8d / %8d\n",
265            tessellationCache.getSize(), tessellationCache.getMaxSize());
266    log.appendFormat("  TextDropShadowCache  %8d / %8d\n", dropShadowCache.getSize(),
267            dropShadowCache.getMaxSize());
268    log.appendFormat("  PatchCache           %8d / %8d\n",
269            patchCache.getSize(), patchCache.getMaxSize());
270    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
271        const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
272        const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
273        log.appendFormat("  FontRenderer %d A8    %8d / %8d\n", i, sizeA8, sizeA8);
274        log.appendFormat("  FontRenderer %d RGBA  %8d / %8d\n", i, sizeRGBA, sizeRGBA);
275        log.appendFormat("  FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
276                sizeA8 + sizeRGBA);
277    }
278    log.appendFormat("Other:\n");
279    log.appendFormat("  FboCache             %8d / %8d\n",
280            fboCache.getSize(), fboCache.getMaxSize());
281
282    total += textureCache.getSize();
283    total += renderBufferCache.getSize();
284    total += gradientCache.getSize();
285    total += pathCache.getSize();
286    total += tessellationCache.getSize();
287    total += dropShadowCache.getSize();
288    total += patchCache.getSize();
289    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
290        total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
291        total += fontRenderer->getFontRendererSize(i, GL_RGBA);
292    }
293
294    log.appendFormat("Total memory usage:\n");
295    log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
296}
297
298///////////////////////////////////////////////////////////////////////////////
299// Memory management
300///////////////////////////////////////////////////////////////////////////////
301
302void Caches::clearGarbage() {
303    textureCache.clearGarbage();
304    pathCache.clearGarbage();
305    patchCache.clearGarbage();
306}
307
308void Caches::flush(FlushMode mode) {
309    FLUSH_LOGD("Flushing caches (mode %d)", mode);
310
311    // We must stop tasks before clearing caches
312    if (mode > kFlushMode_Layers) {
313        tasks.stop();
314    }
315
316    switch (mode) {
317        case kFlushMode_Full:
318            textureCache.clear();
319            patchCache.clear();
320            dropShadowCache.clear();
321            gradientCache.clear();
322            fontRenderer->clear();
323            fboCache.clear();
324            dither.clear();
325            // fall through
326        case kFlushMode_Moderate:
327            fontRenderer->flush();
328            textureCache.flush();
329            pathCache.clear();
330            tessellationCache.clear();
331            // fall through
332        case kFlushMode_Layers:
333            layerCache.clear();
334            renderBufferCache.clear();
335            break;
336    }
337
338    clearGarbage();
339    glFinish();
340}
341
342///////////////////////////////////////////////////////////////////////////////
343// Tiling
344///////////////////////////////////////////////////////////////////////////////
345
346void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
347    if (mExtensions.hasTiledRendering() && !debugOverdraw) {
348        glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
349    }
350}
351
352void Caches::endTiling() {
353    if (mExtensions.hasTiledRendering() && !debugOverdraw) {
354        glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
355    }
356}
357
358bool Caches::hasRegisteredFunctors() {
359    return mFunctorsCount > 0;
360}
361
362void Caches::registerFunctors(uint32_t functorCount) {
363    mFunctorsCount += functorCount;
364}
365
366void Caches::unregisterFunctors(uint32_t functorCount) {
367    if (functorCount > mFunctorsCount) {
368        mFunctorsCount = 0;
369    } else {
370        mFunctorsCount -= functorCount;
371    }
372}
373
374///////////////////////////////////////////////////////////////////////////////
375// Regions
376///////////////////////////////////////////////////////////////////////////////
377
378TextureVertex* Caches::getRegionMesh() {
379    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
380    if (!mRegionMesh) {
381        mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
382    }
383
384    return mRegionMesh.get();
385}
386
387///////////////////////////////////////////////////////////////////////////////
388// Temporary Properties
389///////////////////////////////////////////////////////////////////////////////
390
391void Caches::initTempProperties() {
392    propertyLightDiameter = -1.0f;
393    propertyLightPosY = -1.0f;
394    propertyLightPosZ = -1.0f;
395    propertyAmbientRatio = -1.0f;
396    propertyAmbientShadowStrength = -1;
397    propertySpotShadowStrength = -1;
398}
399
400void Caches::setTempProperty(const char* name, const char* value) {
401    ALOGD("setting property %s to %s", name, value);
402    if (!strcmp(name, "ambientRatio")) {
403        propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
404        ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
405        return;
406    } else if (!strcmp(name, "lightDiameter")) {
407        propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
408        ALOGD("lightDiameter = %.2f", propertyLightDiameter);
409        return;
410    } else if (!strcmp(name, "lightPosY")) {
411        propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
412        ALOGD("lightPos Y = %.2f", propertyLightPosY);
413        return;
414    } else if (!strcmp(name, "lightPosZ")) {
415        propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
416        ALOGD("lightPos Z = %.2f", propertyLightPosZ);
417        return;
418    } else if (!strcmp(name, "ambientShadowStrength")) {
419        propertyAmbientShadowStrength = atoi(value);
420        ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
421        return;
422    } else if (!strcmp(name, "spotShadowStrength")) {
423        propertySpotShadowStrength = atoi(value);
424        ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
425        return;
426    }
427    ALOGD("    failed");
428}
429
430}; // namespace uirenderer
431}; // namespace android
432