blob: e5588708127a8b05894de7f110e9828ef413764c [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 Guy9aaa8262010-09-08 15:15:43 -070021#include <utils/threads.h>
22
Romain Guyce0537b2010-06-29 21:05:21 -070023#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070024#include "Properties.h"
Romain Guyce0537b2010-06-29 21:05:21 -070025
26namespace android {
27namespace uirenderer {
28
Romain Guy121e22422010-07-01 18:26:52 -070029///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guyfb8b7632010-08-23 21:05:08 -070033TextureCache::TextureCache():
34 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
35 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) {
36 char property[PROPERTY_VALUE_MAX];
37 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
38 LOGD(" Setting texture cache size to %sMB", property);
39 setMaxSize(MB(atof(property)));
40 } else {
41 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
42 }
43
44 init();
45}
46
Romain Guy7d139ba2010-07-02 11:20:34 -070047TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070048 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e22422010-07-01 18:26:52 -070049 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070050 init();
Romain Guyce0537b2010-06-29 21:05:21 -070051}
52
53TextureCache::~TextureCache() {
Romain Guy9aaa8262010-09-08 15:15:43 -070054 Mutex::Autolock _l(mLock);
Romain Guyce0537b2010-06-29 21:05:21 -070055 mCache.clear();
56}
57
Romain Guyfb8b7632010-08-23 21:05:08 -070058void TextureCache::init() {
59 mCache.setOnEntryRemovedListener(this);
60
61 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
62 LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
63}
64
Romain Guy121e22422010-07-01 18:26:52 -070065///////////////////////////////////////////////////////////////////////////////
66// Size management
67///////////////////////////////////////////////////////////////////////////////
68
Romain Guy7d139ba2010-07-02 11:20:34 -070069uint32_t TextureCache::getSize() {
Romain Guy9aaa8262010-09-08 15:15:43 -070070 Mutex::Autolock _l(mLock);
Romain Guy121e22422010-07-01 18:26:52 -070071 return mSize;
72}
73
Romain Guy7d139ba2010-07-02 11:20:34 -070074uint32_t TextureCache::getMaxSize() {
Romain Guy9aaa8262010-09-08 15:15:43 -070075 Mutex::Autolock _l(mLock);
Romain Guy121e22422010-07-01 18:26:52 -070076 return mMaxSize;
77}
78
Romain Guy7d139ba2010-07-02 11:20:34 -070079void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy9aaa8262010-09-08 15:15:43 -070080 Mutex::Autolock _l(mLock);
Romain Guy121e22422010-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 Guy121e22422010-07-01 18:26:52 -070087///////////////////////////////////////////////////////////////////////////////
88// Callbacks
89///////////////////////////////////////////////////////////////////////////////
90
Romain Guydda570202010-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 Guy121e22422010-07-01 18:26:52 -070093 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -070094 mSize -= texture->bitmapSize;
Romain Guy121e22422010-07-01 18:26:52 -070095 glDeleteTextures(1, &texture->id);
96 delete texture;
97 }
98}
99
100///////////////////////////////////////////////////////////////////////////////
101// Caching
102///////////////////////////////////////////////////////////////////////////////
103
Romain Guyce0537b2010-06-29 21:05:21 -0700104Texture* TextureCache::get(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700105 Mutex::Autolock _l(mLock);
106
Romain Guyce0537b2010-06-29 21:05:21 -0700107 Texture* texture = mCache.get(bitmap);
108 if (!texture) {
Romain Guy9cccc2b92010-08-07 23:46:15 -0700109 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
110 LOGW("Bitmap too large to be uploaded into a texture");
111 return NULL;
112 }
113
Romain Guy7d139ba2010-07-02 11:20:34 -0700114 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e22422010-07-01 18:26:52 -0700115 // Don't even try to cache a bitmap that's bigger than the cache
116 if (size < mMaxSize) {
117 while (mSize + size > mMaxSize) {
118 mCache.removeOldest();
119 }
120 }
121
Romain Guy364703c2010-06-30 15:51:03 -0700122 texture = new Texture;
Romain Guy9aaa8262010-09-08 15:15:43 -0700123 texture->bitmapSize = size;
Romain Guyc1396e92010-06-30 17:56:19 -0700124 generateTexture(bitmap, texture, false);
Romain Guy121e22422010-07-01 18:26:52 -0700125
126 if (size < mMaxSize) {
127 mSize += size;
128 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700129 } else {
130 texture->cleanup = true;
Romain Guy121e22422010-07-01 18:26:52 -0700131 }
Romain Guyfe880942010-06-30 16:05:32 -0700132 } else if (bitmap->getGenerationID() != texture->generation) {
133 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700134 }
Romain Guy22158e12010-08-06 11:18:34 -0700135
Romain Guyce0537b2010-06-29 21:05:21 -0700136 return texture;
137}
138
Romain Guy121e22422010-07-01 18:26:52 -0700139void TextureCache::remove(SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700140 Mutex::Autolock _l(mLock);
Romain Guy121e22422010-07-01 18:26:52 -0700141 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700142}
143
144void TextureCache::clear() {
Romain Guy9aaa8262010-09-08 15:15:43 -0700145 Mutex::Autolock _l(mLock);
Romain Guyce0537b2010-06-29 21:05:21 -0700146 mCache.clear();
147}
148
Romain Guyfe880942010-06-30 16:05:32 -0700149void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700150 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700151
Romain Guyc1396e92010-06-30 17:56:19 -0700152 if (!bitmap->readyToDraw()) {
153 LOGE("Cannot generate texture from bitmap");
154 return;
155 }
156
Romain Guyfe880942010-06-30 16:05:32 -0700157 if (!regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700158 texture->generation = bitmap->getGenerationID();
Romain Guyfe880942010-06-30 16:05:32 -0700159 texture->width = bitmap->width();
160 texture->height = bitmap->height();
Romain Guyce0537b2010-06-29 21:05:21 -0700161
Romain Guyfe880942010-06-30 16:05:32 -0700162 glGenTextures(1, &texture->id);
163 }
164
Romain Guyce0537b2010-06-29 21:05:21 -0700165 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700166 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
167
168 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700169 case SkBitmap::kA8_Config:
170 texture->blend = true;
Romain Guy9aaa8262010-09-08 15:15:43 -0700171 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guybd0e6aa2010-07-22 18:50:12 -0700172 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0,
173 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels());
174 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700175 case SkBitmap::kRGB_565_Config:
176 texture->blend = false;
177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0,
178 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
179 break;
180 case SkBitmap::kARGB_8888_Config:
Romain Guyc1396e92010-06-30 17:56:19 -0700181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
182 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700183 // Do this after calling getPixels() to make sure Skia's deferred
184 // decoding happened
185 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700186 break;
187 default:
Romain Guy9aaa8262010-09-08 15:15:43 -0700188 LOGW("Unsupported bitmap config");
Romain Guyc1396e92010-06-30 17:56:19 -0700189 break;
190 }
Romain Guyce0537b2010-06-29 21:05:21 -0700191
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guy8ba548f2010-06-30 19:21:21 -0700194
Romain Guyce0537b2010-06-29 21:05:21 -0700195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700197}
198
199}; // namespace uirenderer
200}; // namespace android