blob: 23aa7efecb4e653046862a5ef0fa4cc36cfc0ba5 [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 Guyc15008e2010-11-10 11:59:15 -080024#include "Debug.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070025#include "Texture.h"
Romain Guy4bb94202010-10-12 15:59:26 -070026#include "utils/Compare.h"
Romain Guy21b028a2010-10-08 18:43:58 -070027#include "utils/GenerationCache.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070028
29namespace android {
30namespace uirenderer {
31
Romain Guy9e108412010-11-09 14:35:20 -080032///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
36// Debug
Romain Guy9e108412010-11-09 14:35:20 -080037#if DEBUG_PATHS
38 #define PATH_LOGD(...) LOGD(__VA_ARGS__)
39#else
40 #define PATH_LOGD(...)
41#endif
42
43///////////////////////////////////////////////////////////////////////////////
44// Classes
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy7fbcc042010-08-04 15:40:07 -070047/**
48 * Describe a path in the path cache.
49 */
50struct PathCacheEntry {
51 PathCacheEntry() {
52 path = NULL;
53 join = SkPaint::kDefault_Join;
54 cap = SkPaint::kDefault_Cap;
55 style = SkPaint::kFill_Style;
56 miter = 4.0f;
57 strokeWidth = 1.0f;
58 }
59
60 PathCacheEntry(const PathCacheEntry& entry):
61 path(entry.path), join(entry.join), cap(entry.cap),
62 style(entry.style), miter(entry.miter),
63 strokeWidth(entry.strokeWidth) {
64 }
65
66 PathCacheEntry(SkPath* path, SkPaint* paint) {
67 this->path = path;
68 join = paint->getStrokeJoin();
69 cap = paint->getStrokeCap();
70 miter = paint->getStrokeMiter();
71 strokeWidth = paint->getStrokeWidth();
72 style = paint->getStyle();
73 }
74
75 SkPath* path;
76 SkPaint::Join join;
77 SkPaint::Cap cap;
78 SkPaint::Style style;
79 float miter;
80 float strokeWidth;
81
82 bool operator<(const PathCacheEntry& rhs) const {
Romain Guy2665b852010-10-18 15:11:50 -070083 LTE_INT(path) {
84 LTE_INT(join) {
85 LTE_INT(cap) {
86 LTE_INT(style) {
87 LTE_FLOAT(miter) {
88 LTE_FLOAT(strokeWidth) return false;
Romain Guy4bb94202010-10-12 15:59:26 -070089 }
90 }
91 }
92 }
93 }
94 return false;
Romain Guy7fbcc042010-08-04 15:40:07 -070095 }
96}; // struct PathCacheEntry
97
98/**
99 * Alpha texture used to represent a path.
100 */
101struct PathTexture: public Texture {
Romain Guy22158e12010-08-06 11:18:34 -0700102 PathTexture(): Texture() {
103 }
104
Romain Guy7fbcc042010-08-04 15:40:07 -0700105 /**
106 * Left coordinate of the path bounds.
107 */
108 float left;
109 /**
110 * Top coordinate of the path bounds.
111 */
112 float top;
113 /**
114 * Offset to draw the path at the correct origin.
115 */
116 float offset;
117}; // struct PathTexture
118
119/**
120 * A simple LRU path cache. The cache has a maximum size expressed in bytes.
121 * Any texture added to the cache causing the cache to grow beyond the maximum
122 * allowed size will also cause the oldest texture to be kicked out.
123 */
124class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> {
125public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700126 PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700127 PathCache(uint32_t maxByteSize);
128 ~PathCache();
129
130 /**
131 * Used as a callback when an entry is removed from the cache.
132 * Do not invoke directly.
133 */
134 void operator()(PathCacheEntry& path, PathTexture*& texture);
135
136 /**
137 * Returns the texture associated with the specified path. If the texture
138 * cannot be found in the cache, a new texture is generated.
139 */
140 PathTexture* get(SkPath* path, SkPaint* paint);
141 /**
142 * Clears the cache. This causes all textures to be deleted.
143 */
144 void clear();
Romain Guya2341a92010-09-08 18:04:33 -0700145 /**
146 * Removes an entry.
147 */
148 void remove(SkPath* path);
Romain Guy7fbcc042010-08-04 15:40:07 -0700149
150 /**
151 * Sets the maximum size of the cache in bytes.
152 */
153 void setMaxSize(uint32_t maxSize);
154 /**
155 * Returns the maximum size of the cache in bytes.
156 */
157 uint32_t getMaxSize();
158 /**
159 * Returns the current size of the cache in bytes.
160 */
161 uint32_t getSize();
162
163private:
164 /**
165 * Generates the texture from a bitmap into the specified texture structure.
166 */
167 void generateTexture(SkBitmap& bitmap, Texture* texture);
168
169 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint);
170
Romain Guyfb8b7632010-08-23 21:05:08 -0700171 void init();
172
Romain Guy7fbcc042010-08-04 15:40:07 -0700173 GenerationCache<PathCacheEntry, PathTexture*> mCache;
174
175 uint32_t mSize;
176 uint32_t mMaxSize;
Romain Guy9cccc2b92010-08-07 23:46:15 -0700177 GLuint mMaxTextureSize;
Romain Guya2341a92010-09-08 18:04:33 -0700178
179 /**
180 * Used to access mCache and mSize. All methods are accessed from a single
181 * thread except for remove().
182 */
183 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700184}; // class PathCache
185
186}; // namespace uirenderer
187}; // namespace android
188
Romain Guy5b3b3522010-10-27 18:57:51 -0700189#endif // ANDROID_HWUI_PATH_CACHE_H