blob: 2e8a8be3cf86bed801f478bd5d3b0a9933c17922 [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"
22
23namespace android {
24namespace uirenderer {
25
Romain Guy121e2242010-07-01 18:26:52 -070026///////////////////////////////////////////////////////////////////////////////
27// Constructors/destructor
28///////////////////////////////////////////////////////////////////////////////
29
Romain Guy7d139ba2010-07-02 11:20:34 -070030TextureCache::TextureCache(uint32_t maxByteSize):
Romain Guy6c818932010-07-07 15:15:32 -070031 mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity),
Romain Guy121e2242010-07-01 18:26:52 -070032 mSize(0), mMaxSize(maxByteSize) {
Romain Guyce0537b2010-06-29 21:05:21 -070033 mCache.setOnEntryRemovedListener(this);
Romain Guy9cccc2b2010-08-07 23:46:15 -070034
Romain Guy16393512010-08-08 00:14:31 -070035 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
36 LOGD("Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guyce0537b2010-06-29 21:05:21 -070037}
38
39TextureCache::~TextureCache() {
40 mCache.clear();
41}
42
Romain Guy121e2242010-07-01 18:26:52 -070043///////////////////////////////////////////////////////////////////////////////
44// Size management
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy7d139ba2010-07-02 11:20:34 -070047uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070048 return mSize;
49}
50
Romain Guy7d139ba2010-07-02 11:20:34 -070051uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070052 return mMaxSize;
53}
54
Romain Guy7d139ba2010-07-02 11:20:34 -070055void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070056 mMaxSize = maxSize;
57 while (mSize > mMaxSize) {
58 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070059 }
60}
61
Romain Guy121e2242010-07-01 18:26:52 -070062///////////////////////////////////////////////////////////////////////////////
63// Callbacks
64///////////////////////////////////////////////////////////////////////////////
65
Romain Guydda57022010-07-06 11:39:32 -070066void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy121e2242010-07-01 18:26:52 -070067 if (bitmap) {
Romain Guy7d139ba2010-07-02 11:20:34 -070068 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -070069 mSize -= size;
70 }
71
72 if (texture) {
73 glDeleteTextures(1, &texture->id);
74 delete texture;
75 }
76}
77
78///////////////////////////////////////////////////////////////////////////////
79// Caching
80///////////////////////////////////////////////////////////////////////////////
81
Romain Guyce0537b2010-06-29 21:05:21 -070082Texture* TextureCache::get(SkBitmap* bitmap) {
83 Texture* texture = mCache.get(bitmap);
84 if (!texture) {
Romain Guy9cccc2b2010-08-07 23:46:15 -070085 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
86 LOGW("Bitmap too large to be uploaded into a texture");
87 return NULL;
88 }
89
Romain Guy7d139ba2010-07-02 11:20:34 -070090 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -070091 // Don't even try to cache a bitmap that's bigger than the cache
92 if (size < mMaxSize) {
93 while (mSize + size > mMaxSize) {
94 mCache.removeOldest();
95 }
96 }
97
Romain Guy364703c2010-06-30 15:51:03 -070098 texture = new Texture;
Romain Guyc1396e92010-06-30 17:56:19 -070099 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700100
101 if (size < mMaxSize) {
102 mSize += size;
103 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700104 } else {
105 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -0700106 }
Romain Guyfe880942010-06-30 16:05:32 -0700107 } else if (bitmap->getGenerationID() != texture->generation) {
108 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700109 }
Romain Guy22158e12010-08-06 11:18:34 -0700110
Romain Guyce0537b2010-06-29 21:05:21 -0700111 return texture;
112}
113
Romain Guy121e2242010-07-01 18:26:52 -0700114void TextureCache::remove(SkBitmap* bitmap) {
115 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700116}
117
118void TextureCache::clear() {
119 mCache.clear();
120}
121
Romain Guyfe880942010-06-30 16:05:32 -0700122void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700123 SkAutoLockPixels alp(*bitmap);
124 if (!bitmap->readyToDraw()) {
125 LOGE("Cannot generate texture from bitmap");
126 return;
127 }
128
Romain Guyfe880942010-06-30 16:05:32 -0700129 if (!regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700130 texture->generation = bitmap->getGenerationID();
Romain Guyfe880942010-06-30 16:05:32 -0700131 texture->width = bitmap->width();
132 texture->height = bitmap->height();
Romain Guyce0537b2010-06-29 21:05:21 -0700133
Romain Guyfe880942010-06-30 16:05:32 -0700134 glGenTextures(1, &texture->id);
135 }
136
Romain Guyce0537b2010-06-29 21:05:21 -0700137 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700138 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
139
140 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700141 case SkBitmap::kA8_Config:
142 texture->blend = true;
143 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0,
144 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels());
145 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700146 case SkBitmap::kRGB_565_Config:
147 texture->blend = false;
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0,
149 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
150 break;
151 case SkBitmap::kARGB_8888_Config:
Romain Guyc1396e92010-06-30 17:56:19 -0700152 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
153 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700154 // Do this after calling getPixels() to make sure Skia's deferred
155 // decoding happened
156 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700157 break;
158 default:
159 break;
160 }
Romain Guyce0537b2010-06-29 21:05:21 -0700161
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guy8ba548f2010-06-30 19:21:21 -0700164
Romain Guyce0537b2010-06-29 21:05:21 -0700165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700167}
168
169}; // namespace uirenderer
170}; // namespace android