blob: 19e7bea996696cff907a9e01fa9f44ecfd35dc3c [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 Guyefb4b062017-02-27 11:00:04 -080022#include <cutils/compiler.h>
23
Romain Guy059e12c2012-11-28 17:35:51 -080024#include <utils/LruCache.h>
Derek Sollenberger029f6432012-03-05 16:48:32 -050025#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080026
Romain Guyc15008e2010-11-10 11:59:15 -080027#include "Debug.h"
Romain Guyce0537b2010-06-29 21:05:21 -070028
John Reck38e0c322015-11-10 12:19:17 -080029#include <unordered_map>
John Reck1bcacfd2017-11-03 10:12:19 -070030#include <vector>
John Reck272a6852015-07-29 16:48:58 -070031
Romain Guyce0537b2010-06-29 21:05:21 -070032namespace android {
sergeyvec4a4b12016-10-20 18:39:04 -070033
34class Bitmap;
35
Romain Guyce0537b2010-06-29 21:05:21 -070036namespace uirenderer {
37
Tom Hudson2dc236b2014-10-15 15:46:42 -040038class Texture;
39
Chet Haased98aa2d2010-10-25 15:47:32 -070040///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
44// Debug
Chet Haased98aa2d2010-10-25 15:47:32 -070045#if DEBUG_TEXTURES
John Reck1bcacfd2017-11-03 10:12:19 -070046#define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
Chet Haased98aa2d2010-10-25 15:47:32 -070047#else
John Reck1bcacfd2017-11-03 10:12:19 -070048#define TEXTURE_LOGD(...)
Chet Haased98aa2d2010-10-25 15:47:32 -070049#endif
50
Romain Guy9e108412010-11-09 14:35:20 -080051///////////////////////////////////////////////////////////////////////////////
52// Classes
53///////////////////////////////////////////////////////////////////////////////
54
Romain Guy121e2242010-07-01 18:26:52 -070055/**
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.
59 */
Chris Craik117bdbc2015-02-05 10:12:38 -080060class TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
Romain Guyce0537b2010-06-29 21:05:21 -070061public:
Romain Guyfb8b7632010-08-23 21:05:08 -070062 TextureCache();
Romain Guyce0537b2010-06-29 21:05:21 -070063 ~TextureCache();
64
Romain Guy121e2242010-07-01 18:26:52 -070065 /**
66 * Used as a callback when an entry is removed from the cache.
67 * Do not invoke directly.
68 */
Chris Craike84a2082014-12-22 14:28:49 -080069 void operator()(uint32_t&, Texture*& texture) override;
Romain Guyce0537b2010-06-29 21:05:21 -070070
Romain Guy121e2242010-07-01 18:26:52 -070071 /**
John Reck860d1552014-04-11 19:15:05 -070072 * Resets all Textures to not be marked as in use
73 */
John Reck00e79c92015-07-21 10:23:59 -070074 void resetMarkInUse(void* ownerToken);
John Reck860d1552014-04-11 19:15:05 -070075
76 /**
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
79 * marked as in use.
80 */
sergeyvec4a4b12016-10-20 18:39:04 -070081 bool prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap);
John Reck860d1552014-04-11 19:15:05 -070082
83 /**
John Reck43871902016-08-01 14:39:24 -070084 * 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.
87 */
sergeyvec4a4b12016-10-20 18:39:04 -070088 bool prefetch(Bitmap* bitmap);
John Reck43871902016-08-01 14:39:24 -070089
90 /**
Romain Guy253f2c22016-09-28 17:34:42 -070091 * 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.
Romain Guy121e2242010-07-01 18:26:52 -070093 */
sergeyvec4a4b12016-10-20 18:39:04 -070094 Texture* get(Bitmap* bitmap);
John Reck71d08a02014-11-24 15:21:28 -080095
Romain Guy121e2242010-07-01 18:26:52 -070096 /**
John Reck9a814872017-05-22 15:04:21 -070097 * 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
Romain Guyfe48f652010-11-11 15:36:56 -080099 */
John Reck9a814872017-05-22 15:04:21 -0700100 bool destroyTexture(uint32_t pixelRefStableID);
Romain Guyfe48f652010-11-11 15:36:56 -0800101
102 /**
Romain Guy121e2242010-07-01 18:26:52 -0700103 * Clears the cache. This causes all textures to be deleted.
104 */
Romain Guyce0537b2010-06-29 21:05:21 -0700105 void clear();
106
Romain Guy121e2242010-07-01 18:26:52 -0700107 /**
Romain Guy121e2242010-07-01 18:26:52 -0700108 * Returns the maximum size of the cache in bytes.
109 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700110 uint32_t getMaxSize();
Romain Guy121e2242010-07-01 18:26:52 -0700111 /**
112 * Returns the current size of the cache in bytes.
113 */
Romain Guy7d139ba2010-07-02 11:20:34 -0700114 uint32_t getSize();
Romain Guy121e2242010-07-01 18:26:52 -0700115
Romain Guyeca0ca22011-11-04 15:12:29 -0700116 /**
117 * Partially flushes the cache. The amount of memory freed by a flush
118 * is defined by the flush rate.
119 */
120 void flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700121
Romain Guyce0537b2010-06-29 21:05:21 -0700122private:
sergeyvec4a4b12016-10-20 18:39:04 -0700123 bool canMakeTextureFromBitmap(Bitmap* bitmap);
John Reck860d1552014-04-11 19:15:05 -0700124
sergeyvec4a4b12016-10-20 18:39:04 -0700125 Texture* getCachedTexture(Bitmap* bitmap);
sergeyv00783be2017-01-30 14:24:48 -0800126 Texture* createTexture(Bitmap* bitmap);
John Reck860d1552014-04-11 19:15:05 -0700127
John Reck71d08a02014-11-24 15:21:28 -0800128 LruCache<uint32_t, Texture*> mCache;
Romain Guy121e2242010-07-01 18:26:52 -0700129
Romain Guy7d139ba2010-07-02 11:20:34 -0700130 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800131 const uint32_t mMaxSize;
Romain Guy16393512010-08-08 00:14:31 -0700132 GLint mMaxTextureSize;
Romain Guy9aaa8262010-09-08 15:15:43 -0700133
Chris Craik48a8f432016-02-05 15:59:29 -0800134 const float mFlushRate;
Romain Guyeca0ca22011-11-04 15:12:29 -0700135
Romain Guye190aa62010-11-10 19:01:29 -0800136 bool mDebugEnabled;
137
sergeyv00783be2017-01-30 14:24:48 -0800138 std::unordered_map<uint32_t, std::unique_ptr<Texture>> mHardwareTextures;
John Reck1bcacfd2017-11-03 10:12:19 -0700139}; // class TextureCache
Romain Guyce0537b2010-06-29 21:05:21 -0700140
John Reck1bcacfd2017-11-03 10:12:19 -0700141}; // namespace uirenderer
142}; // namespace android
Romain Guyce0537b2010-06-29 21:05:21 -0700143
John Reck1bcacfd2017-11-03 10:12:19 -0700144#endif // ANDROID_HWUI_TEXTURE_CACHE_H