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