blob: aea71cb757b868d475b13d2e2d9e801b3c46ca48 [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
24#include "Texture.h"
Romain Guy4bb94202010-10-12 15:59:26 -070025#include "utils/Compare.h"
Romain Guy21b028a2010-10-08 18:43:58 -070026#include "utils/GenerationCache.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070027
28namespace android {
29namespace uirenderer {
30
Romain Guy9e108412010-11-09 14:35:20 -080031///////////////////////////////////////////////////////////////////////////////
32// Defines
33///////////////////////////////////////////////////////////////////////////////
34
35// Debug
36#define DEBUG_PATHS 0
37
38// Debug
39#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 Guy7fbcc042010-08-04 15:40:07 -0700151
152 /**
153 * Sets the maximum size of the cache in bytes.
154 */
155 void setMaxSize(uint32_t maxSize);
156 /**
157 * Returns the maximum size of the cache in bytes.
158 */
159 uint32_t getMaxSize();
160 /**
161 * Returns the current size of the cache in bytes.
162 */
163 uint32_t getSize();
164
165private:
166 /**
167 * Generates the texture from a bitmap into the specified texture structure.
168 */
169 void generateTexture(SkBitmap& bitmap, Texture* texture);
170
171 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint);
172
Romain Guyfb8b7632010-08-23 21:05:08 -0700173 void init();
174
Romain Guy7fbcc042010-08-04 15:40:07 -0700175 GenerationCache<PathCacheEntry, PathTexture*> mCache;
176
177 uint32_t mSize;
178 uint32_t mMaxSize;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700179 GLuint mMaxTextureSize;
Romain Guya2341a92010-09-08 18:04:33 -0700180
181 /**
182 * Used to access mCache and mSize. All methods are accessed from a single
183 * thread except for remove().
184 */
185 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700186}; // class PathCache
187
188}; // namespace uirenderer
189}; // namespace android
190
Romain Guy5b3b3522010-10-27 18:57:51 -0700191#endif // ANDROID_HWUI_PATH_CACHE_H