blob: a9ab2c63435b60d513ea1c7edbd4e4e2d31e2d1d [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 Guyca89e2a2013-03-08 17:44:20 -080023#include <utils/Mutex.h>
Romain Guy9aaa8262010-09-08 15:15:43 -070024
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():
Romain Guy059e12c2012-11-28 17:35:51 -080037 mCache(LruCache<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 Guy059e12c2012-11-28 17:35:51 -080061 mCache(LruCache<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 Guyfb8b7632010-08-23 21:05:08 -070077}
78
Romain Guy121e2242010-07-01 18:26:52 -070079///////////////////////////////////////////////////////////////////////////////
80// Size management
81///////////////////////////////////////////////////////////////////////////////
82
Romain Guy7d139ba2010-07-02 11:20:34 -070083uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070084 return mSize;
85}
86
Romain Guy7d139ba2010-07-02 11:20:34 -070087uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070088 return mMaxSize;
89}
90
Romain Guy7d139ba2010-07-02 11:20:34 -070091void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070092 mMaxSize = maxSize;
93 while (mSize > mMaxSize) {
94 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070095 }
96}
97
Romain Guyeca0ca22011-11-04 15:12:29 -070098void TextureCache::setFlushRate(float flushRate) {
99 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate));
100}
101
Romain Guy121e2242010-07-01 18:26:52 -0700102///////////////////////////////////////////////////////////////////////////////
103// Callbacks
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guydda57022010-07-06 11:39:32 -0700106void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700107 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -0700108 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700109 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800110 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
111 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800112 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000113 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800114 }
Romain Guybe1b1272013-06-06 14:02:54 -0700115 texture->deleteTexture();
Romain Guy121e2242010-07-01 18:26:52 -0700116 delete texture;
117 }
118}
119
120///////////////////////////////////////////////////////////////////////////////
121// Caching
122///////////////////////////////////////////////////////////////////////////////
123
Romain Guyce0537b2010-06-29 21:05:21 -0700124Texture* TextureCache::get(SkBitmap* bitmap) {
125 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700126
Romain Guyce0537b2010-06-29 21:05:21 -0700127 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700128 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
Steve Block8564c8d2012-01-05 23:22:43 +0000129 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
Romain Guy8f9a9f62011-12-05 11:53:26 -0800130 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700131 return NULL;
132 }
133
Romain Guy7d139ba2010-07-02 11:20:34 -0700134 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700135 // Don't even try to cache a bitmap that's bigger than the cache
136 if (size < mMaxSize) {
137 while (mSize + size > mMaxSize) {
138 mCache.removeOldest();
139 }
140 }
141
Romain Guy8aa195d2013-06-04 18:00:09 -0700142 texture = new Texture();
Romain Guy9aaa8262010-09-08 15:15:43 -0700143 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700144 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700145
146 if (size < mMaxSize) {
147 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800148 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
149 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800150 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000151 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800152 }
Romain Guy121e2242010-07-01 18:26:52 -0700153 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700154 } else {
155 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700156 }
Romain Guyfe880942010-06-30 16:05:32 -0700157 } else if (bitmap->getGenerationID() != texture->generation) {
158 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700159 }
Romain Guy22158e12010-08-06 11:18:34 -0700160
Romain Guyce0537b2010-06-29 21:05:21 -0700161 return texture;
162}
163
Romain Guye651cc62012-05-14 19:44:40 -0700164Texture* TextureCache::getTransient(SkBitmap* bitmap) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700165 Texture* texture = new Texture();
Romain Guye651cc62012-05-14 19:44:40 -0700166 texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
167 texture->cleanup = true;
168
169 generateTexture(bitmap, texture, false);
170
171 return texture;
172}
173
Romain Guy121e2242010-07-01 18:26:52 -0700174void TextureCache::remove(SkBitmap* bitmap) {
175 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700176}
177
Romain Guyfe48f652010-11-11 15:36:56 -0800178void TextureCache::removeDeferred(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700179 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800180 mGarbage.push(bitmap);
181}
182
183void TextureCache::clearGarbage() {
184 Mutex::Autolock _l(mLock);
185 size_t count = mGarbage.size();
186 for (size_t i = 0; i < count; i++) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900187 const SkBitmap* bitmap = mGarbage.itemAt(i);
188 mCache.remove(bitmap);
189 delete bitmap;
Romain Guyfe48f652010-11-11 15:36:56 -0800190 }
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 Guy52439572012-10-17 12:14:11 -0700222 // We could also enable mipmapping if both bitmap dimensions are powers
223 // of 2 but we'd have to deal with size changes. Let's keep this simple
Romain Guy3bbacf22013-02-06 16:51:04 -0800224 const bool canMipMap = Extensions::getInstance().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700225
Romain Guy713e1bb2012-10-16 18:44:09 -0700226 // If the texture had mipmap enabled but not anymore,
227 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700228 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700229 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700230 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700231
Romain Guy8c749f82010-09-22 14:13:32 -0700232 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700233 glGenTextures(1, &texture->id);
234 }
235
Romain Guy8c749f82010-09-22 14:13:32 -0700236 texture->generation = bitmap->getGenerationID();
237 texture->width = bitmap->width();
238 texture->height = bitmap->height();
239
Romain Guy8aa195d2013-06-04 18:00:09 -0700240 Caches::getInstance().bindTexture(texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700241
242 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700243 case SkBitmap::kA8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700244 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy318ae7b2013-09-24 18:44:54 -0700245 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(),
246 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy8c749f82010-09-22 14:13:32 -0700247 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700248 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700249 case SkBitmap::kRGB_565_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700250 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy318ae7b2013-09-24 18:44:54 -0700251 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(),
252 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700253 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700254 break;
255 case SkBitmap::kARGB_8888_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700256 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy318ae7b2013-09-24 18:44:54 -0700257 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(),
258 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700259 // Do this after calling getPixels() to make sure Skia's deferred
260 // decoding happened
261 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700262 break;
Romain Guyb37cbec2011-02-24 17:21:29 -0800263 case SkBitmap::kARGB_4444_Config:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700264 case SkBitmap::kIndex8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700265 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy5b3b3522010-10-27 18:57:51 -0700266 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800267 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700268 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700269 default:
Steve Block8564c8d2012-01-05 23:22:43 +0000270 ALOGW("Unsupported bitmap config: %d", bitmap->getConfig());
Romain Guyc1396e92010-06-30 17:56:19 -0700271 break;
272 }
Romain Guyce0537b2010-06-29 21:05:21 -0700273
Romain Guy52439572012-10-17 12:14:11 -0700274 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700275 texture->mipMap = bitmap->hasHardwareMipMap();
276 if (texture->mipMap) {
277 glGenerateMipmap(GL_TEXTURE_2D);
278 }
279 }
280
Romain Guyd21b6e12011-11-30 20:21:23 -0800281 if (!regenerate) {
282 texture->setFilter(GL_NEAREST);
283 texture->setWrap(GL_CLAMP_TO_EDGE);
284 }
Romain Guyce0537b2010-06-29 21:05:21 -0700285}
286
Romain Guy5b3b3522010-10-27 18:57:51 -0700287void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700288 uint32_t width, uint32_t height) {
289 SkBitmap rgbaBitmap;
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -0500290 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, bitmap->alphaType());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700291 rgbaBitmap.allocPixels();
292 rgbaBitmap.eraseColor(0);
293
294 SkCanvas canvas(rgbaBitmap);
295 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
296
Romain Guy318ae7b2013-09-24 18:44:54 -0700297 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), width, height,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700298 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
299}
300
Romain Guy318ae7b2013-09-24 18:44:54 -0700301void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride,
302 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) {
303 // TODO: With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
304 // if the stride doesn't match the width
305 const bool useStride = stride != width && Extensions::getInstance().hasUnpackRowLength();
306 if (useStride) {
307 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
308 }
309
Romain Guy8c749f82010-09-22 14:13:32 -0700310 if (resize) {
311 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
312 } else {
313 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
314 }
Romain Guy318ae7b2013-09-24 18:44:54 -0700315
316 if (useStride) {
317 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
318 }
Romain Guy8c749f82010-09-22 14:13:32 -0700319}
320
Romain Guyce0537b2010-06-29 21:05:21 -0700321}; // namespace uirenderer
322}; // namespace android