TextureCache.h revision 117bdbcfa3e8306dad21e7e01fa71b00cdfa7265
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 28namespace android { 29namespace uirenderer { 30 31class Texture; 32 33/////////////////////////////////////////////////////////////////////////////// 34// Defines 35/////////////////////////////////////////////////////////////////////////////// 36 37// Debug 38#if DEBUG_TEXTURES 39 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__) 40#else 41 #define TEXTURE_LOGD(...) 42#endif 43 44/////////////////////////////////////////////////////////////////////////////// 45// Classes 46/////////////////////////////////////////////////////////////////////////////// 47 48class AssetAtlas; 49 50/** 51 * A simple LRU texture cache. The cache has a maximum size expressed in bytes. 52 * Any texture added to the cache causing the cache to grow beyond the maximum 53 * allowed size will also cause the oldest texture to be kicked out. 54 */ 55class TextureCache : public OnEntryRemoved<uint32_t, Texture*> { 56public: 57 TextureCache(); 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) override; 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 pixelRef. This is meant 91 * to be called from threads that are not the EGL context thread. 92 */ 93 ANDROID_API void releaseTexture(uint32_t pixelRefStableID); 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 130private: 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 LruCache<uint32_t, Texture*> mCache; 149 150 uint32_t mSize; 151 uint32_t mMaxSize; 152 GLint mMaxTextureSize; 153 154 float mFlushRate; 155 156 bool mDebugEnabled; 157 158 Vector<uint32_t> mGarbage; 159 mutable Mutex mLock; 160 161 AssetAtlas* mAssetAtlas; 162}; // class TextureCache 163 164}; // namespace uirenderer 165}; // namespace android 166 167#endif // ANDROID_HWUI_TEXTURE_CACHE_H 168