2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_TEXTURE_CACHE_H
18 #define ANDROID_HWUI_TEXTURE_CACHE_H
22 #include <utils/LruCache.h>
23 #include <utils/Mutex.h>
28 #include <unordered_map>
31 namespace uirenderer {
35 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
41 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
43 #define TEXTURE_LOGD(...)
46 ///////////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////////
53 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
54 * Any texture added to the cache causing the cache to grow beyond the maximum
55 * allowed size will also cause the oldest texture to be kicked out.
57 class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
63 * Used as a callback when an entry is removed from the cache.
64 * Do not invoke directly.
66 void operator()(uint32_t&, Texture*& texture) override;
69 * Resets all Textures to not be marked as in use
71 void resetMarkInUse(void* ownerToken);
74 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
75 * acquired for the bitmap, false otherwise. If a Texture was acquired it is
78 bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
81 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
82 * acquired for the bitmap, false otherwise. Does not mark the Texture
83 * as in use and won't update currently in-use Textures.
85 bool prefetch(const SkBitmap* bitmap);
88 * Returns the texture associated with the specified bitmap from either within the cache, or
89 * the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
91 Texture* get(const SkBitmap* bitmap) {
92 return get(bitmap, AtlasUsageType::Use);
96 * Returns the texture associated with the specified bitmap. If the texture cannot be found in
97 * the cache, a new texture is generated, even if it resides in the AssetAtlas.
99 Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
100 return get(bitmap, AtlasUsageType::Bypass);
104 * Removes the texture associated with the specified pixelRef. This is meant
105 * to be called from threads that are not the EGL context thread.
107 ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
109 * Process deferred removals.
114 * Clears the cache. This causes all textures to be deleted.
119 * Returns the maximum size of the cache in bytes.
121 uint32_t getMaxSize();
123 * Returns the current size of the cache in bytes.
128 * Partially flushes the cache. The amount of memory freed by a flush
129 * is defined by the flush rate.
133 void setAssetAtlas(AssetAtlas* assetAtlas);
136 enum class AtlasUsageType {
141 bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
143 Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
144 Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
146 LruCache<uint32_t, Texture*> mCache;
149 const uint32_t mMaxSize;
150 GLint mMaxTextureSize;
152 const float mFlushRate;
156 std::vector<uint32_t> mGarbage;
159 AssetAtlas* mAssetAtlas;
160 }; // class TextureCache
162 }; // namespace uirenderer
163 }; // namespace android
165 #endif // ANDROID_HWUI_TEXTURE_CACHE_H