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 | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 55 | |
| 56 | mDebugLevel = readDebugLevel(); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 57 | ALOGD("Enabling debug mode %d", mDebugLevel); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 58 | |
| 59 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 60 | INIT_LOGD("Layers will be composited as regions"); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 61 | #endif |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 64 | void Caches::init() { |
| 65 | if (mInitialized) return; |
| 66 | |
| 67 | glGenBuffers(1, &meshBuffer); |
| 68 | glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); |
| 69 | glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); |
| 70 | |
| 71 | mCurrentBuffer = meshBuffer; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 72 | mCurrentIndicesBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 73 | mCurrentPositionPointer = this; |
| 74 | mCurrentTexCoordsPointer = this; |
| 75 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 76 | mTexCoordsArrayEnabled = false; |
| 77 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 78 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 79 | scissorEnabled = false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 80 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 81 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 82 | glActiveTexture(gTextureUnits[0]); |
| 83 | mTextureUnit = 0; |
| 84 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 85 | mRegionMesh = NULL; |
| 86 | |
| 87 | blend = false; |
| 88 | lastSrcMode = GL_ZERO; |
| 89 | lastDstMode = GL_ZERO; |
| 90 | currentProgram = NULL; |
| 91 | |
| 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 | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 129 | void Caches::terminate() { |
| 130 | if (!mInitialized) return; |
| 131 | |
| 132 | glDeleteBuffers(1, &meshBuffer); |
| 133 | mCurrentBuffer = 0; |
| 134 | |
| 135 | glDeleteBuffers(1, &mRegionMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 136 | delete[] mRegionMesh; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 137 | mRegionMesh = NULL; |
| 138 | |
| 139 | fboCache.clear(); |
| 140 | |
| 141 | programCache.clear(); |
| 142 | currentProgram = NULL; |
| 143 | |
| 144 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 148 | // Debug |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 152 | String8 stringLog; |
| 153 | dumpMemoryUsage(stringLog); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 154 | ALOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void Caches::dumpMemoryUsage(String8 &log) { |
| 158 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
| 159 | log.appendFormat(" TextureCache %8d / %8d\n", |
| 160 | textureCache.getSize(), textureCache.getMaxSize()); |
| 161 | log.appendFormat(" LayerCache %8d / %8d\n", |
| 162 | layerCache.getSize(), layerCache.getMaxSize()); |
| 163 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 164 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 165 | log.appendFormat(" PathCache %8d / %8d\n", |
| 166 | pathCache.getSize(), pathCache.getMaxSize()); |
| 167 | log.appendFormat(" CircleShapeCache %8d / %8d\n", |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 168 | circleShapeCache.getSize(), circleShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 169 | log.appendFormat(" OvalShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 170 | ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 171 | log.appendFormat(" RoundRectShapeCache %8d / %8d\n", |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 172 | roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 173 | log.appendFormat(" RectShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 174 | rectShapeCache.getSize(), rectShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 175 | log.appendFormat(" ArcShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 176 | arcShapeCache.getSize(), arcShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 177 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 178 | dropShadowCache.getMaxSize()); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 179 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
| 180 | const uint32_t size = fontRenderer->getFontRendererSize(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 181 | log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 182 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 183 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 184 | log.appendFormat(" FboCache %8d / %8d\n", |
| 185 | fboCache.getSize(), fboCache.getMaxSize()); |
| 186 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 187 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 188 | |
| 189 | uint32_t total = 0; |
| 190 | total += textureCache.getSize(); |
| 191 | total += layerCache.getSize(); |
| 192 | total += gradientCache.getSize(); |
| 193 | total += pathCache.getSize(); |
| 194 | total += dropShadowCache.getSize(); |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 195 | total += roundRectShapeCache.getSize(); |
| 196 | total += circleShapeCache.getSize(); |
| 197 | total += ovalShapeCache.getSize(); |
| 198 | total += rectShapeCache.getSize(); |
| 199 | total += arcShapeCache.getSize(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 200 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
| 201 | total += fontRenderer->getFontRendererSize(i); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 204 | log.appendFormat("Total memory usage:\n"); |
| 205 | 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] | 206 | } |
| 207 | |
| 208 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 209 | // Memory management |
| 210 | /////////////////////////////////////////////////////////////////////////////// |
| 211 | |
| 212 | void Caches::clearGarbage() { |
| 213 | textureCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 214 | pathCache.clearGarbage(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 215 | |
| 216 | Mutex::Autolock _l(mGarbageLock); |
| 217 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 218 | size_t count = mLayerGarbage.size(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 219 | for (size_t i = 0; i < count; i++) { |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 220 | Layer* layer = mLayerGarbage.itemAt(i); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 221 | LayerRenderer::destroyLayer(layer); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 222 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 223 | mLayerGarbage.clear(); |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 224 | |
| 225 | count = mDisplayListGarbage.size(); |
| 226 | for (size_t i = 0; i < count; i++) { |
| 227 | DisplayList* displayList = mDisplayListGarbage.itemAt(i); |
| 228 | delete displayList; |
| 229 | } |
| 230 | mDisplayListGarbage.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 233 | void Caches::deleteLayerDeferred(Layer* layer) { |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 234 | Mutex::Autolock _l(mGarbageLock); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 235 | mLayerGarbage.push(layer); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 238 | void Caches::deleteDisplayListDeferred(DisplayList* displayList) { |
| 239 | Mutex::Autolock _l(mGarbageLock); |
| 240 | mDisplayListGarbage.push(displayList); |
| 241 | } |
| 242 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 243 | void Caches::flush(FlushMode mode) { |
| 244 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 245 | |
| 246 | clearGarbage(); |
| 247 | |
| 248 | switch (mode) { |
| 249 | case kFlushMode_Full: |
| 250 | textureCache.clear(); |
| 251 | patchCache.clear(); |
| 252 | dropShadowCache.clear(); |
| 253 | gradientCache.clear(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 254 | fontRenderer->clear(); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame^] | 255 | dither.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 256 | // fall through |
| 257 | case kFlushMode_Moderate: |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 258 | fontRenderer->flush(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 259 | textureCache.flush(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 260 | pathCache.clear(); |
| 261 | roundRectShapeCache.clear(); |
| 262 | circleShapeCache.clear(); |
| 263 | ovalShapeCache.clear(); |
| 264 | rectShapeCache.clear(); |
| 265 | arcShapeCache.clear(); |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 266 | // fall through |
| 267 | case kFlushMode_Layers: |
| 268 | layerCache.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 273 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 274 | // VBO |
| 275 | /////////////////////////////////////////////////////////////////////////////// |
| 276 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 277 | bool Caches::bindMeshBuffer() { |
| 278 | return bindMeshBuffer(meshBuffer); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 281 | bool Caches::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 282 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 283 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 284 | mCurrentBuffer = buffer; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 285 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 286 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 287 | return false; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 290 | bool Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 291 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 292 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 293 | mCurrentBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 294 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 295 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 299 | bool Caches::bindIndicesBuffer(const GLuint buffer) { |
| 300 | if (mCurrentIndicesBuffer != buffer) { |
| 301 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); |
| 302 | mCurrentIndicesBuffer = buffer; |
| 303 | return true; |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | bool Caches::unbindIndicesBuffer() { |
| 309 | if (mCurrentIndicesBuffer) { |
| 310 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 311 | mCurrentIndicesBuffer = 0; |
| 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 317 | void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) { |
| 318 | if (force || vertices != mCurrentPositionPointer) { |
| 319 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
| 320 | mCurrentPositionPointer = vertices; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) { |
| 325 | if (force || vertices != mCurrentTexCoordsPointer) { |
| 326 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices); |
| 327 | mCurrentTexCoordsPointer = vertices; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void Caches::resetVertexPointers() { |
| 332 | mCurrentPositionPointer = this; |
| 333 | mCurrentTexCoordsPointer = this; |
| 334 | } |
| 335 | |
| 336 | void Caches::resetTexCoordsVertexPointer() { |
| 337 | mCurrentTexCoordsPointer = this; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 340 | void Caches::enableTexCoordsVertexArray() { |
| 341 | if (!mTexCoordsArrayEnabled) { |
| 342 | glEnableVertexAttribArray(Program::kBindingTexCoords); |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 343 | mCurrentTexCoordsPointer = this; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 344 | mTexCoordsArrayEnabled = true; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void Caches::disbaleTexCoordsVertexArray() { |
| 349 | if (mTexCoordsArrayEnabled) { |
| 350 | glDisableVertexAttribArray(Program::kBindingTexCoords); |
| 351 | mTexCoordsArrayEnabled = false; |
| 352 | } |
| 353 | } |
| 354 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 355 | void Caches::activeTexture(GLuint textureUnit) { |
| 356 | if (mTextureUnit != textureUnit) { |
| 357 | glActiveTexture(gTextureUnits[textureUnit]); |
| 358 | mTextureUnit = textureUnit; |
| 359 | } |
| 360 | } |
| 361 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 362 | bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 363 | if (scissorEnabled && (x != mScissorX || y != mScissorY || |
| 364 | width != mScissorWidth || height != mScissorHeight)) { |
| 365 | |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 366 | glScissor(x, y, width, height); |
| 367 | |
| 368 | mScissorX = x; |
| 369 | mScissorY = y; |
| 370 | mScissorWidth = width; |
| 371 | mScissorHeight = height; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 372 | |
| 373 | return true; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 374 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 375 | return false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 378 | bool Caches::enableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 379 | if (!scissorEnabled) { |
| 380 | glEnable(GL_SCISSOR_TEST); |
| 381 | scissorEnabled = true; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 382 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 383 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 384 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 387 | bool Caches::disableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 388 | if (scissorEnabled) { |
| 389 | glDisable(GL_SCISSOR_TEST); |
| 390 | scissorEnabled = false; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 391 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 392 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 393 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void Caches::setScissorEnabled(bool enabled) { |
| 397 | if (scissorEnabled != enabled) { |
| 398 | if (enabled) glEnable(GL_SCISSOR_TEST); |
| 399 | else glDisable(GL_SCISSOR_TEST); |
| 400 | scissorEnabled = enabled; |
| 401 | } |
| 402 | } |
| 403 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 404 | void Caches::resetScissor() { |
| 405 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 406 | } |
| 407 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 408 | TextureVertex* Caches::getRegionMesh() { |
| 409 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 410 | if (!mRegionMesh) { |
| 411 | mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; |
| 412 | |
| 413 | uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; |
| 414 | for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { |
| 415 | uint16_t quad = i * 4; |
| 416 | int index = i * 6; |
| 417 | regionIndices[index ] = quad; // top-left |
| 418 | regionIndices[index + 1] = quad + 1; // top-right |
| 419 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 420 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 421 | regionIndices[index + 4] = quad + 1; // top-right |
| 422 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 423 | } |
| 424 | |
| 425 | glGenBuffers(1, &mRegionMeshIndices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 426 | bindIndicesBuffer(mRegionMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 427 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), |
| 428 | regionIndices, GL_STATIC_DRAW); |
| 429 | |
| 430 | delete[] regionIndices; |
| 431 | } else { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 432 | bindIndicesBuffer(mRegionMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return mRegionMesh; |
| 436 | } |
| 437 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 438 | }; // namespace uirenderer |
| 439 | }; // namespace android |