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" |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | #ifdef USE_OPENGL_RENDERER |
| 27 | using namespace uirenderer; |
| 28 | ANDROID_SINGLETON_STATIC_INSTANCE(Caches); |
| 29 | #endif |
| 30 | |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | // Constructors/destructor |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | Caches::Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO), |
| 38 | lastDstMode(GL_ZERO), currentProgram(NULL) { |
| 39 | GLint maxTextureUnits; |
| 40 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 41 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
| 42 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 43 | } |
| 44 | |
| 45 | glGenBuffers(1, &meshBuffer); |
| 46 | glBindBuffer(GL_ARRAY_BUFFER, meshBuffer); |
| 47 | glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW); |
| 48 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 49 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 50 | |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 51 | mCurrentBuffer = meshBuffer; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 52 | mRegionMesh = NULL; |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame^] | 53 | |
| 54 | mDebugLevel = readDebugLevel(); |
| 55 | LOGD("Enabling debug mode %d", mDebugLevel); |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 58 | Caches::~Caches() { |
| 59 | delete[] mRegionMesh; |
| 60 | } |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 63 | // Debug |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | void Caches::dumpMemoryUsage() { |
| 67 | LOGD("Current memory usage / total memory usage (bytes):"); |
| 68 | LOGD(" TextureCache %8d / %8d", textureCache.getSize(), textureCache.getMaxSize()); |
| 69 | LOGD(" LayerCache %8d / %8d", layerCache.getSize(), layerCache.getMaxSize()); |
| 70 | LOGD(" GradientCache %8d / %8d", gradientCache.getSize(), gradientCache.getMaxSize()); |
| 71 | LOGD(" PathCache %8d / %8d", pathCache.getSize(), pathCache.getMaxSize()); |
| 72 | LOGD(" TextDropShadowCache %8d / %8d", dropShadowCache.getSize(), |
| 73 | dropShadowCache.getMaxSize()); |
| 74 | for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { |
| 75 | const uint32_t size = fontRenderer.getFontRendererSize(i); |
| 76 | LOGD(" FontRenderer %d %8d / %8d", i, size, size); |
| 77 | } |
| 78 | LOGD("Other:"); |
| 79 | LOGD(" FboCache %8d / %8d", fboCache.getSize(), fboCache.getMaxSize()); |
| 80 | LOGD(" PatchCache %8d / %8d", patchCache.getSize(), patchCache.getMaxSize()); |
| 81 | |
| 82 | uint32_t total = 0; |
| 83 | total += textureCache.getSize(); |
| 84 | total += layerCache.getSize(); |
| 85 | total += gradientCache.getSize(); |
| 86 | total += pathCache.getSize(); |
| 87 | total += dropShadowCache.getSize(); |
| 88 | for (uint32_t i = 0; i < fontRenderer.getFontRendererCount(); i++) { |
| 89 | total += fontRenderer.getFontRendererSize(i); |
| 90 | } |
| 91 | |
| 92 | LOGD("Total memory usage:"); |
| 93 | LOGD(" %d bytes, %.2f MB", total, total / 1024.0f / 1024.0f); |
| 94 | LOGD("\n"); |
| 95 | } |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 98 | // VBO |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 101 | void Caches::bindMeshBuffer() { |
| 102 | bindMeshBuffer(meshBuffer); |
| 103 | } |
| 104 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 105 | void Caches::bindMeshBuffer(const GLuint buffer) { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 106 | if (mCurrentBuffer != buffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 107 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 108 | mCurrentBuffer = buffer; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 112 | void Caches::unbindMeshBuffer() { |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 113 | if (mCurrentBuffer) { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 114 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 115 | mCurrentBuffer = 0; |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 119 | TextureVertex* Caches::getRegionMesh() { |
| 120 | // Create the mesh, 2 triangles and 4 vertices per rectangle in the region |
| 121 | if (!mRegionMesh) { |
| 122 | mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4]; |
| 123 | |
| 124 | uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6]; |
| 125 | for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) { |
| 126 | uint16_t quad = i * 4; |
| 127 | int index = i * 6; |
| 128 | regionIndices[index ] = quad; // top-left |
| 129 | regionIndices[index + 1] = quad + 1; // top-right |
| 130 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 131 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 132 | regionIndices[index + 4] = quad + 1; // top-right |
| 133 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 134 | } |
| 135 | |
| 136 | glGenBuffers(1, &mRegionMeshIndices); |
| 137 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); |
| 138 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t), |
| 139 | regionIndices, GL_STATIC_DRAW); |
| 140 | |
| 141 | delete[] regionIndices; |
| 142 | } else { |
| 143 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mRegionMeshIndices); |
| 144 | } |
| 145 | |
| 146 | return mRegionMesh; |
| 147 | } |
| 148 | |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 149 | }; // namespace uirenderer |
| 150 | }; // namespace android |