blob: 250efc56f60f49e1e1c8b6f91ac7db3498598a37 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -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_TEXTURE_CACHE_H
18#define ANDROID_UI_TEXTURE_CACHE_H
19
20#include <SkBitmap.h>
21
22#include "Texture.h"
23#include "GenerationCache.h"
24
25namespace android {
26namespace uirenderer {
27
Romain Guy121e2242010-07-01 18:26:52 -070028/**
29 * A simple LRU texture 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 */
Romain Guyce0537b2010-06-29 21:05:21 -070033class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
34public:
Romain Guy7d139ba2010-07-02 11:20:34 -070035 TextureCache(uint32_t maxByteSize);
Romain Guyce0537b2010-06-29 21:05:21 -070036 ~TextureCache();
37
Romain Guy121e2242010-07-01 18:26:52 -070038 /**
39 * Used as a callback when an entry is removed from the cache.
40 * Do not invoke directly.
41 */
42 void operator()(SkBitmap* bitmap, Texture* texture);
Romain Guyce0537b2010-06-29 21:05:21 -070043
Romain Guy121e2242010-07-01 18:26:52 -070044 /**
45 * Returns the texture associated with the specified bitmap. If the texture
46 * cannot be found in the cache, a new texture is generated.
47 */
Romain Guyce0537b2010-06-29 21:05:21 -070048 Texture* get(SkBitmap* bitmap);
Romain Guy121e2242010-07-01 18:26:52 -070049 /**
50 * Removes the texture associated with the specified bitmap. Returns NULL
51 * if the texture cannot be found. Upon remove the texture is freed.
52 */
53 void remove(SkBitmap* bitmap);
54 /**
55 * Clears the cache. This causes all textures to be deleted.
56 */
Romain Guyce0537b2010-06-29 21:05:21 -070057 void clear();
58
Romain Guy121e2242010-07-01 18:26:52 -070059 /**
60 * Sets the maximum size of the cache in bytes.
61 */
Romain Guy7d139ba2010-07-02 11:20:34 -070062 void setMaxSize(uint32_t maxSize);
Romain Guy121e2242010-07-01 18:26:52 -070063 /**
64 * Returns the maximum size of the cache in bytes.
65 */
Romain Guy7d139ba2010-07-02 11:20:34 -070066 uint32_t getMaxSize();
Romain Guy121e2242010-07-01 18:26:52 -070067 /**
68 * Returns the current size of the cache in bytes.
69 */
Romain Guy7d139ba2010-07-02 11:20:34 -070070 uint32_t getSize();
Romain Guy121e2242010-07-01 18:26:52 -070071
Romain Guyce0537b2010-06-29 21:05:21 -070072private:
Romain Guy121e2242010-07-01 18:26:52 -070073 /**
74 * Generates the texture from a bitmap into the specified texture structure.
75 *
76 * @param regenerate If true, the bitmap data is reuploaded into the texture, but
77 * no new texture is generated.
78 */
Romain Guyfe880942010-06-30 16:05:32 -070079 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -070080
81 GenerationCache<SkBitmap, Texture> mCache;
Romain Guy121e2242010-07-01 18:26:52 -070082
Romain Guy7d139ba2010-07-02 11:20:34 -070083 uint32_t mSize;
84 uint32_t mMaxSize;
Romain Guyce0537b2010-06-29 21:05:21 -070085}; // class TextureCache
86
87}; // namespace uirenderer
88}; // namespace android
89
90#endif // ANDROID_UI_TEXTURE_CACHE_H