TextureCache.h revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
1ce0537b80087a6225273040a987414b1dd081aa0Romain Guy/* 2ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Copyright (C) 2010 The Android Open Source Project 3ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * 4ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 5ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * you may not use this file except in compliance with the License. 6ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * You may obtain a copy of the License at 7ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * 8ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * http://www.apache.org/licenses/LICENSE-2.0 9ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * 10ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Unless required by applicable law or agreed to in writing, software 11ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * distributed under the License is distributed on an "AS IS" BASIS, 12ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * See the License for the specific language governing permissions and 14ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * limitations under the License. 15ce0537b80087a6225273040a987414b1dd081aa0Romain Guy */ 16ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#ifndef ANDROID_HWUI_TEXTURE_CACHE_H 185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#define ANDROID_HWUI_TEXTURE_CACHE_H 19ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 20ce0537b80087a6225273040a987414b1dd081aa0Romain Guy#include <SkBitmap.h> 21ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 22ce0537b80087a6225273040a987414b1dd081aa0Romain Guy#include "Texture.h" 2321b028a44f3e0bd9b0f0432b8b92c45f661d22a4Romain Guy#include "utils/GenerationCache.h" 24ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 25ce0537b80087a6225273040a987414b1dd081aa0Romain Guynamespace android { 26ce0537b80087a6225273040a987414b1dd081aa0Romain Guynamespace uirenderer { 27ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 28d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase/////////////////////////////////////////////////////////////////////////////// 29d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Defines 30d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase/////////////////////////////////////////////////////////////////////////////// 31d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase 32d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Debug 33d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#define DEBUG_TEXTURES 0 34d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase 35d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Debug 36d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#if DEBUG_TEXTURES 37d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__) 38d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#else 39d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase #define TEXTURE_LOGD(...) 40d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#endif 41d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase 42121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy/** 43121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * A simple LRU texture cache. The cache has a maximum size expressed in bytes. 44121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Any texture added to the cache causing the cache to grow beyond the maximum 45121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * allowed size will also cause the oldest texture to be kicked out. 46121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 47ce0537b80087a6225273040a987414b1dd081aa0Romain Guyclass TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> { 48ce0537b80087a6225273040a987414b1dd081aa0Romain Guypublic: 49fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy TextureCache(); 507d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy TextureCache(uint32_t maxByteSize); 51ce0537b80087a6225273040a987414b1dd081aa0Romain Guy ~TextureCache(); 52ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 53121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 54121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Used as a callback when an entry is removed from the cache. 55121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Do not invoke directly. 56121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 57dda570201ac851dd85af3861f7e575721d3345daRomain Guy void operator()(SkBitmap*& bitmap, Texture*& texture); 58ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 59121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 60121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Returns the texture associated with the specified bitmap. If the texture 61121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * cannot be found in the cache, a new texture is generated. 62121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 63ce0537b80087a6225273040a987414b1dd081aa0Romain Guy Texture* get(SkBitmap* bitmap); 64121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 65121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Removes the texture associated with the specified bitmap. Returns NULL 66121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * if the texture cannot be found. Upon remove the texture is freed. 67121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 68121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy void remove(SkBitmap* bitmap); 69121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 70121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Clears the cache. This causes all textures to be deleted. 71121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 72ce0537b80087a6225273040a987414b1dd081aa0Romain Guy void clear(); 73ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 74121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 75121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Sets the maximum size of the cache in bytes. 76121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 777d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy void setMaxSize(uint32_t maxSize); 78121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 79121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Returns the maximum size of the cache in bytes. 80121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 817d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy uint32_t getMaxSize(); 82121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 83121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Returns the current size of the cache in bytes. 84121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 857d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy uint32_t getSize(); 86121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy 87ce0537b80087a6225273040a987414b1dd081aa0Romain Guyprivate: 88121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy /** 89121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Generates the texture from a bitmap into the specified texture structure. 90121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * 91121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * @param regenerate If true, the bitmap data is reuploaded into the texture, but 92121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * no new texture is generated. 93121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */ 94fe8809471a40cac8acc984adfa51c39e13e83947Romain Guy void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false); 95ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 965b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height); 978c749f87d29e1a363ddf9027c3a51753c612d510Romain Guy void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height, 988c749f87d29e1a363ddf9027c3a51753c612d510Romain Guy GLenum type, const GLvoid * data); 998c749f87d29e1a363ddf9027c3a51753c612d510Romain Guy 100fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy void init(); 101fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy 1026c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy GenerationCache<SkBitmap*, Texture*> mCache; 103121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy 1047d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy uint32_t mSize; 1057d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy uint32_t mMaxSize; 106163935113919a184122b8b3bd672ef08c8df65dcRomain Guy GLint mMaxTextureSize; 1079aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy 108a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy /** 109a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy * Used to access mCache and mSize. All methods are accessed from a single 110a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy * thread except for remove(). 111a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy */ 1129aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy mutable Mutex mLock; 113ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // class TextureCache 114ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 115ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // namespace uirenderer 116ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // namespace android 117ce0537b80087a6225273040a987414b1dd081aa0Romain Guy 1185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_TEXTURE_CACHE_H 119