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