blob: bde0e7d845e96cd6369c38e0ddcf8c6ef4fafc32 [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
17#ifndef ANDROID_UI_PATH_CACHE_H
18#define ANDROID_UI_PATH_CACHE_H
19
20#include <SkBitmap.h>
21#include <SkPaint.h>
22#include <SkPath.h>
23
24#include "Texture.h"
25#include "GenerationCache.h"
26
27namespace android {
28namespace uirenderer {
29
30/**
31 * Describe a path in the path cache.
32 */
33struct PathCacheEntry {
34 PathCacheEntry() {
35 path = NULL;
36 join = SkPaint::kDefault_Join;
37 cap = SkPaint::kDefault_Cap;
38 style = SkPaint::kFill_Style;
39 miter = 4.0f;
40 strokeWidth = 1.0f;
41 }
42
43 PathCacheEntry(const PathCacheEntry& entry):
44 path(entry.path), join(entry.join), cap(entry.cap),
45 style(entry.style), miter(entry.miter),
46 strokeWidth(entry.strokeWidth) {
47 }
48
49 PathCacheEntry(SkPath* path, SkPaint* paint) {
50 this->path = path;
51 join = paint->getStrokeJoin();
52 cap = paint->getStrokeCap();
53 miter = paint->getStrokeMiter();
54 strokeWidth = paint->getStrokeWidth();
55 style = paint->getStyle();
56 }
57
58 SkPath* path;
59 SkPaint::Join join;
60 SkPaint::Cap cap;
61 SkPaint::Style style;
62 float miter;
63 float strokeWidth;
64
65 bool operator<(const PathCacheEntry& rhs) const {
66 return memcmp(this, &rhs, sizeof(PathCacheEntry)) < 0;
67 }
68}; // struct PathCacheEntry
69
70/**
71 * Alpha texture used to represent a path.
72 */
73struct PathTexture: public Texture {
Romain Guy22158e12010-08-06 11:18:34 -070074 PathTexture(): Texture() {
75 }
76
Romain Guy7fbcc042010-08-04 15:40:07 -070077 /**
78 * Left coordinate of the path bounds.
79 */
80 float left;
81 /**
82 * Top coordinate of the path bounds.
83 */
84 float top;
85 /**
86 * Offset to draw the path at the correct origin.
87 */
88 float offset;
89}; // struct PathTexture
90
91/**
92 * A simple LRU path cache. The cache has a maximum size expressed in bytes.
93 * Any texture added to the cache causing the cache to grow beyond the maximum
94 * allowed size will also cause the oldest texture to be kicked out.
95 */
96class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> {
97public:
Romain Guyfb8b7632010-08-23 21:05:08 -070098 PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -070099 PathCache(uint32_t maxByteSize);
100 ~PathCache();
101
102 /**
103 * Used as a callback when an entry is removed from the cache.
104 * Do not invoke directly.
105 */
106 void operator()(PathCacheEntry& path, PathTexture*& texture);
107
108 /**
109 * Returns the texture associated with the specified path. If the texture
110 * cannot be found in the cache, a new texture is generated.
111 */
112 PathTexture* get(SkPath* path, SkPaint* paint);
113 /**
114 * Clears the cache. This causes all textures to be deleted.
115 */
116 void clear();
Romain Guya2341a92010-09-08 18:04:33 -0700117 /**
118 * Removes an entry.
119 */
120 void remove(SkPath* path);
Romain Guy7fbcc042010-08-04 15:40:07 -0700121
122 /**
123 * Sets the maximum size of the cache in bytes.
124 */
125 void setMaxSize(uint32_t maxSize);
126 /**
127 * Returns the maximum size of the cache in bytes.
128 */
129 uint32_t getMaxSize();
130 /**
131 * Returns the current size of the cache in bytes.
132 */
133 uint32_t getSize();
134
135private:
136 /**
137 * Generates the texture from a bitmap into the specified texture structure.
138 */
139 void generateTexture(SkBitmap& bitmap, Texture* texture);
140
141 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint);
142
Romain Guyfb8b7632010-08-23 21:05:08 -0700143 void init();
144
Romain Guy7fbcc042010-08-04 15:40:07 -0700145 GenerationCache<PathCacheEntry, PathTexture*> mCache;
146
147 uint32_t mSize;
148 uint32_t mMaxSize;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700149 GLuint mMaxTextureSize;
Romain Guya2341a92010-09-08 18:04:33 -0700150
151 /**
152 * Used to access mCache and mSize. All methods are accessed from a single
153 * thread except for remove().
154 */
155 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700156}; // class PathCache
157
158}; // namespace uirenderer
159}; // namespace android
160
161#endif // ANDROID_UI_PATH_CACHE_H