blob: 12c8a231aabe8d7f515207cc42112d3d5a35f062 [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:
35 GradientCache(uint32_t maxByteSize);
36 ~GradientCache();
37
38 /**
39 * Used as a callback when an entry is removed from the cache.
40 * Do not invoke directly.
41 */
42 void operator()(SkShader*& shader, Texture*& texture);
43
44 /**
45 * Adds a new linear gradient to the cache. The generated texture is
46 * returned.
47 */
48 Texture* addLinearGradient(SkShader* shader, float* bounds, uint32_t* colors,
49 float* positions, int count, SkShader::TileMode tileMode);
50 /**
51 * Returns the texture associated with the specified shader.
52 */
53 Texture* get(SkShader* shader);
54 /**
55 * Removes the texture associated with the specified shader. Returns NULL
56 * if the texture cannot be found. Upon remove the texture is freed.
57 */
58 void remove(SkShader* shader);
59 /**
60 * Clears the cache. This causes all textures to be deleted.
61 */
62 void clear();
63
64 /**
65 * Sets the maximum size of the cache in bytes.
66 */
67 void setMaxSize(uint32_t maxSize);
68 /**
69 * Returns the maximum size of the cache in bytes.
70 */
71 uint32_t getMaxSize();
72 /**
73 * Returns the current size of the cache in bytes.
74 */
75 uint32_t getSize();
76
77private:
78 void generateTexture(SkBitmap* bitmap, Texture* texture);
79
80 GenerationCache<SkShader*, Texture*> mCache;
81
82 uint32_t mSize;
83 uint32_t mMaxSize;
84}; // class GradientCache
85
86}; // namespace uirenderer
87}; // namespace android
88
89#endif // ANDROID_UI_GRADIENT_CACHE_H