blob: 51a8c0185adb931ec95fe2f3c489a4a576c025de [file] [log] [blame]
Romain Guyc0ac1932010-07-19 18:43:02 -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_GRADIENT_CACHE_H
18#define ANDROID_UI_GRADIENT_CACHE_H
19
20#include <SkShader.h>
21
22#include "Texture.h"
23#include "GenerationCache.h"
24
25namespace android {
26namespace uirenderer {
27
28/**
29 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
30 * Any texture added to the cache causing the cache to grow beyond the maximum
31 * allowed size will also cause the oldest texture to be kicked out.
32 */
33class GradientCache: public OnEntryRemoved<SkShader*, Texture*> {
34public:
Romain Guyfb8b7632010-08-23 21:05:08 -070035 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -070036 GradientCache(uint32_t maxByteSize);
37 ~GradientCache();
38
39 /**
40 * Used as a callback when an entry is removed from the cache.
41 * Do not invoke directly.
42 */
43 void operator()(SkShader*& shader, Texture*& texture);
44
45 /**
46 * Adds a new linear gradient to the cache. The generated texture is
47 * returned.
48 */
49 Texture* addLinearGradient(SkShader* shader, float* bounds, uint32_t* colors,
50 float* positions, int count, SkShader::TileMode tileMode);
51 /**
52 * Returns the texture associated with the specified shader.
53 */
54 Texture* get(SkShader* shader);
55 /**
56 * Removes the texture associated with the specified shader. Returns NULL
57 * if the texture cannot be found. Upon remove the texture is freed.
58 */
59 void remove(SkShader* shader);
60 /**
61 * Clears the cache. This causes all textures to be deleted.
62 */
63 void clear();
64
65 /**
66 * Sets the maximum size of the cache in bytes.
67 */
68 void setMaxSize(uint32_t maxSize);
69 /**
70 * Returns the maximum size of the cache in bytes.
71 */
72 uint32_t getMaxSize();
73 /**
74 * Returns the current size of the cache in bytes.
75 */
76 uint32_t getSize();
77
78private:
79 void generateTexture(SkBitmap* bitmap, Texture* texture);
80
81 GenerationCache<SkShader*, Texture*> mCache;
82
83 uint32_t mSize;
84 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -070085
86 /**
87 * Used to access mCache and mSize. All methods are accessed from a single
88 * thread except for remove().
89 */
90 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -070091}; // class GradientCache
92
93}; // namespace uirenderer
94}; // namespace android
95
96#endif // ANDROID_UI_GRADIENT_CACHE_H