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