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 | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 23 | #include "Properties.h" |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 24 | #include "LayerRenderer.h" |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | #ifdef USE_OPENGL_RENDERER |
| 29 | using namespace uirenderer; |
| 30 | ANDROID_SINGLETON_STATIC_INSTANCE(Caches); |
| 31 | #endif |
| 32 | |
| 33 | namespace uirenderer { |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 36 | // Macros |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | #if DEBUG_CACHE_FLUSH |
| 40 | #define FLUSH_LOGD(...) LOGD(__VA_ARGS__) |
| 41 | #else |
| 42 | #define FLUSH_LOGD(...) |
| 43 | #endif |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 46 | // Constructors/destructor |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 49 | Caches::Caches(): Singleton<Caches>(), mInitialized(false) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 50 | GLint maxTextureUnits; |
| 51 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 52 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
| 53 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 54 | } |
| 55 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 56 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 57 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 58 | init(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 59 | |
| 60 | mDebugLevel = readDebugLevel(); |
| 61 | LOGD("Enabling debug mode %d", mDebugLevel); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 62 | |
| 63 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 64 | INIT_LOGD("Layers will be composited as regions"); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 65 | #endif |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 68 | void Caches::init() { |
| 69 | if (mInitialized) return; |
| 70 | |
| 71 | glGenBuffers(1, &meshBuffer); |
| 72 | glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); |
| 73 | glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); |
| 74 | |
| 75 | mCurrentBuffer = meshBuffer; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 76 | mCurrentIndicesBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 77 | mCurrentPositionPointer = this; |
| 78 | mCurrentTexCoordsPointer = this; |
| 79 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 80 | mTexCoordsArrayEnabled = false; |
| 81 | |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame^] | 82 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 83 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 84 | glActiveTexture(gTextureUnits[0]); |
| 85 | mTextureUnit = 0; |
| 86 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 87 | mRegionMesh = NULL; |
| 88 | |
| 89 | blend = false; |
| 90 | lastSrcMode = GL_ZERO; |
| 91 | lastDstMode = GL_ZERO; |
| 92 | currentProgram = NULL; |
| 93 | |
| 94 | mInitialized = true; |
| 95 | } |
| 96 | |
| 97 | void Caches::terminate() { |
| 98 | if (!mInitialized) return; |
| 99 | |
| 100 | glDeleteBuffers(1, &meshBuffer); |
| 101 | mCurrentBuffer = 0; |
| 102 | |
| 103 | glDeleteBuffers(1, &mRegionMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 104 | delete[] mRegionMesh; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 105 | mRegionMesh = NULL; |
| 106 | |
| 107 | fboCache.clear(); |
| 108 | |
| 109 | programCache.clear(); |
| 110 | currentProgram = NULL; |
| 111 | |
| 112 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 116 | // Debug |
| 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 120 | String8 stringLog; |
| 121 | dumpMemoryUsage(stringLog); |
| 122 | LOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Caches::dumpMemoryUsage(String8 &log) { |
| 126 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
| 127 | log.appendFormat(" TextureCache %8d / %8d\n", |
| 128 | textureCache.getSize(), textureCache.getMaxSize()); |
| 129 | log.appendFormat(" LayerCache %8d / %8d\n", |
| 130 | layerCache.getSize(), layerCache.getMaxSize()); |
| 131 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 132 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 133 | log.appendFormat(" PathCache %8d / %8d\n", |
| 134 | pathCache.getSize(), pathCache.getMaxSize()); |
| 135 | log.appendFormat(" CircleShapeCache %8d / %8d\n", |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 136 | circleShapeCache.getSize(), circleShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 137 | log.appendFormat(" OvalShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 138 | ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 139 | log.appendFormat(" RoundRectShapeCache %8d / %8d\n", |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 140 | roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 141 | log.appendFormat(" RectShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 142 | rectShapeCache.getSize(), rectShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 143 | log.appendFormat(" ArcShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 144 | arcShapeCache.getSize(), arcShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 145 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 146 | dropShadowCache.getMaxSize()); |
| 147 | for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { |
| 148 | const uint32_t size = fontRenderer.getFontRendererSize(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 149 | log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 150 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 151 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 152 | log.appendFormat(" FboCache %8d / %8d\n", |
| 153 | fboCache.getSize(), fboCache.getMaxSize()); |
| 154 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 155 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 156 | |
| 157 | uint32_t total = 0; |
| 158 | total += textureCache.getSize(); |
| 159 | total += layerCache.getSize(); |
| 160 | total += gradientCache.getSize(); |
| 161 | total += pathCache.getSize(); |
| 162 | total += dropShadowCache.getSize(); |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 163 | total += roundRectShapeCache.getSize(); |
| 164 | total += circleShapeCache.getSize(); |
| 165 | total += ovalShapeCache.getSize(); |
| 166 | total += rectShapeCache.getSize(); |
| 167 | total += arcShapeCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 168 | for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { |
| 169 | total += fontRenderer.getFontRendererSize(i); |
| 170 | } |
| 171 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 172 | log.appendFormat("Total memory usage:\n"); |
| 173 | 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] | 174 | } |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 177 | // Memory management |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | |
| 180 | void Caches::clearGarbage() { |
| 181 | textureCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 182 | pathCache.clearGarbage(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 183 | |
| 184 | Mutex::Autolock _l(mGarbageLock); |
| 185 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 186 | size_t count = mLayerGarbage.size(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 187 | for (size_t i = 0; i < count; i++) { |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 188 | Layer* layer = mLayerGarbage.itemAt(i); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 189 | LayerRenderer::destroyLayer(layer); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 190 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 191 | mLayerGarbage.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 194 | void Caches::deleteLayerDeferred(Layer* layer) { |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 195 | Mutex::Autolock _l(mGarbageLock); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 196 | mLayerGarbage.push(layer); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 199 | void Caches::flush(FlushMode mode) { |
| 200 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 201 | |
| 202 | clearGarbage(); |
| 203 | |
| 204 | switch (mode) { |
| 205 | case kFlushMode_Full: |
| 206 | textureCache.clear(); |
| 207 | patchCache.clear(); |
| 208 | dropShadowCache.clear(); |
| 209 | gradientCache.clear(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 210 | fontRenderer.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 211 | // fall through |
| 212 | case kFlushMode_Moderate: |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 213 | fontRenderer.flush(); |
| 214 | textureCache.flush(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 215 | pathCache.clear(); |
| 216 | roundRectShapeCache.clear(); |
| 217 | circleShapeCache.clear(); |
| 218 | ovalShapeCache.clear(); |
| 219 | rectShapeCache.clear(); |
| 220 | arcShapeCache.clear(); |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 221 | // fall through |
| 222 | case kFlushMode_Layers: |
| 223 | layerCache.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 228 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 229 | // VBO |
| 230 | /////////////////////////////////////////////////////////////////////////////// |
| 231 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 232 | bool Caches::bindMeshBuffer() { |
| 233 | return bindMeshBuffer(meshBuffer); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 236 | bool Caches::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 237 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 238 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 239 | mCurrentBuffer = buffer; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 240 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 241 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 242 | return false; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 245 | bool Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 246 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 247 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 248 | mCurrentBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 249 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 250 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 254 | bool Caches::bindIndicesBuffer(const GLuint buffer) { |
| 255 | if (mCurrentIndicesBuffer != buffer) { |
| 256 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); |
| 257 | mCurrentIndicesBuffer = buffer; |
| 258 | return true; |
| 259 | } |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | bool Caches::unbindIndicesBuffer() { |
| 264 | if (mCurrentIndicesBuffer) { |
| 265 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 266 | mCurrentIndicesBuffer = 0; |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 272 | void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) { |
| 273 | if (force || vertices != mCurrentPositionPointer) { |
| 274 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
| 275 | mCurrentPositionPointer = vertices; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) { |
| 280 | if (force || vertices != mCurrentTexCoordsPointer) { |
| 281 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices); |
| 282 | mCurrentTexCoordsPointer = vertices; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void Caches::resetVertexPointers() { |
| 287 | mCurrentPositionPointer = this; |
| 288 | mCurrentTexCoordsPointer = this; |
| 289 | } |
| 290 | |
| 291 | void Caches::resetTexCoordsVertexPointer() { |
| 292 | mCurrentTexCoordsPointer = this; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 295 | void Caches::enableTexCoordsVertexArray() { |
| 296 | if (!mTexCoordsArrayEnabled) { |
| 297 | glEnableVertexAttribArray(Program::kBindingTexCoords); |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 298 | mCurrentTexCoordsPointer = this; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 299 | mTexCoordsArrayEnabled = true; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void Caches::disbaleTexCoordsVertexArray() { |
| 304 | if (mTexCoordsArrayEnabled) { |
| 305 | glDisableVertexAttribArray(Program::kBindingTexCoords); |
| 306 | mTexCoordsArrayEnabled = false; |
| 307 | } |
| 308 | } |
| 309 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 310 | void Caches::activeTexture(GLuint textureUnit) { |
| 311 | if (mTextureUnit != textureUnit) { |
| 312 | glActiveTexture(gTextureUnits[textureUnit]); |
| 313 | mTextureUnit = textureUnit; |
| 314 | } |
| 315 | } |
| 316 | |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame^] | 317 | void Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { |
| 318 | if (x != mScissorX || y != mScissorY || width != mScissorWidth || height != mScissorHeight) { |
| 319 | glScissor(x, y, width, height); |
| 320 | |
| 321 | mScissorX = x; |
| 322 | mScissorY = y; |
| 323 | mScissorWidth = width; |
| 324 | mScissorHeight = height; |
| 325 | } |
| 326 | } |
| 327 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 328 | TextureVertex* Caches::getRegionMesh() { |
| 329 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 330 | if (!mRegionMesh) { |
| 331 | mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; |
| 332 | |
| 333 | uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; |
| 334 | for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { |
| 335 | uint16_t quad = i * 4; |
| 336 | int index = i * 6; |
| 337 | regionIndices[index ] = quad; // top-left |
| 338 | regionIndices[index + 1] = quad + 1; // top-right |
| 339 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 340 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 341 | regionIndices[index + 4] = quad + 1; // top-right |
| 342 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 343 | } |
| 344 | |
| 345 | glGenBuffers(1, &mRegionMeshIndices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 346 | bindIndicesBuffer(mRegionMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 347 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), |
| 348 | regionIndices, GL_STATIC_DRAW); |
| 349 | |
| 350 | delete[] regionIndices; |
| 351 | } else { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 352 | bindIndicesBuffer(mRegionMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return mRegionMesh; |
| 356 | } |
| 357 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 358 | }; // namespace uirenderer |
| 359 | }; // namespace android |