Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_PATH_CACHE_H |
| 18 | #define ANDROID_HWUI_PATH_CACHE_H |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 19 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame^] | 20 | #include <utils/Thread.h> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 21 | #include <utils/Vector.h> |
| 22 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 23 | #include "Debug.h" |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 24 | #include "ShapeCache.h" |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame^] | 25 | #include "thread/Signal.h" |
| 26 | |
| 27 | class SkPaint; |
| 28 | class SkPath; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 29 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame^] | 33 | class Caches; |
| 34 | |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 35 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 36 | // Classes |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 39 | struct PathCacheEntry: public ShapeCacheEntry { |
| 40 | PathCacheEntry(SkPath* path, SkPaint* paint): |
| 41 | ShapeCacheEntry(ShapeCacheEntry::kShapePath, paint) { |
| 42 | this->path = path; |
| 43 | } |
| 44 | |
| 45 | PathCacheEntry(): ShapeCacheEntry() { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 46 | path = NULL; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 49 | hash_t hash() const { |
| 50 | uint32_t hash = ShapeCacheEntry::hash(); |
| 51 | hash = JenkinsHashMix(hash, android::hash_type(path)); |
| 52 | return JenkinsHashWhiten(hash); |
| 53 | } |
| 54 | |
| 55 | int compare(const ShapeCacheEntry& r) const { |
| 56 | int deltaInt = ShapeCacheEntry::compare(r); |
| 57 | if (deltaInt != 0) return deltaInt; |
| 58 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 59 | const PathCacheEntry& rhs = (const PathCacheEntry&) r; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 60 | return path - rhs.path; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 61 | } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 62 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 63 | SkPath* path; |
Romain Guy | b29cfbf | 2011-03-18 16:24:19 -0700 | [diff] [blame] | 64 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 65 | }; // PathCacheEntry |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 66 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 67 | inline hash_t hash_type(const PathCacheEntry& entry) { |
| 68 | return entry.hash(); |
| 69 | } |
| 70 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 71 | /** |
| 72 | * A simple LRU path cache. The cache has a maximum size expressed in bytes. |
| 73 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 74 | * allowed size will also cause the oldest texture to be kicked out. |
| 75 | */ |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 76 | class PathCache: public ShapeCache<PathCacheEntry> { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 77 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 78 | PathCache(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame^] | 79 | ~PathCache(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Returns the texture associated with the specified path. If the texture |
| 83 | * cannot be found in the cache, a new texture is generated. |
| 84 | */ |
| 85 | PathTexture* get(SkPath* path, SkPaint* paint); |
| 86 | /** |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 87 | * Removes an entry. |
| 88 | */ |
| 89 | void remove(SkPath* path); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Removes the specified path. This is meant to be called from threads |
| 92 | * that are not the EGL context thread. |
| 93 | */ |
| 94 | void removeDeferred(SkPath* path); |
| 95 | /** |
| 96 | * Process deferred removals. |
| 97 | */ |
| 98 | void clearGarbage(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 99 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame^] | 100 | void precache(SkPath* path, SkPaint* paint); |
| 101 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 102 | private: |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame^] | 103 | class PrecacheThread: public Thread { |
| 104 | public: |
| 105 | PrecacheThread(): mSignal(Condition::WAKE_UP_ONE) { } |
| 106 | |
| 107 | void addTask(PathTexture* texture, SkPath* path, SkPaint* paint); |
| 108 | void exit(); |
| 109 | |
| 110 | private: |
| 111 | struct Task { |
| 112 | PathTexture* texture; |
| 113 | SkPath* path; |
| 114 | SkPaint* paint; |
| 115 | }; |
| 116 | |
| 117 | virtual bool threadLoop(); |
| 118 | |
| 119 | // Lock for the list of tasks |
| 120 | Mutex mLock; |
| 121 | Vector<Task> mTasks; |
| 122 | |
| 123 | // Signal used to wake up the thread when a new |
| 124 | // task is available in the list |
| 125 | mutable Signal mSignal; |
| 126 | }; |
| 127 | |
| 128 | sp<PrecacheThread> mThread; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 129 | Vector<SkPath*> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 130 | mutable Mutex mLock; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 131 | }; // class PathCache |
| 132 | |
| 133 | }; // namespace uirenderer |
| 134 | }; // namespace android |
| 135 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 136 | #endif // ANDROID_HWUI_PATH_CACHE_H |