2010-06-29 21:05:21 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
|
2010-10-05 14:58:09 -07:00
|
|
|
#include <SkCanvas.h>
|
|
|
|
|
2010-09-08 15:15:43 -07:00
|
|
|
#include <utils/threads.h>
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
#include "TextureCache.h"
|
2010-08-23 21:05:08 -07:00
|
|
|
#include "Properties.h"
|
2010-06-29 21:05:21 -07:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors/destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-08-23 21:05:08 -07:00
|
|
|
TextureCache::TextureCache():
|
|
|
|
mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
|
|
|
|
mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) {
|
|
|
|
char property[PROPERTY_VALUE_MAX];
|
|
|
|
if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
|
2011-01-21 21:14:15 -08:00
|
|
|
INIT_LOGD(" Setting texture cache size to %sMB", property);
|
2010-08-23 21:05:08 -07:00
|
|
|
setMaxSize(MB(atof(property)));
|
|
|
|
} else {
|
2011-01-21 21:14:15 -08:00
|
|
|
INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
|
2010-08-23 21:05:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
TextureCache::TextureCache(uint32_t maxByteSize):
|
2010-07-07 15:15:32 -07:00
|
|
|
mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
|
2010-07-01 18:26:52 -07:00
|
|
|
mSize(0), mMaxSize(maxByteSize) {
|
2010-08-23 21:05:08 -07:00
|
|
|
init();
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureCache::~TextureCache() {
|
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
2010-08-23 21:05:08 -07:00
|
|
|
void TextureCache::init() {
|
|
|
|
mCache.setOnEntryRemovedListener(this);
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
|
2011-01-23 13:32:12 -08:00
|
|
|
INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
|
2010-11-10 19:01:29 -08:00
|
|
|
|
|
|
|
mDebugEnabled = readDebugLevel() & kDebugCaches;
|
2010-08-23 21:05:08 -07:00
|
|
|
}
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Size management
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
uint32_t TextureCache::getSize() {
|
2010-07-01 18:26:52 -07:00
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
uint32_t TextureCache::getMaxSize() {
|
2010-07-01 18:26:52 -07:00
|
|
|
return mMaxSize;
|
|
|
|
}
|
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
void TextureCache::setMaxSize(uint32_t maxSize) {
|
2010-07-01 18:26:52 -07:00
|
|
|
mMaxSize = maxSize;
|
|
|
|
while (mSize > mMaxSize) {
|
|
|
|
mCache.removeOldest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Callbacks
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-07-06 11:39:32 -07:00
|
|
|
void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
|
2010-09-08 15:15:43 -07:00
|
|
|
// This will be called already locked
|
2010-07-01 18:26:52 -07:00
|
|
|
if (texture) {
|
2010-09-08 15:15:43 -07:00
|
|
|
mSize -= texture->bitmapSize;
|
2010-11-09 14:35:20 -08:00
|
|
|
TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
|
|
|
|
texture->id, texture->bitmapSize, mSize);
|
2010-11-10 19:01:29 -08:00
|
|
|
if (mDebugEnabled) {
|
|
|
|
LOGD("Texture deleted, size = %d", texture->bitmapSize);
|
|
|
|
}
|
2010-07-01 18:26:52 -07:00
|
|
|
glDeleteTextures(1, &texture->id);
|
|
|
|
delete texture;
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Caching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
Texture* TextureCache::get(SkBitmap* bitmap) {
|
|
|
|
Texture* texture = mCache.get(bitmap);
|
2010-09-08 18:04:33 -07:00
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
if (!texture) {
|
2010-08-07 23:46:15 -07:00
|
|
|
if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
|
|
|
|
LOGW("Bitmap too large to be uploaded into a texture");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
const uint32_t size = bitmap->rowBytes() * bitmap->height();
|
2010-07-01 18:26:52 -07:00
|
|
|
// Don't even try to cache a bitmap that's bigger than the cache
|
|
|
|
if (size < mMaxSize) {
|
|
|
|
while (mSize + size > mMaxSize) {
|
|
|
|
mCache.removeOldest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-30 15:51:03 -07:00
|
|
|
texture = new Texture;
|
2010-09-08 15:15:43 -07:00
|
|
|
texture->bitmapSize = size;
|
2010-06-30 17:56:19 -07:00
|
|
|
generateTexture(bitmap, texture, false);
|
2010-07-01 18:26:52 -07:00
|
|
|
|
|
|
|
if (size < mMaxSize) {
|
|
|
|
mSize += size;
|
2010-11-09 14:35:20 -08:00
|
|
|
TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
|
|
|
|
bitmap, texture->id, size, mSize);
|
2010-11-10 19:01:29 -08:00
|
|
|
if (mDebugEnabled) {
|
|
|
|
LOGD("Texture created, size = %d", size);
|
|
|
|
}
|
2010-07-01 18:26:52 -07:00
|
|
|
mCache.put(bitmap, texture);
|
2010-08-06 11:18:34 -07:00
|
|
|
} else {
|
|
|
|
texture->cleanup = true;
|
2010-07-01 18:26:52 -07:00
|
|
|
}
|
2010-06-30 16:05:32 -07:00
|
|
|
} else if (bitmap->getGenerationID() != texture->generation) {
|
|
|
|
generateTexture(bitmap, texture, true);
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
2010-08-06 11:18:34 -07:00
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
void TextureCache::remove(SkBitmap* bitmap) {
|
|
|
|
mCache.remove(bitmap);
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2010-11-11 15:36:56 -08:00
|
|
|
void TextureCache::removeDeferred(SkBitmap* bitmap) {
|
2010-09-08 15:15:43 -07:00
|
|
|
Mutex::Autolock _l(mLock);
|
2010-11-11 15:36:56 -08:00
|
|
|
mGarbage.push(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::clearGarbage() {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
size_t count = mGarbage.size();
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
mCache.remove(mGarbage.itemAt(i));
|
|
|
|
}
|
|
|
|
mGarbage.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::clear() {
|
2010-06-29 21:05:21 -07:00
|
|
|
mCache.clear();
|
2010-10-25 15:47:32 -07:00
|
|
|
TEXTURE_LOGD("TextureCache:clear(), miSize = %d", mSize);
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2010-06-30 16:05:32 -07:00
|
|
|
void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
|
2010-06-30 17:56:19 -07:00
|
|
|
SkAutoLockPixels alp(*bitmap);
|
2010-09-08 15:15:43 -07:00
|
|
|
|
2010-06-30 17:56:19 -07:00
|
|
|
if (!bitmap->readyToDraw()) {
|
|
|
|
LOGE("Cannot generate texture from bitmap");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-22 16:10:57 -07:00
|
|
|
const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
|
|
|
|
bitmap->height() != int(texture->height);
|
2010-06-30 16:05:32 -07:00
|
|
|
|
2010-09-22 14:13:32 -07:00
|
|
|
if (!regenerate) {
|
2010-06-30 16:05:32 -07:00
|
|
|
glGenTextures(1, &texture->id);
|
|
|
|
}
|
2010-06-29 21:05:21 -07:00
|
|
|
|
2010-09-22 14:13:32 -07:00
|
|
|
texture->generation = bitmap->getGenerationID();
|
|
|
|
texture->width = bitmap->width();
|
|
|
|
texture->height = bitmap->height();
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->id);
|
2010-06-30 17:56:19 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
|
2010-06-29 21:05:21 -07:00
|
|
|
|
|
|
|
switch (bitmap->getConfig()) {
|
2010-07-22 18:50:12 -07:00
|
|
|
case SkBitmap::kA8_Config:
|
2010-09-08 15:15:43 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2010-09-22 14:13:32 -07:00
|
|
|
uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height,
|
|
|
|
GL_UNSIGNED_BYTE, bitmap->getPixels());
|
|
|
|
texture->blend = true;
|
2010-07-22 18:50:12 -07:00
|
|
|
break;
|
2010-06-29 21:05:21 -07:00
|
|
|
case SkBitmap::kRGB_565_Config:
|
2010-09-22 14:13:32 -07:00
|
|
|
uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height,
|
|
|
|
GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
|
2010-06-30 17:56:19 -07:00
|
|
|
texture->blend = false;
|
2010-06-29 21:05:21 -07:00
|
|
|
break;
|
|
|
|
case SkBitmap::kARGB_8888_Config:
|
2010-09-22 14:13:32 -07:00
|
|
|
uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height,
|
|
|
|
GL_UNSIGNED_BYTE, bitmap->getPixels());
|
2010-08-19 14:45:42 -07:00
|
|
|
// Do this after calling getPixels() to make sure Skia's deferred
|
|
|
|
// decoding happened
|
|
|
|
texture->blend = !bitmap->isOpaque();
|
2010-06-29 21:05:21 -07:00
|
|
|
break;
|
2010-10-27 18:57:51 -07:00
|
|
|
case SkBitmap::kARGB_4444_Config:
|
2011-02-24 17:21:29 -08:00
|
|
|
case SkBitmap::kIndex8_Config:
|
2010-10-27 18:57:51 -07:00
|
|
|
uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
|
2011-02-24 17:21:29 -08:00
|
|
|
texture->blend = !bitmap->isOpaque();
|
2010-10-27 18:57:51 -07:00
|
|
|
break;
|
2010-06-30 15:51:03 -07:00
|
|
|
default:
|
2010-10-05 14:58:09 -07:00
|
|
|
LOGW("Unsupported bitmap config: %d", bitmap->getConfig());
|
2010-06-30 15:51:03 -07:00
|
|
|
break;
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2011-07-25 16:36:01 -07:00
|
|
|
texture->setFilter(GL_LINEAR, GL_LINEAR);
|
|
|
|
texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2010-10-27 18:57:51 -07:00
|
|
|
void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
|
2010-10-05 14:58:09 -07:00
|
|
|
uint32_t width, uint32_t height) {
|
|
|
|
SkBitmap rgbaBitmap;
|
|
|
|
rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
|
|
|
|
rgbaBitmap.allocPixels();
|
|
|
|
rgbaBitmap.eraseColor(0);
|
2011-02-24 17:21:29 -08:00
|
|
|
rgbaBitmap.setIsOpaque(bitmap->isOpaque());
|
2010-10-05 14:58:09 -07:00
|
|
|
|
|
|
|
SkCanvas canvas(rgbaBitmap);
|
|
|
|
canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
|
|
|
|
|
|
|
|
uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height,
|
|
|
|
GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
|
|
|
|
}
|
|
|
|
|
2010-09-22 14:13:32 -07:00
|
|
|
void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
|
|
|
|
GLenum type, const GLvoid * data) {
|
|
|
|
if (resize) {
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
|
|
|
|
} else {
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-29 21:05:21 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|