blob: c9553f4d69203939b0eb14bc9c9a35e9d035e3fc [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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_GRADIENT_CACHE_H
18#define ANDROID_HWUI_GRADIENT_CACHE_H
Romain Guyc0ac1932010-07-19 18:43:02 -070019
20#include <SkShader.h>
21
22#include "Texture.h"
Romain Guy21b028a2010-10-08 18:43:58 -070023#include "utils/GenerationCache.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070024
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 */
Romain Guyee916f12010-09-20 17:53:08 -070049 Texture* addLinearGradient(SkShader* shader, uint32_t* colors, float* positions,
50 int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -070051 /**
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
Romain Guy5b3b3522010-10-27 18:57:51 -070096#endif // ANDROID_HWUI_GRADIENT_CACHE_H