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