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" |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 26 | #include "ShadowTessellator.h" |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | #ifdef USE_OPENGL_RENDERER |
| 31 | using namespace uirenderer; |
| 32 | ANDROID_SINGLETON_STATIC_INSTANCE(Caches); |
| 33 | #endif |
| 34 | |
| 35 | namespace uirenderer { |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 38 | // Macros |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
| 41 | #if DEBUG_CACHE_FLUSH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 42 | #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 43 | #else |
| 44 | #define FLUSH_LOGD(...) |
| 45 | #endif |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 48 | // Constructors/destructor |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 51 | Caches::Caches(): Singleton<Caches>(), |
| 52 | mExtensions(Extensions::getInstance()), mInitialized(false) { |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 53 | init(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 54 | initFont(); |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 55 | initConstraints(); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 56 | initProperties(); |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 57 | initStaticProperties(); |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 58 | initExtensions(); |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 59 | initTempProperties(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 60 | |
| 61 | mDebugLevel = readDebugLevel(); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 62 | ALOGD("Enabling debug mode %d", mDebugLevel); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 65 | bool Caches::init() { |
| 66 | if (mInitialized) return false; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 67 | |
| 68 | glGenBuffers(1, &meshBuffer); |
| 69 | glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); |
| 70 | glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); |
| 71 | |
| 72 | mCurrentBuffer = meshBuffer; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 73 | mCurrentIndicesBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 74 | mCurrentPositionPointer = this; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 75 | mCurrentPositionStride = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 76 | mCurrentTexCoordsPointer = this; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 77 | mCurrentPixelBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 78 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 79 | mTexCoordsArrayEnabled = false; |
| 80 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 81 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 82 | scissorEnabled = false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 83 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 84 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 85 | glActiveTexture(gTextureUnits[0]); |
| 86 | mTextureUnit = 0; |
| 87 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 88 | mRegionMesh = NULL; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 89 | mMeshIndices = 0; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 90 | mShadowStripsIndices = 0; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 91 | blend = false; |
| 92 | lastSrcMode = GL_ZERO; |
| 93 | lastDstMode = GL_ZERO; |
| 94 | currentProgram = NULL; |
| 95 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 96 | mFunctorsCount = 0; |
| 97 | |
Romain Guy | c2a9721 | 2013-02-06 15:29:46 -0800 | [diff] [blame] | 98 | debugLayersUpdates = false; |
| 99 | debugOverdraw = false; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 100 | debugStencilClip = kStencilHide; |
Romain Guy | c2a9721 | 2013-02-06 15:29:46 -0800 | [diff] [blame] | 101 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 102 | patchCache.init(*this); |
| 103 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 104 | mInitialized = true; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 105 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 106 | resetBoundTextures(); |
| 107 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 108 | return true; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 111 | void Caches::initFont() { |
| 112 | fontRenderer = GammaFontRenderer::createRenderer(); |
| 113 | } |
| 114 | |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 115 | void Caches::initExtensions() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 116 | if (mExtensions.hasDebugMarker()) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 117 | eventMark = glInsertEventMarkerEXT; |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 118 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 119 | startMark = glPushGroupMarkerEXT; |
| 120 | endMark = glPopGroupMarkerEXT; |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 121 | } else { |
| 122 | eventMark = eventMarkNull; |
| 123 | startMark = startMarkNull; |
| 124 | endMark = endMarkNull; |
| 125 | } |
| 126 | |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 127 | if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) { |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 128 | setLabel = glLabelObjectEXT; |
| 129 | getLabel = glGetObjectLabelEXT; |
| 130 | } else { |
| 131 | setLabel = setLabelNull; |
| 132 | getLabel = getLabelNull; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void Caches::initConstraints() { |
| 137 | GLint maxTextureUnits; |
| 138 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 139 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
| 140 | ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 141 | } |
| 142 | |
| 143 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 144 | } |
| 145 | |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 146 | void Caches::initStaticProperties() { |
| 147 | gpuPixelBuffersEnabled = false; |
| 148 | |
| 149 | // OpenGL ES 3.0+ specific features |
Romain Guy | 7f43076 | 2013-06-13 14:29:40 -0700 | [diff] [blame] | 150 | if (mExtensions.hasPixelBufferObjects()) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 151 | char property[PROPERTY_VALUE_MAX]; |
| 152 | if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) { |
| 153 | gpuPixelBuffersEnabled = !strcmp(property, "true"); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 158 | bool Caches::initProperties() { |
| 159 | bool prevDebugLayersUpdates = debugLayersUpdates; |
| 160 | bool prevDebugOverdraw = debugOverdraw; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 161 | StencilClipDebug prevDebugStencilClip = debugStencilClip; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 162 | |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 163 | char property[PROPERTY_VALUE_MAX]; |
| 164 | if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) { |
| 165 | INIT_LOGD(" Layers updates debug enabled: %s", property); |
| 166 | debugLayersUpdates = !strcmp(property, "true"); |
| 167 | } else { |
| 168 | debugLayersUpdates = false; |
| 169 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 170 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 171 | debugOverdraw = false; |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 172 | if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) { |
| 173 | INIT_LOGD(" Overdraw debug enabled: %s", property); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 174 | if (!strcmp(property, "show")) { |
| 175 | debugOverdraw = true; |
| 176 | mOverdrawDebugColorSet = kColorSet_Default; |
| 177 | } else if (!strcmp(property, "show_deuteranomaly")) { |
| 178 | debugOverdraw = true; |
| 179 | mOverdrawDebugColorSet = kColorSet_Deuteranomaly; |
| 180 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 181 | } |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 182 | |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 183 | // See Properties.h for valid values |
| 184 | if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) { |
| 185 | INIT_LOGD(" Stencil clip debug enabled: %s", property); |
| 186 | if (!strcmp(property, "hide")) { |
| 187 | debugStencilClip = kStencilHide; |
| 188 | } else if (!strcmp(property, "highlight")) { |
| 189 | debugStencilClip = kStencilShowHighlight; |
| 190 | } else if (!strcmp(property, "region")) { |
| 191 | debugStencilClip = kStencilShowRegion; |
| 192 | } |
| 193 | } else { |
| 194 | debugStencilClip = kStencilHide; |
| 195 | } |
| 196 | |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 197 | if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) { |
| 198 | drawDeferDisabled = !strcasecmp(property, "true"); |
| 199 | INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled"); |
| 200 | } else { |
| 201 | INIT_LOGD(" Draw defer enabled"); |
| 202 | } |
| 203 | |
| 204 | if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) { |
| 205 | drawReorderDisabled = !strcasecmp(property, "true"); |
| 206 | INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled"); |
| 207 | } else { |
| 208 | INIT_LOGD(" Draw reorder enabled"); |
| 209 | } |
| 210 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 211 | return (prevDebugLayersUpdates != debugLayersUpdates) || |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 212 | (prevDebugOverdraw != debugOverdraw) || |
| 213 | (prevDebugStencilClip != debugStencilClip); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 216 | void Caches::terminate() { |
| 217 | if (!mInitialized) return; |
| 218 | |
| 219 | glDeleteBuffers(1, &meshBuffer); |
| 220 | mCurrentBuffer = 0; |
| 221 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 222 | glDeleteBuffers(1, &mMeshIndices); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 223 | delete[] mRegionMesh; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 224 | mMeshIndices = 0; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 225 | mRegionMesh = NULL; |
| 226 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 227 | glDeleteBuffers(1, &mShadowStripsIndices); |
| 228 | mShadowStripsIndices = 0; |
| 229 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 230 | fboCache.clear(); |
| 231 | |
| 232 | programCache.clear(); |
| 233 | currentProgram = NULL; |
| 234 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 235 | assetAtlas.terminate(); |
| 236 | |
| 237 | patchCache.clear(); |
| 238 | |
Romain Guy | 46bfc48 | 2013-08-16 18:38:29 -0700 | [diff] [blame] | 239 | clearGarbage(); |
| 240 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 241 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 245 | // Debug |
| 246 | /////////////////////////////////////////////////////////////////////////////// |
| 247 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 248 | uint32_t Caches::getOverdrawColor(uint32_t amount) const { |
| 249 | static uint32_t sOverdrawColors[2][4] = { |
| 250 | { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 }, |
| 251 | { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 } |
| 252 | }; |
| 253 | if (amount < 1) amount = 1; |
| 254 | if (amount > 4) amount = 4; |
| 255 | return sOverdrawColors[mOverdrawDebugColorSet][amount - 1]; |
| 256 | } |
| 257 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 258 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 259 | String8 stringLog; |
| 260 | dumpMemoryUsage(stringLog); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 261 | ALOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void Caches::dumpMemoryUsage(String8 &log) { |
| 265 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
| 266 | log.appendFormat(" TextureCache %8d / %8d\n", |
| 267 | textureCache.getSize(), textureCache.getMaxSize()); |
| 268 | log.appendFormat(" LayerCache %8d / %8d\n", |
| 269 | layerCache.getSize(), layerCache.getMaxSize()); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 270 | log.appendFormat(" RenderBufferCache %8d / %8d\n", |
| 271 | renderBufferCache.getSize(), renderBufferCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 272 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 273 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 274 | log.appendFormat(" PathCache %8d / %8d\n", |
| 275 | pathCache.getSize(), pathCache.getMaxSize()); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 276 | log.appendFormat(" TessellationCache %8d / %8d\n", |
| 277 | tessellationCache.getSize(), tessellationCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 278 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 279 | dropShadowCache.getMaxSize()); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 280 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 281 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 282 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 283 | const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA); |
| 284 | const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA); |
| 285 | log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8); |
| 286 | log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA); |
| 287 | log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA, |
| 288 | sizeA8 + sizeRGBA); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 289 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 290 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 291 | log.appendFormat(" FboCache %8d / %8d\n", |
| 292 | fboCache.getSize(), fboCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 293 | |
| 294 | uint32_t total = 0; |
| 295 | total += textureCache.getSize(); |
| 296 | total += layerCache.getSize(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 297 | total += renderBufferCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 298 | total += gradientCache.getSize(); |
| 299 | total += pathCache.getSize(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 300 | total += tessellationCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 301 | total += dropShadowCache.getSize(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 302 | total += patchCache.getSize(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 303 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 304 | total += fontRenderer->getFontRendererSize(i, GL_ALPHA); |
| 305 | total += fontRenderer->getFontRendererSize(i, GL_RGBA); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 308 | log.appendFormat("Total memory usage:\n"); |
| 309 | 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] | 310 | } |
| 311 | |
| 312 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 313 | // Memory management |
| 314 | /////////////////////////////////////////////////////////////////////////////// |
| 315 | |
| 316 | void Caches::clearGarbage() { |
| 317 | textureCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 318 | pathCache.clearGarbage(); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 319 | patchCache.clearGarbage(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 320 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 321 | Vector<Layer*> layers; |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 322 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 323 | { // scope for the lock |
| 324 | Mutex::Autolock _l(mGarbageLock); |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 325 | layers = mLayerGarbage; |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 326 | mLayerGarbage.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 327 | } |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 328 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 329 | size_t count = layers.size(); |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 330 | for (size_t i = 0; i < count; i++) { |
| 331 | Layer* layer = layers.itemAt(i); |
| 332 | delete layer; |
| 333 | } |
| 334 | layers.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 337 | void Caches::deleteLayerDeferred(Layer* layer) { |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 338 | Mutex::Autolock _l(mGarbageLock); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 339 | mLayerGarbage.push(layer); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 342 | void Caches::flush(FlushMode mode) { |
| 343 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 344 | |
Romain Guy | b0a41ed | 2013-08-16 14:44:38 -0700 | [diff] [blame] | 345 | // We must stop tasks before clearing caches |
| 346 | if (mode > kFlushMode_Layers) { |
| 347 | tasks.stop(); |
| 348 | } |
| 349 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 350 | switch (mode) { |
| 351 | case kFlushMode_Full: |
| 352 | textureCache.clear(); |
| 353 | patchCache.clear(); |
| 354 | dropShadowCache.clear(); |
| 355 | gradientCache.clear(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 356 | fontRenderer->clear(); |
Romain Guy | b746371 | 2013-08-16 13:55:29 -0700 | [diff] [blame] | 357 | fboCache.clear(); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 358 | dither.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 359 | // fall through |
| 360 | case kFlushMode_Moderate: |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 361 | fontRenderer->flush(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 362 | textureCache.flush(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 363 | pathCache.clear(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 364 | tessellationCache.clear(); |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 365 | // fall through |
| 366 | case kFlushMode_Layers: |
| 367 | layerCache.clear(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 368 | renderBufferCache.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 369 | break; |
| 370 | } |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 371 | |
| 372 | clearGarbage(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 375 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 376 | // VBO |
| 377 | /////////////////////////////////////////////////////////////////////////////// |
| 378 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 379 | bool Caches::bindMeshBuffer() { |
| 380 | return bindMeshBuffer(meshBuffer); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 383 | bool Caches::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 384 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 385 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 386 | mCurrentBuffer = buffer; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 387 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 388 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 389 | return false; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 392 | bool Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 393 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 394 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 395 | mCurrentBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 396 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 397 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 401 | bool Caches::bindIndicesBufferInternal(const GLuint buffer) { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 402 | if (mCurrentIndicesBuffer != buffer) { |
| 403 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); |
| 404 | mCurrentIndicesBuffer = buffer; |
| 405 | return true; |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 410 | bool Caches::bindQuadIndicesBuffer() { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 411 | if (!mMeshIndices) { |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 412 | uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6]; |
| 413 | for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 414 | uint16_t quad = i * 4; |
| 415 | int index = i * 6; |
| 416 | regionIndices[index ] = quad; // top-left |
| 417 | regionIndices[index + 1] = quad + 1; // top-right |
| 418 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 419 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 420 | regionIndices[index + 4] = quad + 1; // top-right |
| 421 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 422 | } |
| 423 | |
| 424 | glGenBuffers(1, &mMeshIndices); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 425 | bool force = bindIndicesBufferInternal(mMeshIndices); |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 426 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t), |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 427 | regionIndices, GL_STATIC_DRAW); |
| 428 | |
| 429 | delete[] regionIndices; |
| 430 | return force; |
| 431 | } |
| 432 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 433 | return bindIndicesBufferInternal(mMeshIndices); |
| 434 | } |
| 435 | |
| 436 | bool Caches::bindShadowIndicesBuffer() { |
| 437 | if (!mShadowStripsIndices) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 438 | uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT]; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 439 | ShadowTessellator::generateShadowIndices(shadowIndices); |
| 440 | glGenBuffers(1, &mShadowStripsIndices); |
| 441 | bool force = bindIndicesBufferInternal(mShadowStripsIndices); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 442 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t), |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 443 | shadowIndices, GL_STATIC_DRAW); |
| 444 | |
| 445 | delete[] shadowIndices; |
| 446 | return force; |
| 447 | } |
| 448 | |
| 449 | return bindIndicesBufferInternal(mShadowStripsIndices); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 452 | bool Caches::unbindIndicesBuffer() { |
| 453 | if (mCurrentIndicesBuffer) { |
| 454 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 455 | mCurrentIndicesBuffer = 0; |
| 456 | return true; |
| 457 | } |
| 458 | return false; |
| 459 | } |
| 460 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 461 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 462 | // PBO |
| 463 | /////////////////////////////////////////////////////////////////////////////// |
| 464 | |
| 465 | bool Caches::bindPixelBuffer(const GLuint buffer) { |
| 466 | if (mCurrentPixelBuffer != buffer) { |
| 467 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer); |
| 468 | mCurrentPixelBuffer = buffer; |
| 469 | return true; |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | bool Caches::unbindPixelBuffer() { |
| 475 | if (mCurrentPixelBuffer) { |
| 476 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 477 | mCurrentPixelBuffer = 0; |
| 478 | return true; |
| 479 | } |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 484 | // Meshes and textures |
| 485 | /////////////////////////////////////////////////////////////////////////////// |
| 486 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 487 | void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 488 | if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) { |
| 489 | GLuint slot = currentProgram->position; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 490 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
| 491 | mCurrentPositionPointer = vertices; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 492 | mCurrentPositionStride = stride; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 496 | void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 497 | if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 498 | GLuint slot = currentProgram->texCoords; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 499 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 500 | mCurrentTexCoordsPointer = vertices; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 501 | mCurrentTexCoordsStride = stride; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | void Caches::resetVertexPointers() { |
| 506 | mCurrentPositionPointer = this; |
| 507 | mCurrentTexCoordsPointer = this; |
| 508 | } |
| 509 | |
| 510 | void Caches::resetTexCoordsVertexPointer() { |
| 511 | mCurrentTexCoordsPointer = this; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 514 | void Caches::enableTexCoordsVertexArray() { |
| 515 | if (!mTexCoordsArrayEnabled) { |
| 516 | glEnableVertexAttribArray(Program::kBindingTexCoords); |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 517 | mCurrentTexCoordsPointer = this; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 518 | mTexCoordsArrayEnabled = true; |
| 519 | } |
| 520 | } |
| 521 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 522 | void Caches::disableTexCoordsVertexArray() { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 523 | if (mTexCoordsArrayEnabled) { |
| 524 | glDisableVertexAttribArray(Program::kBindingTexCoords); |
| 525 | mTexCoordsArrayEnabled = false; |
| 526 | } |
| 527 | } |
| 528 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 529 | void Caches::activeTexture(GLuint textureUnit) { |
| 530 | if (mTextureUnit != textureUnit) { |
| 531 | glActiveTexture(gTextureUnits[textureUnit]); |
| 532 | mTextureUnit = textureUnit; |
| 533 | } |
| 534 | } |
| 535 | |
Chris Craik | 9ab2d18 | 2013-07-22 16:16:06 -0700 | [diff] [blame] | 536 | void Caches::resetActiveTexture() { |
| 537 | mTextureUnit = -1; |
| 538 | } |
| 539 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 540 | void Caches::bindTexture(GLuint texture) { |
| 541 | if (mBoundTextures[mTextureUnit] != texture) { |
| 542 | glBindTexture(GL_TEXTURE_2D, texture); |
| 543 | mBoundTextures[mTextureUnit] = texture; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | void Caches::bindTexture(GLenum target, GLuint texture) { |
| 548 | if (mBoundTextures[mTextureUnit] != texture) { |
| 549 | glBindTexture(target, texture); |
| 550 | mBoundTextures[mTextureUnit] = texture; |
| 551 | } |
| 552 | } |
| 553 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 554 | void Caches::deleteTexture(GLuint texture) { |
| 555 | // When glDeleteTextures() is called on a currently bound texture, |
| 556 | // OpenGL ES specifies that the texture is then considered unbound |
| 557 | // Consider the following series of calls: |
| 558 | // |
| 559 | // glGenTextures -> creates texture name 2 |
| 560 | // glBindTexture(2) |
| 561 | // glDeleteTextures(2) -> 2 is now unbound |
| 562 | // glGenTextures -> can return 2 again |
| 563 | // |
| 564 | // If we don't call glBindTexture(2) after the second glGenTextures |
| 565 | // call, any texture operation will be performed on the default |
| 566 | // texture (name=0) |
| 567 | |
jiayuanr | 4a473c7d | 2014-06-10 17:41:49 +0800 | [diff] [blame] | 568 | unbindTexture(texture); |
| 569 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 570 | glDeleteTextures(1, &texture); |
| 571 | } |
| 572 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 573 | void Caches::resetBoundTextures() { |
| 574 | memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint)); |
| 575 | } |
| 576 | |
jiayuanr | 4a473c7d | 2014-06-10 17:41:49 +0800 | [diff] [blame] | 577 | void Caches::unbindTexture(GLuint texture) { |
| 578 | for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) { |
| 579 | if (mBoundTextures[i] == texture) { |
| 580 | mBoundTextures[i] = 0; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 585 | /////////////////////////////////////////////////////////////////////////////// |
| 586 | // Scissor |
| 587 | /////////////////////////////////////////////////////////////////////////////// |
| 588 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 589 | bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 590 | if (scissorEnabled && (x != mScissorX || y != mScissorY || |
| 591 | width != mScissorWidth || height != mScissorHeight)) { |
| 592 | |
Chet Haase | aa42c9a | 2012-10-16 17:36:16 -0700 | [diff] [blame] | 593 | if (x < 0) { |
| 594 | width += x; |
| 595 | x = 0; |
| 596 | } |
| 597 | if (y < 0) { |
| 598 | height += y; |
| 599 | y = 0; |
| 600 | } |
| 601 | if (width < 0) { |
| 602 | width = 0; |
| 603 | } |
| 604 | if (height < 0) { |
| 605 | height = 0; |
| 606 | } |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 607 | glScissor(x, y, width, height); |
| 608 | |
| 609 | mScissorX = x; |
| 610 | mScissorY = y; |
| 611 | mScissorWidth = width; |
| 612 | mScissorHeight = height; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 613 | |
| 614 | return true; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 615 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 616 | return false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 619 | bool Caches::enableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 620 | if (!scissorEnabled) { |
| 621 | glEnable(GL_SCISSOR_TEST); |
| 622 | scissorEnabled = true; |
Romain Guy | 50ae66a | 2012-10-07 14:05:59 -0700 | [diff] [blame] | 623 | resetScissor(); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 624 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 625 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 626 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 629 | bool Caches::disableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 630 | if (scissorEnabled) { |
| 631 | glDisable(GL_SCISSOR_TEST); |
| 632 | scissorEnabled = false; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 633 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 634 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 635 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void Caches::setScissorEnabled(bool enabled) { |
| 639 | if (scissorEnabled != enabled) { |
| 640 | if (enabled) glEnable(GL_SCISSOR_TEST); |
| 641 | else glDisable(GL_SCISSOR_TEST); |
| 642 | scissorEnabled = enabled; |
| 643 | } |
| 644 | } |
| 645 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 646 | void Caches::resetScissor() { |
| 647 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 648 | } |
| 649 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 650 | /////////////////////////////////////////////////////////////////////////////// |
| 651 | // Tiling |
| 652 | /////////////////////////////////////////////////////////////////////////////// |
| 653 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 654 | void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 655 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 656 | glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM)); |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | void Caches::endTiling() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 661 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 662 | glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM); |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 666 | bool Caches::hasRegisteredFunctors() { |
| 667 | return mFunctorsCount > 0; |
| 668 | } |
| 669 | |
| 670 | void Caches::registerFunctors(uint32_t functorCount) { |
| 671 | mFunctorsCount += functorCount; |
| 672 | } |
| 673 | |
| 674 | void Caches::unregisterFunctors(uint32_t functorCount) { |
| 675 | if (functorCount > mFunctorsCount) { |
| 676 | mFunctorsCount = 0; |
| 677 | } else { |
| 678 | mFunctorsCount -= functorCount; |
| 679 | } |
| 680 | } |
| 681 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 682 | /////////////////////////////////////////////////////////////////////////////// |
| 683 | // Regions |
| 684 | /////////////////////////////////////////////////////////////////////////////// |
| 685 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 686 | TextureVertex* Caches::getRegionMesh() { |
| 687 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 688 | if (!mRegionMesh) { |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 689 | mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4]; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | return mRegionMesh; |
| 693 | } |
| 694 | |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 695 | /////////////////////////////////////////////////////////////////////////////// |
| 696 | // Temporary Properties |
| 697 | /////////////////////////////////////////////////////////////////////////////// |
| 698 | |
| 699 | void Caches::initTempProperties() { |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 700 | propertyAmbientShadowStrength = 12; |
| 701 | propertySpotShadowStrength = 48; |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 702 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 703 | propertyLightDiameter = -1.0f; |
| 704 | propertyLightPosY = -1.0f; |
| 705 | propertyLightPosZ = -1.0f; |
| 706 | propertyAmbientRatio = -1.0f; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void Caches::setTempProperty(const char* name, const char* value) { |
| 710 | ALOGD("setting property %s to %s", name, value); |
Chris Craik | e361ad7 | 2014-03-11 10:48:43 -0700 | [diff] [blame] | 711 | if (!strcmp(name, "ambientShadowStrength")) { |
ztenghui | ef94c6f | 2014-02-13 17:09:45 -0800 | [diff] [blame] | 712 | propertyAmbientShadowStrength = atoi(value); |
| 713 | ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength); |
| 714 | return; |
| 715 | } else if (!strcmp(name, "spotShadowStrength")) { |
| 716 | propertySpotShadowStrength = atoi(value); |
| 717 | ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength); |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 718 | return; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 719 | } else if (!strcmp(name, "ambientRatio")) { |
| 720 | propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0); |
| 721 | ALOGD("ambientRatio = %.2f", propertyAmbientRatio); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 722 | return; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 723 | } else if (!strcmp(name, "lightDiameter")) { |
| 724 | propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0); |
| 725 | ALOGD("lightDiameter = %.2f", propertyLightDiameter); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 726 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame^] | 727 | } else if (!strcmp(name, "lightPosY")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 728 | propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0); |
| 729 | ALOGD("lightPos Y = %.2f", propertyLightPosY); |
| 730 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame^] | 731 | } else if (!strcmp(name, "lightPosZ")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 732 | propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0); |
| 733 | ALOGD("lightPos Z = %.2f", propertyLightPosZ); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 734 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame^] | 735 | } else if (!strcmp(name, "extraRasterBucket")) { |
| 736 | float bucket = atof(value); |
| 737 | propertyExtraRasterBuckets.push_back(bucket); |
| 738 | return; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 739 | } |
| 740 | ALOGD(" failed"); |
| 741 | } |
| 742 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 743 | }; // namespace uirenderer |
| 744 | }; // namespace android |