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