TextureCache.h revision 029f64303b8fe506ef34c12777da86b830d3bf7a
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/Mutex.h> 23#include <utils/Vector.h> 24 25#include "Debug.h" 26#include "Texture.h" 27#include "utils/GenerationCache.h" 28 29namespace android { 30namespace 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 */ 52class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> { 53public: 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()(SkBitmap*& bitmap, Texture*& texture); 63 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 */ 68 Texture* get(SkBitmap* bitmap); 69 /** 70 * Removes the texture associated with the specified bitmap. 71 * Upon remove the texture is freed. 72 */ 73 void remove(SkBitmap* bitmap); 74 /** 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 /** 85 * Clears the cache. This causes all textures to be deleted. 86 */ 87 void clear(); 88 89 /** 90 * Sets the maximum size of the cache in bytes. 91 */ 92 void setMaxSize(uint32_t maxSize); 93 /** 94 * Returns the maximum size of the cache in bytes. 95 */ 96 uint32_t getMaxSize(); 97 /** 98 * Returns the current size of the cache in bytes. 99 */ 100 uint32_t getSize(); 101 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 113private: 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 */ 120 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false); 121 122 void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height); 123 void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height, 124 GLenum type, const GLvoid * data); 125 126 void init(); 127 128 GenerationCache<SkBitmap*, Texture*> mCache; 129 130 uint32_t mSize; 131 uint32_t mMaxSize; 132 GLint mMaxTextureSize; 133 134 float mFlushRate; 135 136 bool mDebugEnabled; 137 138 Vector<SkBitmap*> mGarbage; 139 mutable Mutex mLock; 140}; // class TextureCache 141 142}; // namespace uirenderer 143}; // namespace android 144 145#endif // ANDROID_HWUI_TEXTURE_CACHE_H 146