blob: ae2e55dfbe2a7f922d7b9d040f211efa9f85af98 [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
20#include <SkBitmap.h>
21#include <SkPaint.h>
22#include <SkPath.h>
23
Romain Guyfe48f652010-11-11 15:36:56 -080024#include <utils/Vector.h>
25
Romain Guyc15008e2010-11-10 11:59:15 -080026#include "Debug.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070027#include "Texture.h"
Romain Guy4bb94202010-10-12 15:59:26 -070028#include "utils/Compare.h"
Romain Guy21b028a2010-10-08 18:43:58 -070029#include "utils/GenerationCache.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070030
31namespace android {
32namespace uirenderer {
33
Romain Guy9e108412010-11-09 14:35:20 -080034///////////////////////////////////////////////////////////////////////////////
35// Defines
36///////////////////////////////////////////////////////////////////////////////
37
38// Debug
Romain Guy9e108412010-11-09 14:35:20 -080039#if DEBUG_PATHS
40 #define PATH_LOGD(...) LOGD(__VA_ARGS__)
41#else
42 #define PATH_LOGD(...)
43#endif
44
45///////////////////////////////////////////////////////////////////////////////
46// Classes
47///////////////////////////////////////////////////////////////////////////////
48
Romain Guy7fbcc042010-08-04 15:40:07 -070049/**
50 * Describe a path in the path cache.
51 */
52struct PathCacheEntry {
53 PathCacheEntry() {
54 path = NULL;
55 join = SkPaint::kDefault_Join;
56 cap = SkPaint::kDefault_Cap;
57 style = SkPaint::kFill_Style;
58 miter = 4.0f;
59 strokeWidth = 1.0f;
60 }
61
62 PathCacheEntry(const PathCacheEntry& entry):
63 path(entry.path), join(entry.join), cap(entry.cap),
64 style(entry.style), miter(entry.miter),
65 strokeWidth(entry.strokeWidth) {
66 }
67
68 PathCacheEntry(SkPath* path, SkPaint* paint) {
69 this->path = path;
70 join = paint->getStrokeJoin();
71 cap = paint->getStrokeCap();
72 miter = paint->getStrokeMiter();
73 strokeWidth = paint->getStrokeWidth();
74 style = paint->getStyle();
75 }
76
77 SkPath* path;
78 SkPaint::Join join;
79 SkPaint::Cap cap;
80 SkPaint::Style style;
81 float miter;
82 float strokeWidth;
83
84 bool operator<(const PathCacheEntry& rhs) const {
Romain Guy2665b852010-10-18 15:11:50 -070085 LTE_INT(path) {
86 LTE_INT(join) {
87 LTE_INT(cap) {
88 LTE_INT(style) {
89 LTE_FLOAT(miter) {
90 LTE_FLOAT(strokeWidth) return false;
Romain Guy4bb94202010-10-12 15:59:26 -070091 }
92 }
93 }
94 }
95 }
96 return false;
Romain Guy7fbcc042010-08-04 15:40:07 -070097 }
98}; // struct PathCacheEntry
99
100/**
101 * Alpha texture used to represent a path.
102 */
103struct PathTexture: public Texture {
Romain Guy22158e12010-08-06 11:18:34 -0700104 PathTexture(): Texture() {
105 }
106
Romain Guy7fbcc042010-08-04 15:40:07 -0700107 /**
108 * Left coordinate of the path bounds.
109 */
110 float left;
111 /**
112 * Top coordinate of the path bounds.
113 */
114 float top;
115 /**
116 * Offset to draw the path at the correct origin.
117 */
118 float offset;
119}; // struct PathTexture
120
121/**
122 * A simple LRU path cache. The cache has a maximum size expressed in bytes.
123 * Any texture added to the cache causing the cache to grow beyond the maximum
124 * allowed size will also cause the oldest texture to be kicked out.
125 */
126class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> {
127public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700128 PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700129 PathCache(uint32_t maxByteSize);
130 ~PathCache();
131
132 /**
133 * Used as a callback when an entry is removed from the cache.
134 * Do not invoke directly.
135 */
136 void operator()(PathCacheEntry& path, PathTexture*& texture);
137
138 /**
139 * Returns the texture associated with the specified path. If the texture
140 * cannot be found in the cache, a new texture is generated.
141 */
142 PathTexture* get(SkPath* path, SkPaint* paint);
143 /**
144 * Clears the cache. This causes all textures to be deleted.
145 */
146 void clear();
Romain Guya2341a92010-09-08 18:04:33 -0700147 /**
148 * Removes an entry.
149 */
150 void remove(SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -0800151 /**
152 * Removes the specified path. This is meant to be called from threads
153 * that are not the EGL context thread.
154 */
155 void removeDeferred(SkPath* path);
156 /**
157 * Process deferred removals.
158 */
159 void clearGarbage();
Romain Guy7fbcc042010-08-04 15:40:07 -0700160
161 /**
162 * Sets the maximum size of the cache in bytes.
163 */
164 void setMaxSize(uint32_t maxSize);
165 /**
166 * Returns the maximum size of the cache in bytes.
167 */
168 uint32_t getMaxSize();
169 /**
170 * Returns the current size of the cache in bytes.
171 */
172 uint32_t getSize();
173
174private:
175 /**
176 * Generates the texture from a bitmap into the specified texture structure.
177 */
178 void generateTexture(SkBitmap& bitmap, Texture* texture);
179
Romain Guyfe48f652010-11-11 15:36:56 -0800180 void removeTexture(PathTexture* texture);
181
Romain Guy7fbcc042010-08-04 15:40:07 -0700182 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint);
183
Romain Guyfb8b7632010-08-23 21:05:08 -0700184 void init();
185
Romain Guy7fbcc042010-08-04 15:40:07 -0700186 GenerationCache<PathCacheEntry, PathTexture*> mCache;
187
188 uint32_t mSize;
189 uint32_t mMaxSize;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700190 GLuint mMaxTextureSize;
Romain Guya2341a92010-09-08 18:04:33 -0700191
Romain Guye190aa62010-11-10 19:01:29 -0800192 bool mDebugEnabled;
193
Romain Guyfe48f652010-11-11 15:36:56 -0800194 Vector<SkPath*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700195 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700196}; // class PathCache
197
198}; // namespace uirenderer
199}; // namespace android
200
Romain Guy5b3b3522010-10-27 18:57:51 -0700201#endif // ANDROID_HWUI_PATH_CACHE_H