blob: 2378eb52632d41b6bdaff1f799a5d640fe4e4ba1 [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 Guy121e22422010-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 Guy121e22422010-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 Guy121e22422010-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 Guy121e22422010-07-01 18:26:52 -070079///////////////////////////////////////////////////////////////////////////////
80// Size management
81///////////////////////////////////////////////////////////////////////////////
82
Romain Guy7d139ba2010-07-02 11:20:34 -070083uint32_t TextureCache::getSize() {
Romain Guy121e22422010-07-01 18:26:52 -070084 return mSize;
85}
86
Romain Guy7d139ba2010-07-02 11:20:34 -070087uint32_t TextureCache::getMaxSize() {
Romain Guy121e22422010-07-01 18:26:52 -070088 return mMaxSize;
89}
90
Romain Guy7d139ba2010-07-02 11:20:34 -070091void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e22422010-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 Guy121e22422010-07-01 18:26:52 -0700102///////////////////////////////////////////////////////////////////////////////
103// Callbacks
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guydda570202010-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 Guy121e22422010-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 Guy121e22422010-07-01 18:26:52 -0700115 glDeleteTextures(1, &texture->id);
116 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 Guy121e22422010-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 Guy364703c2010-06-30 15:51:03 -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 Guy121e22422010-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 Guy121e22422010-07-01 18:26:52 -0700153 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700154 } else {
155 texture->cleanup = true;
Romain Guy121e22422010-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) {
165 Texture* texture = new Texture;
166 texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
167 texture->cleanup = true;
168
169 generateTexture(bitmap, texture, false);
170
171 return texture;
172}
173
Romain Guy121e22422010-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++) {
187 mCache.remove(mGarbage.itemAt(i));
188 }
189 mGarbage.clear();
190}
191
192void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700193 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700194 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700195}
196
Romain Guyeca0ca22011-11-04 15:12:29 -0700197void TextureCache::flush() {
198 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
199 if (mFlushRate <= 0.0f) {
200 clear();
201 return;
202 }
203
204 uint32_t targetSize = uint32_t(mSize * mFlushRate);
205 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
206
207 while (mSize > targetSize) {
208 mCache.removeOldest();
209 }
210}
211
Romain Guyfe880942010-06-30 16:05:32 -0700212void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700213 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700214
Romain Guyc1396e92010-06-30 17:56:19 -0700215 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000216 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700217 return;
218 }
219
Romain Guy52439572012-10-17 12:14:11 -0700220 // We could also enable mipmapping if both bitmap dimensions are powers
221 // of 2 but we'd have to deal with size changes. Let's keep this simple
Romain Guy3bbacf22013-02-06 16:51:04 -0800222 const bool canMipMap = Extensions::getInstance().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700223
Romain Guy713e1bb2012-10-16 18:44:09 -0700224 // If the texture had mipmap enabled but not anymore,
225 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700226 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700227 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700228 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700229
Romain Guy8c749f82010-09-22 14:13:32 -0700230 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700231 glGenTextures(1, &texture->id);
232 }
233
Romain Guy8c749f82010-09-22 14:13:32 -0700234 texture->generation = bitmap->getGenerationID();
235 texture->width = bitmap->width();
236 texture->height = bitmap->height();
237
Romain Guyce0537b2010-06-29 21:05:21 -0700238 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700239
240 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700241 case SkBitmap::kA8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700242 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy8c749f82010-09-22 14:13:32 -0700243 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height,
244 GL_UNSIGNED_BYTE, bitmap->getPixels());
245 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700246 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700247 case SkBitmap::kRGB_565_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700248 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy8c749f82010-09-22 14:13:32 -0700249 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height,
250 GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700251 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700252 break;
253 case SkBitmap::kARGB_8888_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700254 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy8c749f82010-09-22 14:13:32 -0700255 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height,
256 GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700257 // Do this after calling getPixels() to make sure Skia's deferred
258 // decoding happened
259 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700260 break;
Romain Guyb37cbec2011-02-24 17:21:29 -0800261 case SkBitmap::kARGB_4444_Config:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700262 case SkBitmap::kIndex8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700263 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy5b3b3522010-10-27 18:57:51 -0700264 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800265 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700266 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700267 default:
Steve Block8564c8d2012-01-05 23:22:43 +0000268 ALOGW("Unsupported bitmap config: %d", bitmap->getConfig());
Romain Guyc1396e92010-06-30 17:56:19 -0700269 break;
270 }
Romain Guyce0537b2010-06-29 21:05:21 -0700271
Romain Guy52439572012-10-17 12:14:11 -0700272 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700273 texture->mipMap = bitmap->hasHardwareMipMap();
274 if (texture->mipMap) {
275 glGenerateMipmap(GL_TEXTURE_2D);
276 }
277 }
278
Romain Guyd21b6e12011-11-30 20:21:23 -0800279 if (!regenerate) {
280 texture->setFilter(GL_NEAREST);
281 texture->setWrap(GL_CLAMP_TO_EDGE);
282 }
Romain Guyce0537b2010-06-29 21:05:21 -0700283}
284
Romain Guy5b3b3522010-10-27 18:57:51 -0700285void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700286 uint32_t width, uint32_t height) {
287 SkBitmap rgbaBitmap;
288 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
289 rgbaBitmap.allocPixels();
290 rgbaBitmap.eraseColor(0);
Romain Guyb37cbec2011-02-24 17:21:29 -0800291 rgbaBitmap.setIsOpaque(bitmap->isOpaque());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700292
293 SkCanvas canvas(rgbaBitmap);
294 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
295
296 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height,
297 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
298}
299
Romain Guy8c749f82010-09-22 14:13:32 -0700300void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
301 GLenum type, const GLvoid * data) {
302 if (resize) {
303 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
304 } else {
305 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
306 }
307}
308
Romain Guyce0537b2010-06-29 21:05:21 -0700309}; // namespace uirenderer
310}; // namespace android