PathCache.h revision 03750a067e818ca7fbd0f590e2ff6a8fded21e6c
1/*
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 "utils/Compare.h"
26#include "utils/GenerationCache.h"
27
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 {
67        INT_COMPARE(path) {
68            INT_COMPARE(join) {
69                INT_COMPARE(cap) {
70                    INT_COMPARE(style) {
71                        FLOAT_COMPARE(miter) {
72                            FLOAT_COMPARE(strokeWidth) return false;
73                        }
74                    }
75                }
76            }
77        }
78        return false;
79    }
80}; // struct PathCacheEntry
81
82/**
83 * Alpha texture used to represent a path.
84 */
85struct PathTexture: public Texture {
86    PathTexture(): Texture() {
87    }
88
89    /**
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:
110    PathCache();
111    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();
129    /**
130     * Removes an entry.
131     */
132    void remove(SkPath* path);
133
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
155    void init();
156
157    GenerationCache<PathCacheEntry, PathTexture*> mCache;
158
159    uint32_t mSize;
160    uint32_t mMaxSize;
161    GLuint mMaxTextureSize;
162
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;
168}; // class PathCache
169
170}; // namespace uirenderer
171}; // namespace android
172
173#endif // ANDROID_UI_PATH_CACHE_H
174