1dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase/*
2dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * Copyright (C) 2010 The Android Open Source Project
3dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase *
4dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * you may not use this file except in compliance with the License.
6dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * You may obtain a copy of the License at
7dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase *
8dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase *
10dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * Unless required by applicable law or agreed to in writing, software
11dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * See the License for the specific language governing permissions and
14dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase * limitations under the License.
15dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase */
16dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase
17dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase#include "Caches.h"
1865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
192dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "GammaFontRenderer.h"
2009b7c91de73b59aa3f679b3ae3ba299f82ec9f8aRomain Guy#include "LayerRenderer.h"
2165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include "Properties.h"
2265fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include "renderstate/RenderState.h"
2363d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui#include "ShadowTessellator.h"
24d7db4d767246b41d44995acb93d03d220b53c748John Reck#include "utils/GLUtils.h"
2565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
266b50780363d3bb8db600c770183fa07677509ae8John Reck#include <cutils/properties.h>
2765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <utils/Log.h>
2865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <utils/String8.h>
29dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase
30dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haasenamespace android {
31dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haasenamespace uirenderer {
32dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase
3396a5c4c7bab6718524de7253da8309143ab48befChris CraikCaches* Caches::sInstance = nullptr;
3496a5c4c7bab6718524de7253da8309143ab48befChris Craik
35dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase///////////////////////////////////////////////////////////////////////////////
36bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy// Macros
37bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy///////////////////////////////////////////////////////////////////////////////
38bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy
39bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy#if DEBUG_CACHE_FLUSH
405baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
41bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy#else
42bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy    #define FLUSH_LOGD(...)
43bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy#endif
44bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy
45bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy///////////////////////////////////////////////////////////////////////////////
46dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase// Constructors/destructor
47dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase///////////////////////////////////////////////////////////////////////////////
48dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase
4996a5c4c7bab6718524de7253da8309143ab48befChris CraikCaches::Caches(RenderState& renderState)
50117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik        : gradientCache(mExtensions)
51117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik        , patchCache(renderState)
52117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik        , programCache(mExtensions)
5344eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik        , dither(*this)
5496a5c4c7bab6718524de7253da8309143ab48befChris Craik        , mRenderState(&renderState)
5596a5c4c7bab6718524de7253da8309143ab48befChris Craik        , mInitialized(false) {
5696a5c4c7bab6718524de7253da8309143ab48befChris Craik    INIT_LOGD("Creating OpenGL renderer caches");
578ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    init();
58dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy    initConstraints();
59f9f0016b1ff816eb2c7561eed482c056189005f8Romain Guy    initStaticProperties();
600f6675332c04c74909425d1d328f02b32c0ff40eRomain Guy    initExtensions();
61dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase}
62dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase
633b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guybool Caches::init() {
643b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    if (mInitialized) return false;
658ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
66fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck    ATRACE_NAME("Caches::init");
67fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck
68d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    mRegionMesh = nullptr;
696c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    mProgram = nullptr;
708ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
7196a5c4c7bab6718524de7253da8309143ab48befChris Craik    patchCache.init();
723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
738ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    mInitialized = true;
743b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
7544eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mPixelBufferState = new PixelBufferState();
7644eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mTextureState = new TextureState();
77138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik    mTextureState->constructTexture(*this);
788aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
793b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    return true;
808ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy}
818ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
82dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guyvoid Caches::initExtensions() {
833bbacf27c0be1bae4e4483577fc89ae3113abe5dRomain Guy    if (mExtensions.hasDebugMarker()) {
84dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy        eventMark = glInsertEventMarkerEXT;
850f6675332c04c74909425d1d328f02b32c0ff40eRomain Guy
86ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        startMark = glPushGroupMarkerEXT;
87ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        endMark = glPopGroupMarkerEXT;
88dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy    } else {
89dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy        eventMark = eventMarkNull;
90dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy        startMark = startMarkNull;
91dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy        endMark = endMarkNull;
92dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy    }
93dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy}
94dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy
95dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guyvoid Caches::initConstraints() {
96dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
97dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy}
98dfa1046ecab165eb0daf5caa8475e993350f4a2bRomain Guy
99f9f0016b1ff816eb2c7561eed482c056189005f8Romain Guyvoid Caches::initStaticProperties() {
100f9f0016b1ff816eb2c7561eed482c056189005f8Romain Guy    // OpenGL ES 3.0+ specific features
101c08820f587ad94698691a6657e87712de07e484cChris Craik    gpuPixelBuffersEnabled = mExtensions.hasPixelBufferObjects()
102c08820f587ad94698691a6657e87712de07e484cChris Craik            && property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
103f9f0016b1ff816eb2c7561eed482c056189005f8Romain Guy}
104f9f0016b1ff816eb2c7561eed482c056189005f8Romain Guy
1058ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guyvoid Caches::terminate() {
1068ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    if (!mInitialized) return;
107441cc421b51377ec27bf97ed690c63aa5509ae90Sangkyu Lee    mRegionMesh.reset(nullptr);
1088ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
1098ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    fboCache.clear();
1108ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
1118ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    programCache.clear();
11234725687748cc2b4ace2bdb49becfdcd569e9a5dChris Craik    mProgram = nullptr;
1138ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy
1143b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    patchCache.clear();
1153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
11646bfc4811094e5b1e3196246e457d4c6b58332ecRomain Guy    clearGarbage();
11746bfc4811094e5b1e3196246e457d4c6b58332ecRomain Guy
11844eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    delete mPixelBufferState;
11944eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mPixelBufferState = nullptr;
12044eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    delete mTextureState;
12144eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mTextureState = nullptr;
1228ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy    mInitialized = false;
1235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
1245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
1256c15ffa196fc9b7724c189d833c3435d8db12266Chris Craikvoid Caches::setProgram(const ProgramDescription& description) {
1266c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    setProgram(programCache.get(description));
1276c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik}
1286c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
1296c15ffa196fc9b7724c189d833c3435d8db12266Chris Craikvoid Caches::setProgram(Program* program) {
1306c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    if (!program || !program->isInUse()) {
1316c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik        if (mProgram) {
1326c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik            mProgram->remove();
1336c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik        }
1346c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik        if (program) {
1356c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik            program->use();
1366c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik        }
1376c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik        mProgram = program;
1386c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    }
1396c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik}
1406c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
1415b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy///////////////////////////////////////////////////////////////////////////////
142c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy// Debug
143c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy///////////////////////////////////////////////////////////////////////////////
144c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
145627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guyuint32_t Caches::getOverdrawColor(uint32_t amount) const {
146627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy    static uint32_t sOverdrawColors[2][4] = {
147627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy            { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
148627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy            { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
149627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy    };
150627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy    if (amount < 1) amount = 1;
151627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy    if (amount > 4) amount = 4;
1522507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik
1532507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
1542507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    return sOverdrawColors[overdrawColorIndex][amount - 1];
155627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy}
156627c6fd91377ead85f74a365438e25610ef1e2eeRomain Guy
157c15008e72ec00ca20a271c3006dac649fd07533bRomain Guyvoid Caches::dumpMemoryUsage() {
1589c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    String8 stringLog;
1599c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    dumpMemoryUsage(stringLog);
1605baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("%s", stringLog.string());
1619c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase}
1629c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase
1639c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haasevoid Caches::dumpMemoryUsage(String8 &log) {
1640e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    uint32_t total = 0;
1659c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("Current memory usage / total memory usage (bytes):\n");
1669c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("  TextureCache         %8d / %8d\n",
1679c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase            textureCache.getSize(), textureCache.getMaxSize());
16817035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    log.appendFormat("  LayerCache           %8d / %8d (numLayers = %zu)\n",
16917035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck            layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
1700e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    if (mRenderState) {
1710e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        int memused = 0;
17257998017ff137f7d4ec33df21b6596141f8c4547John Reck        for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
1730e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck                it != mRenderState->mActiveLayers.end(); it++) {
1740e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck            const Layer* layer = *it;
1750e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck            log.appendFormat("    Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
1760e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck                    layer->getWidth(), layer->getHeight(),
177f27133df2d179c99d6bc1ae644af09e9153a0071Chris Craik                    layer->isTextureLayer(), layer->getTextureId(),
1780e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck                    layer->getFbo(), layer->getStrongCount());
17988f5fc7c5a9fb1e677c508165f732cd76f5eef3aJohn Reck            memused += layer->getWidth() * layer->getHeight() * 4;
1800e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        }
1810e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        log.appendFormat("  Layers total   %8d (numLayers = %zu)\n",
1820e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck                memused, mRenderState->mActiveLayers.size());
1830e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        total += memused;
1840e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    }
1858d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy    log.appendFormat("  RenderBufferCache    %8d / %8d\n",
1868d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy            renderBufferCache.getSize(), renderBufferCache.getMaxSize());
1879c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("  GradientCache        %8d / %8d\n",
1889c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase            gradientCache.getSize(), gradientCache.getMaxSize());
1899c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("  PathCache            %8d / %8d\n",
1909c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase            pathCache.getSize(), pathCache.getMaxSize());
19105f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik    log.appendFormat("  TessellationCache    %8d / %8d\n",
19205f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik            tessellationCache.getSize(), tessellationCache.getMaxSize());
1939c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("  TextDropShadowCache  %8d / %8d\n", dropShadowCache.getSize(),
194c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy            dropShadowCache.getMaxSize());
1953b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    log.appendFormat("  PatchCache           %8d / %8d\n",
1963b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy            patchCache.getSize(), patchCache.getMaxSize());
197c08820f587ad94698691a6657e87712de07e484cChris Craik
198c08820f587ad94698691a6657e87712de07e484cChris Craik    const uint32_t sizeA8 = fontRenderer.getFontRendererSize(GL_ALPHA);
199c08820f587ad94698691a6657e87712de07e484cChris Craik    const uint32_t sizeRGBA = fontRenderer.getFontRendererSize(GL_RGBA);
200c08820f587ad94698691a6657e87712de07e484cChris Craik    log.appendFormat("  FontRenderer A8    %8d / %8d\n", sizeA8, sizeA8);
201c08820f587ad94698691a6657e87712de07e484cChris Craik    log.appendFormat("  FontRenderer RGBA  %8d / %8d\n", sizeRGBA, sizeRGBA);
202c08820f587ad94698691a6657e87712de07e484cChris Craik    log.appendFormat("  FontRenderer total %8d / %8d\n", sizeA8 + sizeRGBA,
203c08820f587ad94698691a6657e87712de07e484cChris Craik            sizeA8 + sizeRGBA);
204c08820f587ad94698691a6657e87712de07e484cChris Craik
205d2ba50ab861ec0950bda18dd0f463b687e66249fRomain Guy    log.appendFormat("Other:\n");
2069c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("  FboCache             %8d / %8d\n",
2079c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase            fboCache.getSize(), fboCache.getMaxSize());
208c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
209c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    total += textureCache.getSize();
2108d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy    total += renderBufferCache.getSize();
211c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    total += gradientCache.getSize();
212c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    total += pathCache.getSize();
21305f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik    total += tessellationCache.getSize();
214c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    total += dropShadowCache.getSize();
2153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    total += patchCache.getSize();
216c08820f587ad94698691a6657e87712de07e484cChris Craik    total += fontRenderer.getFontRendererSize(GL_ALPHA);
217c08820f587ad94698691a6657e87712de07e484cChris Craik    total += fontRenderer.getFontRendererSize(GL_RGBA);
218c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
2199c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("Total memory usage:\n");
2209c1e23baf5bfbebd1aebbd6d9a18c225325567ceChet Haase    log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
221c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy}
222c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
223c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy///////////////////////////////////////////////////////////////////////////////
224fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy// Memory management
225fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy///////////////////////////////////////////////////////////////////////////////
226fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
227fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guyvoid Caches::clearGarbage() {
228fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    textureCache.clearGarbage();
229fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    pathCache.clearGarbage();
230e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    patchCache.clearGarbage();
231fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy}
232fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
233bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guyvoid Caches::flush(FlushMode mode) {
234bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy    FLUSH_LOGD("Flushing caches (mode %d)", mode);
235bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy
236bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy    switch (mode) {
237b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik        case FlushMode::Full:
238bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            textureCache.clear();
239bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            patchCache.clear();
240bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            dropShadowCache.clear();
241bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            gradientCache.clear();
242c08820f587ad94698691a6657e87712de07e484cChris Craik            fontRenderer.clear();
243b746371de7f21ae36a14953d9b253df06838efb1Romain Guy            fboCache.clear();
244211efea7376371ee755edd2ad03e83ef6eea464eRomain Guy            dither.clear();
245bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            // fall through
246b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik        case FlushMode::Moderate:
247c08820f587ad94698691a6657e87712de07e484cChris Craik            fontRenderer.flush();
248eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy            textureCache.flush();
249bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            pathCache.clear();
25005f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik            tessellationCache.clear();
2516d7475d666baefaa3ba9f0dcee25238739454241Romain Guy            // fall through
252b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik        case FlushMode::Layers:
2536d7475d666baefaa3ba9f0dcee25238739454241Romain Guy            layerCache.clear();
2548d4aeb7111afac0c3c7e56d4ad5d92f9cfce2ffdRomain Guy            renderBufferCache.clear();
255bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy            break;
256bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy    }
2576a2d17f71342f981c9df1dc5beff33e30eb3ae2bChet Haase
2586a2d17f71342f981c9df1dc5beff33e30eb3ae2bChet Haase    clearGarbage();
2594ced2620b730a7c7140f73da503946f9827442bcJohn Reck    glFinish();
260d7db4d767246b41d44995acb93d03d220b53c748John Reck    // Errors during cleanup should be considered non-fatal, dump them and
261d7db4d767246b41d44995acb93d03d220b53c748John Reck    // and move on. TODO: All errors or just errors like bad surface?
262d7db4d767246b41d44995acb93d03d220b53c748John Reck    GLUtils::dumpGLErrors();
263bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy}
264bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy
265fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy///////////////////////////////////////////////////////////////////////////////
26685ef80d8902afe8d26cb75fa7837fd9e6d019620Romain Guy// Regions
26785ef80d8902afe8d26cb75fa7837fd9e6d019620Romain Guy///////////////////////////////////////////////////////////////////////////////
26885ef80d8902afe8d26cb75fa7837fd9e6d019620Romain Guy
2695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain GuyTextureVertex* Caches::getRegionMesh() {
2705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
2715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!mRegionMesh) {
27296a5c4c7bab6718524de7253da8309143ab48befChris Craik        mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
2735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
2745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
27551d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    return mRegionMesh.get();
2765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
2775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
278ba9b613437c34873fa95800a25fc51720638267cChris Craik///////////////////////////////////////////////////////////////////////////////
279ba9b613437c34873fa95800a25fc51720638267cChris Craik// Temporary Properties
280ba9b613437c34873fa95800a25fc51720638267cChris Craik///////////////////////////////////////////////////////////////////////////////
281ba9b613437c34873fa95800a25fc51720638267cChris Craik
282dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase}; // namespace uirenderer
283dd78ccacff83f0714ad90be98b58ad85f5d08c66Chet Haase}; // namespace android
284