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 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 21 | #include "AssetAtlas.h" |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 22 | #include "Caches.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 23 | #include "Texture.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 24 | #include "TextureCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 25 | #include "Properties.h" |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 26 | #include "utils/TraceUtils.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // Constructors/destructor |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 35 | TextureCache::TextureCache() |
| 36 | : mCache(LruCache<uint32_t, Texture*>::kUnlimitedCapacity) |
| 37 | , mSize(0) |
| 38 | , mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) |
| 39 | , mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) |
| 40 | , mAssetAtlas(nullptr) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 41 | char property[PROPERTY_VALUE_MAX]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 42 | if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, nullptr) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 43 | INIT_LOGD(" Setting texture cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 44 | setMaxSize(MB(atof(property))); |
| 45 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 46 | INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 49 | if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, nullptr) > 0) { |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 50 | float flushRate = atof(property); |
| 51 | INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f); |
| 52 | setFlushRate(flushRate); |
| 53 | } else { |
| 54 | INIT_LOGD(" Using default texture cache flush rate of %.2f%%", |
| 55 | DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f); |
| 56 | } |
| 57 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 58 | mCache.setOnEntryRemovedListener(this); |
| 59 | |
| 60 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 61 | INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 62 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 63 | mDebugEnabled = Properties::debugLevel & kDebugCaches; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 66 | TextureCache::~TextureCache() { |
| 67 | mCache.clear(); |
| 68 | } |
| 69 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | // Size management |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 74 | uint32_t TextureCache::getSize() { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 75 | return mSize; |
| 76 | } |
| 77 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 78 | uint32_t TextureCache::getMaxSize() { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 79 | return mMaxSize; |
| 80 | } |
| 81 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 82 | void TextureCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 83 | mMaxSize = maxSize; |
| 84 | while (mSize > mMaxSize) { |
| 85 | mCache.removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 89 | void TextureCache::setFlushRate(float flushRate) { |
Chris Craik | df72b63 | 2015-06-30 17:56:13 -0700 | [diff] [blame] | 90 | mFlushRate = std::max(0.0f, std::min(1.0f, flushRate)); |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | // Callbacks |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 97 | void TextureCache::operator()(uint32_t&, Texture*& texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 98 | // This will be called already locked |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 99 | if (texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 100 | mSize -= texture->bitmapSize; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 101 | TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", |
| 102 | texture->id, texture->bitmapSize, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 103 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 104 | ALOGD("Texture deleted, size = %d", texture->bitmapSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 105 | } |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 106 | texture->deleteTexture(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 107 | delete texture; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | // Caching |
| 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 115 | void TextureCache::setAssetAtlas(AssetAtlas* assetAtlas) { |
| 116 | mAssetAtlas = assetAtlas; |
| 117 | } |
| 118 | |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 119 | void TextureCache::resetMarkInUse(void* ownerToken) { |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 120 | LruCache<uint32_t, Texture*>::Iterator iter(mCache); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 121 | while (iter.next()) { |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 122 | if (iter.value()->isInUse == ownerToken) { |
| 123 | iter.value()->isInUse = nullptr; |
| 124 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | bool TextureCache::canMakeTextureFromBitmap(const SkBitmap* bitmap) { |
| 129 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
| 130 | ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)", |
| 131 | bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize); |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | // Returns a prepared Texture* that either is already in the cache or can fit |
| 138 | // in the cache (and is thus added to the cache) |
Chris Craik | 6ad690e | 2015-07-17 15:51:36 -0700 | [diff] [blame] | 139 | Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) { |
| 140 | if (CC_LIKELY(mAssetAtlas != nullptr) && atlasUsageType == AtlasUsageType::Use) { |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 141 | AssetAtlas::Entry* entry = mAssetAtlas->getEntry(bitmap->pixelRef()); |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 142 | if (CC_UNLIKELY(entry)) { |
| 143 | return entry->texture; |
| 144 | } |
| 145 | } |
| 146 | |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 147 | Texture* texture = mCache.get(bitmap->pixelRef()->getStableID()); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 148 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 149 | if (!texture) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 150 | if (!canMakeTextureFromBitmap(bitmap)) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 151 | return nullptr; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 154 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 155 | bool canCache = size < mMaxSize; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 156 | // 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] | 157 | while (canCache && mSize + size > mMaxSize) { |
| 158 | Texture* oldest = mCache.peekOldestValue(); |
| 159 | if (oldest && !oldest->isInUse) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 160 | mCache.removeOldest(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 161 | } else { |
| 162 | canCache = false; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 166 | if (canCache) { |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 167 | texture = new Texture(Caches::getInstance()); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 168 | texture->bitmapSize = size; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 169 | texture->generation = bitmap->getGenerationID(); |
| 170 | texture->upload(*bitmap); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 171 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 172 | mSize += size; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 173 | TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", |
| 174 | bitmap, texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 175 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 176 | ALOGD("Texture created, size = %d", size); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 177 | } |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 178 | mCache.put(bitmap->pixelRef()->getStableID(), texture); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 179 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 180 | } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) { |
| 181 | // Texture was in the cache but is dirty, re-upload |
| 182 | // TODO: Re-adjust the cache size if the bitmap's dimensions have changed |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 183 | texture->upload(*bitmap); |
| 184 | texture->generation = bitmap->getGenerationID(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 185 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 186 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 187 | return texture; |
| 188 | } |
| 189 | |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 190 | bool TextureCache::prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap) { |
Chris Craik | 6ad690e | 2015-07-17 15:51:36 -0700 | [diff] [blame] | 191 | Texture* texture = getCachedTexture(bitmap, AtlasUsageType::Use); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 192 | if (texture) { |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 193 | texture->isInUse = ownerToken; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 194 | } |
| 195 | return texture; |
| 196 | } |
| 197 | |
Chris Craik | 6ad690e | 2015-07-17 15:51:36 -0700 | [diff] [blame] | 198 | Texture* TextureCache::get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) { |
| 199 | Texture* texture = getCachedTexture(bitmap, atlasUsageType); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 200 | |
| 201 | if (!texture) { |
| 202 | if (!canMakeTextureFromBitmap(bitmap)) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 203 | return nullptr; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Chris Craik | 8e93a7c | 2015-02-23 13:07:57 -0800 | [diff] [blame] | 207 | texture = new Texture(Caches::getInstance()); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 208 | texture->bitmapSize = size; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 209 | texture->upload(*bitmap); |
| 210 | texture->generation = bitmap->getGenerationID(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 211 | texture->cleanup = true; |
| 212 | } |
| 213 | |
| 214 | return texture; |
| 215 | } |
| 216 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 217 | void TextureCache::releaseTexture(uint32_t pixelRefStableID) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 218 | Mutex::Autolock _l(mLock); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 219 | mGarbage.push_back(pixelRefStableID); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void TextureCache::clearGarbage() { |
| 223 | Mutex::Autolock _l(mLock); |
| 224 | size_t count = mGarbage.size(); |
| 225 | for (size_t i = 0; i < count; i++) { |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 226 | uint32_t pixelRefId = mGarbage[i]; |
John Reck | 71d08a0 | 2014-11-24 15:21:28 -0800 | [diff] [blame] | 227 | mCache.remove(pixelRefId); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 228 | } |
| 229 | mGarbage.clear(); |
| 230 | } |
| 231 | |
| 232 | void TextureCache::clear() { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 233 | mCache.clear(); |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 234 | TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 237 | void TextureCache::flush() { |
| 238 | if (mFlushRate >= 1.0f || mCache.size() == 0) return; |
| 239 | if (mFlushRate <= 0.0f) { |
| 240 | clear(); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | uint32_t targetSize = uint32_t(mSize * mFlushRate); |
| 245 | TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize); |
| 246 | |
| 247 | while (mSize > targetSize) { |
| 248 | mCache.removeOldest(); |
| 249 | } |
| 250 | } |
| 251 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 252 | }; // namespace uirenderer |
| 253 | }; // namespace android |