TextureCache.cpp revision 300bdfa13dd903c9335c11838cc0a604d0f8f2e6
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#define LOG_TAG "OpenGLRenderer" 18 19#include <GLES2/gl2.h> 20 21#include <SkCanvas.h> 22 23#include <utils/Mutex.h> 24 25#include "Caches.h" 26#include "TextureCache.h" 27#include "Properties.h" 28 29namespace android { 30namespace uirenderer { 31 32/////////////////////////////////////////////////////////////////////////////// 33// Constructors/destructor 34/////////////////////////////////////////////////////////////////////////////// 35 36TextureCache::TextureCache(): 37 mCache(LruCache<const SkBitmap*, Texture*>::kUnlimitedCapacity), 38 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)), 39 mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) { 40 char property[PROPERTY_VALUE_MAX]; 41 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { 42 INIT_LOGD(" Setting texture cache size to %sMB", property); 43 setMaxSize(MB(atof(property))); 44 } else { 45 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); 46 } 47 48 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, NULL) > 0) { 49 float flushRate = atof(property); 50 INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f); 51 setFlushRate(flushRate); 52 } else { 53 INIT_LOGD(" Using default texture cache flush rate of %.2f%%", 54 DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f); 55 } 56 57 init(); 58} 59 60TextureCache::TextureCache(uint32_t maxByteSize): 61 mCache(LruCache<const SkBitmap*, Texture*>::kUnlimitedCapacity), 62 mSize(0), mMaxSize(maxByteSize) { 63 init(); 64} 65 66TextureCache::~TextureCache() { 67 mCache.clear(); 68} 69 70void TextureCache::init() { 71 mCache.setOnEntryRemovedListener(this); 72 73 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); 74 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); 75 76 mDebugEnabled = readDebugLevel() & kDebugCaches; 77} 78 79/////////////////////////////////////////////////////////////////////////////// 80// Size management 81/////////////////////////////////////////////////////////////////////////////// 82 83uint32_t TextureCache::getSize() { 84 return mSize; 85} 86 87uint32_t TextureCache::getMaxSize() { 88 return mMaxSize; 89} 90 91void TextureCache::setMaxSize(uint32_t maxSize) { 92 mMaxSize = maxSize; 93 while (mSize > mMaxSize) { 94 mCache.removeOldest(); 95 } 96} 97 98void TextureCache::setFlushRate(float flushRate) { 99 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate)); 100} 101 102/////////////////////////////////////////////////////////////////////////////// 103// Callbacks 104/////////////////////////////////////////////////////////////////////////////// 105 106void TextureCache::operator()(const SkBitmap*&, Texture*& texture) { 107 // This will be called already locked 108 if (texture) { 109 mSize -= texture->bitmapSize; 110 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", 111 texture->id, texture->bitmapSize, mSize); 112 if (mDebugEnabled) { 113 ALOGD("Texture deleted, size = %d", texture->bitmapSize); 114 } 115 texture->deleteTexture(); 116 delete texture; 117 } 118} 119 120/////////////////////////////////////////////////////////////////////////////// 121// Caching 122/////////////////////////////////////////////////////////////////////////////// 123 124Texture* TextureCache::get(const SkBitmap* bitmap) { 125 Texture* texture = mCache.get(bitmap); 126 127 if (!texture) { 128 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { 129 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)", 130 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize); 131 return NULL; 132 } 133 134 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 135 // Don't even try to cache a bitmap that's bigger than the cache 136 if (size < mMaxSize) { 137 while (mSize + size > mMaxSize) { 138 mCache.removeOldest(); 139 } 140 } 141 142 texture = new Texture(); 143 texture->bitmapSize = size; 144 generateTexture(bitmap, texture, false); 145 146 if (size < mMaxSize) { 147 mSize += size; 148 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", 149 bitmap, texture->id, size, mSize); 150 if (mDebugEnabled) { 151 ALOGD("Texture created, size = %d", size); 152 } 153 mCache.put(bitmap, texture); 154 } else { 155 texture->cleanup = true; 156 } 157 } else if (bitmap->getGenerationID() != texture->generation) { 158 generateTexture(bitmap, texture, true); 159 } 160 161 return texture; 162} 163 164Texture* TextureCache::getTransient(const SkBitmap* bitmap) { 165 Texture* texture = new Texture(); 166 texture->bitmapSize = bitmap->rowBytes() * bitmap->height(); 167 texture->cleanup = true; 168 169 generateTexture(bitmap, texture, false); 170 171 return texture; 172} 173 174void TextureCache::remove(const SkBitmap* bitmap) { 175 mCache.remove(bitmap); 176} 177 178void TextureCache::removeDeferred(const SkBitmap* bitmap) { 179 Mutex::Autolock _l(mLock); 180 mGarbage.push(bitmap); 181} 182 183void TextureCache::clearGarbage() { 184 Mutex::Autolock _l(mLock); 185 size_t count = mGarbage.size(); 186 for (size_t i = 0; i < count; i++) { 187 const SkBitmap* bitmap = mGarbage.itemAt(i); 188 mCache.remove(bitmap); 189 delete bitmap; 190 } 191 mGarbage.clear(); 192} 193 194void TextureCache::clear() { 195 mCache.clear(); 196 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); 197} 198 199void TextureCache::flush() { 200 if (mFlushRate >= 1.0f || mCache.size() == 0) return; 201 if (mFlushRate <= 0.0f) { 202 clear(); 203 return; 204 } 205 206 uint32_t targetSize = uint32_t(mSize * mFlushRate); 207 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize); 208 209 while (mSize > targetSize) { 210 mCache.removeOldest(); 211 } 212} 213 214void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) { 215 SkAutoLockPixels alp(*bitmap); 216 217 if (!bitmap->readyToDraw()) { 218 ALOGE("Cannot generate texture from bitmap"); 219 return; 220 } 221 222 // We could also enable mipmapping if both bitmap dimensions are powers 223 // of 2 but we'd have to deal with size changes. Let's keep this simple 224 const bool canMipMap = Extensions::getInstance().hasNPot(); 225 226 // If the texture had mipmap enabled but not anymore, 227 // force a glTexImage2D to discard the mipmap levels 228 const bool resize = !regenerate || bitmap->width() != int(texture->width) || 229 bitmap->height() != int(texture->height) || 230 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap()); 231 232 if (!regenerate) { 233 glGenTextures(1, &texture->id); 234 } 235 236 texture->generation = bitmap->getGenerationID(); 237 texture->width = bitmap->width(); 238 texture->height = bitmap->height(); 239 240 Caches::getInstance().bindTexture(texture->id); 241 242 switch (bitmap->config()) { 243 case SkBitmap::kA8_Config: 244 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 245 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), 246 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); 247 texture->blend = true; 248 break; 249 case SkBitmap::kRGB_565_Config: 250 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 251 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), 252 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); 253 texture->blend = false; 254 break; 255 case SkBitmap::kARGB_8888_Config: 256 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 257 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), 258 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); 259 // Do this after calling getPixels() to make sure Skia's deferred 260 // decoding happened 261 texture->blend = !bitmap->isOpaque(); 262 break; 263 case SkBitmap::kARGB_4444_Config: 264 case SkBitmap::kIndex8_Config: 265 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 266 uploadLoFiTexture(resize, bitmap, texture->width, texture->height); 267 texture->blend = !bitmap->isOpaque(); 268 break; 269 default: 270 ALOGW("Unsupported bitmap config: %d", bitmap->config()); 271 break; 272 } 273 274 if (canMipMap) { 275 texture->mipMap = bitmap->hasHardwareMipMap(); 276 if (texture->mipMap) { 277 glGenerateMipmap(GL_TEXTURE_2D); 278 } 279 } 280 281 if (!regenerate) { 282 texture->setFilter(GL_NEAREST); 283 texture->setWrap(GL_CLAMP_TO_EDGE); 284 } 285} 286 287void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap, 288 uint32_t width, uint32_t height) { 289 SkBitmap rgbaBitmap; 290 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, bitmap->alphaType()); 291 rgbaBitmap.allocPixels(); 292 rgbaBitmap.eraseColor(0); 293 294 SkCanvas canvas(rgbaBitmap); 295 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL); 296 297 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), width, height, 298 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels()); 299} 300 301void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride, 302 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) { 303 // TODO: With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer 304 // if the stride doesn't match the width 305 const bool useStride = stride != width && Extensions::getInstance().hasUnpackRowLength(); 306 if (useStride) { 307 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); 308 } 309 310 if (resize) { 311 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); 312 } else { 313 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); 314 } 315 316 if (useStride) { 317 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 318 } 319} 320 321}; // namespace uirenderer 322}; // namespace android 323