Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 17 | #include "Caches.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 18 | |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 19 | #include "GlLayer.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 20 | #include "Properties.h" |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 21 | #include "ShadowTessellator.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include "renderstate/RenderState.h" |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 23 | #include "utils/GLUtils.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 24 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <utils/String8.h> |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 30 | namespace uirenderer { |
| 31 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 32 | Caches* Caches::sInstance = nullptr; |
| 33 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 34 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 35 | // Macros |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | #if DEBUG_CACHE_FLUSH |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 40 | #else |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 41 | #define FLUSH_LOGD(...) |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 45 | // Constructors/destructor |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 48 | Caches::Caches(RenderState& renderState) |
Mike Reed | 8cafcc6 | 2018-05-03 11:32:46 -0400 | [diff] [blame] | 49 | : mRenderState(&renderState) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 50 | , mInitialized(false) { |
| 51 | INIT_LOGD("Creating OpenGL renderer caches"); |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 52 | init(); |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 53 | initConstraints(); |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 54 | initStaticProperties(); |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 55 | initExtensions(); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 58 | bool Caches::init() { |
| 59 | if (mInitialized) return false; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 60 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 61 | ATRACE_NAME("Caches::init"); |
| 62 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 63 | mRegionMesh = nullptr; |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 64 | mProgram = nullptr; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 65 | |
| 66 | mInitialized = true; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 67 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 68 | mPixelBufferState = new PixelBufferState(); |
| 69 | mTextureState = new TextureState(); |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 70 | mTextureState->constructTexture(*this); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 71 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 72 | return true; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 75 | void Caches::initExtensions() { |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 76 | if (extensions().hasDebugMarker()) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 77 | eventMark = glInsertEventMarkerEXT; |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 78 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 79 | startMark = glPushGroupMarkerEXT; |
| 80 | endMark = glPopGroupMarkerEXT; |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 81 | } else { |
| 82 | eventMark = eventMarkNull; |
| 83 | startMark = startMarkNull; |
| 84 | endMark = endMarkNull; |
| 85 | } |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void Caches::initConstraints() { |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 89 | maxTextureSize = DeviceInfo::get()->maxTextureSize(); |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 92 | void Caches::initStaticProperties() { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 93 | // OpenGL ES 3.0+ specific features |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() && |
| 95 | property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true); |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 98 | void Caches::terminate() { |
| 99 | if (!mInitialized) return; |
Sangkyu Lee | 441cc42 | 2015-12-11 16:05:10 +0900 | [diff] [blame] | 100 | mRegionMesh.reset(nullptr); |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 101 | |
Chris Craik | 3472568 | 2015-02-05 09:06:30 -0800 | [diff] [blame] | 102 | mProgram = nullptr; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 103 | |
Romain Guy | 46bfc48 | 2013-08-16 18:38:29 -0700 | [diff] [blame] | 104 | clearGarbage(); |
| 105 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 106 | delete mPixelBufferState; |
| 107 | mPixelBufferState = nullptr; |
| 108 | delete mTextureState; |
| 109 | mTextureState = nullptr; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 110 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 113 | void Caches::setProgram(const ProgramDescription& description) { |
Mike Reed | 8cafcc6 | 2018-05-03 11:32:46 -0400 | [diff] [blame] | 114 | // DEAD CODE |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 130 | // Debug |
| 131 | /////////////////////////////////////////////////////////////////////////////// |
| 132 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 133 | uint32_t Caches::getOverdrawColor(uint32_t amount) const { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000}, |
| 135 | {0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}}; |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 136 | if (amount < 1) amount = 1; |
| 137 | if (amount > 4) amount = 4; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 138 | |
| 139 | int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet); |
| 140 | return sOverdrawColors[overdrawColorIndex][amount - 1]; |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 143 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 144 | String8 stringLog; |
| 145 | dumpMemoryUsage(stringLog); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 146 | ALOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 147 | } |
| 148 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 149 | void Caches::dumpMemoryUsage(String8& log) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 150 | uint32_t total = 0; |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 151 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 152 | if (mRenderState) { |
| 153 | int memused = 0; |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 154 | for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 155 | it != mRenderState->mActiveLayers.end(); it++) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 156 | const Layer* layer = *it; |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 157 | LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL); |
| 158 | const GlLayer* glLayer = static_cast<const GlLayer*>(layer); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 159 | log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(), |
| 160 | layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount()); |
John Reck | 88f5fc7 | 2014-11-03 10:32:24 -0800 | [diff] [blame] | 161 | memused += layer->getWidth() * layer->getHeight() * 4; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 162 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 163 | log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused, |
| 164 | mRenderState->mActiveLayers.size()); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 165 | total += memused; |
| 166 | } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 167 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 168 | log.appendFormat("Total memory usage:\n"); |
| 169 | log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 173 | // Memory management |
| 174 | /////////////////////////////////////////////////////////////////////////////// |
| 175 | |
| 176 | void Caches::clearGarbage() { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 179 | void Caches::flush(FlushMode mode) { |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 180 | clearGarbage(); |
John Reck | 4ced262 | 2014-09-19 10:10:19 -0700 | [diff] [blame] | 181 | glFinish(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 182 | // 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 Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 187 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 188 | // Regions |
| 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 191 | TextureVertex* Caches::getRegionMesh() { |
| 192 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 193 | if (!mRegionMesh) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 194 | mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 197 | return mRegionMesh.get(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 200 | /////////////////////////////////////////////////////////////////////////////// |
| 201 | // Temporary Properties |
| 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 204 | }; // namespace uirenderer |
| 205 | }; // namespace android |