• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 namespace android {
31 namespace uirenderer {
32 
33 class Texture;
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // Defines
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 // Debug
40 #if DEBUG_TEXTURES
41     #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
42 #else
43     #define TEXTURE_LOGD(...)
44 #endif
45 
46 ///////////////////////////////////////////////////////////////////////////////
47 // Classes
48 ///////////////////////////////////////////////////////////////////////////////
49 
50 class AssetAtlas;
51 
52 /**
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.
56  */
57 class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
58 public:
59     TextureCache();
60     ~TextureCache();
61 
62     /**
63      * Used as a callback when an entry is removed from the cache.
64      * Do not invoke directly.
65      */
66     void operator()(uint32_t&, Texture*& texture) override;
67 
68     /**
69      * Resets all Textures to not be marked as in use
70      */
71     void resetMarkInUse(void* ownerToken);
72 
73     /**
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
76      * marked as in use.
77      */
78     bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
79 
80     /**
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.
84      */
85     bool prefetch(const SkBitmap* bitmap);
86 
87     /**
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.
90      */
get(const SkBitmap * bitmap)91     Texture* get(const SkBitmap* bitmap) {
92         return get(bitmap, AtlasUsageType::Use);
93     }
94 
95     /**
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.
98      */
getAndBypassAtlas(const SkBitmap * bitmap)99     Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
100         return get(bitmap, AtlasUsageType::Bypass);
101     }
102 
103     /**
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.
106      */
107     ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
108     /**
109      * Process deferred removals.
110      */
111     void clearGarbage();
112 
113     /**
114      * Clears the cache. This causes all textures to be deleted.
115      */
116     void clear();
117 
118     /**
119      * Returns the maximum size of the cache in bytes.
120      */
121     uint32_t getMaxSize();
122     /**
123      * Returns the current size of the cache in bytes.
124      */
125     uint32_t getSize();
126 
127     /**
128      * Partially flushes the cache. The amount of memory freed by a flush
129      * is defined by the flush rate.
130      */
131     void flush();
132 
133     void setAssetAtlas(AssetAtlas* assetAtlas);
134 
135 private:
136     enum class AtlasUsageType {
137         Use,
138         Bypass,
139     };
140 
141     bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
142 
143     Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
144     Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
145 
146     LruCache<uint32_t, Texture*> mCache;
147 
148     uint32_t mSize;
149     const uint32_t mMaxSize;
150     GLint mMaxTextureSize;
151 
152     const float mFlushRate;
153 
154     bool mDebugEnabled;
155 
156     std::vector<uint32_t> mGarbage;
157     mutable Mutex mLock;
158 
159     AssetAtlas* mAssetAtlas;
160 }; // class TextureCache
161 
162 }; // namespace uirenderer
163 }; // namespace android
164 
165 #endif // ANDROID_HWUI_TEXTURE_CACHE_H
166