PathCache.cpp revision ff26a0c1c905dc1ec53b1bab860b80f2976d59be
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<uint32_t> pathsToRemove;
38    for (uint32_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        mCache.removeAt(pathsToRemove.itemAt(i));
48    }
49    mCache.setOnEntryRemovedListener(this);
50}
51
52void PathCache::removeDeferred(SkPath* path) {
53    Mutex::Autolock _l(mLock);
54    mGarbage.push(path);
55}
56
57void PathCache::clearGarbage() {
58    Mutex::Autolock _l(mLock);
59    size_t count = mGarbage.size();
60    for (size_t i = 0; i < count; i++) {
61        remove(mGarbage.itemAt(i));
62    }
63    mGarbage.clear();
64}
65
66PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
67    PathCacheEntry entry(path, paint);
68
69    PathTexture* texture = mCache.get(entry);
70
71    if (!texture) {
72        texture = addTexture(entry, path, paint);
73    } else if (path->getGenerationID() != texture->generation) {
74        mCache.remove(entry);
75        texture = addTexture(entry, path, paint);
76    }
77
78    return texture;
79}
80
81}; // namespace uirenderer
82}; // namespace android
83