blob: 34e22658c4be20972023a54195fce30f1ad3203d [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():
Chris Craikd218a922014-01-02 17:13:34 -080037 mCache(LruCache<const 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):
Chris Craikd218a922014-01-02 17:13:34 -080061 mCache(LruCache<const 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
Chris Craikd218a922014-01-02 17:13:34 -0800106void TextureCache::operator()(const SkBitmap*&, 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
John Reck860d1552014-04-11 19:15:05 -0700124void TextureCache::resetMarkInUse() {
125 LruCache<const SkBitmap*, Texture*>::Iterator iter(mCache);
126 while (iter.next()) {
127 iter.value()->isInUse = false;
128 }
129}
130
131bool TextureCache::canMakeTextureFromBitmap(const SkBitmap* bitmap) {
132 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
133 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
134 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
135 return false;
136 }
137 return true;
138}
139
140// Returns a prepared Texture* that either is already in the cache or can fit
141// in the cache (and is thus added to the cache)
142Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap) {
Romain Guyce0537b2010-06-29 21:05:21 -0700143 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700144
Romain Guyce0537b2010-06-29 21:05:21 -0700145 if (!texture) {
John Reck860d1552014-04-11 19:15:05 -0700146 if (!canMakeTextureFromBitmap(bitmap)) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700147 return NULL;
148 }
149
Romain Guy7d139ba2010-07-02 11:20:34 -0700150 const uint32_t size = bitmap->rowBytes() * bitmap->height();
John Reck860d1552014-04-11 19:15:05 -0700151 bool canCache = size < mMaxSize;
Romain Guy121e2242010-07-01 18:26:52 -0700152 // Don't even try to cache a bitmap that's bigger than the cache
John Reck860d1552014-04-11 19:15:05 -0700153 while (canCache && mSize + size > mMaxSize) {
154 Texture* oldest = mCache.peekOldestValue();
155 if (oldest && !oldest->isInUse) {
Romain Guy121e2242010-07-01 18:26:52 -0700156 mCache.removeOldest();
John Reck860d1552014-04-11 19:15:05 -0700157 } else {
158 canCache = false;
Romain Guy121e2242010-07-01 18:26:52 -0700159 }
160 }
161
John Reck860d1552014-04-11 19:15:05 -0700162 if (canCache) {
163 texture = new Texture();
164 texture->bitmapSize = size;
165 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700166
Romain Guy121e2242010-07-01 18:26:52 -0700167 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800168 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
169 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800170 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000171 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800172 }
Romain Guy121e2242010-07-01 18:26:52 -0700173 mCache.put(bitmap, texture);
174 }
John Reck860d1552014-04-11 19:15:05 -0700175 } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) {
176 // Texture was in the cache but is dirty, re-upload
177 // TODO: Re-adjust the cache size if the bitmap's dimensions have changed
Romain Guyfe880942010-06-30 16:05:32 -0700178 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700179 }
Romain Guy22158e12010-08-06 11:18:34 -0700180
Romain Guyce0537b2010-06-29 21:05:21 -0700181 return texture;
182}
183
John Reck860d1552014-04-11 19:15:05 -0700184bool TextureCache::prefetchAndMarkInUse(const SkBitmap* bitmap) {
185 Texture* texture = getCachedTexture(bitmap);
186 if (texture) {
187 texture->isInUse = true;
188 }
189 return texture;
190}
191
192Texture* TextureCache::get(const SkBitmap* bitmap) {
193 Texture* texture = getCachedTexture(bitmap);
194
195 if (!texture) {
196 if (!canMakeTextureFromBitmap(bitmap)) {
197 return NULL;
198 }
199
200 const uint32_t size = bitmap->rowBytes() * bitmap->height();
201 texture = new Texture();
202 texture->bitmapSize = size;
203 generateTexture(bitmap, texture, false);
204 texture->cleanup = true;
205 }
206
207 return texture;
208}
209
Chris Craikd218a922014-01-02 17:13:34 -0800210Texture* TextureCache::getTransient(const SkBitmap* bitmap) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700211 Texture* texture = new Texture();
Romain Guye651cc62012-05-14 19:44:40 -0700212 texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
213 texture->cleanup = true;
214
215 generateTexture(bitmap, texture, false);
216
217 return texture;
218}
219
Chris Craikd218a922014-01-02 17:13:34 -0800220void TextureCache::remove(const SkBitmap* bitmap) {
Romain Guy121e2242010-07-01 18:26:52 -0700221 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700222}
223
Chris Craikd218a922014-01-02 17:13:34 -0800224void TextureCache::removeDeferred(const SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700225 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800226 mGarbage.push(bitmap);
227}
228
229void TextureCache::clearGarbage() {
230 Mutex::Autolock _l(mLock);
231 size_t count = mGarbage.size();
232 for (size_t i = 0; i < count; i++) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900233 const SkBitmap* bitmap = mGarbage.itemAt(i);
234 mCache.remove(bitmap);
235 delete bitmap;
Romain Guyfe48f652010-11-11 15:36:56 -0800236 }
237 mGarbage.clear();
238}
239
240void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700241 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700242 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700243}
244
Romain Guyeca0ca22011-11-04 15:12:29 -0700245void TextureCache::flush() {
246 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
247 if (mFlushRate <= 0.0f) {
248 clear();
249 return;
250 }
251
252 uint32_t targetSize = uint32_t(mSize * mFlushRate);
253 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
254
255 while (mSize > targetSize) {
256 mCache.removeOldest();
257 }
258}
259
Chris Craikd218a922014-01-02 17:13:34 -0800260void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700261 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700262
Romain Guyc1396e92010-06-30 17:56:19 -0700263 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000264 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700265 return;
266 }
267
Romain Guy52439572012-10-17 12:14:11 -0700268 // We could also enable mipmapping if both bitmap dimensions are powers
269 // of 2 but we'd have to deal with size changes. Let's keep this simple
Romain Guy3bbacf22013-02-06 16:51:04 -0800270 const bool canMipMap = Extensions::getInstance().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700271
Romain Guy713e1bb2012-10-16 18:44:09 -0700272 // If the texture had mipmap enabled but not anymore,
273 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700274 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700275 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700276 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700277
Romain Guy8c749f82010-09-22 14:13:32 -0700278 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700279 glGenTextures(1, &texture->id);
280 }
281
Romain Guy8c749f82010-09-22 14:13:32 -0700282 texture->generation = bitmap->getGenerationID();
283 texture->width = bitmap->width();
284 texture->height = bitmap->height();
285
Romain Guy8aa195d2013-06-04 18:00:09 -0700286 Caches::getInstance().bindTexture(texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700287
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -0500288 switch (bitmap->config()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700289 case SkBitmap::kA8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700290 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy318ae7b2013-09-24 18:44:54 -0700291 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(),
292 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy8c749f82010-09-22 14:13:32 -0700293 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700294 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700295 case SkBitmap::kRGB_565_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700296 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy318ae7b2013-09-24 18:44:54 -0700297 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(),
298 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700299 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700300 break;
301 case SkBitmap::kARGB_8888_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700302 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy318ae7b2013-09-24 18:44:54 -0700303 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(),
304 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700305 // Do this after calling getPixels() to make sure Skia's deferred
306 // decoding happened
307 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700308 break;
Romain Guyb37cbec2011-02-24 17:21:29 -0800309 case SkBitmap::kARGB_4444_Config:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700310 case SkBitmap::kIndex8_Config:
Romain Guyd43b22d2012-10-16 11:25:06 -0700311 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy5b3b3522010-10-27 18:57:51 -0700312 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800313 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700314 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700315 default:
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -0500316 ALOGW("Unsupported bitmap config: %d", bitmap->config());
Romain Guyc1396e92010-06-30 17:56:19 -0700317 break;
318 }
Romain Guyce0537b2010-06-29 21:05:21 -0700319
Romain Guy52439572012-10-17 12:14:11 -0700320 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700321 texture->mipMap = bitmap->hasHardwareMipMap();
322 if (texture->mipMap) {
323 glGenerateMipmap(GL_TEXTURE_2D);
324 }
325 }
326
Romain Guyd21b6e12011-11-30 20:21:23 -0800327 if (!regenerate) {
328 texture->setFilter(GL_NEAREST);
329 texture->setWrap(GL_CLAMP_TO_EDGE);
330 }
Romain Guyce0537b2010-06-29 21:05:21 -0700331}
332
Chris Craikd218a922014-01-02 17:13:34 -0800333void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700334 uint32_t width, uint32_t height) {
335 SkBitmap rgbaBitmap;
Leon Scroggins III8790be62013-12-03 16:26:51 -0500336 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, bitmap->alphaType());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700337 rgbaBitmap.allocPixels();
338 rgbaBitmap.eraseColor(0);
339
340 SkCanvas canvas(rgbaBitmap);
341 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
342
Romain Guy318ae7b2013-09-24 18:44:54 -0700343 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), width, height,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700344 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
345}
346
Romain Guy318ae7b2013-09-24 18:44:54 -0700347void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride,
348 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) {
349 // TODO: With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
350 // if the stride doesn't match the width
351 const bool useStride = stride != width && Extensions::getInstance().hasUnpackRowLength();
352 if (useStride) {
353 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
354 }
355
Romain Guy8c749f82010-09-22 14:13:32 -0700356 if (resize) {
357 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
358 } else {
359 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
360 }
Romain Guy318ae7b2013-09-24 18:44:54 -0700361
362 if (useStride) {
363 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
364 }
Romain Guy8c749f82010-09-22 14:13:32 -0700365}
366
Romain Guyce0537b2010-06-29 21:05:21 -0700367}; // namespace uirenderer
368}; // namespace android