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 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
| 19 | #include "Caches.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 20 | |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 21 | #include "DisplayListRenderer.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 22 | #include "GammaFontRenderer.h" |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 23 | #include "LayerRenderer.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 24 | #include "Properties.h" |
| 25 | #include "renderstate/RenderState.h" |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 26 | #include "ShadowTessellator.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 27 | |
| 28 | #include <utils/Log.h> |
| 29 | #include <utils/String8.h> |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 32 | namespace uirenderer { |
| 33 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 34 | Caches* Caches::sInstance = nullptr; |
| 35 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 37 | // Macros |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
| 40 | #if DEBUG_CACHE_FLUSH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 41 | #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 42 | #else |
| 43 | #define FLUSH_LOGD(...) |
| 44 | #endif |
| 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 47 | // Constructors/destructor |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 50 | Caches::Caches(RenderState& renderState) |
| 51 | : patchCache(renderState) |
| 52 | , mRenderState(&renderState) |
| 53 | , mExtensions(Extensions::getInstance()) |
| 54 | , mInitialized(false) { |
| 55 | INIT_LOGD("Creating OpenGL renderer caches"); |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 56 | init(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 57 | initFont(); |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 58 | initConstraints(); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 59 | initProperties(); |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 60 | initStaticProperties(); |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 61 | initExtensions(); |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 62 | initTempProperties(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 63 | |
| 64 | mDebugLevel = readDebugLevel(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 65 | ALOGD_IF(mDebugLevel != kDebugDisabled, |
| 66 | "Enabling debug mode %d", mDebugLevel); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 69 | bool Caches::init() { |
| 70 | if (mInitialized) return false; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 71 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 72 | ATRACE_NAME("Caches::init"); |
| 73 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 74 | glActiveTexture(gTextureUnits[0]); |
| 75 | mTextureUnit = 0; |
| 76 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 77 | mRegionMesh = nullptr; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 78 | blend = false; |
| 79 | lastSrcMode = GL_ZERO; |
| 80 | lastDstMode = GL_ZERO; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 81 | currentProgram = nullptr; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 82 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 83 | mFunctorsCount = 0; |
| 84 | |
Romain Guy | c2a9721 | 2013-02-06 15:29:46 -0800 | [diff] [blame] | 85 | debugLayersUpdates = false; |
| 86 | debugOverdraw = false; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 87 | debugStencilClip = kStencilHide; |
Romain Guy | c2a9721 | 2013-02-06 15:29:46 -0800 | [diff] [blame] | 88 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 89 | patchCache.init(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 90 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 91 | mInitialized = true; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 92 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 93 | resetBoundTextures(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 94 | mPixelBufferState.reset(new PixelBufferState()); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 95 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 96 | return true; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 99 | void Caches::initFont() { |
| 100 | fontRenderer = GammaFontRenderer::createRenderer(); |
| 101 | } |
| 102 | |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 103 | void Caches::initExtensions() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 104 | if (mExtensions.hasDebugMarker()) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 105 | eventMark = glInsertEventMarkerEXT; |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 106 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 107 | startMark = glPushGroupMarkerEXT; |
| 108 | endMark = glPopGroupMarkerEXT; |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 109 | } else { |
| 110 | eventMark = eventMarkNull; |
| 111 | startMark = startMarkNull; |
| 112 | endMark = endMarkNull; |
| 113 | } |
| 114 | |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 115 | if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 116 | setLabel = glLabelObjectEXT; |
| 117 | getLabel = glGetObjectLabelEXT; |
| 118 | } else { |
| 119 | setLabel = setLabelNull; |
| 120 | getLabel = getLabelNull; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void Caches::initConstraints() { |
| 125 | GLint maxTextureUnits; |
| 126 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 127 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
| 128 | ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 129 | } |
| 130 | |
| 131 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 132 | } |
| 133 | |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 134 | void Caches::initStaticProperties() { |
| 135 | gpuPixelBuffersEnabled = false; |
| 136 | |
| 137 | // OpenGL ES 3.0+ specific features |
Romain Guy | 7f43076 | 2013-06-13 14:29:40 -0700 | [diff] [blame] | 138 | if (mExtensions.hasPixelBufferObjects()) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 139 | char property[PROPERTY_VALUE_MAX]; |
| 140 | if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) { |
| 141 | gpuPixelBuffersEnabled = !strcmp(property, "true"); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 146 | bool Caches::initProperties() { |
| 147 | bool prevDebugLayersUpdates = debugLayersUpdates; |
| 148 | bool prevDebugOverdraw = debugOverdraw; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 149 | StencilClipDebug prevDebugStencilClip = debugStencilClip; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 150 | |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 151 | char property[PROPERTY_VALUE_MAX]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 152 | if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, nullptr) > 0) { |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 153 | INIT_LOGD(" Layers updates debug enabled: %s", property); |
| 154 | debugLayersUpdates = !strcmp(property, "true"); |
| 155 | } else { |
| 156 | debugLayersUpdates = false; |
| 157 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 158 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 159 | debugOverdraw = false; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 160 | if (property_get(PROPERTY_DEBUG_OVERDRAW, property, nullptr) > 0) { |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 161 | INIT_LOGD(" Overdraw debug enabled: %s", property); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 162 | if (!strcmp(property, "show")) { |
| 163 | debugOverdraw = true; |
| 164 | mOverdrawDebugColorSet = kColorSet_Default; |
| 165 | } else if (!strcmp(property, "show_deuteranomaly")) { |
| 166 | debugOverdraw = true; |
| 167 | mOverdrawDebugColorSet = kColorSet_Deuteranomaly; |
| 168 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 169 | } |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 170 | |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 171 | // See Properties.h for valid values |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 172 | if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, nullptr) > 0) { |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 173 | INIT_LOGD(" Stencil clip debug enabled: %s", property); |
| 174 | if (!strcmp(property, "hide")) { |
| 175 | debugStencilClip = kStencilHide; |
| 176 | } else if (!strcmp(property, "highlight")) { |
| 177 | debugStencilClip = kStencilShowHighlight; |
| 178 | } else if (!strcmp(property, "region")) { |
| 179 | debugStencilClip = kStencilShowRegion; |
| 180 | } |
| 181 | } else { |
| 182 | debugStencilClip = kStencilHide; |
| 183 | } |
| 184 | |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 185 | if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) { |
| 186 | drawDeferDisabled = !strcasecmp(property, "true"); |
| 187 | INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled"); |
| 188 | } else { |
Chris Craik | 58ddced | 2014-07-16 19:11:46 -0700 | [diff] [blame] | 189 | drawDeferDisabled = false; |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 190 | INIT_LOGD(" Draw defer enabled"); |
| 191 | } |
| 192 | |
| 193 | if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) { |
| 194 | drawReorderDisabled = !strcasecmp(property, "true"); |
| 195 | INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled"); |
| 196 | } else { |
Chris Craik | 58ddced | 2014-07-16 19:11:46 -0700 | [diff] [blame] | 197 | drawReorderDisabled = false; |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 198 | INIT_LOGD(" Draw reorder enabled"); |
| 199 | } |
| 200 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 201 | return (prevDebugLayersUpdates != debugLayersUpdates) || |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 202 | (prevDebugOverdraw != debugOverdraw) || |
| 203 | (prevDebugStencilClip != debugStencilClip); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 206 | void Caches::terminate() { |
| 207 | if (!mInitialized) return; |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 208 | mRegionMesh.release(); |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 209 | |
| 210 | fboCache.clear(); |
| 211 | |
| 212 | programCache.clear(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 213 | currentProgram = nullptr; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 214 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 215 | patchCache.clear(); |
| 216 | |
Romain Guy | 46bfc48 | 2013-08-16 18:38:29 -0700 | [diff] [blame] | 217 | clearGarbage(); |
| 218 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 219 | mPixelBufferState.release(); |
| 220 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 221 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 225 | // Debug |
| 226 | /////////////////////////////////////////////////////////////////////////////// |
| 227 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 228 | uint32_t Caches::getOverdrawColor(uint32_t amount) const { |
| 229 | static uint32_t sOverdrawColors[2][4] = { |
| 230 | { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 }, |
| 231 | { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 } |
| 232 | }; |
| 233 | if (amount < 1) amount = 1; |
| 234 | if (amount > 4) amount = 4; |
| 235 | return sOverdrawColors[mOverdrawDebugColorSet][amount - 1]; |
| 236 | } |
| 237 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 238 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 239 | String8 stringLog; |
| 240 | dumpMemoryUsage(stringLog); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 241 | ALOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void Caches::dumpMemoryUsage(String8 &log) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 245 | uint32_t total = 0; |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 246 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
| 247 | log.appendFormat(" TextureCache %8d / %8d\n", |
| 248 | textureCache.getSize(), textureCache.getMaxSize()); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 249 | log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n", |
| 250 | layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount()); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 251 | if (mRenderState) { |
| 252 | int memused = 0; |
| 253 | for (std::set<const Layer*>::iterator it = mRenderState->mActiveLayers.begin(); |
| 254 | it != mRenderState->mActiveLayers.end(); it++) { |
| 255 | const Layer* layer = *it; |
| 256 | log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n", |
| 257 | layer->getWidth(), layer->getHeight(), |
| 258 | layer->isTextureLayer(), layer->getTexture(), |
| 259 | layer->getFbo(), layer->getStrongCount()); |
John Reck | 88f5fc7 | 2014-11-03 10:32:24 -0800 | [diff] [blame] | 260 | memused += layer->getWidth() * layer->getHeight() * 4; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 261 | } |
| 262 | log.appendFormat(" Layers total %8d (numLayers = %zu)\n", |
| 263 | memused, mRenderState->mActiveLayers.size()); |
| 264 | total += memused; |
| 265 | } |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 266 | log.appendFormat(" RenderBufferCache %8d / %8d\n", |
| 267 | renderBufferCache.getSize(), renderBufferCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 268 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 269 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 270 | log.appendFormat(" PathCache %8d / %8d\n", |
| 271 | pathCache.getSize(), pathCache.getMaxSize()); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 272 | log.appendFormat(" TessellationCache %8d / %8d\n", |
| 273 | tessellationCache.getSize(), tessellationCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 274 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 275 | dropShadowCache.getMaxSize()); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 276 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 277 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 278 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 279 | const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA); |
| 280 | const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA); |
| 281 | log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8); |
| 282 | log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA); |
| 283 | log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA, |
| 284 | sizeA8 + sizeRGBA); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 285 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 286 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 287 | log.appendFormat(" FboCache %8d / %8d\n", |
| 288 | fboCache.getSize(), fboCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 289 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 290 | total += textureCache.getSize(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 291 | total += renderBufferCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 292 | total += gradientCache.getSize(); |
| 293 | total += pathCache.getSize(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 294 | total += tessellationCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 295 | total += dropShadowCache.getSize(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 296 | total += patchCache.getSize(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 297 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 298 | total += fontRenderer->getFontRendererSize(i, GL_ALPHA); |
| 299 | total += fontRenderer->getFontRendererSize(i, GL_RGBA); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 302 | log.appendFormat("Total memory usage:\n"); |
| 303 | 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] | 304 | } |
| 305 | |
| 306 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 307 | // Memory management |
| 308 | /////////////////////////////////////////////////////////////////////////////// |
| 309 | |
| 310 | void Caches::clearGarbage() { |
| 311 | textureCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 312 | pathCache.clearGarbage(); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 313 | patchCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 316 | void Caches::flush(FlushMode mode) { |
| 317 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 318 | |
Romain Guy | b0a41ed | 2013-08-16 14:44:38 -0700 | [diff] [blame] | 319 | // We must stop tasks before clearing caches |
| 320 | if (mode > kFlushMode_Layers) { |
| 321 | tasks.stop(); |
| 322 | } |
| 323 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 324 | switch (mode) { |
| 325 | case kFlushMode_Full: |
| 326 | textureCache.clear(); |
| 327 | patchCache.clear(); |
| 328 | dropShadowCache.clear(); |
| 329 | gradientCache.clear(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 330 | fontRenderer->clear(); |
Romain Guy | b746371 | 2013-08-16 13:55:29 -0700 | [diff] [blame] | 331 | fboCache.clear(); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 332 | dither.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 333 | // fall through |
| 334 | case kFlushMode_Moderate: |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 335 | fontRenderer->flush(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 336 | textureCache.flush(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 337 | pathCache.clear(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 338 | tessellationCache.clear(); |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 339 | // fall through |
| 340 | case kFlushMode_Layers: |
| 341 | layerCache.clear(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 342 | renderBufferCache.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 343 | break; |
| 344 | } |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 345 | |
| 346 | clearGarbage(); |
John Reck | 4ced262 | 2014-09-19 10:10:19 -0700 | [diff] [blame] | 347 | glFinish(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 350 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 351 | // Textures |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 352 | /////////////////////////////////////////////////////////////////////////////// |
| 353 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 354 | void Caches::activeTexture(GLuint textureUnit) { |
| 355 | if (mTextureUnit != textureUnit) { |
| 356 | glActiveTexture(gTextureUnits[textureUnit]); |
| 357 | mTextureUnit = textureUnit; |
| 358 | } |
| 359 | } |
| 360 | |
Chris Craik | 9ab2d18 | 2013-07-22 16:16:06 -0700 | [diff] [blame] | 361 | void Caches::resetActiveTexture() { |
| 362 | mTextureUnit = -1; |
| 363 | } |
| 364 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 365 | void Caches::bindTexture(GLuint texture) { |
| 366 | if (mBoundTextures[mTextureUnit] != texture) { |
| 367 | glBindTexture(GL_TEXTURE_2D, texture); |
| 368 | mBoundTextures[mTextureUnit] = texture; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void Caches::bindTexture(GLenum target, GLuint texture) { |
Fred Fettinger | 70735bd | 2014-08-29 14:02:31 -0500 | [diff] [blame] | 373 | if (target == GL_TEXTURE_2D) { |
| 374 | bindTexture(texture); |
| 375 | } else { |
| 376 | // GLConsumer directly calls glBindTexture() with |
| 377 | // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target |
| 378 | // since the cached state could be stale |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 379 | glBindTexture(target, texture); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 383 | void Caches::deleteTexture(GLuint texture) { |
| 384 | // When glDeleteTextures() is called on a currently bound texture, |
| 385 | // OpenGL ES specifies that the texture is then considered unbound |
| 386 | // Consider the following series of calls: |
| 387 | // |
| 388 | // glGenTextures -> creates texture name 2 |
| 389 | // glBindTexture(2) |
| 390 | // glDeleteTextures(2) -> 2 is now unbound |
| 391 | // glGenTextures -> can return 2 again |
| 392 | // |
| 393 | // If we don't call glBindTexture(2) after the second glGenTextures |
| 394 | // call, any texture operation will be performed on the default |
| 395 | // texture (name=0) |
| 396 | |
jiayuanr | 4a473c7d | 2014-06-10 17:41:49 +0800 | [diff] [blame] | 397 | unbindTexture(texture); |
| 398 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 399 | glDeleteTextures(1, &texture); |
| 400 | } |
| 401 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 402 | void Caches::resetBoundTextures() { |
| 403 | memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint)); |
| 404 | } |
| 405 | |
jiayuanr | 4a473c7d | 2014-06-10 17:41:49 +0800 | [diff] [blame] | 406 | void Caches::unbindTexture(GLuint texture) { |
| 407 | for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) { |
| 408 | if (mBoundTextures[i] == texture) { |
| 409 | mBoundTextures[i] = 0; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 414 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 415 | // Tiling |
| 416 | /////////////////////////////////////////////////////////////////////////////// |
| 417 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 418 | void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 419 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 420 | glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM)); |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | void Caches::endTiling() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 425 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 426 | glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM); |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 430 | bool Caches::hasRegisteredFunctors() { |
| 431 | return mFunctorsCount > 0; |
| 432 | } |
| 433 | |
| 434 | void Caches::registerFunctors(uint32_t functorCount) { |
| 435 | mFunctorsCount += functorCount; |
| 436 | } |
| 437 | |
| 438 | void Caches::unregisterFunctors(uint32_t functorCount) { |
| 439 | if (functorCount > mFunctorsCount) { |
| 440 | mFunctorsCount = 0; |
| 441 | } else { |
| 442 | mFunctorsCount -= functorCount; |
| 443 | } |
| 444 | } |
| 445 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 446 | /////////////////////////////////////////////////////////////////////////////// |
| 447 | // Regions |
| 448 | /////////////////////////////////////////////////////////////////////////////// |
| 449 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 450 | TextureVertex* Caches::getRegionMesh() { |
| 451 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 452 | if (!mRegionMesh) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame^] | 453 | mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 456 | return mRegionMesh.get(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 459 | /////////////////////////////////////////////////////////////////////////////// |
| 460 | // Temporary Properties |
| 461 | /////////////////////////////////////////////////////////////////////////////// |
| 462 | |
| 463 | void Caches::initTempProperties() { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 464 | propertyLightDiameter = -1.0f; |
| 465 | propertyLightPosY = -1.0f; |
| 466 | propertyLightPosZ = -1.0f; |
| 467 | propertyAmbientRatio = -1.0f; |
ztenghui | 14a4e35 | 2014-08-13 10:44:39 -0700 | [diff] [blame] | 468 | propertyAmbientShadowStrength = -1; |
| 469 | propertySpotShadowStrength = -1; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void Caches::setTempProperty(const char* name, const char* value) { |
| 473 | ALOGD("setting property %s to %s", name, value); |
Chris Craik | a736cd9 | 2014-08-04 13:18:38 -0700 | [diff] [blame] | 474 | if (!strcmp(name, "ambientRatio")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 475 | propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0); |
| 476 | ALOGD("ambientRatio = %.2f", propertyAmbientRatio); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 477 | return; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 478 | } else if (!strcmp(name, "lightDiameter")) { |
| 479 | propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0); |
| 480 | ALOGD("lightDiameter = %.2f", propertyLightDiameter); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 481 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 482 | } else if (!strcmp(name, "lightPosY")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 483 | propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0); |
| 484 | ALOGD("lightPos Y = %.2f", propertyLightPosY); |
| 485 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 486 | } else if (!strcmp(name, "lightPosZ")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 487 | propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0); |
| 488 | ALOGD("lightPos Z = %.2f", propertyLightPosZ); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 489 | return; |
ztenghui | 14a4e35 | 2014-08-13 10:44:39 -0700 | [diff] [blame] | 490 | } else if (!strcmp(name, "ambientShadowStrength")) { |
| 491 | propertyAmbientShadowStrength = atoi(value); |
| 492 | ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength); |
| 493 | return; |
| 494 | } else if (!strcmp(name, "spotShadowStrength")) { |
| 495 | propertySpotShadowStrength = atoi(value); |
| 496 | ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength); |
| 497 | return; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 498 | } |
| 499 | ALOGD(" failed"); |
| 500 | } |
| 501 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 502 | }; // namespace uirenderer |
| 503 | }; // namespace android |