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" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 23 | #include "utils/GenerationCache.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Defines |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
| 32 | // Debug |
| 33 | #define DEBUG_TEXTURES 0 |
| 34 | |
| 35 | // Debug |
| 36 | #if DEBUG_TEXTURES |
| 37 | #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__) |
| 38 | #else |
| 39 | #define TEXTURE_LOGD(...) |
| 40 | #endif |
| 41 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 42 | /** |
| 43 | * A simple LRU texture cache. The cache has a maximum size expressed in bytes. |
| 44 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 45 | * allowed size will also cause the oldest texture to be kicked out. |
| 46 | */ |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 47 | class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> { |
| 48 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 49 | TextureCache(); |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 50 | TextureCache(uint32_t maxByteSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 51 | ~TextureCache(); |
| 52 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Used as a callback when an entry is removed from the cache. |
| 55 | * Do not invoke directly. |
| 56 | */ |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 57 | void operator()(SkBitmap*& bitmap, Texture*& texture); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 58 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Returns the texture associated with the specified bitmap. If the texture |
| 61 | * cannot be found in the cache, a new texture is generated. |
| 62 | */ |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 63 | Texture* get(SkBitmap* bitmap); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Removes the texture associated with the specified bitmap. Returns NULL |
| 66 | * if the texture cannot be found. Upon remove the texture is freed. |
| 67 | */ |
| 68 | void remove(SkBitmap* bitmap); |
| 69 | /** |
| 70 | * Clears the cache. This causes all textures to be deleted. |
| 71 | */ |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 72 | void clear(); |
| 73 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Sets the maximum size of the cache in bytes. |
| 76 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 77 | void setMaxSize(uint32_t maxSize); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Returns the maximum size of the cache in bytes. |
| 80 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 81 | uint32_t getMaxSize(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Returns the current size of the cache in bytes. |
| 84 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 85 | uint32_t getSize(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 86 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 87 | private: |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Generates the texture from a bitmap into the specified texture structure. |
| 90 | * |
| 91 | * @param regenerate If true, the bitmap data is reuploaded into the texture, but |
| 92 | * no new texture is generated. |
| 93 | */ |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 94 | void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 95 | |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 96 | void uploadPalettedTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 97 | void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height, |
| 98 | GLenum type, const GLvoid * data); |
| 99 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 100 | void init(); |
| 101 | |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 102 | GenerationCache<SkBitmap*, Texture*> mCache; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 103 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 104 | uint32_t mSize; |
| 105 | uint32_t mMaxSize; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 106 | GLint mMaxTextureSize; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 107 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Used to access mCache and mSize. All methods are accessed from a single |
| 110 | * thread except for remove(). |
| 111 | */ |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 112 | mutable Mutex mLock; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 113 | }; // class TextureCache |
| 114 | |
| 115 | }; // namespace uirenderer |
| 116 | }; // namespace android |
| 117 | |
| 118 | #endif // ANDROID_UI_TEXTURE_CACHE_H |