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 <cutils/compiler.h>
24 #include <utils/LruCache.h>
25 #include <utils/Mutex.h>
30 #include <unordered_map>
36 namespace uirenderer {
40 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
46 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
48 #define TEXTURE_LOGD(...)
51 ///////////////////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////////////////
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.
60 class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
66 * Used as a callback when an entry is removed from the cache.
67 * Do not invoke directly.
69 void operator()(uint32_t&, Texture*& texture) override;
72 * Resets all Textures to not be marked as in use
74 void resetMarkInUse(void* ownerToken);
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
81 bool prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap);
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.
88 bool prefetch(Bitmap* bitmap);
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.
94 Texture* get(Bitmap* bitmap);
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
100 bool destroyTexture(uint32_t pixelRefStableID);
103 * Clears the cache. This causes all textures to be deleted.
108 * Returns the maximum size of the cache in bytes.
110 uint32_t getMaxSize();
112 * Returns the current size of the cache in bytes.
117 * Partially flushes the cache. The amount of memory freed by a flush
118 * is defined by the flush rate.
123 bool canMakeTextureFromBitmap(Bitmap* bitmap);
125 Texture* getCachedTexture(Bitmap* bitmap);
126 Texture* createTexture(Bitmap* bitmap);
128 LruCache<uint32_t, Texture*> mCache;
131 const uint32_t mMaxSize;
132 GLint mMaxTextureSize;
134 const float mFlushRate;
138 std::unordered_map<uint32_t, std::unique_ptr<Texture>> mHardwareTextures;
139 }; // class TextureCache
141 }; // namespace uirenderer
142 }; // namespace android
144 #endif // ANDROID_HWUI_TEXTURE_CACHE_H