blob: 753c5446b5bc47a687f410864f3e74ff28560208 [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
21#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070022#include "Properties.h"
Romain Guyce0537b2010-06-29 21:05:21 -070023
24namespace android {
25namespace uirenderer {
26
Romain Guy121e2242010-07-01 18:26:52 -070027///////////////////////////////////////////////////////////////////////////////
28// Constructors/destructor
29///////////////////////////////////////////////////////////////////////////////
30
Romain Guyfb8b7632010-08-23 21:05:08 -070031TextureCache::TextureCache():
32 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
33 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)) {
34 char property[PROPERTY_VALUE_MAX];
35 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
36 LOGD(" Setting texture cache size to %sMB", property);
37 setMaxSize(MB(atof(property)));
38 } else {
39 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
40 }
41
42 init();
43}
44
Romain Guy7d139ba2010-07-02 11:20:34 -070045TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070046 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e2242010-07-01 18:26:52 -070047 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070048 init();
Romain Guyce0537b2010-06-29 21:05:21 -070049}
50
51TextureCache::~TextureCache() {
52 mCache.clear();
53}
54
Romain Guyfb8b7632010-08-23 21:05:08 -070055void TextureCache::init() {
56 mCache.setOnEntryRemovedListener(this);
57
58 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
59 LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
60}
61
Romain Guy121e2242010-07-01 18:26:52 -070062///////////////////////////////////////////////////////////////////////////////
63// Size management
64///////////////////////////////////////////////////////////////////////////////
65
Romain Guy7d139ba2010-07-02 11:20:34 -070066uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070067 return mSize;
68}
69
Romain Guy7d139ba2010-07-02 11:20:34 -070070uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070071 return mMaxSize;
72}
73
Romain Guy7d139ba2010-07-02 11:20:34 -070074void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070075 mMaxSize = maxSize;
76 while (mSize > mMaxSize) {
77 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070078 }
79}
80
Romain Guy121e2242010-07-01 18:26:52 -070081///////////////////////////////////////////////////////////////////////////////
82// Callbacks
83///////////////////////////////////////////////////////////////////////////////
84
Romain Guydda57022010-07-06 11:39:32 -070085void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy121e2242010-07-01 18:26:52 -070086 if (bitmap) {
Romain Guy7d139ba2010-07-02 11:20:34 -070087 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -070088 mSize -= size;
89 }
90
91 if (texture) {
92 glDeleteTextures(1, &texture->id);
93 delete texture;
94 }
95}
96
97///////////////////////////////////////////////////////////////////////////////
98// Caching
99///////////////////////////////////////////////////////////////////////////////
100
Romain Guyce0537b2010-06-29 21:05:21 -0700101Texture* TextureCache::get(SkBitmap* bitmap) {
102 Texture* texture = mCache.get(bitmap);
103 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700104 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
105 LOGW("Bitmap too large to be uploaded into a texture");
106 return NULL;
107 }
108
Romain Guy7d139ba2010-07-02 11:20:34 -0700109 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -0700110 // Don't even try to cache a bitmap that's bigger than the cache
111 if (size < mMaxSize) {
112 while (mSize + size > mMaxSize) {
113 mCache.removeOldest();
114 }
115 }
116
Romain Guy364703c2010-06-30 15:51:03 -0700117 texture = new Texture;
Romain Guyc1396e92010-06-30 17:56:19 -0700118 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700119
120 if (size < mMaxSize) {
121 mSize += size;
122 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700123 } else {
124 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700125 }
Romain Guyfe880942010-06-30 16:05:32 -0700126 } else if (bitmap->getGenerationID() != texture->generation) {
127 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700128 }
Romain Guy22158e12010-08-06 11:18:34 -0700129
Romain Guyce0537b2010-06-29 21:05:21 -0700130 return texture;
131}
132
Romain Guy121e2242010-07-01 18:26:52 -0700133void TextureCache::remove(SkBitmap* bitmap) {
134 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700135}
136
137void TextureCache::clear() {
138 mCache.clear();
139}
140
Romain Guyfe880942010-06-30 16:05:32 -0700141void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700142 SkAutoLockPixels alp(*bitmap);
143 if (!bitmap->readyToDraw()) {
144 LOGE("Cannot generate texture from bitmap");
145 return;
146 }
147
Romain Guyfe880942010-06-30 16:05:32 -0700148 if (!regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700149 texture->generation = bitmap->getGenerationID();
Romain Guyfe880942010-06-30 16:05:32 -0700150 texture->width = bitmap->width();
151 texture->height = bitmap->height();
Romain Guyce0537b2010-06-29 21:05:21 -0700152
Romain Guyfe880942010-06-30 16:05:32 -0700153 glGenTextures(1, &texture->id);
154 }
155
Romain Guyce0537b2010-06-29 21:05:21 -0700156 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700157 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
158
159 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700160 case SkBitmap::kA8_Config:
161 texture->blend = true;
162 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0,
163 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels());
164 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700165 case SkBitmap::kRGB_565_Config:
166 texture->blend = false;
167 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0,
168 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
169 break;
170 case SkBitmap::kARGB_8888_Config:
Romain Guyc1396e92010-06-30 17:56:19 -0700171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
172 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700173 // Do this after calling getPixels() to make sure Skia's deferred
174 // decoding happened
175 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700176 break;
177 default:
178 break;
179 }
Romain Guyce0537b2010-06-29 21:05:21 -0700180
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guy8ba548f2010-06-30 19:21:21 -0700183
Romain Guyce0537b2010-06-29 21:05:21 -0700184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700186}
187
188}; // namespace uirenderer
189}; // namespace android