blob: 6aefb3564339252355f88c9ed75b38b76b0e556c [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
Greg Daniel8cd3edf2017-01-09 14:15:41 -050019#include "GlLayer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080020#include "Properties.h"
ztenghui63d41ab2014-02-14 13:13:41 -080021#include "ShadowTessellator.h"
John Reck1bcacfd2017-11-03 10:12:19 -070022#include "renderstate/RenderState.h"
John Reckd7db4d72015-05-20 07:18:55 -070023#include "utils/GLUtils.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024
John Reck6b507802015-11-03 10:09:59 -080025#include <cutils/properties.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080026#include <utils/Log.h>
27#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070028
29namespace android {
Chet Haasedd78cca2010-10-22 18:59:26 -070030namespace uirenderer {
31
Chris Craik96a5c4c2015-01-27 15:46:35 -080032Caches* Caches::sInstance = nullptr;
33
Chet Haasedd78cca2010-10-22 18:59:26 -070034///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070035// Macros
36///////////////////////////////////////////////////////////////////////////////
37
38#if DEBUG_CACHE_FLUSH
John Reck1bcacfd2017-11-03 10:12:19 -070039#define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070040#else
John Reck1bcacfd2017-11-03 10:12:19 -070041#define FLUSH_LOGD(...)
Romain Guybdf76092011-07-18 15:00:43 -070042#endif
43
44///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070045// Constructors/destructor
46///////////////////////////////////////////////////////////////////////////////
47
Chris Craik96a5c4c2015-01-27 15:46:35 -080048Caches::Caches(RenderState& renderState)
John Reck8dc02f92017-07-17 09:55:02 -070049 : gradientCache(extensions())
Chris Craik117bdbc2015-02-05 10:12:38 -080050 , patchCache(renderState)
John Reck8dc02f92017-07-17 09:55:02 -070051 , programCache(extensions())
Chris Craik96a5c4c2015-01-27 15:46:35 -080052 , mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080053 , mInitialized(false) {
54 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080055 init();
Romain Guydfa10462012-05-12 16:18:58 -070056 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070057 initStaticProperties();
Romain Guy0f6675332013-03-01 14:31:04 -080058 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070059}
60
Romain Guy3b748a42013-04-17 18:54:38 -070061bool Caches::init() {
62 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080063
John Reckfbc8df02014-11-14 16:18:41 -080064 ATRACE_NAME("Caches::init");
65
Chris Craikd41c4d82015-01-05 15:51:13 -080066 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080067 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080068
69 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070070
Chris Craik44eb2c02015-01-29 09:45:09 -080071 mPixelBufferState = new PixelBufferState();
72 mTextureState = new TextureState();
Chris Craik138c21f2016-04-28 16:59:42 -070073 mTextureState->constructTexture(*this);
Romain Guy8aa195d2013-06-04 18:00:09 -070074
Romain Guy3b748a42013-04-17 18:54:38 -070075 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080076}
77
Romain Guydfa10462012-05-12 16:18:58 -070078void Caches::initExtensions() {
John Reck8dc02f92017-07-17 09:55:02 -070079 if (extensions().hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070080 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -080081
Chris Craikff785832013-03-08 13:12:16 -080082 startMark = glPushGroupMarkerEXT;
83 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070084 } else {
85 eventMark = eventMarkNull;
86 startMark = startMarkNull;
87 endMark = endMarkNull;
88 }
Romain Guydfa10462012-05-12 16:18:58 -070089}
90
91void Caches::initConstraints() {
John Reck8dc02f92017-07-17 09:55:02 -070092 maxTextureSize = DeviceInfo::get()->maxTextureSize();
Romain Guydfa10462012-05-12 16:18:58 -070093}
94
Romain Guyf9f00162013-05-09 11:50:12 -070095void Caches::initStaticProperties() {
Romain Guyf9f00162013-05-09 11:50:12 -070096 // OpenGL ES 3.0+ specific features
John Reck1bcacfd2017-11-03 10:12:19 -070097 gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
98 property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
Romain Guyf9f00162013-05-09 11:50:12 -070099}
100
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800101void Caches::terminate() {
102 if (!mInitialized) return;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900103 mRegionMesh.reset(nullptr);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800104
105 fboCache.clear();
106
107 programCache.clear();
Chris Craik34725682015-02-05 09:06:30 -0800108 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800109
Romain Guy3b748a42013-04-17 18:54:38 -0700110 patchCache.clear();
111
Romain Guy46bfc482013-08-16 18:38:29 -0700112 clearGarbage();
113
Chris Craik44eb2c02015-01-29 09:45:09 -0800114 delete mPixelBufferState;
115 mPixelBufferState = nullptr;
116 delete mTextureState;
117 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800118 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700119}
120
Chris Craik6c15ffa2015-02-02 13:50:55 -0800121void Caches::setProgram(const ProgramDescription& description) {
122 setProgram(programCache.get(description));
123}
124
125void Caches::setProgram(Program* program) {
126 if (!program || !program->isInUse()) {
127 if (mProgram) {
128 mProgram->remove();
129 }
130 if (program) {
131 program->use();
132 }
133 mProgram = program;
134 }
135}
136
Romain Guy5b3b3522010-10-27 18:57:51 -0700137///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800138// Debug
139///////////////////////////////////////////////////////////////////////////////
140
Romain Guy627c6fd2013-08-21 11:53:18 -0700141uint32_t Caches::getOverdrawColor(uint32_t amount) const {
John Reck1bcacfd2017-11-03 10:12:19 -0700142 static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000},
143 {0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}};
Romain Guy627c6fd2013-08-21 11:53:18 -0700144 if (amount < 1) amount = 1;
145 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700146
147 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
148 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700149}
150
Romain Guyc15008e2010-11-10 11:59:15 -0800151void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700152 String8 stringLog;
153 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000154 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700155}
156
John Reck1bcacfd2017-11-03 10:12:19 -0700157void Caches::dumpMemoryUsage(String8& log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700158 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700159 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
John Reck1bcacfd2017-11-03 10:12:19 -0700160 log.appendFormat(" TextureCache %8d / %8d\n", textureCache.getSize(),
161 textureCache.getMaxSize());
John Reck0e89e2b2014-10-31 14:49:06 -0700162 if (mRenderState) {
163 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800164 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700165 it != mRenderState->mActiveLayers.end(); it++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700166 const Layer* layer = *it;
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500167 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
168 const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
John Reck1bcacfd2017-11-03 10:12:19 -0700169 log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(),
170 layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800171 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700172 }
John Reck1bcacfd2017-11-03 10:12:19 -0700173 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused,
174 mRenderState->mActiveLayers.size());
John Reck0e89e2b2014-10-31 14:49:06 -0700175 total += memused;
176 }
John Reck1bcacfd2017-11-03 10:12:19 -0700177 log.appendFormat(" RenderBufferCache %8d / %8d\n", renderBufferCache.getSize(),
178 renderBufferCache.getMaxSize());
179 log.appendFormat(" GradientCache %8d / %8d\n", gradientCache.getSize(),
180 gradientCache.getMaxSize());
181 log.appendFormat(" PathCache %8d / %8d\n", pathCache.getSize(),
182 pathCache.getMaxSize());
183 log.appendFormat(" TessellationCache %8d / %8d\n", tessellationCache.getSize(),
184 tessellationCache.getMaxSize());
John Reck1bcacfd2017-11-03 10:12:19 -0700185 log.appendFormat(" PatchCache %8d / %8d\n", patchCache.getSize(),
186 patchCache.getMaxSize());
Chris Craikc08820f2015-09-22 14:22:29 -0700187
Romain Guyd2ba50a2011-05-27 10:21:07 -0700188 log.appendFormat("Other:\n");
John Reck1bcacfd2017-11-03 10:12:19 -0700189 log.appendFormat(" FboCache %8d / %8d\n", fboCache.getSize(),
190 fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800191
Romain Guyc15008e2010-11-10 11:59:15 -0800192 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800193 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800194 total += gradientCache.getSize();
195 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700196 total += tessellationCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700197 total += patchCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800198
Chet Haase9c1e23b2011-03-24 10:51:31 -0700199 log.appendFormat("Total memory usage:\n");
200 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800201}
202
203///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800204// Memory management
205///////////////////////////////////////////////////////////////////////////////
206
207void Caches::clearGarbage() {
Romain Guyfe48f652010-11-11 15:36:56 -0800208 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700209 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800210}
211
Romain Guybdf76092011-07-18 15:00:43 -0700212void Caches::flush(FlushMode mode) {
213 FLUSH_LOGD("Flushing caches (mode %d)", mode);
214
Romain Guybdf76092011-07-18 15:00:43 -0700215 switch (mode) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700216 case FlushMode::Full:
Romain Guybdf76092011-07-18 15:00:43 -0700217 textureCache.clear();
218 patchCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700219 gradientCache.clear();
Romain Guyb7463712013-08-16 13:55:29 -0700220 fboCache.clear();
John Reck1bcacfd2017-11-03 10:12:19 -0700221 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700222 case FlushMode::Moderate:
Romain Guyeca0ca22011-11-04 15:12:29 -0700223 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700224 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700225 tessellationCache.clear();
John Reck1bcacfd2017-11-03 10:12:19 -0700226 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700227 case FlushMode::Layers:
Romain Guy8d4aeb72013-02-12 16:08:55 -0800228 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700229 break;
230 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700231
232 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700233 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700234 // Errors during cleanup should be considered non-fatal, dump them and
235 // and move on. TODO: All errors or just errors like bad surface?
236 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700237}
238
Romain Guyfe48f652010-11-11 15:36:56 -0800239///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700240// Regions
241///////////////////////////////////////////////////////////////////////////////
242
Romain Guy5b3b3522010-10-27 18:57:51 -0700243TextureVertex* Caches::getRegionMesh() {
244 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
245 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800246 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700247 }
248
Chris Craik51d6a3d2014-12-22 17:16:56 -0800249 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700250}
251
Chris Craikba9b6132013-12-15 17:10:19 -0800252///////////////////////////////////////////////////////////////////////////////
253// Temporary Properties
254///////////////////////////////////////////////////////////////////////////////
255
John Reck1bcacfd2017-11-03 10:12:19 -0700256}; // namespace uirenderer
257}; // namespace android