TextureCache.cpp revision a33d161250b0787f4e7a3f3f09244451e22496ce
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/threads.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(GenerationCache<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(GenerationCache<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 mHasNPot = false; //Caches::getInstance().extensions.hasNPot(); 79} 80 81/////////////////////////////////////////////////////////////////////////////// 82// Size management 83/////////////////////////////////////////////////////////////////////////////// 84 85uint32_t TextureCache::getSize() { 86 return mSize; 87} 88 89uint32_t TextureCache::getMaxSize() { 90 return mMaxSize; 91} 92 93void TextureCache::setMaxSize(uint32_t maxSize) { 94 mMaxSize = maxSize; 95 while (mSize > mMaxSize) { 96 mCache.removeOldest(); 97 } 98} 99 100void TextureCache::setFlushRate(float flushRate) { 101 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate)); 102} 103 104/////////////////////////////////////////////////////////////////////////////// 105// Callbacks 106/////////////////////////////////////////////////////////////////////////////// 107 108void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { 109 // This will be called already locked 110 if (texture) { 111 mSize -= texture->bitmapSize; 112 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", 113 texture->id, texture->bitmapSize, mSize); 114 if (mDebugEnabled) { 115 ALOGD("Texture deleted, size = %d", texture->bitmapSize); 116 } 117 glDeleteTextures(1, &texture->id); 118 delete texture; 119 } 120} 121 122/////////////////////////////////////////////////////////////////////////////// 123// Caching 124/////////////////////////////////////////////////////////////////////////////// 125 126Texture* TextureCache::get(SkBitmap* bitmap) { 127 Texture* texture = mCache.get(bitmap); 128 129 if (!texture) { 130 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { 131 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)", 132 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize); 133 return NULL; 134 } 135 136 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 137 // Don't even try to cache a bitmap that's bigger than the cache 138 if (size < mMaxSize) { 139 while (mSize + size > mMaxSize) { 140 mCache.removeOldest(); 141 } 142 } 143 144 texture = new Texture; 145 texture->bitmapSize = size; 146 generateTexture(bitmap, texture, false); 147 148 if (size < mMaxSize) { 149 mSize += size; 150 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", 151 bitmap, texture->id, size, mSize); 152 if (mDebugEnabled) { 153 ALOGD("Texture created, size = %d", size); 154 } 155 mCache.put(bitmap, texture); 156 } else { 157 texture->cleanup = true; 158 } 159 } else if (bitmap->getGenerationID() != texture->generation) { 160 generateTexture(bitmap, texture, true); 161 } 162 163 return texture; 164} 165 166Texture* TextureCache::getTransient(SkBitmap* bitmap) { 167 Texture* texture = new Texture; 168 texture->bitmapSize = bitmap->rowBytes() * bitmap->height(); 169 texture->cleanup = true; 170 171 generateTexture(bitmap, texture, false); 172 173 return texture; 174} 175 176void TextureCache::remove(SkBitmap* bitmap) { 177 mCache.remove(bitmap); 178} 179 180void TextureCache::removeDeferred(SkBitmap* bitmap) { 181 Mutex::Autolock _l(mLock); 182 mGarbage.push(bitmap); 183} 184 185void TextureCache::clearGarbage() { 186 Mutex::Autolock _l(mLock); 187 size_t count = mGarbage.size(); 188 for (size_t i = 0; i < count; i++) { 189 mCache.remove(mGarbage.itemAt(i)); 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(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 // If the texture had mipmap enabled but not anymore, 223 // force a glTexImage2D to discard the mipmap levels 224 const bool resize = !regenerate || bitmap->width() != int(texture->width) || 225 bitmap->height() != int(texture->height) || 226 (regenerate && mHasNPot && texture->mipMap && !bitmap->hasHardwareMipMap()); 227 228 if (!regenerate) { 229 glGenTextures(1, &texture->id); 230 } 231 232 texture->generation = bitmap->getGenerationID(); 233 texture->width = bitmap->width(); 234 texture->height = bitmap->height(); 235 236 glBindTexture(GL_TEXTURE_2D, texture->id); 237 238 switch (bitmap->getConfig()) { 239 case SkBitmap::kA8_Config: 240 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 241 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 242 GL_UNSIGNED_BYTE, bitmap->getPixels()); 243 texture->blend = true; 244 break; 245 case SkBitmap::kRGB_565_Config: 246 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 247 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 248 GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); 249 texture->blend = false; 250 break; 251 case SkBitmap::kARGB_8888_Config: 252 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 253 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 254 GL_UNSIGNED_BYTE, bitmap->getPixels()); 255 // Do this after calling getPixels() to make sure Skia's deferred 256 // decoding happened 257 texture->blend = !bitmap->isOpaque(); 258 break; 259 case SkBitmap::kARGB_4444_Config: 260 case SkBitmap::kIndex8_Config: 261 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 262 uploadLoFiTexture(resize, bitmap, texture->width, texture->height); 263 texture->blend = !bitmap->isOpaque(); 264 break; 265 default: 266 ALOGW("Unsupported bitmap config: %d", bitmap->getConfig()); 267 break; 268 } 269 270 if (mHasNPot) { 271 texture->mipMap = bitmap->hasHardwareMipMap(); 272 if (texture->mipMap) { 273 glGenerateMipmap(GL_TEXTURE_2D); 274 } 275 } 276 277 if (!regenerate) { 278 texture->setFilter(GL_NEAREST); 279 texture->setWrap(GL_CLAMP_TO_EDGE); 280 } 281} 282 283void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap, 284 uint32_t width, uint32_t height) { 285 SkBitmap rgbaBitmap; 286 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 287 rgbaBitmap.allocPixels(); 288 rgbaBitmap.eraseColor(0); 289 rgbaBitmap.setIsOpaque(bitmap->isOpaque()); 290 291 SkCanvas canvas(rgbaBitmap); 292 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL); 293 294 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height, 295 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels()); 296} 297 298void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height, 299 GLenum type, const GLvoid * data) { 300 if (resize) { 301 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); 302 } else { 303 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); 304 } 305} 306 307}; // namespace uirenderer 308}; // namespace android 309