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