blob: 8cf115ebc54069ef5d516050ddf48aa21f867f42 [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"
John Reck1bcacfd2017-11-03 10:12:19 -070021#include "renderstate/RenderState.h"
John Reckd7db4d72015-05-20 07:18:55 -070022#include "utils/GLUtils.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023
John Reck6b507802015-11-03 10:09:59 -080024#include <cutils/properties.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080025#include <utils/Log.h>
26#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070027
28namespace android {
Chet Haasedd78cca2010-10-22 18:59:26 -070029namespace uirenderer {
30
Chris Craik96a5c4c2015-01-27 15:46:35 -080031Caches* Caches::sInstance = nullptr;
32
Chet Haasedd78cca2010-10-22 18:59:26 -070033///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070034// Macros
35///////////////////////////////////////////////////////////////////////////////
36
37#if DEBUG_CACHE_FLUSH
John Reck1bcacfd2017-11-03 10:12:19 -070038#define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070039#else
John Reck1bcacfd2017-11-03 10:12:19 -070040#define FLUSH_LOGD(...)
Romain Guybdf76092011-07-18 15:00:43 -070041#endif
42
43///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070044// Constructors/destructor
45///////////////////////////////////////////////////////////////////////////////
46
John Recke170fb62018-05-07 08:12:07 -070047Caches::Caches(RenderState& renderState) : mRenderState(&renderState), mInitialized(false) {
Chris Craik96a5c4c2015-01-27 15:46:35 -080048 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080049 init();
Romain Guydfa10462012-05-12 16:18:58 -070050 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070051 initStaticProperties();
Romain Guy0f6675332013-03-01 14:31:04 -080052 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070053}
54
Romain Guy3b748a42013-04-17 18:54:38 -070055bool Caches::init() {
56 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080057
John Reckfbc8df02014-11-14 16:18:41 -080058 ATRACE_NAME("Caches::init");
59
Chris Craikd41c4d82015-01-05 15:51:13 -080060 mRegionMesh = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080061
62 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070063
Chris Craik44eb2c02015-01-29 09:45:09 -080064 mPixelBufferState = new PixelBufferState();
65 mTextureState = new TextureState();
Chris Craik138c21f2016-04-28 16:59:42 -070066 mTextureState->constructTexture(*this);
Romain Guy8aa195d2013-06-04 18:00:09 -070067
Romain Guy3b748a42013-04-17 18:54:38 -070068 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080069}
70
Romain Guydfa10462012-05-12 16:18:58 -070071void Caches::initExtensions() {
John Reck8dc02f92017-07-17 09:55:02 -070072 if (extensions().hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070073 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -080074
Chris Craikff785832013-03-08 13:12:16 -080075 startMark = glPushGroupMarkerEXT;
76 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070077 } else {
78 eventMark = eventMarkNull;
79 startMark = startMarkNull;
80 endMark = endMarkNull;
81 }
Romain Guydfa10462012-05-12 16:18:58 -070082}
83
84void Caches::initConstraints() {
John Reck8dc02f92017-07-17 09:55:02 -070085 maxTextureSize = DeviceInfo::get()->maxTextureSize();
Romain Guydfa10462012-05-12 16:18:58 -070086}
87
Romain Guyf9f00162013-05-09 11:50:12 -070088void Caches::initStaticProperties() {
Romain Guyf9f00162013-05-09 11:50:12 -070089 // OpenGL ES 3.0+ specific features
John Reck1bcacfd2017-11-03 10:12:19 -070090 gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
91 property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
Romain Guyf9f00162013-05-09 11:50:12 -070092}
93
Romain Guy8ff6b9e2011-11-09 20:10:18 -080094void Caches::terminate() {
95 if (!mInitialized) return;
Sangkyu Lee441cc422015-12-11 16:05:10 +090096 mRegionMesh.reset(nullptr);
Romain Guy8ff6b9e2011-11-09 20:10:18 -080097
Romain Guy46bfc482013-08-16 18:38:29 -070098 clearGarbage();
99
Chris Craik44eb2c02015-01-29 09:45:09 -0800100 delete mPixelBufferState;
101 mPixelBufferState = nullptr;
102 delete mTextureState;
103 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800104 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700105}
106
107///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800108// Debug
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guy627c6fd2013-08-21 11:53:18 -0700111uint32_t Caches::getOverdrawColor(uint32_t amount) const {
John Reck1bcacfd2017-11-03 10:12:19 -0700112 static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000},
113 {0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}};
Romain Guy627c6fd2013-08-21 11:53:18 -0700114 if (amount < 1) amount = 1;
115 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700116
117 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
118 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700119}
120
Romain Guyc15008e2010-11-10 11:59:15 -0800121void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700122 String8 stringLog;
123 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000124 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700125}
126
John Reck1bcacfd2017-11-03 10:12:19 -0700127void Caches::dumpMemoryUsage(String8& log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700128 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700129 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
John Reck0e89e2b2014-10-31 14:49:06 -0700130 if (mRenderState) {
131 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800132 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700133 it != mRenderState->mActiveLayers.end(); it++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700134 const Layer* layer = *it;
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500135 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
136 const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
John Reck1bcacfd2017-11-03 10:12:19 -0700137 log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(),
138 layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800139 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700140 }
John Reck1bcacfd2017-11-03 10:12:19 -0700141 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused,
142 mRenderState->mActiveLayers.size());
John Reck0e89e2b2014-10-31 14:49:06 -0700143 total += memused;
144 }
Romain Guyc15008e2010-11-10 11:59:15 -0800145
Chet Haase9c1e23b2011-03-24 10:51:31 -0700146 log.appendFormat("Total memory usage:\n");
147 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800148}
149
150///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800151// Memory management
152///////////////////////////////////////////////////////////////////////////////
153
John Recke170fb62018-05-07 08:12:07 -0700154void Caches::clearGarbage() {}
Romain Guyfe48f652010-11-11 15:36:56 -0800155
Romain Guybdf76092011-07-18 15:00:43 -0700156void Caches::flush(FlushMode mode) {
Chet Haase6a2d17f2012-09-30 12:14:13 -0700157 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700158 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700159 // Errors during cleanup should be considered non-fatal, dump them and
160 // and move on. TODO: All errors or just errors like bad surface?
161 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700162}
163
Romain Guyfe48f652010-11-11 15:36:56 -0800164///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700165// Regions
166///////////////////////////////////////////////////////////////////////////////
167
Romain Guy5b3b3522010-10-27 18:57:51 -0700168TextureVertex* Caches::getRegionMesh() {
Mike Reede7f688b2018-05-04 15:21:43 -0400169 return nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700170}
171
Chris Craikba9b6132013-12-15 17:10:19 -0800172///////////////////////////////////////////////////////////////////////////////
173// Temporary Properties
174///////////////////////////////////////////////////////////////////////////////
175
John Reck1bcacfd2017-11-03 10:12:19 -0700176}; // namespace uirenderer
177}; // namespace android