blob: 9c2ab5cd5cf31cbebbaea62dee65ae739f2a0bcf [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)
Mike Reed8cafcc62018-05-03 11:32:46 -040049 : mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080050 , mInitialized(false) {
51 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080052 init();
Romain Guydfa10462012-05-12 16:18:58 -070053 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070054 initStaticProperties();
Romain Guy0f6675332013-03-01 14:31:04 -080055 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070056}
57
Romain Guy3b748a42013-04-17 18:54:38 -070058bool Caches::init() {
59 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080060
John Reckfbc8df02014-11-14 16:18:41 -080061 ATRACE_NAME("Caches::init");
62
Chris Craikd41c4d82015-01-05 15:51:13 -080063 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080064 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080065
66 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070067
Chris Craik44eb2c02015-01-29 09:45:09 -080068 mPixelBufferState = new PixelBufferState();
69 mTextureState = new TextureState();
Chris Craik138c21f2016-04-28 16:59:42 -070070 mTextureState->constructTexture(*this);
Romain Guy8aa195d2013-06-04 18:00:09 -070071
Romain Guy3b748a42013-04-17 18:54:38 -070072 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080073}
74
Romain Guydfa10462012-05-12 16:18:58 -070075void Caches::initExtensions() {
John Reck8dc02f92017-07-17 09:55:02 -070076 if (extensions().hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070077 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -080078
Chris Craikff785832013-03-08 13:12:16 -080079 startMark = glPushGroupMarkerEXT;
80 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070081 } else {
82 eventMark = eventMarkNull;
83 startMark = startMarkNull;
84 endMark = endMarkNull;
85 }
Romain Guydfa10462012-05-12 16:18:58 -070086}
87
88void Caches::initConstraints() {
John Reck8dc02f92017-07-17 09:55:02 -070089 maxTextureSize = DeviceInfo::get()->maxTextureSize();
Romain Guydfa10462012-05-12 16:18:58 -070090}
91
Romain Guyf9f00162013-05-09 11:50:12 -070092void Caches::initStaticProperties() {
Romain Guyf9f00162013-05-09 11:50:12 -070093 // OpenGL ES 3.0+ specific features
John Reck1bcacfd2017-11-03 10:12:19 -070094 gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
95 property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
Romain Guyf9f00162013-05-09 11:50:12 -070096}
97
Romain Guy8ff6b9e2011-11-09 20:10:18 -080098void Caches::terminate() {
99 if (!mInitialized) return;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900100 mRegionMesh.reset(nullptr);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800101
Chris Craik34725682015-02-05 09:06:30 -0800102 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800103
Romain Guy46bfc482013-08-16 18:38:29 -0700104 clearGarbage();
105
Chris Craik44eb2c02015-01-29 09:45:09 -0800106 delete mPixelBufferState;
107 mPixelBufferState = nullptr;
108 delete mTextureState;
109 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800110 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700111}
112
Chris Craik6c15ffa2015-02-02 13:50:55 -0800113void Caches::setProgram(const ProgramDescription& description) {
Mike Reed8cafcc62018-05-03 11:32:46 -0400114 // DEAD CODE
Chris Craik6c15ffa2015-02-02 13:50:55 -0800115}
116
117void Caches::setProgram(Program* program) {
118 if (!program || !program->isInUse()) {
119 if (mProgram) {
120 mProgram->remove();
121 }
122 if (program) {
123 program->use();
124 }
125 mProgram = program;
126 }
127}
128
Romain Guy5b3b3522010-10-27 18:57:51 -0700129///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800130// Debug
131///////////////////////////////////////////////////////////////////////////////
132
Romain Guy627c6fd2013-08-21 11:53:18 -0700133uint32_t Caches::getOverdrawColor(uint32_t amount) const {
John Reck1bcacfd2017-11-03 10:12:19 -0700134 static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000},
135 {0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}};
Romain Guy627c6fd2013-08-21 11:53:18 -0700136 if (amount < 1) amount = 1;
137 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700138
139 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
140 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700141}
142
Romain Guyc15008e2010-11-10 11:59:15 -0800143void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700144 String8 stringLog;
145 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000146 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700147}
148
John Reck1bcacfd2017-11-03 10:12:19 -0700149void Caches::dumpMemoryUsage(String8& log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700150 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700151 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
John Reck0e89e2b2014-10-31 14:49:06 -0700152 if (mRenderState) {
153 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800154 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700155 it != mRenderState->mActiveLayers.end(); it++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700156 const Layer* layer = *it;
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500157 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
158 const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
John Reck1bcacfd2017-11-03 10:12:19 -0700159 log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(),
160 layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800161 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700162 }
John Reck1bcacfd2017-11-03 10:12:19 -0700163 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused,
164 mRenderState->mActiveLayers.size());
John Reck0e89e2b2014-10-31 14:49:06 -0700165 total += memused;
166 }
Romain Guyc15008e2010-11-10 11:59:15 -0800167
Chet Haase9c1e23b2011-03-24 10:51:31 -0700168 log.appendFormat("Total memory usage:\n");
169 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800170}
171
172///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800173// Memory management
174///////////////////////////////////////////////////////////////////////////////
175
176void Caches::clearGarbage() {
Romain Guyfe48f652010-11-11 15:36:56 -0800177}
178
Romain Guybdf76092011-07-18 15:00:43 -0700179void Caches::flush(FlushMode mode) {
Chet Haase6a2d17f2012-09-30 12:14:13 -0700180 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700181 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700182 // Errors during cleanup should be considered non-fatal, dump them and
183 // and move on. TODO: All errors or just errors like bad surface?
184 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700185}
186
Romain Guyfe48f652010-11-11 15:36:56 -0800187///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700188// Regions
189///////////////////////////////////////////////////////////////////////////////
190
Romain Guy5b3b3522010-10-27 18:57:51 -0700191TextureVertex* Caches::getRegionMesh() {
192 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
193 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800194 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700195 }
196
Chris Craik51d6a3d2014-12-22 17:16:56 -0800197 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700198}
199
Chris Craikba9b6132013-12-15 17:10:19 -0800200///////////////////////////////////////////////////////////////////////////////
201// Temporary Properties
202///////////////////////////////////////////////////////////////////////////////
203
John Reck1bcacfd2017-11-03 10:12:19 -0700204}; // namespace uirenderer
205}; // namespace android