blob: 3c774a3313d21bbc89acb74c3a1884e3e1e53931 [file] [log] [blame]
Chet Haasedd78cca2010-10-22 18:59:26 -07001/*
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
Chet Haasedd78cca2010-10-22 18:59:26 -070017#include "Caches.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080018
Tom Hudson2dc236b2014-10-15 15:46:42 -040019#include "GammaFontRenderer.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050020#include "GlLayer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "Properties.h"
ztenghui63d41ab2014-02-14 13:13:41 -080022#include "ShadowTessellator.h"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "renderstate/RenderState.h"
sergeyvaf102be2016-09-09 18:02:07 -070024#ifdef BUGREPORT_FONT_CACHE_USAGE
25#include "font/FontCacheHistoryTracker.h"
26#endif
John Reckd7db4d72015-05-20 07:18:55 -070027#include "utils/GLUtils.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080028
John Reck6b507802015-11-03 10:09:59 -080029#include <cutils/properties.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080030#include <utils/Log.h>
31#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070032
33namespace android {
Chet Haasedd78cca2010-10-22 18:59:26 -070034namespace uirenderer {
35
Chris Craik96a5c4c2015-01-27 15:46:35 -080036Caches* Caches::sInstance = nullptr;
37
Chet Haasedd78cca2010-10-22 18:59:26 -070038///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070039// Macros
40///////////////////////////////////////////////////////////////////////////////
41
42#if DEBUG_CACHE_FLUSH
John Reck1bcacfd2017-11-03 10:12:19 -070043#define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070044#else
John Reck1bcacfd2017-11-03 10:12:19 -070045#define FLUSH_LOGD(...)
Romain Guybdf76092011-07-18 15:00:43 -070046#endif
47
48///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070049// Constructors/destructor
50///////////////////////////////////////////////////////////////////////////////
51
Chris Craik96a5c4c2015-01-27 15:46:35 -080052Caches::Caches(RenderState& renderState)
John Reck8dc02f92017-07-17 09:55:02 -070053 : gradientCache(extensions())
Chris Craik117bdbc2015-02-05 10:12:38 -080054 , patchCache(renderState)
John Reck8dc02f92017-07-17 09:55:02 -070055 , programCache(extensions())
Chris Craik96a5c4c2015-01-27 15:46:35 -080056 , mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080057 , mInitialized(false) {
58 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080059 init();
Romain Guydfa10462012-05-12 16:18:58 -070060 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070061 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080062 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070063}
64
Romain Guy3b748a42013-04-17 18:54:38 -070065bool Caches::init() {
66 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080067
John Reckfbc8df02014-11-14 16:18:41 -080068 ATRACE_NAME("Caches::init");
69
Chris Craikd41c4d82015-01-05 15:51:13 -080070 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080071 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080072
73 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070074
Chris Craik44eb2c02015-01-29 09:45:09 -080075 mPixelBufferState = new PixelBufferState();
76 mTextureState = new TextureState();
Chris Craik138c21f2016-04-28 16:59:42 -070077 mTextureState->constructTexture(*this);
Romain Guy8aa195d2013-06-04 18:00:09 -070078
Romain Guy3b748a42013-04-17 18:54:38 -070079 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080080}
81
Romain Guydfa10462012-05-12 16:18:58 -070082void Caches::initExtensions() {
John Reck8dc02f92017-07-17 09:55:02 -070083 if (extensions().hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070084 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -080085
Chris Craikff785832013-03-08 13:12:16 -080086 startMark = glPushGroupMarkerEXT;
87 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070088 } else {
89 eventMark = eventMarkNull;
90 startMark = startMarkNull;
91 endMark = endMarkNull;
92 }
Romain Guydfa10462012-05-12 16:18:58 -070093}
94
95void Caches::initConstraints() {
John Reck8dc02f92017-07-17 09:55:02 -070096 maxTextureSize = DeviceInfo::get()->maxTextureSize();
Romain Guydfa10462012-05-12 16:18:58 -070097}
98
Romain Guyf9f00162013-05-09 11:50:12 -070099void Caches::initStaticProperties() {
Romain Guyf9f00162013-05-09 11:50:12 -0700100 // OpenGL ES 3.0+ specific features
John Reck1bcacfd2017-11-03 10:12:19 -0700101 gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
102 property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
Romain Guyf9f00162013-05-09 11:50:12 -0700103}
104
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800105void Caches::terminate() {
106 if (!mInitialized) return;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900107 mRegionMesh.reset(nullptr);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800108
109 fboCache.clear();
110
111 programCache.clear();
Chris Craik34725682015-02-05 09:06:30 -0800112 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800113
Romain Guy3b748a42013-04-17 18:54:38 -0700114 patchCache.clear();
115
Romain Guy46bfc482013-08-16 18:38:29 -0700116 clearGarbage();
117
Chris Craik44eb2c02015-01-29 09:45:09 -0800118 delete mPixelBufferState;
119 mPixelBufferState = nullptr;
120 delete mTextureState;
121 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800122 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700123}
124
Chris Craik6c15ffa2015-02-02 13:50:55 -0800125void Caches::setProgram(const ProgramDescription& description) {
126 setProgram(programCache.get(description));
127}
128
129void 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
Romain Guy5b3b3522010-10-27 18:57:51 -0700141///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800142// Debug
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guy627c6fd2013-08-21 11:53:18 -0700145uint32_t Caches::getOverdrawColor(uint32_t amount) const {
John Reck1bcacfd2017-11-03 10:12:19 -0700146 static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000},
147 {0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}};
Romain Guy627c6fd2013-08-21 11:53:18 -0700148 if (amount < 1) amount = 1;
149 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700150
151 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
152 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700153}
154
Romain Guyc15008e2010-11-10 11:59:15 -0800155void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700156 String8 stringLog;
157 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000158 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700159}
160
John Reck1bcacfd2017-11-03 10:12:19 -0700161void Caches::dumpMemoryUsage(String8& log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700162 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700163 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
John Reck1bcacfd2017-11-03 10:12:19 -0700164 log.appendFormat(" TextureCache %8d / %8d\n", textureCache.getSize(),
165 textureCache.getMaxSize());
John Reck0e89e2b2014-10-31 14:49:06 -0700166 if (mRenderState) {
167 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800168 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700169 it != mRenderState->mActiveLayers.end(); it++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700170 const Layer* layer = *it;
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500171 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
172 const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
John Reck1bcacfd2017-11-03 10:12:19 -0700173 log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(),
174 layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800175 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700176 }
John Reck1bcacfd2017-11-03 10:12:19 -0700177 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused,
178 mRenderState->mActiveLayers.size());
John Reck0e89e2b2014-10-31 14:49:06 -0700179 total += memused;
180 }
John Reck1bcacfd2017-11-03 10:12:19 -0700181 log.appendFormat(" RenderBufferCache %8d / %8d\n", renderBufferCache.getSize(),
182 renderBufferCache.getMaxSize());
183 log.appendFormat(" GradientCache %8d / %8d\n", gradientCache.getSize(),
184 gradientCache.getMaxSize());
185 log.appendFormat(" PathCache %8d / %8d\n", pathCache.getSize(),
186 pathCache.getMaxSize());
187 log.appendFormat(" TessellationCache %8d / %8d\n", tessellationCache.getSize(),
188 tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700189 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
John Reck1bcacfd2017-11-03 10:12:19 -0700190 dropShadowCache.getMaxSize());
191 log.appendFormat(" PatchCache %8d / %8d\n", patchCache.getSize(),
192 patchCache.getMaxSize());
Chris Craikc08820f2015-09-22 14:22:29 -0700193
sergeyvbaf29e72016-09-08 11:09:34 -0700194 fontRenderer.dumpMemoryUsage(log);
Chris Craikc08820f2015-09-22 14:22:29 -0700195
Romain Guyd2ba50a2011-05-27 10:21:07 -0700196 log.appendFormat("Other:\n");
John Reck1bcacfd2017-11-03 10:12:19 -0700197 log.appendFormat(" FboCache %8d / %8d\n", fboCache.getSize(),
198 fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800199
Romain Guyc15008e2010-11-10 11:59:15 -0800200 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800201 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800202 total += gradientCache.getSize();
203 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700204 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800205 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700206 total += patchCache.getSize();
sergeyvbaf29e72016-09-08 11:09:34 -0700207 total += fontRenderer.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800208
Chet Haase9c1e23b2011-03-24 10:51:31 -0700209 log.appendFormat("Total memory usage:\n");
210 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
sergeyvaf102be2016-09-09 18:02:07 -0700211
212#ifdef BUGREPORT_FONT_CACHE_USAGE
213 fontRenderer.getFontRenderer().historyTracker().dump(log);
214#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800215}
216
217///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800218// Memory management
219///////////////////////////////////////////////////////////////////////////////
220
221void Caches::clearGarbage() {
Romain Guyfe48f652010-11-11 15:36:56 -0800222 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700223 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800224}
225
Romain Guybdf76092011-07-18 15:00:43 -0700226void Caches::flush(FlushMode mode) {
227 FLUSH_LOGD("Flushing caches (mode %d)", mode);
228
Romain Guybdf76092011-07-18 15:00:43 -0700229 switch (mode) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700230 case FlushMode::Full:
Romain Guybdf76092011-07-18 15:00:43 -0700231 textureCache.clear();
232 patchCache.clear();
233 dropShadowCache.clear();
234 gradientCache.clear();
Chris Craikc08820f2015-09-22 14:22:29 -0700235 fontRenderer.clear();
Romain Guyb7463712013-08-16 13:55:29 -0700236 fboCache.clear();
John Reck1bcacfd2017-11-03 10:12:19 -0700237 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700238 case FlushMode::Moderate:
Chris Craikc08820f2015-09-22 14:22:29 -0700239 fontRenderer.flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700240 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700241 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700242 tessellationCache.clear();
John Reck1bcacfd2017-11-03 10:12:19 -0700243 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700244 case FlushMode::Layers:
Romain Guy8d4aeb72013-02-12 16:08:55 -0800245 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700246 break;
247 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700248
249 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700250 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700251 // Errors during cleanup should be considered non-fatal, dump them and
252 // and move on. TODO: All errors or just errors like bad surface?
253 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700254}
255
Romain Guyfe48f652010-11-11 15:36:56 -0800256///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700257// Regions
258///////////////////////////////////////////////////////////////////////////////
259
Romain Guy5b3b3522010-10-27 18:57:51 -0700260TextureVertex* Caches::getRegionMesh() {
261 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
262 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800263 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700264 }
265
Chris Craik51d6a3d2014-12-22 17:16:56 -0800266 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700267}
268
Chris Craikba9b6132013-12-15 17:10:19 -0800269///////////////////////////////////////////////////////////////////////////////
270// Temporary Properties
271///////////////////////////////////////////////////////////////////////////////
272
John Reck1bcacfd2017-11-03 10:12:19 -0700273}; // namespace uirenderer
274}; // namespace android