blob: b5e4c7c19038f7278bfc37f716b93290c09b434a [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 Guyfb8b7632010-08-23 21:05:08 -070035 TextureCache();
Romain Guy7d139ba2010-07-02 11:20:34 -070036 TextureCache(uint32_t maxByteSize);
Romain Guyce0537b2010-06-29 21:05:21 -070037 ~TextureCache();
38
Romain Guy121e2242010-07-01 18:26:52 -070039 /**
40 * Used as a callback when an entry is removed from the cache.
41 * Do not invoke directly.
42 */
Romain Guydda57022010-07-06 11:39:32 -070043 void operator()(SkBitmap*& bitmap, Texture*& texture);
Romain Guyce0537b2010-06-29 21:05:21 -070044
Romain Guy121e2242010-07-01 18:26:52 -070045 /**
46 * Returns the texture associated with the specified bitmap. If the texture
47 * cannot be found in the cache, a new texture is generated.
48 */
Romain Guyce0537b2010-06-29 21:05:21 -070049 Texture* get(SkBitmap* bitmap);
Romain Guy121e2242010-07-01 18:26:52 -070050 /**
51 * Removes the texture associated with the specified bitmap. Returns NULL
52 * if the texture cannot be found. Upon remove the texture is freed.
53 */
54 void remove(SkBitmap* bitmap);
55 /**
56 * Clears the cache. This causes all textures to be deleted.
57 */
Romain Guyce0537b2010-06-29 21:05:21 -070058 void clear();
59
Romain Guy121e2242010-07-01 18:26:52 -070060 /**
61 * Sets the maximum size of the cache in bytes.
62 */
Romain Guy7d139ba2010-07-02 11:20:34 -070063 void setMaxSize(uint32_t maxSize);
Romain Guy121e2242010-07-01 18:26:52 -070064 /**
65 * Returns the maximum size of the cache in bytes.
66 */
Romain Guy7d139ba2010-07-02 11:20:34 -070067 uint32_t getMaxSize();
Romain Guy121e2242010-07-01 18:26:52 -070068 /**
69 * Returns the current size of the cache in bytes.
70 */
Romain Guy7d139ba2010-07-02 11:20:34 -070071 uint32_t getSize();
Romain Guy121e2242010-07-01 18:26:52 -070072
Romain Guyce0537b2010-06-29 21:05:21 -070073private:
Romain Guy121e2242010-07-01 18:26:52 -070074 /**
75 * Generates the texture from a bitmap into the specified texture structure.
76 *
77 * @param regenerate If true, the bitmap data is reuploaded into the texture, but
78 * no new texture is generated.
79 */
Romain Guyfe880942010-06-30 16:05:32 -070080 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -070081
Romain Guyfb8b7632010-08-23 21:05:08 -070082 void init();
83
Romain Guy6c818932010-07-07 15:15:32 -070084 GenerationCache<SkBitmap*, Texture*> mCache;
Romain Guy121e2242010-07-01 18:26:52 -070085
Romain Guy7d139ba2010-07-02 11:20:34 -070086 uint32_t mSize;
87 uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -070088 GLint mMaxTextureSize;
Romain Guyce0537b2010-06-29 21:05:21 -070089}; // class TextureCache
90
91}; // namespace uirenderer
92}; // namespace android
93
94#endif // ANDROID_UI_TEXTURE_CACHE_H