blob: 927070a54ec5a9fe5e27994da8db101b1cb3c17e [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
Romain Guy121e2242010-07-01 18:26:52 -070017#define LOG_TAG "OpenGLRenderer"
18
Romain Guyce0537b2010-06-29 21:05:21 -070019#include <GLES2/gl2.h>
20
Romain Guy9aaa8262010-09-08 15:15:43 -070021#include <utils/threads.h>
22
Romain Guyce0537b2010-06-29 21:05:21 -070023#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070024#include "Properties.h"
Romain Guyce0537b2010-06-29 21:05:21 -070025
26namespace android {
27namespace uirenderer {
28
Romain Guy121e2242010-07-01 18:26:52 -070029///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guyfb8b7632010-08-23 21:05:08 -070033TextureCache::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
Romain Guy7d139ba2010-07-02 11:20:34 -070047TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070048 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e2242010-07-01 18:26:52 -070049 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070050 init();
Romain Guyce0537b2010-06-29 21:05:21 -070051}
52
53TextureCache::~TextureCache() {
Romain Guy9aaa8262010-09-08 15:15:43 -070054 Mutex::Autolock _l(mLock);
Romain Guyce0537b2010-06-29 21:05:21 -070055 mCache.clear();
56}
57
Romain Guyfb8b7632010-08-23 21:05:08 -070058void 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
Romain Guy121e2242010-07-01 18:26:52 -070065///////////////////////////////////////////////////////////////////////////////
66// Size management
67///////////////////////////////////////////////////////////////////////////////
68
Romain Guy7d139ba2010-07-02 11:20:34 -070069uint32_t TextureCache::getSize() {
Romain Guy9aaa8262010-09-08 15:15:43 -070070 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -070071 return mSize;
72}
73
Romain Guy7d139ba2010-07-02 11:20:34 -070074uint32_t TextureCache::getMaxSize() {
Romain Guy9aaa8262010-09-08 15:15:43 -070075 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -070076 return mMaxSize;
77}
78
Romain Guy7d139ba2010-07-02 11:20:34 -070079void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy9aaa8262010-09-08 15:15:43 -070080 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -070081 mMaxSize = maxSize;
82 while (mSize > mMaxSize) {
83 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070084 }
85}
86
Romain Guy121e2242010-07-01 18:26:52 -070087///////////////////////////////////////////////////////////////////////////////
88// Callbacks
89///////////////////////////////////////////////////////////////////////////////
90
Romain Guydda57022010-07-06 11:39:32 -070091void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070092 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -070093 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070094 mSize -= texture->bitmapSize;
Romain Guy121e2242010-07-01 18:26:52 -070095 glDeleteTextures(1, &texture->id);
96 delete texture;
97 }
98}
99
100///////////////////////////////////////////////////////////////////////////////
101// Caching
102///////////////////////////////////////////////////////////////////////////////
103
Romain Guyce0537b2010-06-29 21:05:21 -0700104Texture* TextureCache::get(SkBitmap* bitmap) {
Romain Guya2341a92010-09-08 18:04:33 -0700105 mLock.lock();
Romain Guyce0537b2010-06-29 21:05:21 -0700106 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700107 mLock.unlock();
108
Romain Guyce0537b2010-06-29 21:05:21 -0700109 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700110 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
111 LOGW("Bitmap too large to be uploaded into a texture");
112 return NULL;
113 }
114
Romain Guy7d139ba2010-07-02 11:20:34 -0700115 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700116 // Don't even try to cache a bitmap that's bigger than the cache
117 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700118 mLock.lock();
Romain Guy121e2242010-07-01 18:26:52 -0700119 while (mSize + size > mMaxSize) {
120 mCache.removeOldest();
121 }
Romain Guya2341a92010-09-08 18:04:33 -0700122 mLock.unlock();
Romain Guy121e2242010-07-01 18:26:52 -0700123 }
124
Romain Guy364703c2010-06-30 15:51:03 -0700125 texture = new Texture;
Romain Guy9aaa8262010-09-08 15:15:43 -0700126 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700127 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700128
129 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700130 mLock.lock();
Romain Guy121e2242010-07-01 18:26:52 -0700131 mSize += size;
132 mCache.put(bitmap, texture);
Romain Guya2341a92010-09-08 18:04:33 -0700133 mLock.unlock();
Romain Guy22158e12010-08-06 11:18:34 -0700134 } else {
135 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700136 }
Romain Guyfe880942010-06-30 16:05:32 -0700137 } else if (bitmap->getGenerationID() != texture->generation) {
138 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700139 }
Romain Guy22158e12010-08-06 11:18:34 -0700140
Romain Guyce0537b2010-06-29 21:05:21 -0700141 return texture;
142}
143
Romain Guy121e2242010-07-01 18:26:52 -0700144void TextureCache::remove(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700145 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -0700146 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700147}
148
149void TextureCache::clear() {
Romain Guy9aaa8262010-09-08 15:15:43 -0700150 Mutex::Autolock _l(mLock);
Romain Guyce0537b2010-06-29 21:05:21 -0700151 mCache.clear();
152}
153
Romain Guyfe880942010-06-30 16:05:32 -0700154void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700155 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700156
Romain Guyc1396e92010-06-30 17:56:19 -0700157 if (!bitmap->readyToDraw()) {
158 LOGE("Cannot generate texture from bitmap");
159 return;
160 }
161
Romain Guyfe880942010-06-30 16:05:32 -0700162 if (!regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700163 texture->generation = bitmap->getGenerationID();
Romain Guyfe880942010-06-30 16:05:32 -0700164 texture->width = bitmap->width();
165 texture->height = bitmap->height();
Romain Guyce0537b2010-06-29 21:05:21 -0700166
Romain Guyfe880942010-06-30 16:05:32 -0700167 glGenTextures(1, &texture->id);
168 }
169
Romain Guyce0537b2010-06-29 21:05:21 -0700170 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700171 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
172
173 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700174 case SkBitmap::kA8_Config:
175 texture->blend = true;
Romain Guy9aaa8262010-09-08 15:15:43 -0700176 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guybd0e6aa2010-07-22 18:50:12 -0700177 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0,
178 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels());
179 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700180 case SkBitmap::kRGB_565_Config:
181 texture->blend = false;
182 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0,
183 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
184 break;
185 case SkBitmap::kARGB_8888_Config:
Romain Guyc1396e92010-06-30 17:56:19 -0700186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
187 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700188 // Do this after calling getPixels() to make sure Skia's deferred
189 // decoding happened
190 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700191 break;
192 default:
Romain Guy9aaa8262010-09-08 15:15:43 -0700193 LOGW("Unsupported bitmap config");
Romain Guyc1396e92010-06-30 17:56:19 -0700194 break;
195 }
Romain Guyce0537b2010-06-29 21:05:21 -0700196
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guy8ba548f2010-06-30 19:21:21 -0700199
Romain Guyce0537b2010-06-29 21:05:21 -0700200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700202}
203
204}; // namespace uirenderer
205}; // namespace android