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_HWUI_TEXTURE_CACHE_H 18 #define ANDROID_HWUI_TEXTURE_CACHE_H 19 20 #include <SkBitmap.h> 21 22 #include <utils/LruCache.h> 23 #include <utils/Mutex.h> 24 #include <utils/Vector.h> 25 26 #include "Debug.h" 27 #include "Texture.h" 28 29 namespace android { 30 namespace uirenderer { 31 32 /////////////////////////////////////////////////////////////////////////////// 33 // Defines 34 /////////////////////////////////////////////////////////////////////////////// 35 36 // Debug 37 #if DEBUG_TEXTURES 38 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__) 39 #else 40 #define TEXTURE_LOGD(...) 41 #endif 42 43 /////////////////////////////////////////////////////////////////////////////// 44 // Classes 45 /////////////////////////////////////////////////////////////////////////////// 46 47 class AssetAtlas; 48 49 /** 50 * A simple LRU texture cache. The cache has a maximum size expressed in bytes. 51 * Any texture added to the cache causing the cache to grow beyond the maximum 52 * allowed size will also cause the oldest texture to be kicked out. 53 */ 54 class TextureCache: public OnEntryRemoved<uint32_t, Texture*> { 55 public: 56 TextureCache(); 57 TextureCache(uint32_t maxByteSize); 58 ~TextureCache(); 59 60 /** 61 * Used as a callback when an entry is removed from the cache. 62 * Do not invoke directly. 63 */ 64 void operator()(uint32_t&, Texture*& texture); 65 66 /** 67 * Resets all Textures to not be marked as in use 68 */ 69 void resetMarkInUse(); 70 71 /** 72 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully 73 * acquired for the bitmap, false otherwise. If a Texture was acquired it is 74 * marked as in use. 75 */ 76 bool prefetchAndMarkInUse(const SkBitmap* bitmap); 77 78 /** 79 * Returns the texture associated with the specified bitmap. If the texture 80 * cannot be found in the cache, a new texture is generated. 81 */ 82 Texture* get(const SkBitmap* bitmap); 83 /** 84 * Returns the texture associated with the specified bitmap. The generated 85 * texture is not kept in the cache. The caller must destroy the texture. 86 */ 87 Texture* getTransient(const SkBitmap* bitmap); 88 89 /** 90 * Removes the texture associated with the specified bitmap. This is meant 91 * to be called from threads that are not the EGL context thread. 92 */ 93 void releaseTexture(const SkBitmap* bitmap); 94 /** 95 * Process deferred removals. 96 */ 97 void clearGarbage(); 98 99 /** 100 * Clears the cache. This causes all textures to be deleted. 101 */ 102 void clear(); 103 104 /** 105 * Sets the maximum size of the cache in bytes. 106 */ 107 void setMaxSize(uint32_t maxSize); 108 /** 109 * Returns the maximum size of the cache in bytes. 110 */ 111 uint32_t getMaxSize(); 112 /** 113 * Returns the current size of the cache in bytes. 114 */ 115 uint32_t getSize(); 116 117 /** 118 * Partially flushes the cache. The amount of memory freed by a flush 119 * is defined by the flush rate. 120 */ 121 void flush(); 122 /** 123 * Indicates the percentage of the cache to retain when a 124 * memory trim is requested (see Caches::flush). 125 */ 126 void setFlushRate(float flushRate); 127 128 void setAssetAtlas(AssetAtlas* assetAtlas); 129 130 private: 131 132 bool canMakeTextureFromBitmap(const SkBitmap* bitmap); 133 134 Texture* getCachedTexture(const SkBitmap* bitmap); 135 136 /** 137 * Generates the texture from a bitmap into the specified texture structure. 138 * 139 * @param regenerate If true, the bitmap data is reuploaded into the texture, but 140 * no new texture is generated. 141 */ 142 void generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate = false); 143 144 void uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height); 145 void uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp, 146 GLsizei width, GLsizei height, GLenum type, const GLvoid * data); 147 148 void init(); 149 150 LruCache<uint32_t, Texture*> mCache; 151 152 uint32_t mSize; 153 uint32_t mMaxSize; 154 GLint mMaxTextureSize; 155 156 float mFlushRate; 157 158 bool mDebugEnabled; 159 160 Vector<uint32_t> mGarbage; 161 mutable Mutex mLock; 162 163 AssetAtlas* mAssetAtlas; 164 }; // class TextureCache 165 166 }; // namespace uirenderer 167 }; // namespace android 168 169 #endif // ANDROID_HWUI_TEXTURE_CACHE_H 170