blob: 1be6868cc0514b9f7cca2e673f6c7c44c75bad56 [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) {
40 LOGD(" Setting texture cache size to %sMB", property);
41 setMaxSize(MB(atof(property)));
42 } else {
43 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
44 }
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() {
Romain Guy9aaa8262010-09-08 15:15:43 -070056 Mutex::Autolock _l(mLock);
Romain Guyce0537b2010-06-29 21:05:21 -070057 mCache.clear();
58}
59
Romain Guyfb8b7632010-08-23 21:05:08 -070060void TextureCache::init() {
61 mCache.setOnEntryRemovedListener(this);
62
63 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
64 LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080065
66 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070067}
68
Romain Guy121e2242010-07-01 18:26:52 -070069///////////////////////////////////////////////////////////////////////////////
70// Size management
71///////////////////////////////////////////////////////////////////////////////
72
Romain Guy7d139ba2010-07-02 11:20:34 -070073uint32_t TextureCache::getSize() {
Romain Guy9aaa8262010-09-08 15:15:43 -070074 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -070075 return mSize;
76}
77
Romain Guy7d139ba2010-07-02 11:20:34 -070078uint32_t TextureCache::getMaxSize() {
Romain Guy9aaa8262010-09-08 15:15:43 -070079 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -070080 return mMaxSize;
81}
82
Romain Guy7d139ba2010-07-02 11:20:34 -070083void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy9aaa8262010-09-08 15:15:43 -070084 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -070085 mMaxSize = maxSize;
86 while (mSize > mMaxSize) {
87 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070088 }
89}
90
Romain Guy121e2242010-07-01 18:26:52 -070091///////////////////////////////////////////////////////////////////////////////
92// Callbacks
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guydda57022010-07-06 11:39:32 -070095void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070096 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -070097 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070098 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -080099 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
100 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800101 if (mDebugEnabled) {
102 LOGD("Texture deleted, size = %d", texture->bitmapSize);
103 }
Romain Guy121e2242010-07-01 18:26:52 -0700104 glDeleteTextures(1, &texture->id);
105 delete texture;
106 }
107}
108
109///////////////////////////////////////////////////////////////////////////////
110// Caching
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guyce0537b2010-06-29 21:05:21 -0700113Texture* TextureCache::get(SkBitmap* bitmap) {
Romain Guya2341a92010-09-08 18:04:33 -0700114 mLock.lock();
Romain Guyce0537b2010-06-29 21:05:21 -0700115 Texture* texture = mCache.get(bitmap);
Romain Guya2341a92010-09-08 18:04:33 -0700116 mLock.unlock();
117
Romain Guyce0537b2010-06-29 21:05:21 -0700118 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700119 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
120 LOGW("Bitmap too large to be uploaded into a texture");
121 return NULL;
122 }
123
Romain Guy7d139ba2010-07-02 11:20:34 -0700124 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700125 // Don't even try to cache a bitmap that's bigger than the cache
126 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700127 mLock.lock();
Romain Guy121e2242010-07-01 18:26:52 -0700128 while (mSize + size > mMaxSize) {
129 mCache.removeOldest();
130 }
Romain Guya2341a92010-09-08 18:04:33 -0700131 mLock.unlock();
Romain Guy121e2242010-07-01 18:26:52 -0700132 }
133
Romain Guy364703c2010-06-30 15:51:03 -0700134 texture = new Texture;
Romain Guy9aaa8262010-09-08 15:15:43 -0700135 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700136 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700137
138 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700139 mLock.lock();
Romain Guy121e2242010-07-01 18:26:52 -0700140 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800141 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
142 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800143 if (mDebugEnabled) {
144 LOGD("Texture created, size = %d", size);
145 }
Romain Guy121e2242010-07-01 18:26:52 -0700146 mCache.put(bitmap, texture);
Romain Guya2341a92010-09-08 18:04:33 -0700147 mLock.unlock();
Romain Guy22158e12010-08-06 11:18:34 -0700148 } else {
149 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700150 }
Romain Guyfe880942010-06-30 16:05:32 -0700151 } else if (bitmap->getGenerationID() != texture->generation) {
152 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700153 }
Romain Guy22158e12010-08-06 11:18:34 -0700154
Romain Guyce0537b2010-06-29 21:05:21 -0700155 return texture;
156}
157
Romain Guy121e2242010-07-01 18:26:52 -0700158void TextureCache::remove(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700159 Mutex::Autolock _l(mLock);
Romain Guy121e2242010-07-01 18:26:52 -0700160 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700161}
162
163void TextureCache::clear() {
Romain Guy9aaa8262010-09-08 15:15:43 -0700164 Mutex::Autolock _l(mLock);
Romain Guyce0537b2010-06-29 21:05:21 -0700165 mCache.clear();
Chet Haased98aa2d2010-10-25 15:47:32 -0700166 TEXTURE_LOGD("TextureCache:clear(), miSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700167}
168
Romain Guyfe880942010-06-30 16:05:32 -0700169void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700170 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700171
Romain Guyc1396e92010-06-30 17:56:19 -0700172 if (!bitmap->readyToDraw()) {
173 LOGE("Cannot generate texture from bitmap");
174 return;
175 }
176
Romain Guy29d89972010-09-22 16:10:57 -0700177 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
178 bitmap->height() != int(texture->height);
Romain Guyce0537b2010-06-29 21:05:21 -0700179
Romain Guy8c749f82010-09-22 14:13:32 -0700180 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700181 glGenTextures(1, &texture->id);
182 }
183
Romain Guy8c749f82010-09-22 14:13:32 -0700184 texture->generation = bitmap->getGenerationID();
185 texture->width = bitmap->width();
186 texture->height = bitmap->height();
187
Romain Guyce0537b2010-06-29 21:05:21 -0700188 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700189 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
190
191 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700192 case SkBitmap::kA8_Config:
Romain Guy9aaa8262010-09-08 15:15:43 -0700193 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy8c749f82010-09-22 14:13:32 -0700194 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height,
195 GL_UNSIGNED_BYTE, bitmap->getPixels());
196 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700197 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700198 case SkBitmap::kRGB_565_Config:
Romain Guy8c749f82010-09-22 14:13:32 -0700199 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height,
200 GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700201 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700202 break;
203 case SkBitmap::kARGB_8888_Config:
Romain Guy8c749f82010-09-22 14:13:32 -0700204 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height,
205 GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700206 // Do this after calling getPixels() to make sure Skia's deferred
207 // decoding happened
208 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700209 break;
Romain Guy7adaf3d2010-10-05 14:58:09 -0700210 case SkBitmap::kIndex8_Config:
Romain Guy5b3b3522010-10-27 18:57:51 -0700211 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guy7adaf3d2010-10-05 14:58:09 -0700212 texture->blend = false;
213 break;
Romain Guy5b3b3522010-10-27 18:57:51 -0700214 case SkBitmap::kARGB_4444_Config:
215 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
216 texture->blend = true;
217 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700218 default:
Romain Guy7adaf3d2010-10-05 14:58:09 -0700219 LOGW("Unsupported bitmap config: %d", bitmap->getConfig());
Romain Guyc1396e92010-06-30 17:56:19 -0700220 break;
221 }
Romain Guyce0537b2010-06-29 21:05:21 -0700222
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guy8ba548f2010-06-30 19:21:21 -0700225
Romain Guyce0537b2010-06-29 21:05:21 -0700226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700228}
229
Romain Guy5b3b3522010-10-27 18:57:51 -0700230void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700231 uint32_t width, uint32_t height) {
232 SkBitmap rgbaBitmap;
233 rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
234 rgbaBitmap.allocPixels();
235 rgbaBitmap.eraseColor(0);
236
237 SkCanvas canvas(rgbaBitmap);
238 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
239
240 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height,
241 GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
242}
243
Romain Guy8c749f82010-09-22 14:13:32 -0700244void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
245 GLenum type, const GLvoid * data) {
246 if (resize) {
247 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
248 } else {
249 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
250 }
251}
252
Romain Guyce0537b2010-06-29 21:05:21 -0700253}; // namespace uirenderer
254}; // namespace android