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