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