blob: 27031a5e04a7d193bc5248379d543770b529047e [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 Guyca89e2a2013-03-08 17:44:20 -080020#include <utils/Thread.h>
Romain Guyfe48f652010-11-11 15:36:56 -080021#include <utils/Vector.h>
22
Romain Guyc15008e2010-11-10 11:59:15 -080023#include "Debug.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080024#include "ShapeCache.h"
Romain Guyca89e2a2013-03-08 17:44:20 -080025#include "thread/Signal.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070026#include "thread/Task.h"
27#include "thread/TaskProcessor.h"
Romain Guyca89e2a2013-03-08 17:44:20 -080028
29class SkPaint;
30class SkPath;
Romain Guyff26a0c2011-01-20 11:35:46 -080031
Romain Guy7fbcc042010-08-04 15:40:07 -070032namespace android {
33namespace uirenderer {
34
Romain Guyca89e2a2013-03-08 17:44:20 -080035class Caches;
36
Romain Guy9e108412010-11-09 14:35:20 -080037///////////////////////////////////////////////////////////////////////////////
Romain Guy9e108412010-11-09 14:35:20 -080038// Classes
39///////////////////////////////////////////////////////////////////////////////
40
Romain Guyff26a0c2011-01-20 11:35:46 -080041struct PathCacheEntry: public ShapeCacheEntry {
42 PathCacheEntry(SkPath* path, SkPaint* paint):
43 ShapeCacheEntry(ShapeCacheEntry::kShapePath, paint) {
44 this->path = path;
45 }
46
47 PathCacheEntry(): ShapeCacheEntry() {
Romain Guy7fbcc042010-08-04 15:40:07 -070048 path = NULL;
Romain Guy7fbcc042010-08-04 15:40:07 -070049 }
50
Romain Guy059e12c2012-11-28 17:35:51 -080051 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 Guyff26a0c2011-01-20 11:35:46 -080061 const PathCacheEntry& rhs = (const PathCacheEntry&) r;
Romain Guy059e12c2012-11-28 17:35:51 -080062 return path - rhs.path;
Romain Guy7fbcc042010-08-04 15:40:07 -070063 }
Romain Guy7fbcc042010-08-04 15:40:07 -070064
Romain Guyff26a0c2011-01-20 11:35:46 -080065 SkPath* path;
Romain Guyb29cfbf2011-03-18 16:24:19 -070066
Romain Guyff26a0c2011-01-20 11:35:46 -080067}; // PathCacheEntry
Romain Guy7fbcc042010-08-04 15:40:07 -070068
Romain Guy059e12c2012-11-28 17:35:51 -080069inline hash_t hash_type(const PathCacheEntry& entry) {
70 return entry.hash();
71}
72
Romain Guy7fbcc042010-08-04 15:40:07 -070073/**
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 Guyff26a0c2011-01-20 11:35:46 -080078class PathCache: public ShapeCache<PathCacheEntry> {
Romain Guy7fbcc042010-08-04 15:40:07 -070079public:
Romain Guyfb8b7632010-08-23 21:05:08 -070080 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -080081 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -070082
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 Guya2341a92010-09-08 18:04:33 -070089 * Removes an entry.
90 */
91 void remove(SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -080092 /**
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 Guy7fbcc042010-08-04 15:40:07 -0700101
Romain Guyca89e2a2013-03-08 17:44:20 -0800102 void precache(SkPath* path, SkPaint* paint);
103
Romain Guy7fbcc042010-08-04 15:40:07 -0700104private:
Romain Guy5dc7fa72013-03-11 20:48:31 -0700105 class PathTask: public Task<SkBitmap*> {
Romain Guyca89e2a2013-03-08 17:44:20 -0800106 public:
Romain Guy5dc7fa72013-03-11 20:48:31 -0700107 PathTask(SkPath* path, SkPaint* paint, PathTexture* texture):
108 path(path), paint(paint), texture(texture) {
109 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800110
Romain Guy5dc7fa72013-03-11 20:48:31 -0700111 ~PathTask() {
112 delete future()->get();
113 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800114
Romain Guy5dc7fa72013-03-11 20:48:31 -0700115 SkPath* path;
116 SkPaint* paint;
117 PathTexture* texture;
Romain Guyca89e2a2013-03-08 17:44:20 -0800118 };
119
Romain Guy5dc7fa72013-03-11 20:48:31 -0700120 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 Guyfe48f652010-11-11 15:36:56 -0800132 Vector<SkPath*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700133 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700134}; // class PathCache
135
136}; // namespace uirenderer
137}; // namespace android
138
Romain Guy5b3b3522010-10-27 18:57:51 -0700139#endif // ANDROID_HWUI_PATH_CACHE_H