PatchCache.cpp revision 21b028a44f3e0bd9b0f0432b8b92c45f661d22a4
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#include <utils/ResourceTypes.h>
21
22#include "PatchCache.h"
23#include "Properties.h"
24
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Constructors/destructor
30///////////////////////////////////////////////////////////////////////////////
31
32PatchCache::PatchCache(): mMaxEntries(DEFAULT_PATCH_CACHE_SIZE) {
33}
34
35PatchCache::PatchCache(uint32_t maxEntries): mMaxEntries(maxEntries) {
36}
37
38PatchCache::~PatchCache() {
39    clear();
40}
41
42///////////////////////////////////////////////////////////////////////////////
43// Caching
44///////////////////////////////////////////////////////////////////////////////
45
46void PatchCache::clear() {
47    size_t count = mCache.size();
48    for (int i = 0; i < count; i++) {
49        delete mCache.valueAt(i);
50    }
51    mCache.clear();
52}
53
54Patch* PatchCache::get(const float bitmapWidth, const float bitmapHeight,
55        const float pixelWidth, const float pixelHeight,
56        const int32_t* xDivs, const int32_t* yDivs,
57        const uint32_t width, const uint32_t height) {
58
59    const PatchDescription description(bitmapWidth, bitmapHeight,
60            pixelWidth, pixelHeight, width, height);
61
62    ssize_t index = mCache.indexOfKey(description);
63    Patch* mesh = NULL;
64    if (index >= 0) {
65        mesh = mCache.valueAt(index);
66    }
67
68    if (!mesh) {
69        PATCH_LOGD("Creating new patch mesh, w=%d h=%d", width, height);
70
71        mesh = new Patch(width, height);
72        mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f,
73                pixelWidth, pixelHeight, xDivs, yDivs, width, height);
74
75        if (mCache.size() >= mMaxEntries) {
76            delete mCache.valueAt(mCache.size() - 1);
77            mCache.removeItemsAt(mCache.size() - 1, 1);
78        }
79
80        mCache.add(description, mesh);
81    }
82
83    return mesh;
84}
85
86}; // namespace uirenderer
87}; // namespace android
88