Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_TEXTURE_CACHE_H |
| 18 | #define ANDROID_HWUI_TEXTURE_CACHE_H |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 19 | |
| 20 | #include <SkBitmap.h> |
| 21 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 22 | #include <utils/LruCache.h> |
Derek Sollenberger | 029f643 | 2012-03-05 16:48:32 -0500 | [diff] [blame] | 23 | #include <utils/Mutex.h> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 24 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 25 | #include "Debug.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 26 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 27 | #include <vector> |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 28 | #include <unordered_map> |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 29 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 30 | namespace android { |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame^] | 31 | |
| 32 | class Bitmap; |
| 33 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 34 | namespace uirenderer { |
| 35 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 36 | class Texture; |
| 37 | |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | // Defines |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | // Debug |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 43 | #if DEBUG_TEXTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 44 | #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__) |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 45 | #else |
| 46 | #define TEXTURE_LOGD(...) |
| 47 | #endif |
| 48 | |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | // Classes |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 53 | /** |
| 54 | * A simple LRU texture cache. The cache has a maximum size expressed in bytes. |
| 55 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 56 | * allowed size will also cause the oldest texture to be kicked out. |
| 57 | */ |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 58 | class TextureCache : public OnEntryRemoved<uint32_t, Texture*> { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 59 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 60 | TextureCache(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 61 | ~TextureCache(); |
| 62 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Used as a callback when an entry is removed from the cache. |
| 65 | * Do not invoke directly. |
| 66 | */ |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 67 | void operator()(uint32_t&, Texture*& texture) override; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 68 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 69 | /** |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 70 | * Resets all Textures to not be marked as in use |
| 71 | */ |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 72 | void resetMarkInUse(void* ownerToken); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Attempts to precache the SkBitmap. Returns true if a Texture was successfully |
| 76 | * acquired for the bitmap, false otherwise. If a Texture was acquired it is |
| 77 | * marked as in use. |
| 78 | */ |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame^] | 79 | bool prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 82 | * Attempts to precache the SkBitmap. Returns true if a Texture was successfully |
| 83 | * acquired for the bitmap, false otherwise. Does not mark the Texture |
| 84 | * as in use and won't update currently in-use Textures. |
| 85 | */ |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame^] | 86 | bool prefetch(Bitmap* bitmap); |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 89 | * Returns the texture associated with the specified bitmap from within the cache. |
| 90 | * If the texture cannot be found in the cache, a new texture is generated. |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 91 | */ |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame^] | 92 | Texture* get(Bitmap* bitmap); |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 93 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 94 | /** |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 95 | * Removes the texture associated with the specified pixelRef. This is meant |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 96 | * to be called from threads that are not the EGL context thread. |
| 97 | */ |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 98 | ANDROID_API void releaseTexture(uint32_t pixelRefStableID); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Process deferred removals. |
| 101 | */ |
| 102 | void clearGarbage(); |
| 103 | |
| 104 | /** |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 105 | * Clears the cache. This causes all textures to be deleted. |
| 106 | */ |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 107 | void clear(); |
| 108 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 109 | /** |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 110 | * Returns the maximum size of the cache in bytes. |
| 111 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 112 | uint32_t getMaxSize(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Returns the current size of the cache in bytes. |
| 115 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 116 | uint32_t getSize(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 117 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Partially flushes the cache. The amount of memory freed by a flush |
| 120 | * is defined by the flush rate. |
| 121 | */ |
| 122 | void flush(); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 123 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 124 | private: |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame^] | 125 | bool canMakeTextureFromBitmap(Bitmap* bitmap); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 126 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame^] | 127 | Texture* getCachedTexture(Bitmap* bitmap); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 128 | |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 129 | LruCache<uint32_t, Texture*> mCache; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 130 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 131 | uint32_t mSize; |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 132 | const uint32_t mMaxSize; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 133 | GLint mMaxTextureSize; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 134 | |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 135 | const float mFlushRate; |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 136 | |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 137 | bool mDebugEnabled; |
| 138 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 139 | std::vector<uint32_t> mGarbage; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 140 | mutable Mutex mLock; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 141 | }; // class TextureCache |
| 142 | |
| 143 | }; // namespace uirenderer |
| 144 | }; // namespace android |
| 145 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 146 | #endif // ANDROID_HWUI_TEXTURE_CACHE_H |