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 | |
| 32 | #ifdef USE_OPENGL_RENDERER |
| 33 | using namespace uirenderer; |
| 34 | ANDROID_SINGLETON_STATIC_INSTANCE(Caches); |
| 35 | #endif |
| 36 | |
| 37 | namespace uirenderer { |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 40 | // Macros |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | #if DEBUG_CACHE_FLUSH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 44 | #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 45 | #else |
| 46 | #define FLUSH_LOGD(...) |
| 47 | #endif |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 50 | // Constructors/destructor |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 53 | Caches::Caches(): Singleton<Caches>(), |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 54 | mExtensions(Extensions::getInstance()), mInitialized(false), mRenderState(NULL) { |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 55 | init(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 56 | initFont(); |
Romain Guy | dfa1046 | 2012-05-12 16:18:58 -0700 | [diff] [blame] | 57 | initConstraints(); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 58 | initProperties(); |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 59 | initStaticProperties(); |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 60 | initExtensions(); |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 61 | initTempProperties(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 62 | |
| 63 | mDebugLevel = readDebugLevel(); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 64 | ALOGD("Enabling debug mode %d", mDebugLevel); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 67 | bool Caches::init() { |
| 68 | if (mInitialized) return false; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 69 | |
| 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 | 0f667533 | 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 | 0f667533 | 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 | 0f667533 | 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 | 0f667533 | 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 | 0f667533 | 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 | assetAtlas.terminate(); |
| 240 | |
| 241 | patchCache.clear(); |
| 242 | |
Romain Guy | 46bfc48 | 2013-08-16 18:38:29 -0700 | [diff] [blame] | 243 | clearGarbage(); |
| 244 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 245 | mInitialized = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 249 | // Debug |
| 250 | /////////////////////////////////////////////////////////////////////////////// |
| 251 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 252 | uint32_t Caches::getOverdrawColor(uint32_t amount) const { |
| 253 | static uint32_t sOverdrawColors[2][4] = { |
| 254 | { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 }, |
| 255 | { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 } |
| 256 | }; |
| 257 | if (amount < 1) amount = 1; |
| 258 | if (amount > 4) amount = 4; |
| 259 | return sOverdrawColors[mOverdrawDebugColorSet][amount - 1]; |
| 260 | } |
| 261 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 262 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 263 | String8 stringLog; |
| 264 | dumpMemoryUsage(stringLog); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 265 | ALOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void Caches::dumpMemoryUsage(String8 &log) { |
| 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()); |
| 274 | log.appendFormat(" Garbage layers %8zu\n", mLayerGarbage.size()); |
| 275 | log.appendFormat(" Active layers %8zu\n", |
| 276 | mRenderState ? mRenderState->mActiveLayers.size() : 0); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 277 | log.appendFormat(" RenderBufferCache %8d / %8d\n", |
| 278 | renderBufferCache.getSize(), renderBufferCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 279 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 280 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 281 | log.appendFormat(" PathCache %8d / %8d\n", |
| 282 | pathCache.getSize(), pathCache.getMaxSize()); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 283 | log.appendFormat(" TessellationCache %8d / %8d\n", |
| 284 | tessellationCache.getSize(), tessellationCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 285 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 286 | dropShadowCache.getMaxSize()); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 287 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 288 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 289 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 290 | const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA); |
| 291 | const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA); |
| 292 | log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8); |
| 293 | log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA); |
| 294 | log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA, |
| 295 | sizeA8 + sizeRGBA); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 296 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 297 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 298 | log.appendFormat(" FboCache %8d / %8d\n", |
| 299 | fboCache.getSize(), fboCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 300 | |
| 301 | uint32_t total = 0; |
| 302 | total += textureCache.getSize(); |
| 303 | total += layerCache.getSize(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 304 | total += renderBufferCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 305 | total += gradientCache.getSize(); |
| 306 | total += pathCache.getSize(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 307 | total += tessellationCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 308 | total += dropShadowCache.getSize(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 309 | total += patchCache.getSize(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 310 | for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) { |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 311 | total += fontRenderer->getFontRendererSize(i, GL_ALPHA); |
| 312 | total += fontRenderer->getFontRendererSize(i, GL_RGBA); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 315 | log.appendFormat("Total memory usage:\n"); |
| 316 | 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] | 317 | } |
| 318 | |
| 319 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 320 | // Memory management |
| 321 | /////////////////////////////////////////////////////////////////////////////// |
| 322 | |
| 323 | void Caches::clearGarbage() { |
| 324 | textureCache.clearGarbage(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 325 | pathCache.clearGarbage(); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 326 | patchCache.clearGarbage(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 327 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 328 | Vector<Layer*> layers; |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 329 | |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 330 | { // scope for the lock |
| 331 | Mutex::Autolock _l(mGarbageLock); |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 332 | layers = mLayerGarbage; |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 333 | mLayerGarbage.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 334 | } |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 335 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 336 | size_t count = layers.size(); |
Mathias Agopian | 17ef62c | 2012-09-25 22:52:40 -0700 | [diff] [blame] | 337 | for (size_t i = 0; i < count; i++) { |
| 338 | Layer* layer = layers.itemAt(i); |
| 339 | delete layer; |
| 340 | } |
| 341 | layers.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 344 | void Caches::deleteLayerDeferred(Layer* layer) { |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 345 | Mutex::Autolock _l(mGarbageLock); |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 346 | layer->state = Layer::kState_InGarbageList; |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 347 | mLayerGarbage.push(layer); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 350 | void Caches::flush(FlushMode mode) { |
| 351 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 352 | |
Romain Guy | b0a41ed | 2013-08-16 14:44:38 -0700 | [diff] [blame] | 353 | // We must stop tasks before clearing caches |
| 354 | if (mode > kFlushMode_Layers) { |
| 355 | tasks.stop(); |
| 356 | } |
| 357 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 358 | switch (mode) { |
| 359 | case kFlushMode_Full: |
| 360 | textureCache.clear(); |
| 361 | patchCache.clear(); |
| 362 | dropShadowCache.clear(); |
| 363 | gradientCache.clear(); |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 364 | fontRenderer->clear(); |
Romain Guy | b746371 | 2013-08-16 13:55:29 -0700 | [diff] [blame] | 365 | fboCache.clear(); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 366 | dither.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 367 | // fall through |
| 368 | case kFlushMode_Moderate: |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 369 | fontRenderer->flush(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 370 | textureCache.flush(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 371 | pathCache.clear(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 372 | tessellationCache.clear(); |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 373 | // fall through |
| 374 | case kFlushMode_Layers: |
| 375 | layerCache.clear(); |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 376 | renderBufferCache.clear(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 377 | break; |
| 378 | } |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 379 | |
| 380 | clearGarbage(); |
John Reck | 4ced262 | 2014-09-19 10:10:19 -0700 | [diff] [blame] | 381 | glFinish(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 384 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 385 | // VBO |
| 386 | /////////////////////////////////////////////////////////////////////////////// |
| 387 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 388 | bool Caches::bindMeshBuffer() { |
| 389 | return bindMeshBuffer(meshBuffer); |
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::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 393 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 394 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 395 | mCurrentBuffer = buffer; |
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; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 401 | bool Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 402 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 403 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 404 | mCurrentBuffer = 0; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 405 | return true; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 406 | } |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 410 | bool Caches::bindIndicesBufferInternal(const GLuint buffer) { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 411 | if (mCurrentIndicesBuffer != buffer) { |
| 412 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); |
| 413 | mCurrentIndicesBuffer = buffer; |
| 414 | return true; |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 419 | bool Caches::bindQuadIndicesBuffer() { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 420 | if (!mMeshIndices) { |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 421 | uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6]; |
| 422 | for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 423 | uint16_t quad = i * 4; |
| 424 | int index = i * 6; |
| 425 | regionIndices[index ] = quad; // top-left |
| 426 | regionIndices[index + 1] = quad + 1; // top-right |
| 427 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 428 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 429 | regionIndices[index + 4] = quad + 1; // top-right |
| 430 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 431 | } |
| 432 | |
| 433 | glGenBuffers(1, &mMeshIndices); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 434 | bool force = bindIndicesBufferInternal(mMeshIndices); |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 435 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t), |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 436 | regionIndices, GL_STATIC_DRAW); |
| 437 | |
| 438 | delete[] regionIndices; |
| 439 | return force; |
| 440 | } |
| 441 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 442 | return bindIndicesBufferInternal(mMeshIndices); |
| 443 | } |
| 444 | |
| 445 | bool Caches::bindShadowIndicesBuffer() { |
| 446 | if (!mShadowStripsIndices) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 447 | uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT]; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 448 | ShadowTessellator::generateShadowIndices(shadowIndices); |
| 449 | glGenBuffers(1, &mShadowStripsIndices); |
| 450 | bool force = bindIndicesBufferInternal(mShadowStripsIndices); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 451 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t), |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 452 | shadowIndices, GL_STATIC_DRAW); |
| 453 | |
| 454 | delete[] shadowIndices; |
| 455 | return force; |
| 456 | } |
| 457 | |
| 458 | return bindIndicesBufferInternal(mShadowStripsIndices); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 461 | bool Caches::unbindIndicesBuffer() { |
| 462 | if (mCurrentIndicesBuffer) { |
| 463 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 464 | mCurrentIndicesBuffer = 0; |
| 465 | return true; |
| 466 | } |
| 467 | return false; |
| 468 | } |
| 469 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 470 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 471 | // PBO |
| 472 | /////////////////////////////////////////////////////////////////////////////// |
| 473 | |
| 474 | bool Caches::bindPixelBuffer(const GLuint buffer) { |
| 475 | if (mCurrentPixelBuffer != buffer) { |
| 476 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer); |
| 477 | mCurrentPixelBuffer = buffer; |
| 478 | return true; |
| 479 | } |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | bool Caches::unbindPixelBuffer() { |
| 484 | if (mCurrentPixelBuffer) { |
| 485 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 486 | mCurrentPixelBuffer = 0; |
| 487 | return true; |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 493 | // Meshes and textures |
| 494 | /////////////////////////////////////////////////////////////////////////////// |
| 495 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 496 | void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 497 | if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) { |
| 498 | GLuint slot = currentProgram->position; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 499 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
| 500 | mCurrentPositionPointer = vertices; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 501 | mCurrentPositionStride = stride; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 505 | void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 506 | if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 507 | GLuint slot = currentProgram->texCoords; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 508 | glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 509 | mCurrentTexCoordsPointer = vertices; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 510 | mCurrentTexCoordsStride = stride; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | void Caches::resetVertexPointers() { |
| 515 | mCurrentPositionPointer = this; |
| 516 | mCurrentTexCoordsPointer = this; |
| 517 | } |
| 518 | |
| 519 | void Caches::resetTexCoordsVertexPointer() { |
| 520 | mCurrentTexCoordsPointer = this; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 523 | void Caches::enableTexCoordsVertexArray() { |
| 524 | if (!mTexCoordsArrayEnabled) { |
| 525 | glEnableVertexAttribArray(Program::kBindingTexCoords); |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 526 | mCurrentTexCoordsPointer = this; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 527 | mTexCoordsArrayEnabled = true; |
| 528 | } |
| 529 | } |
| 530 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 531 | void Caches::disableTexCoordsVertexArray() { |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 532 | if (mTexCoordsArrayEnabled) { |
| 533 | glDisableVertexAttribArray(Program::kBindingTexCoords); |
| 534 | mTexCoordsArrayEnabled = false; |
| 535 | } |
| 536 | } |
| 537 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 538 | void Caches::activeTexture(GLuint textureUnit) { |
| 539 | if (mTextureUnit != textureUnit) { |
| 540 | glActiveTexture(gTextureUnits[textureUnit]); |
| 541 | mTextureUnit = textureUnit; |
| 542 | } |
| 543 | } |
| 544 | |
Chris Craik | 9ab2d18 | 2013-07-22 16:16:06 -0700 | [diff] [blame] | 545 | void Caches::resetActiveTexture() { |
| 546 | mTextureUnit = -1; |
| 547 | } |
| 548 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 549 | void Caches::bindTexture(GLuint texture) { |
| 550 | if (mBoundTextures[mTextureUnit] != texture) { |
| 551 | glBindTexture(GL_TEXTURE_2D, texture); |
| 552 | mBoundTextures[mTextureUnit] = texture; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void Caches::bindTexture(GLenum target, GLuint texture) { |
Fred Fettinger | 70735bd | 2014-08-29 14:02:31 -0500 | [diff] [blame] | 557 | if (target == GL_TEXTURE_2D) { |
| 558 | bindTexture(texture); |
| 559 | } else { |
| 560 | // GLConsumer directly calls glBindTexture() with |
| 561 | // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target |
| 562 | // since the cached state could be stale |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 563 | glBindTexture(target, texture); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 567 | void Caches::deleteTexture(GLuint texture) { |
| 568 | // When glDeleteTextures() is called on a currently bound texture, |
| 569 | // OpenGL ES specifies that the texture is then considered unbound |
| 570 | // Consider the following series of calls: |
| 571 | // |
| 572 | // glGenTextures -> creates texture name 2 |
| 573 | // glBindTexture(2) |
| 574 | // glDeleteTextures(2) -> 2 is now unbound |
| 575 | // glGenTextures -> can return 2 again |
| 576 | // |
| 577 | // If we don't call glBindTexture(2) after the second glGenTextures |
| 578 | // call, any texture operation will be performed on the default |
| 579 | // texture (name=0) |
| 580 | |
jiayuanr | 4a473c7d | 2014-06-10 17:41:49 +0800 | [diff] [blame] | 581 | unbindTexture(texture); |
| 582 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 583 | glDeleteTextures(1, &texture); |
| 584 | } |
| 585 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 586 | void Caches::resetBoundTextures() { |
| 587 | memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint)); |
| 588 | } |
| 589 | |
jiayuanr | 4a473c7d | 2014-06-10 17:41:49 +0800 | [diff] [blame] | 590 | void Caches::unbindTexture(GLuint texture) { |
| 591 | for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) { |
| 592 | if (mBoundTextures[i] == texture) { |
| 593 | mBoundTextures[i] = 0; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 598 | /////////////////////////////////////////////////////////////////////////////// |
| 599 | // Scissor |
| 600 | /////////////////////////////////////////////////////////////////////////////// |
| 601 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 602 | bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 603 | if (scissorEnabled && (x != mScissorX || y != mScissorY || |
| 604 | width != mScissorWidth || height != mScissorHeight)) { |
| 605 | |
Chet Haase | aa42c9a | 2012-10-16 17:36:16 -0700 | [diff] [blame] | 606 | if (x < 0) { |
| 607 | width += x; |
| 608 | x = 0; |
| 609 | } |
| 610 | if (y < 0) { |
| 611 | height += y; |
| 612 | y = 0; |
| 613 | } |
| 614 | if (width < 0) { |
| 615 | width = 0; |
| 616 | } |
| 617 | if (height < 0) { |
| 618 | height = 0; |
| 619 | } |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 620 | glScissor(x, y, width, height); |
| 621 | |
| 622 | mScissorX = x; |
| 623 | mScissorY = y; |
| 624 | mScissorWidth = width; |
| 625 | mScissorHeight = height; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 626 | |
| 627 | return true; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 628 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 629 | return false; |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 632 | bool Caches::enableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 633 | if (!scissorEnabled) { |
| 634 | glEnable(GL_SCISSOR_TEST); |
| 635 | scissorEnabled = true; |
Romain Guy | 50ae66a | 2012-10-07 14:05:59 -0700 | [diff] [blame] | 636 | resetScissor(); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 637 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 638 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 639 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 642 | bool Caches::disableScissor() { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 643 | if (scissorEnabled) { |
| 644 | glDisable(GL_SCISSOR_TEST); |
| 645 | scissorEnabled = false; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 646 | return true; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 647 | } |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 648 | return false; |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | void Caches::setScissorEnabled(bool enabled) { |
| 652 | if (scissorEnabled != enabled) { |
| 653 | if (enabled) glEnable(GL_SCISSOR_TEST); |
| 654 | else glDisable(GL_SCISSOR_TEST); |
| 655 | scissorEnabled = enabled; |
| 656 | } |
| 657 | } |
| 658 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 659 | void Caches::resetScissor() { |
| 660 | mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0; |
| 661 | } |
| 662 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 663 | /////////////////////////////////////////////////////////////////////////////// |
| 664 | // Tiling |
| 665 | /////////////////////////////////////////////////////////////////////////////// |
| 666 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 667 | 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] | 668 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 669 | 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] | 670 | } |
| 671 | } |
| 672 | |
| 673 | void Caches::endTiling() { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 674 | if (mExtensions.hasTiledRendering() && !debugOverdraw) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 675 | glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM); |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 679 | bool Caches::hasRegisteredFunctors() { |
| 680 | return mFunctorsCount > 0; |
| 681 | } |
| 682 | |
| 683 | void Caches::registerFunctors(uint32_t functorCount) { |
| 684 | mFunctorsCount += functorCount; |
| 685 | } |
| 686 | |
| 687 | void Caches::unregisterFunctors(uint32_t functorCount) { |
| 688 | if (functorCount > mFunctorsCount) { |
| 689 | mFunctorsCount = 0; |
| 690 | } else { |
| 691 | mFunctorsCount -= functorCount; |
| 692 | } |
| 693 | } |
| 694 | |
Romain Guy | 85ef80d | 2012-09-13 20:26:50 -0700 | [diff] [blame] | 695 | /////////////////////////////////////////////////////////////////////////////// |
| 696 | // Regions |
| 697 | /////////////////////////////////////////////////////////////////////////////// |
| 698 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 699 | TextureVertex* Caches::getRegionMesh() { |
| 700 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 701 | if (!mRegionMesh) { |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 702 | mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4]; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | return mRegionMesh; |
| 706 | } |
| 707 | |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 708 | /////////////////////////////////////////////////////////////////////////////// |
| 709 | // Temporary Properties |
| 710 | /////////////////////////////////////////////////////////////////////////////// |
| 711 | |
| 712 | void Caches::initTempProperties() { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 713 | propertyLightDiameter = -1.0f; |
| 714 | propertyLightPosY = -1.0f; |
| 715 | propertyLightPosZ = -1.0f; |
| 716 | propertyAmbientRatio = -1.0f; |
ztenghui | 14a4e35 | 2014-08-13 10:44:39 -0700 | [diff] [blame] | 717 | propertyAmbientShadowStrength = -1; |
| 718 | propertySpotShadowStrength = -1; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void Caches::setTempProperty(const char* name, const char* value) { |
| 722 | ALOGD("setting property %s to %s", name, value); |
Chris Craik | a736cd9 | 2014-08-04 13:18:38 -0700 | [diff] [blame] | 723 | if (!strcmp(name, "ambientRatio")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 724 | propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0); |
| 725 | ALOGD("ambientRatio = %.2f", propertyAmbientRatio); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 726 | return; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 727 | } else if (!strcmp(name, "lightDiameter")) { |
| 728 | propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0); |
| 729 | ALOGD("lightDiameter = %.2f", propertyLightDiameter); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 730 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 731 | } else if (!strcmp(name, "lightPosY")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 732 | propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0); |
| 733 | ALOGD("lightPos Y = %.2f", propertyLightPosY); |
| 734 | return; |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 735 | } else if (!strcmp(name, "lightPosZ")) { |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 736 | propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0); |
| 737 | ALOGD("lightPos Z = %.2f", propertyLightPosZ); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 738 | return; |
ztenghui | 14a4e35 | 2014-08-13 10:44:39 -0700 | [diff] [blame] | 739 | } else if (!strcmp(name, "ambientShadowStrength")) { |
| 740 | propertyAmbientShadowStrength = atoi(value); |
| 741 | ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength); |
| 742 | return; |
| 743 | } else if (!strcmp(name, "spotShadowStrength")) { |
| 744 | propertySpotShadowStrength = atoi(value); |
| 745 | ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength); |
| 746 | return; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 747 | } |
| 748 | ALOGD(" failed"); |
| 749 | } |
| 750 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 751 | }; // namespace uirenderer |
| 752 | }; // namespace android |