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