Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 28 | /** |
| 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 Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 33 | class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> { |
| 34 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 35 | TextureCache(); |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 36 | TextureCache(uint32_t maxByteSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 37 | ~TextureCache(); |
| 38 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Used as a callback when an entry is removed from the cache. |
| 41 | * Do not invoke directly. |
| 42 | */ |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 43 | void operator()(SkBitmap*& bitmap, Texture*& texture); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 44 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 45 | /** |
| 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 Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 49 | Texture* get(SkBitmap* bitmap); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 50 | /** |
| 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 Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 58 | void clear(); |
| 59 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Sets the maximum size of the cache in bytes. |
| 62 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 63 | void setMaxSize(uint32_t maxSize); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Returns the maximum size of the cache in bytes. |
| 66 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 67 | uint32_t getMaxSize(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Returns the current size of the cache in bytes. |
| 70 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 71 | uint32_t getSize(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 72 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 73 | private: |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 74 | /** |
| 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 Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 80 | void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 81 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 82 | void init(); |
| 83 | |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 84 | GenerationCache<SkBitmap*, Texture*> mCache; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 85 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 86 | uint32_t mSize; |
| 87 | uint32_t mMaxSize; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 88 | GLint mMaxTextureSize; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 89 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame^] | 90 | /** |
| 91 | * Used to access mCache and mSize. All methods are accessed from a single |
| 92 | * thread except for remove(). |
| 93 | */ |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 94 | mutable Mutex mLock; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 95 | }; // class TextureCache |
| 96 | |
| 97 | }; // namespace uirenderer |
| 98 | }; // namespace android |
| 99 | |
| 100 | #endif // ANDROID_UI_TEXTURE_CACHE_H |