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