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