blob: e893f7a95adb7ca7b1175873980694917b82b1b5 [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -07001/*
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
Romain Guya2341a92010-09-08 18:04:33 -070019#include <utils/threads.h>
20
Romain Guy7fbcc042010-08-04 15:40:07 -070021#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070022#include "Properties.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070023
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
Romain Guyff26a0c2011-01-20 11:35:46 -080028// Path cache
Romain Guy7fbcc042010-08-04 15:40:07 -070029///////////////////////////////////////////////////////////////////////////////
30
Romain Guyff26a0c2011-01-20 11:35:46 -080031PathCache::PathCache(): ShapeCache<PathCacheEntry>("path",
32 PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) {
Romain Guy7fbcc042010-08-04 15:40:07 -070033}
34
Romain Guya2341a92010-09-08 18:04:33 -070035void PathCache::remove(SkPath* path) {
Romain Guya2341a92010-09-08 18:04:33 -070036 // TODO: Linear search...
Jeff Brownd9e688c2011-11-11 15:40:13 -080037 Vector<size_t> pathsToRemove;
38 for (size_t i = 0; i < mCache.size(); i++) {
Romain Guya2341a92010-09-08 18:04:33 -070039 if (mCache.getKeyAt(i).path == path) {
Romain Guy9e108412010-11-09 14:35:20 -080040 pathsToRemove.push(i);
Romain Guyfe48f652010-11-11 15:36:56 -080041 removeTexture(mCache.getValueAt(i));
Romain Guya2341a92010-09-08 18:04:33 -070042 }
43 }
Romain Guy9e108412010-11-09 14:35:20 -080044
Romain Guyfe48f652010-11-11 15:36:56 -080045 mCache.setOnEntryRemovedListener(NULL);
Romain Guy9e108412010-11-09 14:35:20 -080046 for (size_t i = 0; i < pathsToRemove.size(); i++) {
Romain Guy7b8b7582011-02-24 19:52:37 -080047 // This will work because pathsToRemove is sorted
48 // and because the cache is a sorted keyed vector
49 mCache.removeAt(pathsToRemove.itemAt(i) - i);
Romain Guy9e108412010-11-09 14:35:20 -080050 }
Romain Guyfe48f652010-11-11 15:36:56 -080051 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();
Romain Guya2341a92010-09-08 18:04:33 -070066}
67
Romain Guy7fbcc042010-08-04 15:40:07 -070068PathTexture* 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
Romain Guy7fbcc042010-08-04 15:40:07 -070079 return texture;
80}
81
Romain Guy7fbcc042010-08-04 15:40:07 -070082}; // namespace uirenderer
83}; // namespace android