blob: 431367a34354efa93d1c14d7cd592909df3d6508 [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 Guy7adaf3d2010-10-05 14:58:09 -070021#include <SkCanvas.h>
22
Romain Guy9aaa8262010-09-08 15:15:43 -070023#include <utils/threads.h>
24
Romain Guy713e1bb2012-10-16 18:44:09 -070025#include "Caches.h"
Romain Guyce0537b2010-06-29 21:05:21 -070026#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070027#include "Properties.h"
Romain Guyce0537b2010-06-29 21:05:21 -070028
29namespace android {
30namespace uirenderer {
31
Romain Guy121e2242010-07-01 18:26:52 -070032///////////////////////////////////////////////////////////////////////////////
33// Constructors/destructor
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guyfb8b7632010-08-23 21:05:08 -070036TextureCache::TextureCache():
37 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guyeca0ca22011-11-04 15:12:29 -070038 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
39 mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) {
Romain Guyfb8b7632010-08-23 21:05:08 -070040 char property[PROPERTY_VALUE_MAX];
41 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080042 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070043 setMaxSize(MB(atof(property)));
44 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080045 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070046 }
47
Romain Guyeca0ca22011-11-04 15:12:29 -070048 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, NULL) > 0) {
49 float flushRate = atof(property);
50 INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f);
51 setFlushRate(flushRate);
52 } else {
53 INIT_LOGD(" Using default texture cache flush rate of %.2f%%",
54 DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f);
55 }
56
Romain Guyfb8b7632010-08-23 21:05:08 -070057 init();
58}
59
Romain Guy7d139ba2010-07-02 11:20:34 -070060TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070061 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e2242010-07-01 18:26:52 -070062 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070063 init();
Romain Guyce0537b2010-06-29 21:05:21 -070064}
65
66TextureCache::~TextureCache() {
67 mCache.clear();
68}
69
Romain Guyfb8b7632010-08-23 21:05:08 -070070void TextureCache::init() {
71 mCache.setOnEntryRemovedListener(this);
72
73 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080074 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080075
76 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guy713e1bb2012-10-16 18:44:09 -070077
78 mHasNPot = Caches::getInstance().extensions.hasNPot();
Romain Guyfb8b7632010-08-23 21:05:08 -070079}
80
Romain Guy121e2242010-07-01 18:26:52 -070081///////////////////////////////////////////////////////////////////////////////
82// Size management
83///////////////////////////////////////////////////////////////////////////////
84
Romain Guy7d139ba2010-07-02 11:20:34 -070085uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070086 return mSize;
87}
88
Romain Guy7d139ba2010-07-02 11:20:34 -070089uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070090 return mMaxSize;
91}
92
Romain Guy7d139ba2010-07-02 11:20:34 -070093void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070094 mMaxSize = maxSize;
95 while (mSize > mMaxSize) {
96 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070097 }
98}
99
Romain Guyeca0ca22011-11-04 15:12:29 -0700100void TextureCache::setFlushRate(float flushRate) {
101 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate));
102}
103
Romain Guy121e2242010-07-01 18:26:52 -0700104///////////////////////////////////////////////////////////////////////////////
105// Callbacks
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guydda57022010-07-06 11:39:32 -0700108void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700109 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -0700110 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700111 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800112 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
113 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800114 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000115 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800116 }
Romain Guy121e2242010-07-01 18:26:52 -0700117 glDeleteTextures(1, &texture->id);
118 delete texture;
119 }
120}
121
122///////////////////////////////////////////////////////////////////////////////
123// Caching
124///////////////////////////////////////////////////////////////////////////////
125
Romain Guyce0537b2010-06-29 21:05:21 -0700126Texture* TextureCache::get(SkBitmap* bitmap) {
127 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700128
Romain Guyce0537b2010-06-29 21:05:21 -0700129 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700130 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
Steve Block8564c8d2012-01-05 23:22:43 +0000131 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
Romain Guy8f9a9f62011-12-05 11:53:26 -0800132 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700133 return NULL;
134 }
135
Romain Guy7d139ba2010-07-02 11:20:34 -0700136 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700137 // Don't even try to cache a bitmap that's bigger than the cache
138 if (size < mMaxSize) {
139 while (mSize + size > mMaxSize) {
140 mCache.removeOldest();
141 }
142 }
143
Romain Guy364703c2010-06-30 15:51:03 -0700144 texture = new Texture;
Romain Guy9aaa8262010-09-08 15:15:43 -0700145 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700146 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700147
148 if (size < mMaxSize) {
149 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800150 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
151 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800152 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000153 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800154 }
Romain Guy121e2242010-07-01 18:26:52 -0700155 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700156 } else {
157 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700158 }
Romain Guyfe880942010-06-30 16:05:32 -0700159 } else if (bitmap->getGenerationID() != texture->generation) {
160 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700161 }
Romain Guy22158e12010-08-06 11:18:34 -0700162
Romain Guyce0537b2010-06-29 21:05:21 -0700163 return texture;
164}
165
Romain Guye651cc62012-05-14 19:44:40 -0700166Texture* TextureCache::getTransient(SkBitmap* bitmap) {
167 Texture* texture = new Texture;
168 texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
169 texture->cleanup = true;
170
171 generateTexture(bitmap, texture, false);
172
173 return texture;
174}
175
Romain Guy121e2242010-07-01 18:26:52 -0700176void TextureCache::remove(SkBitmap* bitmap) {
177 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700178}
179
Romain Guyfe48f652010-11-11 15:36:56 -0800180void TextureCache::removeDeferred(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700181 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800182 mGarbage.push(bitmap);
183}
184
185void TextureCache::clearGarbage() {
186 Mutex::Autolock _l(mLock);
187 size_t count = mGarbage.size();
188 for (size_t i = 0; i < count; i++) {
189 mCache.remove(mGarbage.itemAt(i));
190 }
191 mGarbage.clear();
192}
193
194void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700195 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700196 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700197}
198
Romain Guyeca0ca22011-11-04 15:12:29 -0700199void TextureCache::flush() {
200 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
201 if (mFlushRate <= 0.0f) {
202 clear();
203 return;
204 }
205
206 uint32_t targetSize = uint32_t(mSize * mFlushRate);
207 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
208
209 while (mSize > targetSize) {
210 mCache.removeOldest();
211 }
212}
213
Romain Guyfe880942010-06-30 16:05:32 -0700214void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700215 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700216
Romain Guyc1396e92010-06-30 17:56:19 -0700217 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000218 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700219 return;
220 }
221
Romain Guy713e1bb2012-10-16 18:44:09 -0700222 // If the texture had mipmap enabled but not anymore,
223 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700224 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700225 bitmap->height() != int(texture->height) ||
226 (regenerate && mHasNPot && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700227
Romain Guy8c749f82010-09-22 14:13:32 -0700228 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700229 glGenTextures(1, &texture->id);
230 }
231
Romain Guy8c749f82010-09-22 14:13:32 -0700232 texture->generation = bitmap->getGenerationID();
233 texture->width = bitmap->width();
234 texture->height = bitmap->height();
235
Romain Guyce0537b2010-06-29 21:05:21 -0700236 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700237
238 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700239 case SkBitmap::kA8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700240 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy8c749f82010-09-22 14:13:32 -0700241 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height,
242 GL_UNSIGNED_BYTE, bitmap->getPixels());
243 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700244 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700245 case SkBitmap::kRGB_565_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700246 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy8c749f82010-09-22 14:13:32 -0700247 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height,
248 GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700249 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700250 break;
251 case SkBitmap::kARGB_8888_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700252 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy8c749f82010-09-22 14:13:32 -0700253 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height,
254 GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700255 // Do this after calling getPixels() to make sure Skia's deferred
256 // decoding happened
257 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700258 break;
Romain Guyb37cbec2011-02-24 17:21:29 -0800259 case SkBitmap::kARGB_4444_Config:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700260 case SkBitmap::kIndex8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700261 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy5b3b3522010-10-27 18:57:51 -0700262 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800263 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700264 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700265 default:
Steve Block8564c8d2012-01-05 23:22:43 +0000266 ALOGW("Unsupported bitmap config: %d", bitmap->getConfig());
Romain Guyc1396e92010-06-30 17:56:19 -0700267 break;
268 }
Romain Guyce0537b2010-06-29 21:05:21 -0700269
Romain Guy713e1bb2012-10-16 18:44:09 -0700270 if (mHasNPot) {
271 texture->mipMap = bitmap->hasHardwareMipMap();
272 if (texture->mipMap) {
273 glGenerateMipmap(GL_TEXTURE_2D);
274 }
275 }
276
Romain Guyd21b6e12011-11-30 20:21:23 -0800277 if (!regenerate) {
278 texture->setFilter(GL_NEAREST);
279 texture->setWrap(GL_CLAMP_TO_EDGE);
280 }
Romain Guyce0537b2010-06-29 21:05:21 -0700281}
282
Romain Guy5b3b3522010-10-27 18:57:51 -0700283void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700284 uint32_t width, uint32_t height) {
285 SkBitmap rgbaBitmap;
286 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
287 rgbaBitmap.allocPixels();
288 rgbaBitmap.eraseColor(0);
Romain Guyb37cbec2011-02-24 17:21:29 -0800289 rgbaBitmap.setIsOpaque(bitmap->isOpaque());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700290
291 SkCanvas canvas(rgbaBitmap);
292 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
293
294 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height,
295 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
296}
297
Romain Guy8c749f82010-09-22 14:13:32 -0700298void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
299 GLenum type, const GLvoid * data) {
300 if (resize) {
301 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
302 } else {
303 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
304 }
305}
306
Romain Guyce0537b2010-06-29 21:05:21 -0700307}; // namespace uirenderer
308}; // namespace android