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 #include "Caches.h"
18
19 #include "GammaFontRenderer.h"
20 #include "GlLayer.h"
21 #include "Properties.h"
22 #include "renderstate/RenderState.h"
23 #include "ShadowTessellator.h"
24 #ifdef BUGREPORT_FONT_CACHE_USAGE
25 #include "font/FontCacheHistoryTracker.h"
26 #endif
27 #include "utils/GLUtils.h"
28
29 #include <cutils/properties.h>
30 #include <utils/Log.h>
31 #include <utils/String8.h>
32
33 namespace android {
34 namespace uirenderer {
35
36 Caches* Caches::sInstance = nullptr;
37
38 ///////////////////////////////////////////////////////////////////////////////
39 // Macros
40 ///////////////////////////////////////////////////////////////////////////////
41
42 #if DEBUG_CACHE_FLUSH
43 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
44 #else
45 #define FLUSH_LOGD(...)
46 #endif
47
48 ///////////////////////////////////////////////////////////////////////////////
49 // Constructors/destructor
50 ///////////////////////////////////////////////////////////////////////////////
51
Caches(RenderState & renderState)52 Caches::Caches(RenderState& renderState)
53 : gradientCache(mExtensions)
54 , patchCache(renderState)
55 , programCache(mExtensions)
56 , mRenderState(&renderState)
57 , mInitialized(false) {
58 INIT_LOGD("Creating OpenGL renderer caches");
59 init();
60 initConstraints();
61 initStaticProperties();
62 initExtensions();
63 }
64
init()65 bool Caches::init() {
66 if (mInitialized) return false;
67
68 ATRACE_NAME("Caches::init");
69
70 mRegionMesh = nullptr;
71 mProgram = nullptr;
72
73 mInitialized = true;
74
75 mPixelBufferState = new PixelBufferState();
76 mTextureState = new TextureState();
77 mTextureState->constructTexture(*this);
78
79 return true;
80 }
81
initExtensions()82 void Caches::initExtensions() {
83 if (mExtensions.hasDebugMarker()) {
84 eventMark = glInsertEventMarkerEXT;
85
86 startMark = glPushGroupMarkerEXT;
87 endMark = glPopGroupMarkerEXT;
88 } else {
89 eventMark = eventMarkNull;
90 startMark = startMarkNull;
91 endMark = endMarkNull;
92 }
93 }
94
initConstraints()95 void Caches::initConstraints() {
96 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
97 }
98
initStaticProperties()99 void Caches::initStaticProperties() {
100 // OpenGL ES 3.0+ specific features
101 gpuPixelBuffersEnabled = mExtensions.hasPixelBufferObjects()
102 && property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
103 }
104
terminate()105 void Caches::terminate() {
106 if (!mInitialized) return;
107 mRegionMesh.reset(nullptr);
108
109 fboCache.clear();
110
111 programCache.clear();
112 mProgram = nullptr;
113
114 patchCache.clear();
115
116 clearGarbage();
117
118 delete mPixelBufferState;
119 mPixelBufferState = nullptr;
120 delete mTextureState;
121 mTextureState = nullptr;
122 mInitialized = false;
123 }
124
setProgram(const ProgramDescription & description)125 void Caches::setProgram(const ProgramDescription& description) {
126 setProgram(programCache.get(description));
127 }
128
setProgram(Program * program)129 void Caches::setProgram(Program* program) {
130 if (!program || !program->isInUse()) {
131 if (mProgram) {
132 mProgram->remove();
133 }
134 if (program) {
135 program->use();
136 }
137 mProgram = program;
138 }
139 }
140
141 ///////////////////////////////////////////////////////////////////////////////
142 // Debug
143 ///////////////////////////////////////////////////////////////////////////////
144
getOverdrawColor(uint32_t amount) const145 uint32_t Caches::getOverdrawColor(uint32_t amount) const {
146 static uint32_t sOverdrawColors[2][4] = {
147 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
148 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
149 };
150 if (amount < 1) amount = 1;
151 if (amount > 4) amount = 4;
152
153 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
154 return sOverdrawColors[overdrawColorIndex][amount - 1];
155 }
156
dumpMemoryUsage()157 void Caches::dumpMemoryUsage() {
158 String8 stringLog;
159 dumpMemoryUsage(stringLog);
160 ALOGD("%s", stringLog.string());
161 }
162
dumpMemoryUsage(String8 & log)163 void Caches::dumpMemoryUsage(String8 &log) {
164 uint32_t total = 0;
165 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
166 log.appendFormat(" TextureCache %8d / %8d\n",
167 textureCache.getSize(), textureCache.getMaxSize());
168 if (mRenderState) {
169 int memused = 0;
170 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
171 it != mRenderState->mActiveLayers.end(); it++) {
172 const Layer* layer = *it;
173 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
174 const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
175 log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n",
176 layer->getWidth(), layer->getHeight(),
177 glLayer->getTextureId(),
178 layer->getStrongCount());
179 memused += layer->getWidth() * layer->getHeight() * 4;
180 }
181 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
182 memused, mRenderState->mActiveLayers.size());
183 total += memused;
184 }
185 log.appendFormat(" RenderBufferCache %8d / %8d\n",
186 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
187 log.appendFormat(" GradientCache %8d / %8d\n",
188 gradientCache.getSize(), gradientCache.getMaxSize());
189 log.appendFormat(" PathCache %8d / %8d\n",
190 pathCache.getSize(), pathCache.getMaxSize());
191 log.appendFormat(" TessellationCache %8d / %8d\n",
192 tessellationCache.getSize(), tessellationCache.getMaxSize());
193 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
194 dropShadowCache.getMaxSize());
195 log.appendFormat(" PatchCache %8d / %8d\n",
196 patchCache.getSize(), patchCache.getMaxSize());
197
198 fontRenderer.dumpMemoryUsage(log);
199
200 log.appendFormat("Other:\n");
201 log.appendFormat(" FboCache %8d / %8d\n",
202 fboCache.getSize(), fboCache.getMaxSize());
203
204 total += textureCache.getSize();
205 total += renderBufferCache.getSize();
206 total += gradientCache.getSize();
207 total += pathCache.getSize();
208 total += tessellationCache.getSize();
209 total += dropShadowCache.getSize();
210 total += patchCache.getSize();
211 total += fontRenderer.getSize();
212
213 log.appendFormat("Total memory usage:\n");
214 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
215
216 #ifdef BUGREPORT_FONT_CACHE_USAGE
217 fontRenderer.getFontRenderer().historyTracker().dump(log);
218 #endif
219 }
220
221 ///////////////////////////////////////////////////////////////////////////////
222 // Memory management
223 ///////////////////////////////////////////////////////////////////////////////
224
clearGarbage()225 void Caches::clearGarbage() {
226 pathCache.clearGarbage();
227 patchCache.clearGarbage();
228 }
229
flush(FlushMode mode)230 void Caches::flush(FlushMode mode) {
231 FLUSH_LOGD("Flushing caches (mode %d)", mode);
232
233 switch (mode) {
234 case FlushMode::Full:
235 textureCache.clear();
236 patchCache.clear();
237 dropShadowCache.clear();
238 gradientCache.clear();
239 fontRenderer.clear();
240 fboCache.clear();
241 // fall through
242 case FlushMode::Moderate:
243 fontRenderer.flush();
244 textureCache.flush();
245 pathCache.clear();
246 tessellationCache.clear();
247 // fall through
248 case FlushMode::Layers:
249 renderBufferCache.clear();
250 break;
251 }
252
253 clearGarbage();
254 glFinish();
255 // Errors during cleanup should be considered non-fatal, dump them and
256 // and move on. TODO: All errors or just errors like bad surface?
257 GLUtils::dumpGLErrors();
258 }
259
260 ///////////////////////////////////////////////////////////////////////////////
261 // Regions
262 ///////////////////////////////////////////////////////////////////////////////
263
getRegionMesh()264 TextureVertex* Caches::getRegionMesh() {
265 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
266 if (!mRegionMesh) {
267 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
268 }
269
270 return mRegionMesh.get();
271 }
272
273 ///////////////////////////////////////////////////////////////////////////////
274 // Temporary Properties
275 ///////////////////////////////////////////////////////////////////////////////
276
277 }; // namespace uirenderer
278 }; // namespace android
279