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