blob: a8ced9b2597b0cee0a3a0fffeeca975f0e5d658c [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"
Romain Guy09b7c912011-02-02 20:28:09 -080020#include "LayerRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "Properties.h"
22#include "renderstate/RenderState.h"
ztenghui63d41ab2014-02-14 13:13:41 -080023#include "ShadowTessellator.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
Steve Block5baa3a62011-12-20 16:23:08 +000043 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070044#else
45 #define FLUSH_LOGD(...)
46#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)
Chris Craik117bdbc2015-02-05 10:12:38 -080053 : gradientCache(mExtensions)
54 , patchCache(renderState)
55 , programCache(mExtensions)
Chris Craik44eb2c02015-01-29 09:45:09 -080056 , dither(*this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080057 , mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080058 , mInitialized(false) {
59 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080060 init();
Romain Guydfa10462012-05-12 16:18:58 -070061 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070062 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080063 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070064}
65
Romain Guy3b748a42013-04-17 18:54:38 -070066bool Caches::init() {
67 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080068
John Reckfbc8df02014-11-14 16:18:41 -080069 ATRACE_NAME("Caches::init");
70
Chris Craikd41c4d82015-01-05 15:51:13 -080071 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080072 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080073
Chris Craik96a5c4c2015-01-27 15:46:35 -080074 patchCache.init();
Romain Guy3b748a42013-04-17 18:54:38 -070075
Romain Guy8ff6b9e2011-11-09 20:10:18 -080076 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070077
Chris Craik44eb2c02015-01-29 09:45:09 -080078 mPixelBufferState = new PixelBufferState();
79 mTextureState = new TextureState();
Chris Craik138c21f2016-04-28 16:59:42 -070080 mTextureState->constructTexture(*this);
Romain Guy8aa195d2013-06-04 18:00:09 -070081
Romain Guy3b748a42013-04-17 18:54:38 -070082 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080083}
84
Romain Guydfa10462012-05-12 16:18:58 -070085void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -080086 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070087 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -080088
Chris Craikff785832013-03-08 13:12:16 -080089 startMark = glPushGroupMarkerEXT;
90 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070091 } else {
92 eventMark = eventMarkNull;
93 startMark = startMarkNull;
94 endMark = endMarkNull;
95 }
Romain Guydfa10462012-05-12 16:18:58 -070096}
97
98void Caches::initConstraints() {
Romain Guydfa10462012-05-12 16:18:58 -070099 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
100}
101
Romain Guyf9f00162013-05-09 11:50:12 -0700102void Caches::initStaticProperties() {
Romain Guyf9f00162013-05-09 11:50:12 -0700103 // OpenGL ES 3.0+ specific features
Chris Craikc08820f2015-09-22 14:22:29 -0700104 gpuPixelBuffersEnabled = mExtensions.hasPixelBufferObjects()
105 && property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
Romain Guyf9f00162013-05-09 11:50:12 -0700106}
107
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800108void Caches::terminate() {
109 if (!mInitialized) return;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900110 mRegionMesh.reset(nullptr);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800111
112 fboCache.clear();
113
114 programCache.clear();
Chris Craik34725682015-02-05 09:06:30 -0800115 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800116
Romain Guy3b748a42013-04-17 18:54:38 -0700117 patchCache.clear();
118
Romain Guy46bfc482013-08-16 18:38:29 -0700119 clearGarbage();
120
Chris Craik44eb2c02015-01-29 09:45:09 -0800121 delete mPixelBufferState;
122 mPixelBufferState = nullptr;
123 delete mTextureState;
124 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800125 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700126}
127
Chris Craik6c15ffa2015-02-02 13:50:55 -0800128void Caches::setProgram(const ProgramDescription& description) {
129 setProgram(programCache.get(description));
130}
131
132void Caches::setProgram(Program* program) {
133 if (!program || !program->isInUse()) {
134 if (mProgram) {
135 mProgram->remove();
136 }
137 if (program) {
138 program->use();
139 }
140 mProgram = program;
141 }
142}
143
Romain Guy5b3b3522010-10-27 18:57:51 -0700144///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800145// Debug
146///////////////////////////////////////////////////////////////////////////////
147
Romain Guy627c6fd2013-08-21 11:53:18 -0700148uint32_t Caches::getOverdrawColor(uint32_t amount) const {
149 static uint32_t sOverdrawColors[2][4] = {
150 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
151 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
152 };
153 if (amount < 1) amount = 1;
154 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700155
156 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
157 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700158}
159
Romain Guyc15008e2010-11-10 11:59:15 -0800160void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700161 String8 stringLog;
162 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000163 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700164}
165
166void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700167 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700168 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
169 log.appendFormat(" TextureCache %8d / %8d\n",
170 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700171 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
172 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700173 if (mRenderState) {
174 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800175 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck0e89e2b2014-10-31 14:49:06 -0700176 it != mRenderState->mActiveLayers.end(); it++) {
177 const Layer* layer = *it;
178 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
179 layer->getWidth(), layer->getHeight(),
Chris Craikf27133d2015-02-19 09:51:53 -0800180 layer->isTextureLayer(), layer->getTextureId(),
John Reck0e89e2b2014-10-31 14:49:06 -0700181 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800182 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700183 }
184 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
185 memused, mRenderState->mActiveLayers.size());
186 total += memused;
187 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800188 log.appendFormat(" RenderBufferCache %8d / %8d\n",
189 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700190 log.appendFormat(" GradientCache %8d / %8d\n",
191 gradientCache.getSize(), gradientCache.getMaxSize());
192 log.appendFormat(" PathCache %8d / %8d\n",
193 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700194 log.appendFormat(" TessellationCache %8d / %8d\n",
195 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700196 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800197 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700198 log.appendFormat(" PatchCache %8d / %8d\n",
199 patchCache.getSize(), patchCache.getMaxSize());
Chris Craikc08820f2015-09-22 14:22:29 -0700200
sergeyvbaf29e72016-09-08 11:09:34 -0700201 fontRenderer.dumpMemoryUsage(log);
Chris Craikc08820f2015-09-22 14:22:29 -0700202
Romain Guyd2ba50a2011-05-27 10:21:07 -0700203 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700204 log.appendFormat(" FboCache %8d / %8d\n",
205 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800206
Romain Guyc15008e2010-11-10 11:59:15 -0800207 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800208 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800209 total += gradientCache.getSize();
210 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700211 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800212 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700213 total += patchCache.getSize();
sergeyvbaf29e72016-09-08 11:09:34 -0700214 total += fontRenderer.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800215
Chet Haase9c1e23b2011-03-24 10:51:31 -0700216 log.appendFormat("Total memory usage:\n");
217 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
sergeyvaf102be2016-09-09 18:02:07 -0700218
219#ifdef BUGREPORT_FONT_CACHE_USAGE
220 fontRenderer.getFontRenderer().historyTracker().dump(log);
221#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800222}
223
224///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800225// Memory management
226///////////////////////////////////////////////////////////////////////////////
227
228void Caches::clearGarbage() {
229 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800230 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700231 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800232}
233
Romain Guybdf76092011-07-18 15:00:43 -0700234void Caches::flush(FlushMode mode) {
235 FLUSH_LOGD("Flushing caches (mode %d)", mode);
236
Romain Guybdf76092011-07-18 15:00:43 -0700237 switch (mode) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700238 case FlushMode::Full:
Romain Guybdf76092011-07-18 15:00:43 -0700239 textureCache.clear();
240 patchCache.clear();
241 dropShadowCache.clear();
242 gradientCache.clear();
Chris Craikc08820f2015-09-22 14:22:29 -0700243 fontRenderer.clear();
Romain Guyb7463712013-08-16 13:55:29 -0700244 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700245 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700246 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700247 case FlushMode::Moderate:
Chris Craikc08820f2015-09-22 14:22:29 -0700248 fontRenderer.flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700249 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700250 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700251 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700252 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700253 case FlushMode::Layers:
Romain Guy6d7475d2011-07-27 16:28:21 -0700254 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800255 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700256 break;
257 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700258
259 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700260 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700261 // Errors during cleanup should be considered non-fatal, dump them and
262 // and move on. TODO: All errors or just errors like bad surface?
263 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700264}
265
Romain Guyfe48f652010-11-11 15:36:56 -0800266///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700267// Regions
268///////////////////////////////////////////////////////////////////////////////
269
Romain Guy5b3b3522010-10-27 18:57:51 -0700270TextureVertex* Caches::getRegionMesh() {
271 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
272 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800273 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700274 }
275
Chris Craik51d6a3d2014-12-22 17:16:56 -0800276 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700277}
278
Chris Craikba9b6132013-12-15 17:10:19 -0800279///////////////////////////////////////////////////////////////////////////////
280// Temporary Properties
281///////////////////////////////////////////////////////////////////////////////
282
Chet Haasedd78cca2010-10-22 18:59:26 -0700283}; // namespace uirenderer
284}; // namespace android