2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_PATH_CACHE_H
18 #define ANDROID_HWUI_PATH_CACHE_H
22 #include "hwui/Bitmap.h"
23 #include "thread/Task.h"
24 #include "thread/TaskProcessor.h"
25 #include "utils/Macros.h"
26 #include "utils/Pair.h"
28 #include <GLES2/gl2.h>
31 #include <utils/LruCache.h>
32 #include <utils/Mutex.h>
41 namespace uirenderer {
44 ///////////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////////
50 #define PATH_LOGD(...) ALOGD(__VA_ARGS__)
52 #define PATH_LOGD(...)
55 ///////////////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////////////
60 class PathTask: public Task<sk_sp<Bitmap>> {
62 PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture):
63 path(*path), paint(*paint), texture(texture) {
66 // copied, since input path not guaranteed to survive for duration of task
69 // copied, since input paint may not be immutable
75 * Alpha texture used to represent a path.
77 struct PathTexture: public Texture {
78 PathTexture(Caches& caches, int generation)
80 this->generation = generation;
88 * Left coordinate of the path bounds.
92 * Top coordinate of the path bounds.
96 * Offset to draw the path at the correct origin.
100 sp<PathTask> task() const {
104 void setTask(const sp<PathTask>& task) {
109 if (mTask != nullptr) {
116 }; // struct PathTexture
118 enum class ShapeType {
128 struct PathDescription {
129 HASHABLE_TYPE(PathDescription);
133 SkPaint::Style style;
136 SkPathEffect* pathEffect;
139 uint32_t mGenerationID;
168 PathDescription(ShapeType shapeType, const SkPaint* paint);
172 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
173 * Any texture added to the cache causing the cache to grow beyond the maximum
174 * allowed size will also cause the oldest texture to be kicked out.
176 class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
182 * Used as a callback when an entry is removed from the cache.
183 * Do not invoke directly.
185 void operator()(PathDescription& path, PathTexture*& texture) override;
188 * Clears the cache. This causes all textures to be deleted.
193 * Returns the maximum size of the cache in bytes.
195 uint32_t getMaxSize();
197 * Returns the current size of the cache in bytes.
201 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
202 PathTexture* getCircle(float radius, const SkPaint* paint);
203 PathTexture* getOval(float width, float height, const SkPaint* paint);
204 PathTexture* getRect(float width, float height, const SkPaint* paint);
205 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
206 bool useCenter, const SkPaint* paint);
207 PathTexture* get(const SkPath* path, const SkPaint* paint);
208 void remove(const SkPath* path, const SkPaint* paint);
211 * Removes the specified path. This is meant to be called from threads
212 * that are not the EGL context thread.
214 ANDROID_API void removeDeferred(const SkPath* path);
216 * Process deferred removals.
220 * Trims the contents of the cache, removing items until it's under its
223 * Trimming is used for caches that support pre-caching from a worker
224 * thread. During pre-caching the maximum limit of the cache can be
225 * exceeded for the duration of the frame. It is therefore required to
226 * trim the cache at the end of the frame to keep the total amount of
227 * memory used under control.
232 * Precaches the specified path using background threads.
234 void precache(const SkPath* path, const SkPaint* paint);
237 PathTexture* addTexture(const PathDescription& entry,
238 const SkPath *path, const SkPaint* paint);
241 * Generates the texture from a bitmap into the specified texture structure.
243 void generateTexture(Bitmap& bitmap, Texture* texture);
244 void generateTexture(const PathDescription& entry, Bitmap& bitmap, PathTexture* texture,
245 bool addToCache = true);
247 PathTexture* get(const PathDescription& entry) {
248 return mCache.get(entry);
252 * Ensures there is enough space in the cache for a texture of the specified
255 void purgeCache(uint32_t width, uint32_t height);
257 void removeTexture(PathTexture* texture);
262 class PathProcessor: public TaskProcessor<sk_sp<Bitmap> > {
264 explicit PathProcessor(Caches& caches);
267 virtual void onProcess(const sp<Task<sk_sp<Bitmap> > >& task) override;
270 uint32_t mMaxTextureSize;
273 LruCache<PathDescription, PathTexture*> mCache;
275 const uint32_t mMaxSize;
276 GLuint mMaxTextureSize;
280 sp<PathProcessor> mProcessor;
282 std::vector<uint32_t> mGarbage;
284 }; // class PathCache
286 }; // namespace uirenderer
287 }; // namespace android
289 #endif // ANDROID_HWUI_PATH_CACHE_H