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