TextureCache.cpp revision 5f0c6a483900f3989f4d2a8f913cf5b6a9777d03
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 "TextureCache.h" 22 23namespace android { 24namespace uirenderer { 25 26/////////////////////////////////////////////////////////////////////////////// 27// Constructors/destructor 28/////////////////////////////////////////////////////////////////////////////// 29 30TextureCache::TextureCache(uint32_t maxByteSize): 31 mCache(GenerationSingleCache<SkBitmap*, Texture*>::kUnlimitedCapacity), 32 mSize(0), mMaxSize(maxByteSize) { 33 mCache.setOnEntryRemovedListener(this); 34} 35 36TextureCache::~TextureCache() { 37 mCache.clear(); 38} 39 40/////////////////////////////////////////////////////////////////////////////// 41// Size management 42/////////////////////////////////////////////////////////////////////////////// 43 44uint32_t TextureCache::getSize() { 45 return mSize; 46} 47 48uint32_t TextureCache::getMaxSize() { 49 return mMaxSize; 50} 51 52void TextureCache::setMaxSize(uint32_t maxSize) { 53 mMaxSize = maxSize; 54 while (mSize > mMaxSize) { 55 mCache.removeOldest(); 56 } 57} 58 59/////////////////////////////////////////////////////////////////////////////// 60// Callbacks 61/////////////////////////////////////////////////////////////////////////////// 62 63void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { 64 if (bitmap) { 65 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 66 mSize -= size; 67 } 68 69 if (texture) { 70 glDeleteTextures(1, &texture->id); 71 delete texture; 72 } 73} 74 75/////////////////////////////////////////////////////////////////////////////// 76// Caching 77/////////////////////////////////////////////////////////////////////////////// 78 79Texture* TextureCache::get(SkBitmap* bitmap) { 80 Texture* texture = mCache.get(bitmap); 81 if (!texture) { 82 const uint32_t size = bitmap->rowBytes() * bitmap->height(); 83 // Don't even try to cache a bitmap that's bigger than the cache 84 if (size < mMaxSize) { 85 while (mSize + size > mMaxSize) { 86 mCache.removeOldest(); 87 } 88 } 89 90 texture = new Texture; 91 generateTexture(bitmap, texture, false); 92 93 if (size < mMaxSize) { 94 mSize += size; 95 mCache.put(bitmap, texture); 96 } 97 } else if (bitmap->getGenerationID() != texture->generation) { 98 generateTexture(bitmap, texture, true); 99 } 100 return texture; 101} 102 103void TextureCache::remove(SkBitmap* bitmap) { 104 mCache.remove(bitmap); 105} 106 107void TextureCache::clear() { 108 mCache.clear(); 109} 110 111void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { 112 SkAutoLockPixels alp(*bitmap); 113 if (!bitmap->readyToDraw()) { 114 LOGE("Cannot generate texture from bitmap"); 115 return; 116 } 117 118 if (!regenerate) { 119 texture->generation = bitmap->getGenerationID(); 120 texture->width = bitmap->width(); 121 texture->height = bitmap->height(); 122 123 glGenTextures(1, &texture->id); 124 } 125 126 glBindTexture(GL_TEXTURE_2D, texture->id); 127 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); 128 129 switch (bitmap->getConfig()) { 130 case SkBitmap::kRGB_565_Config: 131 texture->blend = false; 132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0, 133 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); 134 break; 135 case SkBitmap::kARGB_8888_Config: 136 texture->blend = true; 137 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, 138 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); 139 break; 140 default: 141 break; 142 } 143 144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 146 147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 149 150 glBindTexture(GL_TEXTURE_2D, 0); 151} 152 153}; // namespace uirenderer 154}; // namespace android 155