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 /** 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 */ 52 class TextureCache: public OnEntryRemoved<const SkPixelRef*, Texture*> { 53 public: 54 TextureCache(); 55 TextureCache(uint32_t maxByteSize); 56 ~TextureCache(); 57 58 /** 59 * Used as a callback when an entry is removed from the cache. 60 * Do not invoke directly. 61 */ 62 void operator()(const SkPixelRef*& pixelRef, Texture*& texture); 63 64 /** 65 * Resets all Textures to not be marked as in use 66 */ 67 void resetMarkInUse(); 68 69 /** 70 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully 71 * acquired for the bitmap, false otherwise. If a Texture was acquired it is 72 * marked as in use. 73 */ 74 bool prefetchAndMarkInUse(const SkBitmap* bitmap); 75 76 /** 77 * Returns the texture associated with the specified bitmap. If the texture 78 * cannot be found in the cache, a new texture is generated. 79 */ 80 Texture* get(const SkBitmap* bitmap); 81 /** 82 * Returns the texture associated with the specified bitmap. The generated 83 * texture is not kept in the cache. The caller must destroy the texture. 84 */ 85 Texture* getTransient(const SkBitmap* bitmap); 86 /** 87 * Removes the texture associated with the specified bitmap. 88 * Upon remove the texture is freed. 89 */ 90 void remove(const SkBitmap* bitmap); 91 /** 92 * Removes the texture associated with the specified bitmap. This is meant 93 * to be called from threads that are not the EGL context thread. 94 */ 95 void removeDeferred(const SkBitmap* bitmap); 96 /** 97 * Process deferred removals. 98 */ 99 void clearGarbage(); 100 101 /** 102 * Clears the cache. This causes all textures to be deleted. 103 */ 104 void clear(); 105 106 /** 107 * Sets the maximum size of the cache in bytes. 108 */ 109 void setMaxSize(uint32_t maxSize); 110 /** 111 * Returns the maximum size of the cache in bytes. 112 */ 113 uint32_t getMaxSize(); 114 /** 115 * Returns the current size of the cache in bytes. 116 */ 117 uint32_t getSize(); 118 119 /** 120 * Partially flushes the cache. The amount of memory freed by a flush 121 * is defined by the flush rate. 122 */ 123 void flush(); 124 /** 125 * Indicates the percentage of the cache to retain when a 126 * memory trim is requested (see Caches::flush). 127 */ 128 void setFlushRate(float flushRate); 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<const SkPixelRef*, 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<const SkBitmap*> mGarbage; 161 mutable Mutex mLock; 162 }; // class TextureCache 163 164 }; // namespace uirenderer 165 }; // namespace android 166 167 #endif // ANDROID_HWUI_TEXTURE_CACHE_H 168