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