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