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 | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 23 | #include "Properties.h" |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 24 | #include "LayerRenderer.h" |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | #ifdef USE_OPENGL_RENDERER |
| 29 | using namespace uirenderer; |
| 30 | ANDROID_SINGLETON_STATIC_INSTANCE(Caches); |
| 31 | #endif |
| 32 | |
| 33 | namespace uirenderer { |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame^] | 36 | // Macros |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | #if DEBUG_CACHE_FLUSH |
| 40 | #define FLUSH_LOGD(...) LOGD(__VA_ARGS__) |
| 41 | #else |
| 42 | #define FLUSH_LOGD(...) |
| 43 | #endif |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 46 | // Constructors/destructor |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
| 49 | Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO), |
| 50 | lastDstMode(GL_ZERO), currentProgram(NULL) { |
| 51 | GLint maxTextureUnits; |
| 52 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 53 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
| 54 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 55 | } |
| 56 | |
| 57 | glGenBuffers(1, &meshBuffer); |
| 58 | glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); |
| 59 | glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); |
| 60 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 61 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 62 | |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 63 | mCurrentBuffer = meshBuffer; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 64 | mRegionMesh = NULL; |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 65 | |
| 66 | mDebugLevel = readDebugLevel(); |
| 67 | LOGD("Enabling debug mode %d", mDebugLevel); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 68 | |
| 69 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 70 | INIT_LOGD("Layers will be composited as regions"); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 71 | #endif |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 74 | Caches::~Caches() { |
| 75 | delete[] mRegionMesh; |
| 76 | } |
| 77 | |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 79 | // Debug |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | void Caches::dumpMemoryUsage() { |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 83 | String8 stringLog; |
| 84 | dumpMemoryUsage(stringLog); |
| 85 | LOGD("%s", stringLog.string()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void Caches::dumpMemoryUsage(String8 &log) { |
| 89 | log.appendFormat("Current memory usage / total memory usage (bytes):\n"); |
| 90 | log.appendFormat(" TextureCache %8d / %8d\n", |
| 91 | textureCache.getSize(), textureCache.getMaxSize()); |
| 92 | log.appendFormat(" LayerCache %8d / %8d\n", |
| 93 | layerCache.getSize(), layerCache.getMaxSize()); |
| 94 | log.appendFormat(" GradientCache %8d / %8d\n", |
| 95 | gradientCache.getSize(), gradientCache.getMaxSize()); |
| 96 | log.appendFormat(" PathCache %8d / %8d\n", |
| 97 | pathCache.getSize(), pathCache.getMaxSize()); |
| 98 | log.appendFormat(" CircleShapeCache %8d / %8d\n", |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 99 | circleShapeCache.getSize(), circleShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 100 | log.appendFormat(" OvalShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 101 | ovalShapeCache.getSize(), ovalShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 102 | log.appendFormat(" RoundRectShapeCache %8d / %8d\n", |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 103 | roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 104 | log.appendFormat(" RectShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 105 | rectShapeCache.getSize(), rectShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 106 | log.appendFormat(" ArcShapeCache %8d / %8d\n", |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 107 | arcShapeCache.getSize(), arcShapeCache.getMaxSize()); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 108 | log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(), |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 109 | dropShadowCache.getMaxSize()); |
| 110 | for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { |
| 111 | const uint32_t size = fontRenderer.getFontRendererSize(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 112 | log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 113 | } |
Romain Guy | d2ba50a | 2011-05-27 10:21:07 -0700 | [diff] [blame] | 114 | log.appendFormat("Other:\n"); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 115 | log.appendFormat(" FboCache %8d / %8d\n", |
| 116 | fboCache.getSize(), fboCache.getMaxSize()); |
| 117 | log.appendFormat(" PatchCache %8d / %8d\n", |
| 118 | patchCache.getSize(), patchCache.getMaxSize()); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 119 | |
| 120 | uint32_t total = 0; |
| 121 | total += textureCache.getSize(); |
| 122 | total += layerCache.getSize(); |
| 123 | total += gradientCache.getSize(); |
| 124 | total += pathCache.getSize(); |
| 125 | total += dropShadowCache.getSize(); |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 126 | total += roundRectShapeCache.getSize(); |
| 127 | total += circleShapeCache.getSize(); |
| 128 | total += ovalShapeCache.getSize(); |
| 129 | total += rectShapeCache.getSize(); |
| 130 | total += arcShapeCache.getSize(); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 131 | for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { |
| 132 | total += fontRenderer.getFontRendererSize(i); |
| 133 | } |
| 134 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 135 | log.appendFormat("Total memory usage:\n"); |
| 136 | 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] | 137 | } |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 140 | // Memory management |
| 141 | /////////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | void Caches::clearGarbage() { |
| 144 | textureCache.clearGarbage(); |
| 145 | gradientCache.clearGarbage(); |
| 146 | pathCache.clearGarbage(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 147 | |
| 148 | Mutex::Autolock _l(mGarbageLock); |
| 149 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 150 | size_t count = mLayerGarbage.size(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 151 | for (size_t i = 0; i < count; i++) { |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 152 | Layer* layer = mLayerGarbage.itemAt(i); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 153 | LayerRenderer::destroyLayer(layer); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 154 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 155 | mLayerGarbage.clear(); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 158 | void Caches::deleteLayerDeferred(Layer* layer) { |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 159 | Mutex::Autolock _l(mGarbageLock); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 160 | mLayerGarbage.push(layer); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame^] | 163 | void Caches::flush(FlushMode mode) { |
| 164 | FLUSH_LOGD("Flushing caches (mode %d)", mode); |
| 165 | |
| 166 | clearGarbage(); |
| 167 | |
| 168 | switch (mode) { |
| 169 | case kFlushMode_Full: |
| 170 | textureCache.clear(); |
| 171 | patchCache.clear(); |
| 172 | dropShadowCache.clear(); |
| 173 | gradientCache.clear(); |
| 174 | // fall through |
| 175 | case kFlushMode_Moderate: |
| 176 | layerCache.clear(); |
| 177 | pathCache.clear(); |
| 178 | roundRectShapeCache.clear(); |
| 179 | circleShapeCache.clear(); |
| 180 | ovalShapeCache.clear(); |
| 181 | rectShapeCache.clear(); |
| 182 | arcShapeCache.clear(); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 187 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 188 | // VBO |
| 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 191 | void Caches::bindMeshBuffer() { |
| 192 | bindMeshBuffer(meshBuffer); |
| 193 | } |
| 194 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 195 | void Caches::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 196 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 197 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 198 | mCurrentBuffer = buffer; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 202 | void Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 203 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 204 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 205 | mCurrentBuffer = 0; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 209 | TextureVertex* Caches::getRegionMesh() { |
| 210 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 211 | if (!mRegionMesh) { |
| 212 | mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; |
| 213 | |
| 214 | uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; |
| 215 | for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { |
| 216 | uint16_t quad = i * 4; |
| 217 | int index = i * 6; |
| 218 | regionIndices[index ] = quad; // top-left |
| 219 | regionIndices[index + 1] = quad + 1; // top-right |
| 220 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 221 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 222 | regionIndices[index + 4] = quad + 1; // top-right |
| 223 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 224 | } |
| 225 | |
| 226 | glGenBuffers(1, &mRegionMeshIndices); |
| 227 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); |
| 228 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), |
| 229 | regionIndices, GL_STATIC_DRAW); |
| 230 | |
| 231 | delete[] regionIndices; |
| 232 | } else { |
| 233 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); |
| 234 | } |
| 235 | |
| 236 | return mRegionMesh; |
| 237 | } |
| 238 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 239 | }; // namespace uirenderer |
| 240 | }; // namespace android |