PatchCache.cpp revision fb8b763f762ae21923c58d64caa729b012f40e05
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(): mCache(DEFAULT_PATCH_CACHE_SIZE) {
33}
34
35PatchCache::PatchCache(uint32_t maxEntries): mCache(maxEntries) {
36}
37
38PatchCache::~PatchCache() {
39    clear();
40}
41
42///////////////////////////////////////////////////////////////////////////////
43// Callbacks
44///////////////////////////////////////////////////////////////////////////////
45
46void PatchCache::operator()(PatchDescription& description, Patch*& mesh) {
47    if (mesh) delete mesh;
48}
49
50///////////////////////////////////////////////////////////////////////////////
51// Caching
52///////////////////////////////////////////////////////////////////////////////
53
54void PatchCache::clear() {
55    mCache.setOnEntryRemovedListener(this);
56    mCache.clear();
57    mCache.setOnEntryRemovedListener(NULL);
58}
59
60Patch* PatchCache::get(const Res_png_9patch* patch) {
61    const uint32_t width = patch->numXDivs;
62    const uint32_t height = patch->numYDivs;
63    const PatchDescription description(width, height);
64
65    Patch* mesh = mCache.get(description);
66    if (!mesh) {
67        PATCH_LOGD("Creating new patch mesh, w=%d h=%d", width, height);
68        mesh = new Patch(width, height);
69        mCache.put(description, mesh);
70    }
71
72    return mesh;
73}
74
75}; // namespace uirenderer
76}; // namespace android
77