blob: fbdbf922895f8519a743a2c07e451cc07e0de641 [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),
37 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) {
38 char property[PROPERTY_VALUE_MAX];
39 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080040 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070041 setMaxSize(MB(atof(property)));
42 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080043 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070044 }
45
46 init();
47}
48
Romain Guy7d139ba2010-07-02 11:20:34 -070049TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070050 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e2242010-07-01 18:26:52 -070051 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070052 init();
Romain Guyce0537b2010-06-29 21:05:21 -070053}
54
55TextureCache::~TextureCache() {
56 mCache.clear();
57}
58
Romain Guyfb8b7632010-08-23 21:05:08 -070059void TextureCache::init() {
60 mCache.setOnEntryRemovedListener(this);
61
62 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080063 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080064
65 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070066}
67
Romain Guy121e2242010-07-01 18:26:52 -070068///////////////////////////////////////////////////////////////////////////////
69// Size management
70///////////////////////////////////////////////////////////////////////////////
71
Romain Guy7d139ba2010-07-02 11:20:34 -070072uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070073 return mSize;
74}
75
Romain Guy7d139ba2010-07-02 11:20:34 -070076uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070077 return mMaxSize;
78}
79
Romain Guy7d139ba2010-07-02 11:20:34 -070080void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070081 mMaxSize = maxSize;
82 while (mSize > mMaxSize) {
83 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070084 }
85}
86
Romain Guy121e2242010-07-01 18:26:52 -070087///////////////////////////////////////////////////////////////////////////////
88// Callbacks
89///////////////////////////////////////////////////////////////////////////////
90
Romain Guydda57022010-07-06 11:39:32 -070091void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070092 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -070093 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070094 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -080095 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
96 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -080097 if (mDebugEnabled) {
98 LOGD("Texture deleted, size = %d", texture->bitmapSize);
99 }
Romain Guy121e2242010-07-01 18:26:52 -0700100 glDeleteTextures(1, &texture->id);
101 delete texture;
102 }
103}
104
105///////////////////////////////////////////////////////////////////////////////
106// Caching
107///////////////////////////////////////////////////////////////////////////////
108
Romain Guyce0537b2010-06-29 21:05:21 -0700109Texture* TextureCache::get(SkBitmap* bitmap) {
110 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700111
Romain Guyce0537b2010-06-29 21:05:21 -0700112 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700113 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
114 LOGW("Bitmap too large to be uploaded into a texture");
115 return NULL;
116 }
117
Romain Guy7d139ba2010-07-02 11:20:34 -0700118 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700119 // Don't even try to cache a bitmap that's bigger than the cache
120 if (size < mMaxSize) {
121 while (mSize + size > mMaxSize) {
122 mCache.removeOldest();
123 }
124 }
125
Romain Guy364703c2010-06-30 15:51:03 -0700126 texture = new Texture;
Romain Guy9aaa8262010-09-08 15:15:43 -0700127 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700128 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700129
130 if (size < mMaxSize) {
131 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800132 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
133 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800134 if (mDebugEnabled) {
135 LOGD("Texture created, size = %d", size);
136 }
Romain Guy121e2242010-07-01 18:26:52 -0700137 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700138 } else {
139 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700140 }
Romain Guyfe880942010-06-30 16:05:32 -0700141 } else if (bitmap->getGenerationID() != texture->generation) {
142 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700143 }
Romain Guy22158e12010-08-06 11:18:34 -0700144
Romain Guyce0537b2010-06-29 21:05:21 -0700145 return texture;
146}
147
Romain Guy121e2242010-07-01 18:26:52 -0700148void TextureCache::remove(SkBitmap* bitmap) {
149 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700150}
151
Romain Guyfe48f652010-11-11 15:36:56 -0800152void TextureCache::removeDeferred(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700153 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800154 mGarbage.push(bitmap);
155}
156
157void TextureCache::clearGarbage() {
158 Mutex::Autolock _l(mLock);
159 size_t count = mGarbage.size();
160 for (size_t i = 0; i < count; i++) {
161 mCache.remove(mGarbage.itemAt(i));
162 }
163 mGarbage.clear();
164}
165
166void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700167 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700168 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700169}
170
Romain Guyfe880942010-06-30 16:05:32 -0700171void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700172 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700173
Romain Guyc1396e92010-06-30 17:56:19 -0700174 if (!bitmap->readyToDraw()) {
175 LOGE("Cannot generate texture from bitmap");
176 return;
177 }
178
Romain Guy29d89972010-09-22 16:10:57 -0700179 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
180 bitmap->height() != int(texture->height);
Romain Guyce0537b2010-06-29 21:05:21 -0700181
Romain Guy8c749f82010-09-22 14:13:32 -0700182 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700183 glGenTextures(1, &texture->id);
184 }
185
Romain Guy8c749f82010-09-22 14:13:32 -0700186 texture->generation = bitmap->getGenerationID();
187 texture->width = bitmap->width();
188 texture->height = bitmap->height();
189
Romain Guyce0537b2010-06-29 21:05:21 -0700190 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700191 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
192
193 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700194 case SkBitmap::kA8_Config:
Romain Guy9aaa8262010-09-08 15:15:43 -0700195 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy8c749f82010-09-22 14:13:32 -0700196 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height,
197 GL_UNSIGNED_BYTE, bitmap->getPixels());
198 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700199 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700200 case SkBitmap::kRGB_565_Config:
Romain Guy8c749f82010-09-22 14:13:32 -0700201 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height,
202 GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700203 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700204 break;
205 case SkBitmap::kARGB_8888_Config:
Romain Guy8c749f82010-09-22 14:13:32 -0700206 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height,
207 GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700208 // Do this after calling getPixels() to make sure Skia's deferred
209 // decoding happened
210 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700211 break;
Romain Guyb37cbec2011-02-24 17:21:29 -0800212 case SkBitmap::kARGB_4444_Config:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700213 case SkBitmap::kIndex8_Config:
Romain Guy5b3b3522010-10-27 18:57:51 -0700214 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800215 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700216 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700217 default:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700218 LOGW("Unsupported bitmap config: %d", bitmap->getConfig());
Romain Guyc1396e92010-06-30 17:56:19 -0700219 break;
220 }
Romain Guyce0537b2010-06-29 21:05:21 -0700221
Romain Guye3c26852011-07-25 16:36:01 -0700222 texture->setFilter(GL_LINEAR, GL_LINEAR);
223 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700224}
225
Romain Guy5b3b3522010-10-27 18:57:51 -0700226void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700227 uint32_t width, uint32_t height) {
228 SkBitmap rgbaBitmap;
229 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
230 rgbaBitmap.allocPixels();
231 rgbaBitmap.eraseColor(0);
Romain Guyb37cbec2011-02-24 17:21:29 -0800232 rgbaBitmap.setIsOpaque(bitmap->isOpaque());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700233
234 SkCanvas canvas(rgbaBitmap);
235 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
236
237 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height,
238 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
239}
240
Romain Guy8c749f82010-09-22 14:13:32 -0700241void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
242 GLenum type, const GLvoid * data) {
243 if (resize) {
244 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
245 } else {
246 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
247 }
248}
249
Romain Guyce0537b2010-06-29 21:05:21 -0700250}; // namespace uirenderer
251}; // namespace android