blob: c0944e03fdeadc911d93b21eee75891db0afa293 [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
Chris Craik96a5c4c2015-01-27 15:46:35 -080047Caches::Caches(RenderState& renderState)
Mike Reed8cafcc62018-05-03 11:32:46 -040048 : mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080049 , mInitialized(false) {
50 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080051 init();
Romain Guydfa10462012-05-12 16:18:58 -070052 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070053 initStaticProperties();
Romain Guy0f6675332013-03-01 14:31:04 -080054 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070055}
56
Romain Guy3b748a42013-04-17 18:54:38 -070057bool Caches::init() {
58 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080059
John Reckfbc8df02014-11-14 16:18:41 -080060 ATRACE_NAME("Caches::init");
61
Chris Craikd41c4d82015-01-05 15:51:13 -080062 mRegionMesh = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080063
64 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070065
Chris Craik44eb2c02015-01-29 09:45:09 -080066 mPixelBufferState = new PixelBufferState();
67 mTextureState = new TextureState();
Chris Craik138c21f2016-04-28 16:59:42 -070068 mTextureState->constructTexture(*this);
Romain Guy8aa195d2013-06-04 18:00:09 -070069
Romain Guy3b748a42013-04-17 18:54:38 -070070 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080071}
72
Romain Guydfa10462012-05-12 16:18:58 -070073void Caches::initExtensions() {
John Reck8dc02f92017-07-17 09:55:02 -070074 if (extensions().hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070075 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -080076
Chris Craikff785832013-03-08 13:12:16 -080077 startMark = glPushGroupMarkerEXT;
78 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070079 } else {
80 eventMark = eventMarkNull;
81 startMark = startMarkNull;
82 endMark = endMarkNull;
83 }
Romain Guydfa10462012-05-12 16:18:58 -070084}
85
86void Caches::initConstraints() {
John Reck8dc02f92017-07-17 09:55:02 -070087 maxTextureSize = DeviceInfo::get()->maxTextureSize();
Romain Guydfa10462012-05-12 16:18:58 -070088}
89
Romain Guyf9f00162013-05-09 11:50:12 -070090void Caches::initStaticProperties() {
Romain Guyf9f00162013-05-09 11:50:12 -070091 // OpenGL ES 3.0+ specific features
John Reck1bcacfd2017-11-03 10:12:19 -070092 gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
93 property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
Romain Guyf9f00162013-05-09 11:50:12 -070094}
95
Romain Guy8ff6b9e2011-11-09 20:10:18 -080096void Caches::terminate() {
97 if (!mInitialized) return;
Sangkyu Lee441cc422015-12-11 16:05:10 +090098 mRegionMesh.reset(nullptr);
Romain Guy8ff6b9e2011-11-09 20:10:18 -080099
Romain Guy46bfc482013-08-16 18:38:29 -0700100 clearGarbage();
101
Chris Craik44eb2c02015-01-29 09:45:09 -0800102 delete mPixelBufferState;
103 mPixelBufferState = nullptr;
104 delete mTextureState;
105 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800106 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700107}
108
109///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800110// Debug
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy627c6fd2013-08-21 11:53:18 -0700113uint32_t Caches::getOverdrawColor(uint32_t amount) const {
John Reck1bcacfd2017-11-03 10:12:19 -0700114 static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000},
115 {0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}};
Romain Guy627c6fd2013-08-21 11:53:18 -0700116 if (amount < 1) amount = 1;
117 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700118
119 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
120 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700121}
122
Romain Guyc15008e2010-11-10 11:59:15 -0800123void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700124 String8 stringLog;
125 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000126 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700127}
128
John Reck1bcacfd2017-11-03 10:12:19 -0700129void Caches::dumpMemoryUsage(String8& log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700130 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700131 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
John Reck0e89e2b2014-10-31 14:49:06 -0700132 if (mRenderState) {
133 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800134 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700135 it != mRenderState->mActiveLayers.end(); it++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700136 const Layer* layer = *it;
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500137 LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
138 const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
John Reck1bcacfd2017-11-03 10:12:19 -0700139 log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(),
140 layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800141 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700142 }
John Reck1bcacfd2017-11-03 10:12:19 -0700143 log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused,
144 mRenderState->mActiveLayers.size());
John Reck0e89e2b2014-10-31 14:49:06 -0700145 total += memused;
146 }
Romain Guyc15008e2010-11-10 11:59:15 -0800147
Chet Haase9c1e23b2011-03-24 10:51:31 -0700148 log.appendFormat("Total memory usage:\n");
149 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800150}
151
152///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800153// Memory management
154///////////////////////////////////////////////////////////////////////////////
155
156void Caches::clearGarbage() {
Romain Guyfe48f652010-11-11 15:36:56 -0800157}
158
Romain Guybdf76092011-07-18 15:00:43 -0700159void Caches::flush(FlushMode mode) {
Chet Haase6a2d17f2012-09-30 12:14:13 -0700160 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700161 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700162 // Errors during cleanup should be considered non-fatal, dump them and
163 // and move on. TODO: All errors or just errors like bad surface?
164 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700165}
166
Romain Guyfe48f652010-11-11 15:36:56 -0800167///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700168// Regions
169///////////////////////////////////////////////////////////////////////////////
170
Romain Guy5b3b3522010-10-27 18:57:51 -0700171TextureVertex* Caches::getRegionMesh() {
Mike Reede7f688b2018-05-04 15:21:43 -0400172 return nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700173}
174
Chris Craikba9b6132013-12-15 17:10:19 -0800175///////////////////////////////////////////////////////////////////////////////
176// Temporary Properties
177///////////////////////////////////////////////////////////////////////////////
178
John Reck1bcacfd2017-11-03 10:12:19 -0700179}; // namespace uirenderer
180}; // namespace android