TextureCache.h revision ec4a4b13eae2241d1613890c1c1c096bed891845
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 25#include "Debug.h" 26 27#include <vector> 28#include <unordered_map> 29 30namespace android { 31 32class Bitmap; 33 34namespace uirenderer { 35 36class Texture; 37 38/////////////////////////////////////////////////////////////////////////////// 39// Defines 40/////////////////////////////////////////////////////////////////////////////// 41 42// Debug 43#if DEBUG_TEXTURES 44 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__) 45#else 46 #define TEXTURE_LOGD(...) 47#endif 48 49/////////////////////////////////////////////////////////////////////////////// 50// Classes 51/////////////////////////////////////////////////////////////////////////////// 52 53/** 54 * A simple LRU texture cache. The cache has a maximum size expressed in bytes. 55 * Any texture added to the cache causing the cache to grow beyond the maximum 56 * allowed size will also cause the oldest texture to be kicked out. 57 */ 58class TextureCache : public OnEntryRemoved<uint32_t, Texture*> { 59public: 60 TextureCache(); 61 ~TextureCache(); 62 63 /** 64 * Used as a callback when an entry is removed from the cache. 65 * Do not invoke directly. 66 */ 67 void operator()(uint32_t&, Texture*& texture) override; 68 69 /** 70 * Resets all Textures to not be marked as in use 71 */ 72 void resetMarkInUse(void* ownerToken); 73 74 /** 75 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully 76 * acquired for the bitmap, false otherwise. If a Texture was acquired it is 77 * marked as in use. 78 */ 79 bool prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap); 80 81 /** 82 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully 83 * acquired for the bitmap, false otherwise. Does not mark the Texture 84 * as in use and won't update currently in-use Textures. 85 */ 86 bool prefetch(Bitmap* bitmap); 87 88 /** 89 * Returns the texture associated with the specified bitmap from within the cache. 90 * If the texture cannot be found in the cache, a new texture is generated. 91 */ 92 Texture* get(Bitmap* bitmap); 93 94 /** 95 * Removes the texture associated with the specified pixelRef. This is meant 96 * to be called from threads that are not the EGL context thread. 97 */ 98 ANDROID_API void releaseTexture(uint32_t pixelRefStableID); 99 /** 100 * Process deferred removals. 101 */ 102 void clearGarbage(); 103 104 /** 105 * Clears the cache. This causes all textures to be deleted. 106 */ 107 void clear(); 108 109 /** 110 * Returns the maximum size of the cache in bytes. 111 */ 112 uint32_t getMaxSize(); 113 /** 114 * Returns the current size of the cache in bytes. 115 */ 116 uint32_t getSize(); 117 118 /** 119 * Partially flushes the cache. The amount of memory freed by a flush 120 * is defined by the flush rate. 121 */ 122 void flush(); 123 124private: 125 bool canMakeTextureFromBitmap(Bitmap* bitmap); 126 127 Texture* getCachedTexture(Bitmap* bitmap); 128 129 LruCache<uint32_t, Texture*> mCache; 130 131 uint32_t mSize; 132 const uint32_t mMaxSize; 133 GLint mMaxTextureSize; 134 135 const float mFlushRate; 136 137 bool mDebugEnabled; 138 139 std::vector<uint32_t> mGarbage; 140 mutable Mutex mLock; 141}; // class TextureCache 142 143}; // namespace uirenderer 144}; // namespace android 145 146#endif // ANDROID_HWUI_TEXTURE_CACHE_H 147