blob: db5ce08336357493df3b3a41b27d7461200b8248 [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
31/**
32 * Describe a path in the path cache.
33 */
34struct PathCacheEntry {
35 PathCacheEntry() {
36 path = NULL;
37 join = SkPaint::kDefault_Join;
38 cap = SkPaint::kDefault_Cap;
39 style = SkPaint::kFill_Style;
40 miter = 4.0f;
41 strokeWidth = 1.0f;
42 }
43
44 PathCacheEntry(const PathCacheEntry& entry):
45 path(entry.path), join(entry.join), cap(entry.cap),
46 style(entry.style), miter(entry.miter),
47 strokeWidth(entry.strokeWidth) {
48 }
49
50 PathCacheEntry(SkPath* path, SkPaint* paint) {
51 this->path = path;
52 join = paint->getStrokeJoin();
53 cap = paint->getStrokeCap();
54 miter = paint->getStrokeMiter();
55 strokeWidth = paint->getStrokeWidth();
56 style = paint->getStyle();
57 }
58
59 SkPath* path;
60 SkPaint::Join join;
61 SkPaint::Cap cap;
62 SkPaint::Style style;
63 float miter;
64 float strokeWidth;
65
66 bool operator<(const PathCacheEntry& rhs) const {
Romain Guy2665b852010-10-18 15:11:50 -070067 LTE_INT(path) {
68 LTE_INT(join) {
69 LTE_INT(cap) {
70 LTE_INT(style) {
71 LTE_FLOAT(miter) {
72 LTE_FLOAT(strokeWidth) return false;
Romain Guy4bb94202010-10-12 15:59:26 -070073 }
74 }
75 }
76 }
77 }
78 return false;
Romain Guy7fbcc042010-08-04 15:40:07 -070079 }
80}; // struct PathCacheEntry
81
82/**
83 * Alpha texture used to represent a path.
84 */
85struct PathTexture: public Texture {
Romain Guy22158e12010-08-06 11:18:34 -070086 PathTexture(): Texture() {
87 }
88
Romain Guy7fbcc042010-08-04 15:40:07 -070089 /**
90 * Left coordinate of the path bounds.
91 */
92 float left;
93 /**
94 * Top coordinate of the path bounds.
95 */
96 float top;
97 /**
98 * Offset to draw the path at the correct origin.
99 */
100 float offset;
101}; // struct PathTexture
102
103/**
104 * A simple LRU path cache. The cache has a maximum size expressed in bytes.
105 * Any texture added to the cache causing the cache to grow beyond the maximum
106 * allowed size will also cause the oldest texture to be kicked out.
107 */
108class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> {
109public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700110 PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700111 PathCache(uint32_t maxByteSize);
112 ~PathCache();
113
114 /**
115 * Used as a callback when an entry is removed from the cache.
116 * Do not invoke directly.
117 */
118 void operator()(PathCacheEntry& path, PathTexture*& texture);
119
120 /**
121 * Returns the texture associated with the specified path. If the texture
122 * cannot be found in the cache, a new texture is generated.
123 */
124 PathTexture* get(SkPath* path, SkPaint* paint);
125 /**
126 * Clears the cache. This causes all textures to be deleted.
127 */
128 void clear();
Romain Guya2341a92010-09-08 18:04:33 -0700129 /**
130 * Removes an entry.
131 */
132 void remove(SkPath* path);
Romain Guy7fbcc042010-08-04 15:40:07 -0700133
134 /**
135 * Sets the maximum size of the cache in bytes.
136 */
137 void setMaxSize(uint32_t maxSize);
138 /**
139 * Returns the maximum size of the cache in bytes.
140 */
141 uint32_t getMaxSize();
142 /**
143 * Returns the current size of the cache in bytes.
144 */
145 uint32_t getSize();
146
147private:
148 /**
149 * Generates the texture from a bitmap into the specified texture structure.
150 */
151 void generateTexture(SkBitmap& bitmap, Texture* texture);
152
153 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint);
154
Romain Guyfb8b7632010-08-23 21:05:08 -0700155 void init();
156
Romain Guy7fbcc042010-08-04 15:40:07 -0700157 GenerationCache<PathCacheEntry, PathTexture*> mCache;
158
159 uint32_t mSize;
160 uint32_t mMaxSize;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700161 GLuint mMaxTextureSize;
Romain Guya2341a92010-09-08 18:04:33 -0700162
163 /**
164 * Used to access mCache and mSize. All methods are accessed from a single
165 * thread except for remove().
166 */
167 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700168}; // class PathCache
169
170}; // namespace uirenderer
171}; // namespace android
172
Romain Guy5b3b3522010-10-27 18:57:51 -0700173#endif // ANDROID_HWUI_PATH_CACHE_H