TextureCache.cpp revision 98fa4f9e7b33a3004ce9142c9acd4300391b9a0e
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 "Caches.h" 22#include "Texture.h" 23#include "TextureCache.h" 24#include "Properties.h" 25#include "utils/TraceUtils.h" 26#include "hwui/Bitmap.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(Properties::textureCacheSize) 39 , mFlushRate(Properties::textureCacheFlushRate) { 40 mCache.setOnEntryRemovedListener(this); 41 42 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); 43 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); 44 45 mDebugEnabled = Properties::debugLevel & kDebugCaches; 46} 47 48TextureCache::~TextureCache() { 49 mCache.clear(); 50} 51 52/////////////////////////////////////////////////////////////////////////////// 53// Size management 54/////////////////////////////////////////////////////////////////////////////// 55 56uint32_t TextureCache::getSize() { 57 return mSize; 58} 59 60uint32_t TextureCache::getMaxSize() { 61 return mMaxSize; 62} 63 64/////////////////////////////////////////////////////////////////////////////// 65// Callbacks 66/////////////////////////////////////////////////////////////////////////////// 67 68void TextureCache::operator()(uint32_t&, Texture*& texture) { 69 // This will be called already locked 70 if (texture) { 71 mSize -= texture->bitmapSize; 72 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", 73 texture->id, texture->bitmapSize, mSize); 74 if (mDebugEnabled) { 75 ALOGD("Texture deleted, size = %d", texture->bitmapSize); 76 } 77 texture->deleteTexture(); 78 delete texture; 79 } 80} 81 82/////////////////////////////////////////////////////////////////////////////// 83// Caching 84/////////////////////////////////////////////////////////////////////////////// 85 86void TextureCache::resetMarkInUse(void* ownerToken) { 87 LruCache<uint32_t, Texture*>::Iterator iter(mCache); 88 while (iter.next()) { 89 if (iter.value()->isInUse == ownerToken) { 90 iter.value()->isInUse = nullptr; 91 } 92 } 93} 94 95bool TextureCache::canMakeTextureFromBitmap(Bitmap* bitmap) { 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) 106Texture* TextureCache::getCachedTexture(Bitmap* bitmap) { 107 Texture* texture = mCache.get(bitmap->getStableID()); 108 109 if (!texture) { 110 if (!canMakeTextureFromBitmap(bitmap)) { 111 return nullptr; 112 } 113 114 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 115 bool canCache = size < mMaxSize; 116 // Don't even try to cache a bitmap that's bigger than the cache 117 while (canCache && mSize + size > mMaxSize) { 118 Texture* oldest = mCache.peekOldestValue(); 119 if (oldest && !oldest->isInUse) { 120 mCache.removeOldest(); 121 } else { 122 canCache = false; 123 } 124 } 125 126 if (canCache) { 127 texture = new Texture(Caches::getInstance()); 128 texture->bitmapSize = size; 129 texture->generation = bitmap->getGenerationID(); 130 texture->upload(*bitmap); 131 132 mSize += size; 133 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", 134 bitmap, texture->id, size, mSize); 135 if (mDebugEnabled) { 136 ALOGD("Texture created, size = %d", size); 137 } 138 mCache.put(bitmap->getStableID(), texture); 139 } 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 143 texture->upload(*bitmap); 144 texture->generation = bitmap->getGenerationID(); 145 } 146 147 return texture; 148} 149 150bool TextureCache::prefetchAndMarkInUse(void* ownerToken, Bitmap* bitmap) { 151 Texture* texture = getCachedTexture(bitmap); 152 if (texture) { 153 texture->isInUse = ownerToken; 154 } 155 return texture; 156} 157 158bool TextureCache::prefetch(Bitmap* bitmap) { 159 return getCachedTexture(bitmap); 160} 161 162Texture* TextureCache::get(Bitmap* bitmap) { 163 Texture* texture = getCachedTexture(bitmap); 164 165 if (!texture) { 166 if (!canMakeTextureFromBitmap(bitmap)) { 167 return nullptr; 168 } 169 170 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 171 texture = new Texture(Caches::getInstance()); 172 texture->bitmapSize = size; 173 texture->upload(*bitmap); 174 texture->generation = bitmap->getGenerationID(); 175 texture->cleanup = true; 176 } 177 178 return texture; 179} 180 181void TextureCache::releaseTexture(uint32_t pixelRefStableID) { 182 Mutex::Autolock _l(mLock); 183 mGarbage.push_back(pixelRefStableID); 184} 185 186void TextureCache::clearGarbage() { 187 Mutex::Autolock _l(mLock); 188 size_t count = mGarbage.size(); 189 for (size_t i = 0; i < count; i++) { 190 uint32_t pixelRefId = mGarbage[i]; 191 mCache.remove(pixelRefId); 192 } 193 mGarbage.clear(); 194} 195 196void TextureCache::clear() { 197 mCache.clear(); 198 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); 199} 200 201void 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 216}; // namespace uirenderer 217}; // namespace android 218