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