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>
|
|
|
|
|
2013-03-08 17:44:20 -08:00
|
|
|
#include <utils/Mutex.h>
|
2010-09-08 15:15:43 -07:00
|
|
|
|
2012-10-16 18:44:09 -07:00
|
|
|
#include "Caches.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():
|
2012-11-28 17:35:51 -08:00
|
|
|
mCache(LruCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
|
2011-11-04 15:12:29 -07:00
|
|
|
mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
|
|
|
|
mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) {
|
2010-08-23 21:05:08 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-11-04 15:12:29 -07:00
|
|
|
if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, NULL) > 0) {
|
|
|
|
float flushRate = atof(property);
|
|
|
|
INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f);
|
|
|
|
setFlushRate(flushRate);
|
|
|
|
} else {
|
|
|
|
INIT_LOGD(" Using default texture cache flush rate of %.2f%%",
|
|
|
|
DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f);
|
|
|
|
}
|
|
|
|
|
2010-08-23 21:05:08 -07:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2010-07-02 11:20:34 -07:00
|
|
|
TextureCache::TextureCache(uint32_t maxByteSize):
|
2012-11-28 17:35:51 -08:00
|
|
|
mCache(LruCache<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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:12:29 -07:00
|
|
|
void TextureCache::setFlushRate(float flushRate) {
|
|
|
|
mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate));
|
|
|
|
}
|
|
|
|
|
2010-07-01 18:26:52 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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) {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Texture deleted, size = %d", texture->bitmapSize);
|
2010-11-10 19:01:29 -08:00
|
|
|
}
|
2013-06-06 14:02:54 -07:00
|
|
|
texture->deleteTexture();
|
2010-07-01 18:26:52 -07:00
|
|
|
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) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
|
2011-12-05 11:53:26 -08:00
|
|
|
bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
|
2010-08-07 23:46:15 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:09 -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) {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Texture created, size = %d", size);
|
2010-11-10 19:01:29 -08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-14 19:44:40 -07:00
|
|
|
Texture* TextureCache::getTransient(SkBitmap* bitmap) {
|
2013-06-04 18:00:09 -07:00
|
|
|
Texture* texture = new Texture();
|
2012-05-14 19:44:40 -07:00
|
|
|
texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
|
|
|
|
texture->cleanup = true;
|
|
|
|
|
|
|
|
generateTexture(bitmap, texture, false);
|
|
|
|
|
|
|
|
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();
|
2011-07-26 18:57:28 -07:00
|
|
|
TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2011-11-04 15:12:29 -07:00
|
|
|
void TextureCache::flush() {
|
|
|
|
if (mFlushRate >= 1.0f || mCache.size() == 0) return;
|
|
|
|
if (mFlushRate <= 0.0f) {
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t targetSize = uint32_t(mSize * mFlushRate);
|
|
|
|
TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
|
|
|
|
|
|
|
|
while (mSize > targetSize) {
|
|
|
|
mCache.removeOldest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Cannot generate texture from bitmap");
|
2010-06-30 17:56:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-17 12:14:11 -07:00
|
|
|
// We could also enable mipmapping if both bitmap dimensions are powers
|
|
|
|
// of 2 but we'd have to deal with size changes. Let's keep this simple
|
2013-02-06 16:51:04 -08:00
|
|
|
const bool canMipMap = Extensions::getInstance().hasNPot();
|
2012-10-17 12:14:11 -07:00
|
|
|
|
2012-10-16 18:44:09 -07:00
|
|
|
// If the texture had mipmap enabled but not anymore,
|
|
|
|
// force a glTexImage2D to discard the mipmap levels
|
2010-09-22 16:10:57 -07:00
|
|
|
const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
|
2012-10-16 18:44:09 -07:00
|
|
|
bitmap->height() != int(texture->height) ||
|
2012-10-17 12:14:11 -07:00
|
|
|
(regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
|
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();
|
|
|
|
|
2013-06-04 18:00:09 -07:00
|
|
|
Caches::getInstance().bindTexture(texture->id);
|
2010-06-29 21:05:21 -07:00
|
|
|
|
|
|
|
switch (bitmap->getConfig()) {
|
2010-07-22 18:50:12 -07:00
|
|
|
case SkBitmap::kA8_Config:
|
2012-10-16 11:25:06 -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:
|
2012-10-16 11:25:06 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
|
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:
|
2012-10-16 11:25:06 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
|
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:
|
2012-10-16 11:25:06 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
|
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:
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Unsupported bitmap config: %d", bitmap->getConfig());
|
2010-06-30 15:51:03 -07:00
|
|
|
break;
|
2010-06-29 21:05:21 -07:00
|
|
|
}
|
|
|
|
|
2012-10-17 12:14:11 -07:00
|
|
|
if (canMipMap) {
|
2012-10-16 18:44:09 -07:00
|
|
|
texture->mipMap = bitmap->hasHardwareMipMap();
|
|
|
|
if (texture->mipMap) {
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 20:21:23 -08:00
|
|
|
if (!regenerate) {
|
|
|
|
texture->setFilter(GL_NEAREST);
|
|
|
|
texture->setWrap(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
|