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