PathCache.cpp revision d9e688cab3015d858110fb8240cf7378c6befd82
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/threads.h>
20
21#include "PathCache.h"
22#include "Properties.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Path cache
29///////////////////////////////////////////////////////////////////////////////
30
31PathCache::PathCache(): ShapeCache<PathCacheEntry>("path",
32        PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) {
33}
34
35void PathCache::remove(SkPath* path) {
36    // TODO: Linear search...
37    Vector<size_t> pathsToRemove;
38    for (size_t i = 0; i < mCache.size(); i++) {
39        if (mCache.getKeyAt(i).path == path) {
40            pathsToRemove.push(i);
41            removeTexture(mCache.getValueAt(i));
42        }
43    }
44
45    mCache.setOnEntryRemovedListener(NULL);
46    for (size_t i = 0; i < pathsToRemove.size(); i++) {
47        // This will work because pathsToRemove is sorted
48        // and because the cache is a sorted keyed vector
49        mCache.removeAt(pathsToRemove.itemAt(i) - i);
50    }
51    mCache.setOnEntryRemovedListener(this);
52}
53
54void PathCache::removeDeferred(SkPath* path) {
55    Mutex::Autolock _l(mLock);
56    mGarbage.push(path);
57}
58
59void PathCache::clearGarbage() {
60    Mutex::Autolock _l(mLock);
61    size_t count = mGarbage.size();
62    for (size_t i = 0; i < count; i++) {
63        remove(mGarbage.itemAt(i));
64    }
65    mGarbage.clear();
66}
67
68PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
69    PathCacheEntry entry(path, paint);
70    PathTexture* texture = mCache.get(entry);
71
72    if (!texture) {
73        texture = addTexture(entry, path, paint);
74    } else if (path->getGenerationID() != texture->generation) {
75        mCache.remove(entry);
76        texture = addTexture(entry, path, paint);
77    }
78
79    return texture;
80}
81
82}; // namespace uirenderer
83}; // namespace android
84