blob: 7a7ee5aeb554c31ac860e9a031625bcbabb3b6bf [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#include <utils/Vector.h>
25
Romain Guyc15008e2010-11-10 11:59:15 -080026#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070027
28namespace android {
29namespace uirenderer {
30
Tom Hudson2dc236b2014-10-15 15:46:42 -040031class Texture;
32
Chet Haased98aa2d2010-10-25 15:47:32 -070033///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070038#if DEBUG_TEXTURES
Steve Block5baa3a62011-12-20 16:23:08 +000039 #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
Chet Haased98aa2d2010-10-25 15:47:32 -070040#else
41 #define TEXTURE_LOGD(...)
42#endif
43
Romain Guy9e108412010-11-09 14:35:20 -080044///////////////////////////////////////////////////////////////////////////////
45// Classes
46///////////////////////////////////////////////////////////////////////////////
47
John Reckebd52612014-12-10 16:47:36 -080048class AssetAtlas;
49
Romain Guy121e22422010-07-01 18:26:52 -070050/**
51 * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
52 * Any texture added to the cache causing the cache to grow beyond the maximum
53 * allowed size will also cause the oldest texture to be kicked out.
54 */
Chris Craik117bdbc2015-02-05 10:12:38 -080055class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
Romain Guyce0537b2010-06-29 21:05:21 -070056public:
Romain Guyfb8b7632010-08-23 21:05:08 -070057 TextureCache();
Romain Guyce0537b2010-06-29 21:05:21 -070058 ~TextureCache();
59
Romain Guy121e22422010-07-01 18:26:52 -070060 /**
61 * Used as a callback when an entry is removed from the cache.
62 * Do not invoke directly.
63 */
Chris Craike84a2082014-12-22 14:28:49 -080064 void operator()(uint32_t&, Texture*& texture) override;
Romain Guyce0537b2010-06-29 21:05:21 -070065
Romain Guy121e22422010-07-01 18:26:52 -070066 /**
John Reck860d1552014-04-11 19:15:05 -070067 * Resets all Textures to not be marked as in use
68 */
John Reck00e79c92015-07-21 10:23:59 -070069 void resetMarkInUse(void* ownerToken);
John Reck860d1552014-04-11 19:15:05 -070070
71 /**
72 * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
73 * acquired for the bitmap, false otherwise. If a Texture was acquired it is
74 * marked as in use.
75 */
John Reck00e79c92015-07-21 10:23:59 -070076 bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
John Reck860d1552014-04-11 19:15:05 -070077
78 /**
Chris Craik6ad690e2015-07-17 15:51:36 -070079 * Returns the texture associated with the specified bitmap from either within the cache, or
80 * the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
Romain Guy121e22422010-07-01 18:26:52 -070081 */
Chris Craik6ad690e2015-07-17 15:51:36 -070082 Texture* get(const SkBitmap* bitmap) {
83 return get(bitmap, AtlasUsageType::Use);
84 }
85
86 /**
87 * Returns the texture associated with the specified bitmap. If the texture cannot be found in
88 * the cache, a new texture is generated, even if it resides in the AssetAtlas.
89 */
90 Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
91 return get(bitmap, AtlasUsageType::Bypass);
92 }
John Reck71d08a02014-11-24 15:21:28 -080093
Romain Guy121e22422010-07-01 18:26:52 -070094 /**
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050095 * Removes the texture associated with the specified pixelRef. This is meant
Romain Guyfe48f652010-11-11 15:36:56 -080096 * to be called from threads that are not the EGL context thread.
97 */
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050098 ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
Romain Guyfe48f652010-11-11 15:36:56 -080099 /**
100 * Process deferred removals.
101 */
102 void clearGarbage();
103
104 /**
Romain Guy121e22422010-07-01 18:26:52 -0700105 * Clears the cache. This causes all textures to be deleted.
106 */
Romain Guyce0537b2010-06-29 21:05:21 -0700107 void clear();
108
Romain Guy121e22422010-07-01 18:26:52 -0700109 /**
110 * Sets the maximum size of the cache in bytes.
111 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700112 void setMaxSize(uint32_t maxSize);
Romain Guy121e22422010-07-01 18:26:52 -0700113 /**
114 * Returns the maximum size of the cache in bytes.
115 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700116 uint32_t getMaxSize();
Romain Guy121e22422010-07-01 18:26:52 -0700117 /**
118 * Returns the current size of the cache in bytes.
119 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700120 uint32_t getSize();
Romain Guy121e22422010-07-01 18:26:52 -0700121
Romain Guyeca0ca22011-11-04 15:12:29 -0700122 /**
123 * Partially flushes the cache. The amount of memory freed by a flush
124 * is defined by the flush rate.
125 */
126 void flush();
127 /**
128 * Indicates the percentage of the cache to retain when a
129 * memory trim is requested (see Caches::flush).
130 */
131 void setFlushRate(float flushRate);
132
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
Romain Guy121e22422010-07-01 18:26:52 -0700146 /**
147 * Generates the texture from a bitmap into the specified texture structure.
148 *
149 * @param regenerate If true, the bitmap data is reuploaded into the texture, but
150 * no new texture is generated.
151 */
Chris Craikd218a922014-01-02 17:13:34 -0800152 void generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate = false);
Romain Guyce0537b2010-06-29 21:05:21 -0700153
Chris Craikd218a922014-01-02 17:13:34 -0800154 void uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height);
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800155 void uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
Romain Guy318ae7b2013-09-24 18:44:54 -0700156 GLsizei width, GLsizei height, GLenum type, const GLvoid * data);
Romain Guy8c749f82010-09-22 14:13:32 -0700157
John Reck71d08a02014-11-24 15:21:28 -0800158 LruCache<uint32_t, Texture*> mCache;
Romain Guy121e22422010-07-01 18:26:52 -0700159
Romain Guy7d139ba2010-07-02 11:20:34 -0700160 uint32_t mSize;
161 uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700162 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700163
Romain Guyeca0ca22011-11-04 15:12:29 -0700164 float mFlushRate;
165
Romain Guye190aa62010-11-10 19:01:29 -0800166 bool mDebugEnabled;
167
John Reck71d08a02014-11-24 15:21:28 -0800168 Vector<uint32_t> mGarbage;
Romain Guy9aaa8262010-09-08 15:15:43 -0700169 mutable Mutex mLock;
John Reckebd52612014-12-10 16:47:36 -0800170
171 AssetAtlas* mAssetAtlas;
Romain Guyce0537b2010-06-29 21:05:21 -0700172}; // class TextureCache
173
174}; // namespace uirenderer
175}; // namespace android
176
Romain Guy5b3b3522010-10-27 18:57:51 -0700177#endif // ANDROID_HWUI_TEXTURE_CACHE_H