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