TextureCache.cpp revision 15c3f19a445b8df575911a16e8a6dba755a084b5
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 19#include <utils/Mutex.h> 20 21#include "AssetAtlas.h" 22#include "Caches.h" 23#include "Texture.h" 24#include "TextureCache.h" 25#include "Properties.h" 26#include "utils/TraceUtils.h" 27 28namespace android { 29namespace uirenderer { 30 31/////////////////////////////////////////////////////////////////////////////// 32// Constructors/destructor 33/////////////////////////////////////////////////////////////////////////////// 34 35TextureCache::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) { 41 char property[PROPERTY_VALUE_MAX]; 42 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, nullptr) > 0) { 43 INIT_LOGD(" Setting texture cache size to %sMB", property); 44 setMaxSize(MB(atof(property))); 45 } else { 46 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); 47 } 48 49 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, nullptr) > 0) { 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 58 mCache.setOnEntryRemovedListener(this); 59 60 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); 61 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); 62 63 mDebugEnabled = Properties::debugLevel & kDebugCaches; 64} 65 66TextureCache::~TextureCache() { 67 mCache.clear(); 68} 69 70/////////////////////////////////////////////////////////////////////////////// 71// Size management 72/////////////////////////////////////////////////////////////////////////////// 73 74uint32_t TextureCache::getSize() { 75 return mSize; 76} 77 78uint32_t TextureCache::getMaxSize() { 79 return mMaxSize; 80} 81 82void TextureCache::setMaxSize(uint32_t maxSize) { 83 mMaxSize = maxSize; 84 while (mSize > mMaxSize) { 85 mCache.removeOldest(); 86 } 87} 88 89void TextureCache::setFlushRate(float flushRate) { 90 mFlushRate = std::max(0.0f, std::min(1.0f, flushRate)); 91} 92 93/////////////////////////////////////////////////////////////////////////////// 94// Callbacks 95/////////////////////////////////////////////////////////////////////////////// 96 97void TextureCache::operator()(uint32_t&, Texture*& texture) { 98 // This will be called already locked 99 if (texture) { 100 mSize -= texture->bitmapSize; 101 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", 102 texture->id, texture->bitmapSize, mSize); 103 if (mDebugEnabled) { 104 ALOGD("Texture deleted, size = %d", texture->bitmapSize); 105 } 106 texture->deleteTexture(); 107 delete texture; 108 } 109} 110 111/////////////////////////////////////////////////////////////////////////////// 112// Caching 113/////////////////////////////////////////////////////////////////////////////// 114 115void TextureCache::setAssetAtlas(AssetAtlas* assetAtlas) { 116 mAssetAtlas = assetAtlas; 117} 118 119void TextureCache::resetMarkInUse(void* ownerToken) { 120 LruCache<uint32_t, Texture*>::Iterator iter(mCache); 121 while (iter.next()) { 122 if (iter.value()->isInUse == ownerToken) { 123 iter.value()->isInUse = nullptr; 124 } 125 } 126} 127 128bool 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) 139Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) { 140 if (CC_LIKELY(mAssetAtlas != nullptr) && atlasUsageType == AtlasUsageType::Use) { 141 AssetAtlas::Entry* entry = mAssetAtlas->getEntry(bitmap->pixelRef()); 142 if (CC_UNLIKELY(entry)) { 143 return entry->texture; 144 } 145 } 146 147 Texture* texture = mCache.get(bitmap->pixelRef()->getStableID()); 148 149 if (!texture) { 150 if (!canMakeTextureFromBitmap(bitmap)) { 151 return nullptr; 152 } 153 154 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 155 bool canCache = size < mMaxSize; 156 // Don't even try to cache a bitmap that's bigger than the cache 157 while (canCache && mSize + size > mMaxSize) { 158 Texture* oldest = mCache.peekOldestValue(); 159 if (oldest && !oldest->isInUse) { 160 mCache.removeOldest(); 161 } else { 162 canCache = false; 163 } 164 } 165 166 if (canCache) { 167 texture = new Texture(Caches::getInstance()); 168 texture->bitmapSize = size; 169 Caches::getInstance().textureState().generateTexture(bitmap, texture, false); 170 171 mSize += size; 172 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", 173 bitmap, texture->id, size, mSize); 174 if (mDebugEnabled) { 175 ALOGD("Texture created, size = %d", size); 176 } 177 mCache.put(bitmap->pixelRef()->getStableID(), texture); 178 } 179 } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) { 180 // Texture was in the cache but is dirty, re-upload 181 // TODO: Re-adjust the cache size if the bitmap's dimensions have changed 182 Caches::getInstance().textureState().generateTexture(bitmap, texture, true); 183 } 184 185 return texture; 186} 187 188bool TextureCache::prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap) { 189 Texture* texture = getCachedTexture(bitmap, AtlasUsageType::Use); 190 if (texture) { 191 texture->isInUse = ownerToken; 192 } 193 return texture; 194} 195 196Texture* TextureCache::get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) { 197 Texture* texture = getCachedTexture(bitmap, atlasUsageType); 198 199 if (!texture) { 200 if (!canMakeTextureFromBitmap(bitmap)) { 201 return nullptr; 202 } 203 204 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 205 texture = new Texture(Caches::getInstance()); 206 texture->bitmapSize = size; 207 Caches::getInstance().textureState().generateTexture(bitmap, texture, false); 208 texture->cleanup = true; 209 } 210 211 return texture; 212} 213 214void TextureCache::releaseTexture(uint32_t pixelRefStableID) { 215 Mutex::Autolock _l(mLock); 216 mGarbage.push_back(pixelRefStableID); 217} 218 219void TextureCache::clearGarbage() { 220 Mutex::Autolock _l(mLock); 221 size_t count = mGarbage.size(); 222 for (size_t i = 0; i < count; i++) { 223 uint32_t pixelRefId = mGarbage[i]; 224 mCache.remove(pixelRefId); 225 } 226 mGarbage.clear(); 227} 228 229void TextureCache::clear() { 230 mCache.clear(); 231 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); 232} 233 234void TextureCache::flush() { 235 if (mFlushRate >= 1.0f || mCache.size() == 0) return; 236 if (mFlushRate <= 0.0f) { 237 clear(); 238 return; 239 } 240 241 uint32_t targetSize = uint32_t(mSize * mFlushRate); 242 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize); 243 244 while (mSize > targetSize) { 245 mCache.removeOldest(); 246 } 247} 248 249}; // namespace uirenderer 250}; // namespace android 251