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 | #include <utils/Vector.h> |
| 25 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 26 | #include "Debug.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 31 | class Texture; |
| 32 | |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | // Defines |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | // Debug |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 38 | #if DEBUG_TEXTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 39 | #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__) |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 40 | #else |
| 41 | #define TEXTURE_LOGD(...) |
| 42 | #endif |
| 43 | |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | // Classes |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 48 | class AssetAtlas; |
| 49 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 50 | /** |
| 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 | */ |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 55 | class TextureCache: public OnEntryRemoved<uint32_t, Texture*> { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 56 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 57 | TextureCache(); |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 58 | TextureCache(uint32_t maxByteSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 59 | ~TextureCache(); |
| 60 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Used as a callback when an entry is removed from the cache. |
| 63 | * Do not invoke directly. |
| 64 | */ |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 65 | void operator()(uint32_t&, Texture*& texture) override; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 66 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 67 | /** |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 68 | * Resets all Textures to not be marked as in use |
| 69 | */ |
| 70 | void resetMarkInUse(); |
| 71 | |
| 72 | /** |
| 73 | * Attempts to precache the SkBitmap. Returns true if a Texture was successfully |
| 74 | * acquired for the bitmap, false otherwise. If a Texture was acquired it is |
| 75 | * marked as in use. |
| 76 | */ |
| 77 | bool prefetchAndMarkInUse(const SkBitmap* bitmap); |
| 78 | |
| 79 | /** |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 80 | * Returns the texture associated with the specified bitmap. If the texture |
| 81 | * cannot be found in the cache, a new texture is generated. |
| 82 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 83 | Texture* get(const SkBitmap* bitmap); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 84 | /** |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 85 | * Returns the texture associated with the specified bitmap. The generated |
| 86 | * texture is not kept in the cache. The caller must destroy the texture. |
| 87 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 88 | Texture* getTransient(const SkBitmap* bitmap); |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 89 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 90 | /** |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 91 | * Removes the texture associated with the specified bitmap. This is meant |
| 92 | * to be called from threads that are not the EGL context thread. |
| 93 | */ |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 94 | void releaseTexture(const SkBitmap* bitmap); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Process deferred removals. |
| 97 | */ |
| 98 | void clearGarbage(); |
| 99 | |
| 100 | /** |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 101 | * Clears the cache. This causes all textures to be deleted. |
| 102 | */ |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 103 | void clear(); |
| 104 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Sets the maximum size of the cache in bytes. |
| 107 | */ |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 108 | void setMaxSize(uint32_t maxSize); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 109 | /** |
| 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 | 121e224 | 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 | 121e224 | 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(); |
| 123 | /** |
| 124 | * Indicates the percentage of the cache to retain when a |
| 125 | * memory trim is requested (see Caches::flush). |
| 126 | */ |
| 127 | void setFlushRate(float flushRate); |
| 128 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 129 | void setAssetAtlas(AssetAtlas* assetAtlas); |
| 130 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 131 | private: |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 132 | |
| 133 | bool canMakeTextureFromBitmap(const SkBitmap* bitmap); |
| 134 | |
| 135 | Texture* getCachedTexture(const SkBitmap* bitmap); |
| 136 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Generates the texture from a bitmap into the specified texture structure. |
| 139 | * |
| 140 | * @param regenerate If true, the bitmap data is reuploaded into the texture, but |
| 141 | * no new texture is generated. |
| 142 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 143 | void generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate = false); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 144 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 145 | void uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height); |
Lu, Shenghua | c5e0a29 | 2013-11-27 20:16:43 +0800 | [diff] [blame] | 146 | void uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp, |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 147 | GLsizei width, GLsizei height, GLenum type, const GLvoid * data); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 148 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 149 | void init(); |
| 150 | |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 151 | LruCache<uint32_t, Texture*> mCache; |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 152 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 153 | uint32_t mSize; |
| 154 | uint32_t mMaxSize; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 155 | GLint mMaxTextureSize; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 156 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 157 | float mFlushRate; |
| 158 | |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 159 | bool mDebugEnabled; |
| 160 | |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 161 | Vector<uint32_t> mGarbage; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 162 | mutable Mutex mLock; |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 163 | |
| 164 | AssetAtlas* mAssetAtlas; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 165 | }; // class TextureCache |
| 166 | |
| 167 | }; // namespace uirenderer |
| 168 | }; // namespace android |
| 169 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 170 | #endif // ANDROID_HWUI_TEXTURE_CACHE_H |