TextureCache.h revision 9e10841c27d973b930e1b49a099c69d866659505
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 "Texture.h" 23#include "utils/GenerationCache.h" 24 25namespace android { 26namespace uirenderer { 27 28/////////////////////////////////////////////////////////////////////////////// 29// Defines 30/////////////////////////////////////////////////////////////////////////////// 31 32// Debug 33#define DEBUG_TEXTURES 0 34 35// Debug 36#if DEBUG_TEXTURES 37 #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__) 38#else 39 #define TEXTURE_LOGD(...) 40#endif 41 42/////////////////////////////////////////////////////////////////////////////// 43// Classes 44/////////////////////////////////////////////////////////////////////////////// 45 46/** 47 * A simple LRU texture cache. The cache has a maximum size expressed in bytes. 48 * Any texture added to the cache causing the cache to grow beyond the maximum 49 * allowed size will also cause the oldest texture to be kicked out. 50 */ 51class TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> { 52public: 53 TextureCache(); 54 TextureCache(uint32_t maxByteSize); 55 ~TextureCache(); 56 57 /** 58 * Used as a callback when an entry is removed from the cache. 59 * Do not invoke directly. 60 */ 61 void operator()(SkBitmap*& bitmap, Texture*& texture); 62 63 /** 64 * Returns the texture associated with the specified bitmap. If the texture 65 * cannot be found in the cache, a new texture is generated. 66 */ 67 Texture* get(SkBitmap* bitmap); 68 /** 69 * Removes the texture associated with the specified bitmap. Returns NULL 70 * if the texture cannot be found. Upon remove the texture is freed. 71 */ 72 void remove(SkBitmap* bitmap); 73 /** 74 * Clears the cache. This causes all textures to be deleted. 75 */ 76 void clear(); 77 78 /** 79 * Sets the maximum size of the cache in bytes. 80 */ 81 void setMaxSize(uint32_t maxSize); 82 /** 83 * Returns the maximum size of the cache in bytes. 84 */ 85 uint32_t getMaxSize(); 86 /** 87 * Returns the current size of the cache in bytes. 88 */ 89 uint32_t getSize(); 90 91private: 92 /** 93 * Generates the texture from a bitmap into the specified texture structure. 94 * 95 * @param regenerate If true, the bitmap data is reuploaded into the texture, but 96 * no new texture is generated. 97 */ 98 void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false); 99 100 void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height); 101 void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height, 102 GLenum type, const GLvoid * data); 103 104 void init(); 105 106 GenerationCache<SkBitmap*, Texture*> mCache; 107 108 uint32_t mSize; 109 uint32_t mMaxSize; 110 GLint mMaxTextureSize; 111 112 /** 113 * Used to access mCache and mSize. All methods are accessed from a single 114 * thread except for remove(). 115 */ 116 mutable Mutex mLock; 117}; // class TextureCache 118 119}; // namespace uirenderer 120}; // namespace android 121 122#endif // ANDROID_HWUI_TEXTURE_CACHE_H 123