PatchCache.cpp revision 03c00b5a135e68d22ca5bb829b899ebda6ed7e9d
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/JenkinsHash.h>
20#include <utils/Log.h>
21
22#include "Caches.h"
23#include "PatchCache.h"
24#include "Properties.h"
25
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
33PatchCache::PatchCache():
34        mSize(0), mCache(LruCache<PatchDescription, Patch*>::kUnlimitedCapacity),
35        mMeshBuffer(0), mGenerationId(0) {
36    char property[PROPERTY_VALUE_MAX];
37    if (property_get(PROPERTY_PATCH_CACHE_SIZE, property, NULL) > 0) {
38        INIT_LOGD("  Setting patch cache size to %skB", property);
39        mMaxSize = KB(atoi(property));
40    } else {
41        INIT_LOGD("  Using default patch cache size of %.2fkB", DEFAULT_PATCH_CACHE_SIZE);
42        mMaxSize = KB(DEFAULT_PATCH_CACHE_SIZE);
43    }
44}
45
46PatchCache::~PatchCache() {
47    clear();
48}
49
50void PatchCache::init(Caches& caches) {
51    bool created = false;
52    if (!mMeshBuffer) {
53        glGenBuffers(1, &mMeshBuffer);
54        created = true;
55    }
56
57    caches.bindMeshBuffer(mMeshBuffer);
58    caches.resetVertexPointers();
59
60    if (created) {
61        createVertexBuffer();
62    }
63}
64
65///////////////////////////////////////////////////////////////////////////////
66// Caching
67///////////////////////////////////////////////////////////////////////////////
68
69hash_t PatchCache::PatchDescription::hash() const {
70    uint32_t hash = JenkinsHashMix(0, android::hash_type(mPatch));
71    hash = JenkinsHashMix(hash, mBitmapWidth);
72    hash = JenkinsHashMix(hash, mBitmapHeight);
73    hash = JenkinsHashMix(hash, mPixelWidth);
74    hash = JenkinsHashMix(hash, mPixelHeight);
75    return JenkinsHashWhiten(hash);
76}
77
78int PatchCache::PatchDescription::compare(const PatchCache::PatchDescription& lhs,
79            const PatchCache::PatchDescription& rhs) {
80    return memcmp(&lhs, &rhs, sizeof(PatchDescription));
81}
82
83void PatchCache::clear() {
84    clearCache();
85
86    if (mMeshBuffer) {
87        Caches::getInstance().unbindMeshBuffer();
88        glDeleteBuffers(1, &mMeshBuffer);
89        mMeshBuffer = 0;
90        mSize = 0;
91    }
92}
93
94void PatchCache::clearCache() {
95    LruCache<PatchDescription, Patch*>::Iterator i(mCache);
96    while (i.next()) {
97        delete i.value();
98    }
99    mCache.clear();
100}
101
102void PatchCache::createVertexBuffer() {
103    glBufferData(GL_ARRAY_BUFFER, mMaxSize, NULL, GL_DYNAMIC_DRAW);
104    mSize = 0;
105    mGenerationId++;
106}
107
108const Patch* PatchCache::get(const AssetAtlas::Entry* entry,
109        const uint32_t bitmapWidth, const uint32_t bitmapHeight,
110        const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch) {
111
112    const PatchDescription description(bitmapWidth, bitmapHeight, pixelWidth, pixelHeight, patch);
113    const Patch* mesh = mCache.get(description);
114
115    if (!mesh) {
116        Patch* newMesh = new Patch();
117        TextureVertex* vertices;
118
119        if (entry) {
120            vertices = newMesh->createMesh(bitmapWidth, bitmapHeight,
121                    pixelWidth, pixelHeight, entry->uvMapper, patch);
122        } else {
123            vertices = newMesh->createMesh(bitmapWidth, bitmapHeight,
124                    pixelWidth, pixelHeight, patch);
125        }
126
127        if (vertices) {
128            // This call ensures the VBO exists and that it is bound
129            init(Caches::getInstance());
130
131            // TODO: Simply remove the oldest items until we have enough room
132            // This will require to keep a list of free blocks in the VBO
133            uint32_t size = newMesh->getSize();
134            if (mSize + size > mMaxSize) {
135                clearCache();
136                createVertexBuffer();
137            }
138
139            newMesh->offset = (GLintptr) mSize;
140            newMesh->textureOffset = newMesh->offset + gMeshTextureOffset;
141            mSize += size;
142
143            glBufferSubData(GL_ARRAY_BUFFER, newMesh->offset, size, vertices);
144        }
145
146        mCache.put(description, newMesh);
147        return newMesh;
148    }
149
150    return mesh;
151}
152
153}; // namespace uirenderer
154}; // namespace android
155