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