blob: 237911bebcfa4982aaf331581a91a6ee88a8fe63 [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 Guyce0537b2010-06-29 21:05:21 -070025#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070026#include "Properties.h"
Romain Guyce0537b2010-06-29 21:05:21 -070027
28namespace android {
29namespace uirenderer {
30
Romain Guy121e2242010-07-01 18:26:52 -070031///////////////////////////////////////////////////////////////////////////////
32// Constructors/destructor
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guyfb8b7632010-08-23 21:05:08 -070035TextureCache::TextureCache():
36 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guyeca0ca22011-11-04 15:12:29 -070037 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
38 mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) {
Romain Guyfb8b7632010-08-23 21:05:08 -070039 char property[PROPERTY_VALUE_MAX];
40 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080041 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070042 setMaxSize(MB(atof(property)));
43 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080044 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070045 }
46
Romain Guyeca0ca22011-11-04 15:12:29 -070047 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, NULL) > 0) {
48 float flushRate = atof(property);
49 INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f);
50 setFlushRate(flushRate);
51 } else {
52 INIT_LOGD(" Using default texture cache flush rate of %.2f%%",
53 DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f);
54 }
55
Romain Guyfb8b7632010-08-23 21:05:08 -070056 init();
57}
58
Romain Guy7d139ba2010-07-02 11:20:34 -070059TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070060 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e2242010-07-01 18:26:52 -070061 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070062 init();
Romain Guyce0537b2010-06-29 21:05:21 -070063}
64
65TextureCache::~TextureCache() {
66 mCache.clear();
67}
68
Romain Guyfb8b7632010-08-23 21:05:08 -070069void TextureCache::init() {
70 mCache.setOnEntryRemovedListener(this);
71
72 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080073 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080074
75 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070076}
77
Romain Guy121e2242010-07-01 18:26:52 -070078///////////////////////////////////////////////////////////////////////////////
79// Size management
80///////////////////////////////////////////////////////////////////////////////
81
Romain Guy7d139ba2010-07-02 11:20:34 -070082uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070083 return mSize;
84}
85
Romain Guy7d139ba2010-07-02 11:20:34 -070086uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070087 return mMaxSize;
88}
89
Romain Guy7d139ba2010-07-02 11:20:34 -070090void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070091 mMaxSize = maxSize;
92 while (mSize > mMaxSize) {
93 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070094 }
95}
96
Romain Guyeca0ca22011-11-04 15:12:29 -070097void TextureCache::setFlushRate(float flushRate) {
98 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate));
99}
100
Romain Guy121e2242010-07-01 18:26:52 -0700101///////////////////////////////////////////////////////////////////////////////
102// Callbacks
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guydda57022010-07-06 11:39:32 -0700105void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700106 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -0700107 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700108 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800109 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
110 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800111 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000112 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800113 }
Romain Guy121e2242010-07-01 18:26:52 -0700114 glDeleteTextures(1, &texture->id);
115 delete texture;
116 }
117}
118
119///////////////////////////////////////////////////////////////////////////////
120// Caching
121///////////////////////////////////////////////////////////////////////////////
122
Romain Guyce0537b2010-06-29 21:05:21 -0700123Texture* TextureCache::get(SkBitmap* bitmap) {
124 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700125
Romain Guyce0537b2010-06-29 21:05:21 -0700126 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700127 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
Romain Guy8f9a9f62011-12-05 11:53:26 -0800128 LOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
129 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700130 return NULL;
131 }
132
Romain Guy7d139ba2010-07-02 11:20:34 -0700133 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700134 // Don't even try to cache a bitmap that's bigger than the cache
135 if (size < mMaxSize) {
136 while (mSize + size > mMaxSize) {
137 mCache.removeOldest();
138 }
139 }
140
Romain Guy364703c2010-06-30 15:51:03 -0700141 texture = new Texture;
Romain Guy9aaa8262010-09-08 15:15:43 -0700142 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700143 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700144
145 if (size < mMaxSize) {
146 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800147 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
148 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800149 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000150 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800151 }
Romain Guy121e2242010-07-01 18:26:52 -0700152 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700153 } else {
154 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700155 }
Romain Guyfe880942010-06-30 16:05:32 -0700156 } else if (bitmap->getGenerationID() != texture->generation) {
157 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700158 }
Romain Guy22158e12010-08-06 11:18:34 -0700159
Romain Guyce0537b2010-06-29 21:05:21 -0700160 return texture;
161}
162
Romain Guy121e2242010-07-01 18:26:52 -0700163void TextureCache::remove(SkBitmap* bitmap) {
164 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700165}
166
Romain Guyfe48f652010-11-11 15:36:56 -0800167void TextureCache::removeDeferred(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700168 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800169 mGarbage.push(bitmap);
170}
171
172void TextureCache::clearGarbage() {
173 Mutex::Autolock _l(mLock);
174 size_t count = mGarbage.size();
175 for (size_t i = 0; i < count; i++) {
176 mCache.remove(mGarbage.itemAt(i));
177 }
178 mGarbage.clear();
179}
180
181void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700182 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700183 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700184}
185
Romain Guyeca0ca22011-11-04 15:12:29 -0700186void TextureCache::flush() {
187 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
188 if (mFlushRate <= 0.0f) {
189 clear();
190 return;
191 }
192
193 uint32_t targetSize = uint32_t(mSize * mFlushRate);
194 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
195
196 while (mSize > targetSize) {
197 mCache.removeOldest();
198 }
199}
200
Romain Guyfe880942010-06-30 16:05:32 -0700201void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700202 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700203
Romain Guyc1396e92010-06-30 17:56:19 -0700204 if (!bitmap->readyToDraw()) {
205 LOGE("Cannot generate texture from bitmap");
206 return;
207 }
208
Romain Guy29d89972010-09-22 16:10:57 -0700209 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
210 bitmap->height() != int(texture->height);
Romain Guyce0537b2010-06-29 21:05:21 -0700211
Romain Guy8c749f82010-09-22 14:13:32 -0700212 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700213 glGenTextures(1, &texture->id);
214 }
215
Romain Guy8c749f82010-09-22 14:13:32 -0700216 texture->generation = bitmap->getGenerationID();
217 texture->width = bitmap->width();
218 texture->height = bitmap->height();
219
Romain Guyce0537b2010-06-29 21:05:21 -0700220 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -0800221 if (!regenerate) {
222 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
223 }
Romain Guyc1396e92010-06-30 17:56:19 -0700224
225 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700226 case SkBitmap::kA8_Config:
Romain Guyd21b6e12011-11-30 20:21:23 -0800227 if (!regenerate) {
228 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
229 }
Romain Guy8c749f82010-09-22 14:13:32 -0700230 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height,
231 GL_UNSIGNED_BYTE, bitmap->getPixels());
232 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700233 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700234 case SkBitmap::kRGB_565_Config:
Romain Guy8c749f82010-09-22 14:13:32 -0700235 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height,
236 GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700237 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700238 break;
239 case SkBitmap::kARGB_8888_Config:
Romain Guy8c749f82010-09-22 14:13:32 -0700240 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height,
241 GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700242 // Do this after calling getPixels() to make sure Skia's deferred
243 // decoding happened
244 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700245 break;
Romain Guyb37cbec2011-02-24 17:21:29 -0800246 case SkBitmap::kARGB_4444_Config:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700247 case SkBitmap::kIndex8_Config:
Romain Guy5b3b3522010-10-27 18:57:51 -0700248 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800249 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700250 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700251 default:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700252 LOGW("Unsupported bitmap config: %d", bitmap->getConfig());
Romain Guyc1396e92010-06-30 17:56:19 -0700253 break;
254 }
Romain Guyce0537b2010-06-29 21:05:21 -0700255
Romain Guyd21b6e12011-11-30 20:21:23 -0800256 if (!regenerate) {
257 texture->setFilter(GL_NEAREST);
258 texture->setWrap(GL_CLAMP_TO_EDGE);
259 }
Romain Guyce0537b2010-06-29 21:05:21 -0700260}
261
Romain Guy5b3b3522010-10-27 18:57:51 -0700262void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700263 uint32_t width, uint32_t height) {
264 SkBitmap rgbaBitmap;
265 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
266 rgbaBitmap.allocPixels();
267 rgbaBitmap.eraseColor(0);
Romain Guyb37cbec2011-02-24 17:21:29 -0800268 rgbaBitmap.setIsOpaque(bitmap->isOpaque());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700269
270 SkCanvas canvas(rgbaBitmap);
271 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
272
273 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height,
274 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
275}
276
Romain Guy8c749f82010-09-22 14:13:32 -0700277void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
278 GLenum type, const GLvoid * data) {
279 if (resize) {
280 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
281 } else {
282 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
283 }
284}
285
Romain Guyce0537b2010-06-29 21:05:21 -0700286}; // namespace uirenderer
287}; // namespace android