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 | |
| 20 | #include <SkBitmap.h> |
| 21 | #include <SkPaint.h> |
| 22 | #include <SkPath.h> |
| 23 | |
| 24 | #include "Texture.h" |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 25 | #include "utils/Compare.h" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 26 | #include "utils/GenerationCache.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /** |
| 32 | * Describe a path in the path cache. |
| 33 | */ |
| 34 | struct PathCacheEntry { |
| 35 | PathCacheEntry() { |
| 36 | path = NULL; |
| 37 | join = SkPaint::kDefault_Join; |
| 38 | cap = SkPaint::kDefault_Cap; |
| 39 | style = SkPaint::kFill_Style; |
| 40 | miter = 4.0f; |
| 41 | strokeWidth = 1.0f; |
| 42 | } |
| 43 | |
| 44 | PathCacheEntry(const PathCacheEntry& entry): |
| 45 | path(entry.path), join(entry.join), cap(entry.cap), |
| 46 | style(entry.style), miter(entry.miter), |
| 47 | strokeWidth(entry.strokeWidth) { |
| 48 | } |
| 49 | |
| 50 | PathCacheEntry(SkPath* path, SkPaint* paint) { |
| 51 | this->path = path; |
| 52 | join = paint->getStrokeJoin(); |
| 53 | cap = paint->getStrokeCap(); |
| 54 | miter = paint->getStrokeMiter(); |
| 55 | strokeWidth = paint->getStrokeWidth(); |
| 56 | style = paint->getStyle(); |
| 57 | } |
| 58 | |
| 59 | SkPath* path; |
| 60 | SkPaint::Join join; |
| 61 | SkPaint::Cap cap; |
| 62 | SkPaint::Style style; |
| 63 | float miter; |
| 64 | float strokeWidth; |
| 65 | |
| 66 | bool operator<(const PathCacheEntry& rhs) const { |
Romain Guy | 2665b85 | 2010-10-18 15:11:50 -0700 | [diff] [blame] | 67 | LTE_INT(path) { |
| 68 | LTE_INT(join) { |
| 69 | LTE_INT(cap) { |
| 70 | LTE_INT(style) { |
| 71 | LTE_FLOAT(miter) { |
| 72 | LTE_FLOAT(strokeWidth) return false; |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return false; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 79 | } |
| 80 | }; // struct PathCacheEntry |
| 81 | |
| 82 | /** |
| 83 | * Alpha texture used to represent a path. |
| 84 | */ |
| 85 | struct PathTexture: public Texture { |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 86 | PathTexture(): Texture() { |
| 87 | } |
| 88 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Left coordinate of the path bounds. |
| 91 | */ |
| 92 | float left; |
| 93 | /** |
| 94 | * Top coordinate of the path bounds. |
| 95 | */ |
| 96 | float top; |
| 97 | /** |
| 98 | * Offset to draw the path at the correct origin. |
| 99 | */ |
| 100 | float offset; |
| 101 | }; // struct PathTexture |
| 102 | |
| 103 | /** |
| 104 | * A simple LRU path cache. The cache has a maximum size expressed in bytes. |
| 105 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 106 | * allowed size will also cause the oldest texture to be kicked out. |
| 107 | */ |
| 108 | class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> { |
| 109 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 110 | PathCache(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 111 | PathCache(uint32_t maxByteSize); |
| 112 | ~PathCache(); |
| 113 | |
| 114 | /** |
| 115 | * Used as a callback when an entry is removed from the cache. |
| 116 | * Do not invoke directly. |
| 117 | */ |
| 118 | void operator()(PathCacheEntry& path, PathTexture*& texture); |
| 119 | |
| 120 | /** |
| 121 | * Returns the texture associated with the specified path. If the texture |
| 122 | * cannot be found in the cache, a new texture is generated. |
| 123 | */ |
| 124 | PathTexture* get(SkPath* path, SkPaint* paint); |
| 125 | /** |
| 126 | * Clears the cache. This causes all textures to be deleted. |
| 127 | */ |
| 128 | void clear(); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Removes an entry. |
| 131 | */ |
| 132 | void remove(SkPath* path); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Sets the maximum size of the cache in bytes. |
| 136 | */ |
| 137 | void setMaxSize(uint32_t maxSize); |
| 138 | /** |
| 139 | * Returns the maximum size of the cache in bytes. |
| 140 | */ |
| 141 | uint32_t getMaxSize(); |
| 142 | /** |
| 143 | * Returns the current size of the cache in bytes. |
| 144 | */ |
| 145 | uint32_t getSize(); |
| 146 | |
| 147 | private: |
| 148 | /** |
| 149 | * Generates the texture from a bitmap into the specified texture structure. |
| 150 | */ |
| 151 | void generateTexture(SkBitmap& bitmap, Texture* texture); |
| 152 | |
| 153 | PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint); |
| 154 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 155 | void init(); |
| 156 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 157 | GenerationCache<PathCacheEntry, PathTexture*> mCache; |
| 158 | |
| 159 | uint32_t mSize; |
| 160 | uint32_t mMaxSize; |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 161 | GLuint mMaxTextureSize; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Used to access mCache and mSize. All methods are accessed from a single |
| 165 | * thread except for remove(). |
| 166 | */ |
| 167 | mutable Mutex mLock; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 168 | }; // class PathCache |
| 169 | |
| 170 | }; // namespace uirenderer |
| 171 | }; // namespace android |
| 172 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame^] | 173 | #endif // ANDROID_HWUI_PATH_CACHE_H |