blob: 0a61b6b1a52223c62420ac1d6b7962589b3e47b7 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_TEXTURE_CACHE_H
18#define ANDROID_HWUI_TEXTURE_CACHE_H
Romain Guyce0537b2010-06-29 21:05:21 -070019
20#include <SkBitmap.h>
21
Romain Guy059e12c2012-11-28 17:35:51 -080022#include <utils/LruCache.h>
Derek Sollenberger029f6432012-03-05 16:48:32 -050023#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080024
Romain Guyc15008e2010-11-10 11:59:15 -080025#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070026
John Reck272a6852015-07-29 16:48:58 -070027#include <vector>
John Reck38e0c322015-11-10 12:19:17 -080028#include <unordered_map>
John Reck272a6852015-07-29 16:48:58 -070029
Romain Guyce0537b2010-06-29 21:05:21 -070030namespace android {
31namespace uirenderer {
32
Tom Hudson2dc236b2014-10-15 15:46:42 -040033class Texture;
34
Chet Haased98aa2d2010-10-25 15:47:32 -070035///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
39// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070040#if DEBUG_TEXTURES
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
Chet Haased98aa2d2010-10-25 15:47:32 -070042#else
43 #define TEXTURE_LOGD(...)
44#endif
45
Romain Guy9e108412010-11-09 14:35:20 -080046///////////////////////////////////////////////////////////////////////////////
47// Classes
48///////////////////////////////////////////////////////////////////////////////
49
John Reckebd52612014-12-10 16:47:36 -080050class AssetAtlas;
51
Romain Guy121e22422010-07-01 18:26:52 -070052/**
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 */
Chris Craik117bdbc2015-02-05 10:12:38 -080057class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
Romain Guyce0537b2010-06-29 21:05:21 -070058public:
Romain Guyfb8b7632010-08-23 21:05:08 -070059 TextureCache();
Romain Guyce0537b2010-06-29 21:05:21 -070060 ~TextureCache();
61
Romain Guy121e22422010-07-01 18:26:52 -070062 /**
63 * Used as a callback when an entry is removed from the cache.
64 * Do not invoke directly.
65 */
Chris Craike84a2082014-12-22 14:28:49 -080066 void operator()(uint32_t&, Texture*& texture) override;
Romain Guyce0537b2010-06-29 21:05:21 -070067
Romain Guy121e22422010-07-01 18:26:52 -070068 /**
John Reck860d1552014-04-11 19:15:05 -070069 * Resets all Textures to not be marked as in use
70 */
John Reck00e79c92015-07-21 10:23:59 -070071 void resetMarkInUse(void* ownerToken);
John Reck860d1552014-04-11 19:15:05 -070072
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 */
John Reck00e79c92015-07-21 10:23:59 -070078 bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
John Reck860d1552014-04-11 19:15:05 -070079
80 /**
John Reck43871902016-08-01 14:39:24 -070081 * 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 /**
Chris Craik6ad690e2015-07-17 15:51:36 -070088 * 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.
Romain Guy121e22422010-07-01 18:26:52 -070090 */
Chris Craik6ad690e2015-07-17 15:51:36 -070091 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 */
99 Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
100 return get(bitmap, AtlasUsageType::Bypass);
101 }
John Reck71d08a02014-11-24 15:21:28 -0800102
Romain Guy121e22422010-07-01 18:26:52 -0700103 /**
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500104 * Removes the texture associated with the specified pixelRef. This is meant
Romain Guyfe48f652010-11-11 15:36:56 -0800105 * to be called from threads that are not the EGL context thread.
106 */
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500107 ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
Romain Guyfe48f652010-11-11 15:36:56 -0800108 /**
109 * Process deferred removals.
110 */
111 void clearGarbage();
112
113 /**
Romain Guy121e22422010-07-01 18:26:52 -0700114 * Clears the cache. This causes all textures to be deleted.
115 */
Romain Guyce0537b2010-06-29 21:05:21 -0700116 void clear();
117
Romain Guy121e22422010-07-01 18:26:52 -0700118 /**
Romain Guy121e22422010-07-01 18:26:52 -0700119 * Returns the maximum size of the cache in bytes.
120 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700121 uint32_t getMaxSize();
Romain Guy121e22422010-07-01 18:26:52 -0700122 /**
123 * Returns the current size of the cache in bytes.
124 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700125 uint32_t getSize();
Romain Guy121e22422010-07-01 18:26:52 -0700126
Romain Guyeca0ca22011-11-04 15:12:29 -0700127 /**
128 * Partially flushes the cache. The amount of memory freed by a flush
129 * is defined by the flush rate.
130 */
131 void flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700132
John Reckebd52612014-12-10 16:47:36 -0800133 void setAssetAtlas(AssetAtlas* assetAtlas);
134
Romain Guyce0537b2010-06-29 21:05:21 -0700135private:
Chris Craik6ad690e2015-07-17 15:51:36 -0700136 enum class AtlasUsageType {
137 Use,
138 Bypass,
139 };
John Reck860d1552014-04-11 19:15:05 -0700140
141 bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
142
Chris Craik6ad690e2015-07-17 15:51:36 -0700143 Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
144 Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
John Reck860d1552014-04-11 19:15:05 -0700145
John Reck71d08a02014-11-24 15:21:28 -0800146 LruCache<uint32_t, Texture*> mCache;
Romain Guy121e22422010-07-01 18:26:52 -0700147
Romain Guy7d139ba2010-07-02 11:20:34 -0700148 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800149 const uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700150 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700151
Chris Craik48a8f432016-02-05 15:59:29 -0800152 const float mFlushRate;
Romain Guyeca0ca22011-11-04 15:12:29 -0700153
Romain Guye190aa62010-11-10 19:01:29 -0800154 bool mDebugEnabled;
155
John Reck272a6852015-07-29 16:48:58 -0700156 std::vector<uint32_t> mGarbage;
Romain Guy9aaa8262010-09-08 15:15:43 -0700157 mutable Mutex mLock;
John Reckebd52612014-12-10 16:47:36 -0800158
159 AssetAtlas* mAssetAtlas;
Romain Guyce0537b2010-06-29 21:05:21 -0700160}; // class TextureCache
161
162}; // namespace uirenderer
163}; // namespace android
164
Romain Guy5b3b3522010-10-27 18:57:51 -0700165#endif // ANDROID_HWUI_TEXTURE_CACHE_H