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 | |
| 17 | #include <GLES2/gl2.h> |
| 18 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 19 | #include <utils/Mutex.h> |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 20 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 21 | #include "Caches.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include "DeviceInfo.h" |
| 23 | #include "Properties.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 24 | #include "Texture.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 25 | #include "TextureCache.h" |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 26 | #include "hwui/Bitmap.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 27 | #include "utils/TraceUtils.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | // Constructors/destructor |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 36 | TextureCache::TextureCache() |
| 37 | : mCache(LruCache<uint32_t, Texture*>::kUnlimitedCapacity) |
| 38 | , mSize(0) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | , mMaxSize(DeviceInfo::multiplyByResolution(4 * 6)) // 6 screen-sized RGBA_8888 bitmaps |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 40 | , mFlushRate(.4f) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 41 | mCache.setOnEntryRemovedListener(this); |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 42 | mMaxTextureSize = DeviceInfo::get()->maxTextureSize(); |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 43 | mDebugEnabled = Properties::debugLevel & kDebugCaches; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 46 | TextureCache::~TextureCache() { |
sergeyv | 83809fe | 2017-02-01 10:27:33 -0800 | [diff] [blame] | 47 | this->clear(); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | // Size management |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 54 | uint32_t TextureCache::getSize() { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 55 | return mSize; |
| 56 | } |
| 57 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 58 | uint32_t TextureCache::getMaxSize() { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 59 | return mMaxSize; |
| 60 | } |
| 61 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | // Callbacks |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 66 | void TextureCache::operator()(uint32_t&, Texture*& texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 67 | // This will be called already locked |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 68 | if (texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 69 | mSize -= texture->bitmapSize; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 70 | TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", texture->id, |
| 71 | texture->bitmapSize, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 72 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 73 | ALOGD("Texture deleted, size = %d", texture->bitmapSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 74 | } |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 75 | texture->deleteTexture(); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 76 | delete texture; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | // Caching |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 84 | void TextureCache::resetMarkInUse(void* ownerToken) { |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 85 | LruCache<uint32_t, Texture*>::Iterator iter(mCache); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 86 | while (iter.next()) { |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 87 | if (iter.value()->isInUse == ownerToken) { |
| 88 | iter.value()->isInUse = nullptr; |
| 89 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 93 | bool TextureCache::canMakeTextureFromBitmap(Bitmap* bitmap) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 94 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)", bitmap->width(), |
| 96 | bitmap->height(), mMaxTextureSize, mMaxTextureSize); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
sergeyv | 00783be | 2017-01-30 14:24:48 -0800 | [diff] [blame] | 102 | Texture* TextureCache::createTexture(Bitmap* bitmap) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 103 | Texture* texture = new Texture(Caches::getInstance()); |
| 104 | texture->bitmapSize = bitmap->rowBytes() * bitmap->height(); |
| 105 | texture->generation = bitmap->getGenerationID(); |
| 106 | texture->upload(*bitmap); |
| 107 | return texture; |
sergeyv | 00783be | 2017-01-30 14:24:48 -0800 | [diff] [blame] | 108 | } |
| 109 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 110 | // Returns a prepared Texture* that either is already in the cache or can fit |
| 111 | // in the cache (and is thus added to the cache) |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 112 | Texture* TextureCache::getCachedTexture(Bitmap* bitmap) { |
sergeyv | 00783be | 2017-01-30 14:24:48 -0800 | [diff] [blame] | 113 | if (bitmap->isHardware()) { |
| 114 | auto textureIterator = mHardwareTextures.find(bitmap->getStableID()); |
| 115 | if (textureIterator == mHardwareTextures.end()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | Texture* texture = createTexture(bitmap); |
| 117 | mHardwareTextures.insert( |
| 118 | std::make_pair(bitmap->getStableID(), std::unique_ptr<Texture>(texture))); |
sergeyv | 00783be | 2017-01-30 14:24:48 -0800 | [diff] [blame] | 119 | if (mDebugEnabled) { |
| 120 | ALOGD("Texture created for hw bitmap size = %d", texture->bitmapSize); |
| 121 | } |
| 122 | return texture; |
| 123 | } |
| 124 | return textureIterator->second.get(); |
| 125 | } |
| 126 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 127 | Texture* texture = mCache.get(bitmap->getStableID()); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 128 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 129 | if (!texture) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 130 | if (!canMakeTextureFromBitmap(bitmap)) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 131 | return nullptr; |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 134 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 135 | bool canCache = size < mMaxSize; |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 136 | // Don't even try to cache a bitmap that's bigger than the cache |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 137 | while (canCache && mSize + size > mMaxSize) { |
| 138 | Texture* oldest = mCache.peekOldestValue(); |
| 139 | if (oldest && !oldest->isInUse) { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 140 | mCache.removeOldest(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 141 | } else { |
| 142 | canCache = false; |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 146 | if (canCache) { |
sergeyv | 00783be | 2017-01-30 14:24:48 -0800 | [diff] [blame] | 147 | texture = createTexture(bitmap); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 148 | mSize += size; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 149 | TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 150 | bitmap, texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 151 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 152 | ALOGD("Texture created, size = %d", size); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 153 | } |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 154 | mCache.put(bitmap->getStableID(), texture); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 155 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 156 | } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) { |
| 157 | // Texture was in the cache but is dirty, re-upload |
| 158 | // TODO: Re-adjust the cache size if the bitmap's dimensions have changed |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 159 | texture->upload(*bitmap); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 160 | texture->generation = bitmap->getGenerationID(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 161 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 162 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 163 | return texture; |
| 164 | } |
| 165 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 166 | bool TextureCache::prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 167 | Texture* texture = getCachedTexture(bitmap); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 168 | if (texture) { |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 169 | texture->isInUse = ownerToken; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 170 | } |
| 171 | return texture; |
| 172 | } |
| 173 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 174 | bool TextureCache::prefetch(Bitmap* bitmap) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 175 | return getCachedTexture(bitmap); |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 176 | } |
| 177 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 178 | Texture* TextureCache::get(Bitmap* bitmap) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 179 | Texture* texture = getCachedTexture(bitmap); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 180 | |
| 181 | if (!texture) { |
| 182 | if (!canMakeTextureFromBitmap(bitmap)) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 183 | return nullptr; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 184 | } |
sergeyv | 00783be | 2017-01-30 14:24:48 -0800 | [diff] [blame] | 185 | texture = createTexture(bitmap); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 186 | texture->cleanup = true; |
| 187 | } |
| 188 | |
| 189 | return texture; |
| 190 | } |
| 191 | |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 192 | bool TextureCache::destroyTexture(uint32_t pixelRefStableID) { |
| 193 | auto hardwareIter = mHardwareTextures.find(pixelRefStableID); |
| 194 | if (hardwareIter != mHardwareTextures.end()) { |
| 195 | hardwareIter->second->deleteTexture(); |
| 196 | mHardwareTextures.erase(hardwareIter); |
| 197 | return true; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 198 | } |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 199 | return mCache.remove(pixelRefStableID); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void TextureCache::clear() { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 203 | mCache.clear(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 204 | for (auto& iter : mHardwareTextures) { |
sergeyv | 83809fe | 2017-02-01 10:27:33 -0800 | [diff] [blame] | 205 | iter.second->deleteTexture(); |
| 206 | } |
| 207 | mHardwareTextures.clear(); |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 208 | TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 211 | void TextureCache::flush() { |
| 212 | if (mFlushRate >= 1.0f || mCache.size() == 0) return; |
| 213 | if (mFlushRate <= 0.0f) { |
| 214 | clear(); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | uint32_t targetSize = uint32_t(mSize * mFlushRate); |
| 219 | TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize); |
| 220 | |
| 221 | while (mSize > targetSize) { |
| 222 | mCache.removeOldest(); |
| 223 | } |
| 224 | } |
| 225 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 226 | }; // namespace uirenderer |
| 227 | }; // namespace android |