TextureCache.cpp revision 9aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4
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 <utils/threads.h> 22 23#include "TextureCache.h" 24#include "Properties.h" 25 26namespace android { 27namespace uirenderer { 28 29/////////////////////////////////////////////////////////////////////////////// 30// Constructors/destructor 31/////////////////////////////////////////////////////////////////////////////// 32 33TextureCache::TextureCache(): 34 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), 35 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) { 36 char property[PROPERTY_VALUE_MAX]; 37 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { 38 LOGD(" Setting texture cache size to %sMB", property); 39 setMaxSize(MB(atof(property))); 40 } else { 41 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); 42 } 43 44 init(); 45} 46 47TextureCache::TextureCache(uint32_t maxByteSize): 48 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), 49 mSize(0), mMaxSize(maxByteSize) { 50 init(); 51} 52 53TextureCache::~TextureCache() { 54 Mutex::Autolock _l(mLock); 55 mCache.clear(); 56} 57 58void TextureCache::init() { 59 mCache.setOnEntryRemovedListener(this); 60 61 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); 62 LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); 63} 64 65/////////////////////////////////////////////////////////////////////////////// 66// Size management 67/////////////////////////////////////////////////////////////////////////////// 68 69uint32_t TextureCache::getSize() { 70 Mutex::Autolock _l(mLock); 71 return mSize; 72} 73 74uint32_t TextureCache::getMaxSize() { 75 Mutex::Autolock _l(mLock); 76 return mMaxSize; 77} 78 79void TextureCache::setMaxSize(uint32_t maxSize) { 80 Mutex::Autolock _l(mLock); 81 mMaxSize = maxSize; 82 while (mSize > mMaxSize) { 83 mCache.removeOldest(); 84 } 85} 86 87/////////////////////////////////////////////////////////////////////////////// 88// Callbacks 89/////////////////////////////////////////////////////////////////////////////// 90 91void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { 92 // This will be called already locked 93 if (texture) { 94 mSize -= texture->bitmapSize; 95 glDeleteTextures(1, &texture->id); 96 delete texture; 97 } 98} 99 100/////////////////////////////////////////////////////////////////////////////// 101// Caching 102/////////////////////////////////////////////////////////////////////////////// 103 104Texture* TextureCache::get(SkBitmap* bitmap) { 105 Mutex::Autolock _l(mLock); 106 107 Texture* texture = mCache.get(bitmap); 108 if (!texture) { 109 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { 110 LOGW("Bitmap too large to be uploaded into a texture"); 111 return NULL; 112 } 113 114 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 115 // Don't even try to cache a bitmap that's bigger than the cache 116 if (size < mMaxSize) { 117 while (mSize + size > mMaxSize) { 118 mCache.removeOldest(); 119 } 120 } 121 122 texture = new Texture; 123 texture->bitmapSize = size; 124 generateTexture(bitmap, texture, false); 125 126 if (size < mMaxSize) { 127 mSize += size; 128 mCache.put(bitmap, texture); 129 } else { 130 texture->cleanup = true; 131 } 132 } else if (bitmap->getGenerationID() != texture->generation) { 133 generateTexture(bitmap, texture, true); 134 } 135 136 return texture; 137} 138 139void TextureCache::remove(SkBitmap* bitmap) { 140 Mutex::Autolock _l(mLock); 141 mCache.remove(bitmap); 142} 143 144void TextureCache::clear() { 145 Mutex::Autolock _l(mLock); 146 mCache.clear(); 147} 148 149void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { 150 SkAutoLockPixels alp(*bitmap); 151 152 if (!bitmap->readyToDraw()) { 153 LOGE("Cannot generate texture from bitmap"); 154 return; 155 } 156 157 if (!regenerate) { 158 texture->generation = bitmap->getGenerationID(); 159 texture->width = bitmap->width(); 160 texture->height = bitmap->height(); 161 162 glGenTextures(1, &texture->id); 163 } 164 165 glBindTexture(GL_TEXTURE_2D, texture->id); 166 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 167 168 switch (bitmap->getConfig()) { 169 case SkBitmap::kA8_Config: 170 texture->blend = true; 171 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 172 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0, 173 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels()); 174 break; 175 case SkBitmap::kRGB_565_Config: 176 texture->blend = false; 177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0, 178 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); 179 break; 180 case SkBitmap::kARGB_8888_Config: 181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, 182 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); 183 // Do this after calling getPixels() to make sure Skia's deferred 184 // decoding happened 185 texture->blend = !bitmap->isOpaque(); 186 break; 187 default: 188 LOGW("Unsupported bitmap config"); 189 break; 190 } 191 192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 194 195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 197} 198 199}; // namespace uirenderer 200}; // namespace android 201