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