blob: 8a0235b88a640767ac91cd03cda33cdcdef168b9 [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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_PATH_CACHE_H
18#define ANDROID_HWUI_PATH_CACHE_H
Romain Guy7fbcc042010-08-04 15:40:07 -070019
Romain Guyfe48f652010-11-11 15:36:56 -080020#include <utils/Vector.h>
21
Romain Guyc15008e2010-11-10 11:59:15 -080022#include "Debug.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080023#include "ShapeCache.h"
24
Romain Guy7fbcc042010-08-04 15:40:07 -070025namespace android {
26namespace uirenderer {
27
Romain Guy9e108412010-11-09 14:35:20 -080028///////////////////////////////////////////////////////////////////////////////
Romain Guy9e108412010-11-09 14:35:20 -080029// Classes
30///////////////////////////////////////////////////////////////////////////////
31
Romain Guyff26a0c2011-01-20 11:35:46 -080032struct PathCacheEntry: public ShapeCacheEntry {
33 PathCacheEntry(SkPath* path, SkPaint* paint):
34 ShapeCacheEntry(ShapeCacheEntry::kShapePath, paint) {
35 this->path = path;
36 }
37
38 PathCacheEntry(): ShapeCacheEntry() {
Romain Guy7fbcc042010-08-04 15:40:07 -070039 path = NULL;
Romain Guy7fbcc042010-08-04 15:40:07 -070040 }
41
Romain Guy059e12c2012-11-28 17:35:51 -080042 hash_t hash() const {
43 uint32_t hash = ShapeCacheEntry::hash();
44 hash = JenkinsHashMix(hash, android::hash_type(path));
45 return JenkinsHashWhiten(hash);
46 }
47
48 int compare(const ShapeCacheEntry& r) const {
49 int deltaInt = ShapeCacheEntry::compare(r);
50 if (deltaInt != 0) return deltaInt;
51
Romain Guyff26a0c2011-01-20 11:35:46 -080052 const PathCacheEntry& rhs = (const PathCacheEntry&) r;
Romain Guy059e12c2012-11-28 17:35:51 -080053 return path - rhs.path;
Romain Guy7fbcc042010-08-04 15:40:07 -070054 }
Romain Guy7fbcc042010-08-04 15:40:07 -070055
Romain Guyff26a0c2011-01-20 11:35:46 -080056 SkPath* path;
Romain Guyb29cfbf2011-03-18 16:24:19 -070057
Romain Guyff26a0c2011-01-20 11:35:46 -080058}; // PathCacheEntry
Romain Guy7fbcc042010-08-04 15:40:07 -070059
Romain Guy059e12c2012-11-28 17:35:51 -080060inline hash_t hash_type(const PathCacheEntry& entry) {
61 return entry.hash();
62}
63
Romain Guy7fbcc042010-08-04 15:40:07 -070064/**
65 * A simple LRU path cache. The cache has a maximum size expressed in bytes.
66 * Any texture added to the cache causing the cache to grow beyond the maximum
67 * allowed size will also cause the oldest texture to be kicked out.
68 */
Romain Guyff26a0c2011-01-20 11:35:46 -080069class PathCache: public ShapeCache<PathCacheEntry> {
Romain Guy7fbcc042010-08-04 15:40:07 -070070public:
Romain Guyfb8b7632010-08-23 21:05:08 -070071 PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -070072
73 /**
74 * Returns the texture associated with the specified path. If the texture
75 * cannot be found in the cache, a new texture is generated.
76 */
77 PathTexture* get(SkPath* path, SkPaint* paint);
78 /**
Romain Guya2341a92010-09-08 18:04:33 -070079 * Removes an entry.
80 */
81 void remove(SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -080082 /**
83 * Removes the specified path. This is meant to be called from threads
84 * that are not the EGL context thread.
85 */
86 void removeDeferred(SkPath* path);
87 /**
88 * Process deferred removals.
89 */
90 void clearGarbage();
Romain Guy7fbcc042010-08-04 15:40:07 -070091
Romain Guy7fbcc042010-08-04 15:40:07 -070092private:
Romain Guyfe48f652010-11-11 15:36:56 -080093 Vector<SkPath*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -070094 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -070095}; // class PathCache
96
97}; // namespace uirenderer
98}; // namespace android
99
Romain Guy5b3b3522010-10-27 18:57:51 -0700100#endif // ANDROID_HWUI_PATH_CACHE_H