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 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 19 | #include <utils/Log.h> |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 20 | #include <utils/String8.h> |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 21 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 22 | #include "Caches.h" |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 23 | #include "DisplayListRenderer.h" |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 24 | #include "Properties.h" |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 25 | #include "LayerRenderer.h" |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | #ifdef USE_OPENGL_RENDERER |
| 30 | using namespace uirenderer; |
| 31 | ANDROID_SINGLETON_STATIC_INSTANCE(Caches); |
| 32 | #endif |
| 33 | |
| 34 | namespace uirenderer { |
| 35 | |
| 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 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 50 | Caches::Caches(): Singleton<Caches>(), mExtensions(Extensions::getInstance()), mInitialized(false) { |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 51 | init(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 52 | initFont(); |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 53 | initConstraints(); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 54 | initProperties(); |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 55 | initExtensions(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 56 | |
| 57 | mDebugLevel = readDebugLevel(); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 58 | ALOGD("Enabling debug mode %d", mDebugLevel); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 61 | bool Caches::init() { |
| 62 | if (mInitialized) return false; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 63 | |
| 64 | glGenBuffers(1, &meshBuffer); |
| 65 | glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); |
| 66 | glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); |
| 67 | |
| 68 | mCurrentBuffer = meshBuffer; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 69 | mCurrentIndicesBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 70 | mCurrentPositionPointer = this; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 71 | mCurrentPositionStride = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 72 | mCurrentTexCoordsPointer = this; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 73 | mCurrentPixelBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 74 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 75 | mTexCoordsArrayEnabled = false; |
| 76 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 77 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 78 | scissorEnabled = false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 79 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 80 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 81 | glActiveTexture(gTextureUnits[0]); |
| 82 | mTextureUnit = 0; |
| 83 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 84 | mRegionMesh = NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 85 | mMeshIndices = 0; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 86 | |
| 87 | blend = false; |
| 88 | lastSrcMode = GL_ZERO; |
| 89 | lastDstMode = GL_ZERO; |
| 90 | currentProgram = NULL; |
| 91 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 92 | mFunctorsCount = 0; |
| 93 | |
Romain Guy | c2a9721 | 2013-02-06 15:29:46 -0800 | [diff] [blame] | 94 | debugLayersUpdates = false; |
| 95 | debugOverdraw = false; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 96 | debugStencilClip = kStencilHide; |
Romain Guy | c2a9721 | 2013-02-06 15:29:46 -0800 | [diff] [blame] | 97 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 98 | patchCache.init(*this); |
| 99 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 100 | mInitialized = true; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 101 | |
| 102 | return true; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 105 | void Caches::initFont() { |
| 106 | fontRenderer = GammaFontRenderer::createRenderer(); |
| 107 | } |
| 108 | |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 109 | void Caches::initExtensions() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 110 | if (mExtensions.hasDebugMarker()) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 111 | eventMark = glInsertEventMarkerEXT; |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 112 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 113 | startMark = glPushGroupMarkerEXT; |
| 114 | endMark = glPopGroupMarkerEXT; |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 115 | } else { |
| 116 | eventMark = eventMarkNull; |
| 117 | startMark = startMarkNull; |
| 118 | endMark = endMarkNull; |
| 119 | } |
| 120 | |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 121 | if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 122 | setLabel = glLabelObjectEXT; |
| 123 | getLabel = glGetObjectLabelEXT; |
| 124 | } else { |
| 125 | setLabel = setLabelNull; |
| 126 | getLabel = getLabelNull; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void Caches::initConstraints() { |
| 131 | GLint maxTextureUnits; |
| 132 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 133 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
| 134 | ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 135 | } |
| 136 | |
| 137 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 138 | } |
| 139 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 140 | bool Caches::initProperties() { |
| 141 | bool prevDebugLayersUpdates = debugLayersUpdates; |
| 142 | bool prevDebugOverdraw = debugOverdraw; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 143 | StencilClipDebug prevDebugStencilClip = debugStencilClip; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 144 | |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 145 | char property[PROPERTY_VALUE_MAX]; |
| 146 | if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) { |
| 147 | INIT_LOGD(" Layers updates debug enabled: %s", property); |
| 148 | debugLayersUpdates = !strcmp(property, "true"); |
| 149 | } else { |
| 150 | debugLayersUpdates = false; |
| 151 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 152 | |
| 153 | if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) { |
| 154 | INIT_LOGD(" Overdraw debug enabled: %s", property); |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 155 | debugOverdraw = !strcmp(property, "show"); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 156 | } else { |
| 157 | debugOverdraw = false; |
| 158 | } |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 159 | |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 160 | // See Properties.h for valid values |
| 161 | if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) { |
| 162 | INIT_LOGD(" Stencil clip debug enabled: %s", property); |
| 163 | if (!strcmp(property, "hide")) { |
| 164 | debugStencilClip = kStencilHide; |
| 165 | } else if (!strcmp(property, "highlight")) { |
| 166 | debugStencilClip = kStencilShowHighlight; |
| 167 | } else if (!strcmp(property, "region")) { |
| 168 | debugStencilClip = kStencilShowRegion; |
| 169 | } |
| 170 | } else { |
| 171 | debugStencilClip = kStencilHide; |
| 172 | } |
| 173 | |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 174 | if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) { |
| 175 | drawDeferDisabled = !strcasecmp(property, "true"); |
| 176 | INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled"); |
| 177 | } else { |
| 178 | INIT_LOGD(" Draw defer enabled"); |
| 179 | } |
| 180 | |
| 181 | if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) { |
| 182 | drawReorderDisabled = !strcasecmp(property, "true"); |
| 183 | INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled"); |
| 184 | } else { |
| 185 | INIT_LOGD(" Draw reorder enabled"); |
| 186 | } |
| 187 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 188 | return (prevDebugLayersUpdates != debugLayersUpdates) || |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 189 | (prevDebugOverdraw != debugOverdraw) || |
| 190 | (prevDebugStencilClip != debugStencilClip); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 193 | void Caches::terminate() { |
| 194 | if (!mInitialized) return; |
| 195 | |
| 196 | glDeleteBuffers(1, &meshBuffer); |
| 197 | mCurrentBuffer = 0; |
| 198 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 199 | glDeleteBuffers(1, &mMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 200 | delete[] mRegionMesh; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 201 | mMeshIndices = 0; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 202 | mRegionMesh = NULL; |
| 203 | |
| 204 | fboCache.clear(); |
| 205 | |
| 206 | programCache.clear(); |
| 207 | currentProgram = NULL; |
| 208 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 209 | assetAtlas.terminate(); |
| 210 | |
| 211 | patchCache.clear(); |
| 212 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 213 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 217 | // Debug |
| 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | |
| 220 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 221 | String8 stringLog; |
| 222 | dumpMemoryUsage(stringLog); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 223 | ALOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void Caches::dumpMemoryUsage(String8 &log) { |
| 227 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
| 228 | log.appendFormat(" TextureCache %8d / %8d\n", |
| 229 | textureCache.getSize(), textureCache.getMaxSize()); |
| 230 | log.appendFormat(" LayerCache %8d / %8d\n", |
| 231 | layerCache.getSize(), layerCache.getMaxSize()); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 232 | log.appendFormat(" RenderBufferCache %8d / %8d\n", |
| 233 | renderBufferCache.getSize(), renderBufferCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 234 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 235 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 236 | log.appendFormat(" PathCache %8d / %8d\n", |
| 237 | pathCache.getSize(), pathCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 238 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 239 | dropShadowCache.getMaxSize()); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 240 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 241 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 242 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
| 243 | const uint32_t size = fontRenderer->getFontRendererSize(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 244 | log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 245 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 246 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 247 | log.appendFormat(" FboCache %8d / %8d\n", |
| 248 | fboCache.getSize(), fboCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 249 | |
| 250 | uint32_t total = 0; |
| 251 | total += textureCache.getSize(); |
| 252 | total += layerCache.getSize(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 253 | total += renderBufferCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 254 | total += gradientCache.getSize(); |
| 255 | total += pathCache.getSize(); |
| 256 | total += dropShadowCache.getSize(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 257 | total += patchCache.getSize(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 258 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
| 259 | total += fontRenderer->getFontRendererSize(i); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 262 | log.appendFormat("Total memory usage:\n"); |
| 263 | 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] | 264 | } |
| 265 | |
| 266 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 267 | // Memory management |
| 268 | /////////////////////////////////////////////////////////////////////////////// |
| 269 | |
| 270 | void Caches::clearGarbage() { |
| 271 | textureCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 272 | pathCache.clearGarbage(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 273 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 274 | Vector<DisplayList*> displayLists; |
| 275 | Vector<Layer*> layers; |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 276 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 277 | { // scope for the lock |
| 278 | Mutex::Autolock _l(mGarbageLock); |
| 279 | displayLists = mDisplayListGarbage; |
| 280 | layers = mLayerGarbage; |
| 281 | mDisplayListGarbage.clear(); |
| 282 | mLayerGarbage.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 283 | } |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 284 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 285 | size_t count = displayLists.size(); |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 286 | for (size_t i = 0; i < count; i++) { |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 287 | DisplayList* displayList = displayLists.itemAt(i); |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 288 | delete displayList; |
| 289 | } |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 290 | |
| 291 | count = layers.size(); |
| 292 | for (size_t i = 0; i < count; i++) { |
| 293 | Layer* layer = layers.itemAt(i); |
| 294 | delete layer; |
| 295 | } |
| 296 | layers.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 299 | void Caches::deleteLayerDeferred(Layer* layer) { |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 300 | Mutex::Autolock _l(mGarbageLock); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 301 | mLayerGarbage.push(layer); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 304 | void Caches::deleteDisplayListDeferred(DisplayList* displayList) { |
| 305 | Mutex::Autolock _l(mGarbageLock); |
| 306 | mDisplayListGarbage.push(displayList); |
| 307 | } |
| 308 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 309 | void Caches::flush(FlushMode mode) { |
| 310 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 311 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 312 | switch (mode) { |
| 313 | case kFlushMode_Full: |
| 314 | textureCache.clear(); |
| 315 | patchCache.clear(); |
| 316 | dropShadowCache.clear(); |
| 317 | gradientCache.clear(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 318 | fontRenderer->clear(); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 319 | dither.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 320 | // fall through |
| 321 | case kFlushMode_Moderate: |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 322 | fontRenderer->flush(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 323 | textureCache.flush(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 324 | pathCache.clear(); |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 325 | tasks.stop(); |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 326 | // fall through |
| 327 | case kFlushMode_Layers: |
| 328 | layerCache.clear(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 329 | renderBufferCache.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 330 | break; |
| 331 | } |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 332 | |
| 333 | clearGarbage(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 336 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 337 | // VBO |
| 338 | /////////////////////////////////////////////////////////////////////////////// |
| 339 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 340 | bool Caches::bindMeshBuffer() { |
| 341 | return bindMeshBuffer(meshBuffer); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 344 | bool Caches::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 345 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 346 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 347 | mCurrentBuffer = buffer; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 348 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 349 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 350 | return false; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 353 | bool Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 354 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 355 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 356 | mCurrentBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 357 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 358 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 362 | bool Caches::bindIndicesBuffer(const GLuint buffer) { |
| 363 | if (mCurrentIndicesBuffer != buffer) { |
| 364 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); |
| 365 | mCurrentIndicesBuffer = buffer; |
| 366 | return true; |
| 367 | } |
| 368 | return false; |
| 369 | } |
| 370 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 371 | bool Caches::bindIndicesBuffer() { |
| 372 | if (!mMeshIndices) { |
| 373 | uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; |
| 374 | for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { |
| 375 | uint16_t quad = i * 4; |
| 376 | int index = i * 6; |
| 377 | regionIndices[index ] = quad; // top-left |
| 378 | regionIndices[index + 1] = quad + 1; // top-right |
| 379 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 380 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 381 | regionIndices[index + 4] = quad + 1; // top-right |
| 382 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 383 | } |
| 384 | |
| 385 | glGenBuffers(1, &mMeshIndices); |
| 386 | bool force = bindIndicesBuffer(mMeshIndices); |
| 387 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), |
| 388 | regionIndices, GL_STATIC_DRAW); |
| 389 | |
| 390 | delete[] regionIndices; |
| 391 | return force; |
| 392 | } |
| 393 | |
| 394 | return bindIndicesBuffer(mMeshIndices); |
| 395 | } |
| 396 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 397 | bool Caches::unbindIndicesBuffer() { |
| 398 | if (mCurrentIndicesBuffer) { |
| 399 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 400 | mCurrentIndicesBuffer = 0; |
| 401 | return true; |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 406 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 407 | // PBO |
| 408 | /////////////////////////////////////////////////////////////////////////////// |
| 409 | |
| 410 | bool Caches::bindPixelBuffer(const GLuint buffer) { |
| 411 | if (mCurrentPixelBuffer != buffer) { |
| 412 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer); |
| 413 | mCurrentPixelBuffer = buffer; |
| 414 | return true; |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | bool Caches::unbindPixelBuffer() { |
| 420 | if (mCurrentPixelBuffer) { |
| 421 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 422 | mCurrentPixelBuffer = 0; |
| 423 | return true; |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 429 | // Meshes and textures |
| 430 | /////////////////////////////////////////////////////////////////////////////// |
| 431 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 432 | void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) { |
| 433 | if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) { |
| 434 | GLuint slot = currentProgram->position; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 435 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
| 436 | mCurrentPositionPointer = vertices; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 437 | mCurrentPositionStride = stride; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 441 | void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) { |
| 442 | if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 443 | GLuint slot = currentProgram->texCoords; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 444 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 445 | mCurrentTexCoordsPointer = vertices; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 446 | mCurrentTexCoordsStride = stride; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | void Caches::resetVertexPointers() { |
| 451 | mCurrentPositionPointer = this; |
| 452 | mCurrentTexCoordsPointer = this; |
| 453 | } |
| 454 | |
| 455 | void Caches::resetTexCoordsVertexPointer() { |
| 456 | mCurrentTexCoordsPointer = this; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 459 | void Caches::enableTexCoordsVertexArray() { |
| 460 | if (!mTexCoordsArrayEnabled) { |
| 461 | glEnableVertexAttribArray(Program::kBindingTexCoords); |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 462 | mCurrentTexCoordsPointer = this; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 463 | mTexCoordsArrayEnabled = true; |
| 464 | } |
| 465 | } |
| 466 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 467 | void Caches::disableTexCoordsVertexArray() { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 468 | if (mTexCoordsArrayEnabled) { |
| 469 | glDisableVertexAttribArray(Program::kBindingTexCoords); |
| 470 | mTexCoordsArrayEnabled = false; |
| 471 | } |
| 472 | } |
| 473 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 474 | void Caches::activeTexture(GLuint textureUnit) { |
| 475 | if (mTextureUnit != textureUnit) { |
| 476 | glActiveTexture(gTextureUnits[textureUnit]); |
| 477 | mTextureUnit = textureUnit; |
| 478 | } |
| 479 | } |
| 480 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 481 | /////////////////////////////////////////////////////////////////////////////// |
| 482 | // Scissor |
| 483 | /////////////////////////////////////////////////////////////////////////////// |
| 484 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 485 | bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 486 | if (scissorEnabled && (x != mScissorX || y != mScissorY || |
| 487 | width != mScissorWidth || height != mScissorHeight)) { |
| 488 | |
Chet Haase | aa42c9a | 2012-10-16 17:36:16 -0700 | [diff] [blame] | 489 | if (x < 0) { |
| 490 | width += x; |
| 491 | x = 0; |
| 492 | } |
| 493 | if (y < 0) { |
| 494 | height += y; |
| 495 | y = 0; |
| 496 | } |
| 497 | if (width < 0) { |
| 498 | width = 0; |
| 499 | } |
| 500 | if (height < 0) { |
| 501 | height = 0; |
| 502 | } |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 503 | glScissor(x, y, width, height); |
| 504 | |
| 505 | mScissorX = x; |
| 506 | mScissorY = y; |
| 507 | mScissorWidth = width; |
| 508 | mScissorHeight = height; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 509 | |
| 510 | return true; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 511 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 512 | return false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 515 | bool Caches::enableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 516 | if (!scissorEnabled) { |
| 517 | glEnable(GL_SCISSOR_TEST); |
| 518 | scissorEnabled = true; |
Romain Guy | 50ae66a | 2012-10-07 14:05:59 -0700 | [diff] [blame] | 519 | resetScissor(); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 520 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 521 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 522 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 525 | bool Caches::disableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 526 | if (scissorEnabled) { |
| 527 | glDisable(GL_SCISSOR_TEST); |
| 528 | scissorEnabled = false; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 529 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 530 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 531 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void Caches::setScissorEnabled(bool enabled) { |
| 535 | if (scissorEnabled != enabled) { |
| 536 | if (enabled) glEnable(GL_SCISSOR_TEST); |
| 537 | else glDisable(GL_SCISSOR_TEST); |
| 538 | scissorEnabled = enabled; |
| 539 | } |
| 540 | } |
| 541 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 542 | void Caches::resetScissor() { |
| 543 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 544 | } |
| 545 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 546 | /////////////////////////////////////////////////////////////////////////////// |
| 547 | // Tiling |
| 548 | /////////////////////////////////////////////////////////////////////////////// |
| 549 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 550 | 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] | 551 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 552 | 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] | 553 | } |
| 554 | } |
| 555 | |
| 556 | void Caches::endTiling() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 557 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 558 | glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM); |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 562 | bool Caches::hasRegisteredFunctors() { |
| 563 | return mFunctorsCount > 0; |
| 564 | } |
| 565 | |
| 566 | void Caches::registerFunctors(uint32_t functorCount) { |
| 567 | mFunctorsCount += functorCount; |
| 568 | } |
| 569 | |
| 570 | void Caches::unregisterFunctors(uint32_t functorCount) { |
| 571 | if (functorCount > mFunctorsCount) { |
| 572 | mFunctorsCount = 0; |
| 573 | } else { |
| 574 | mFunctorsCount -= functorCount; |
| 575 | } |
| 576 | } |
| 577 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 578 | /////////////////////////////////////////////////////////////////////////////// |
| 579 | // Regions |
| 580 | /////////////////////////////////////////////////////////////////////////////// |
| 581 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 582 | TextureVertex* Caches::getRegionMesh() { |
| 583 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 584 | if (!mRegionMesh) { |
| 585 | mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | return mRegionMesh; |
| 589 | } |
| 590 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 591 | }; // namespace uirenderer |
| 592 | }; // namespace android |