blob: 59903b70a6a1d5a28f49b8dcb6be4231dd0f4566 [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);
34}
35
36TextureCache::~TextureCache() {
37 mCache.clear();
38}
39
Romain Guy121e2242010-07-01 18:26:52 -070040///////////////////////////////////////////////////////////////////////////////
41// Size management
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy7d139ba2010-07-02 11:20:34 -070044uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070045 return mSize;
46}
47
Romain Guy7d139ba2010-07-02 11:20:34 -070048uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070049 return mMaxSize;
50}
51
Romain Guy7d139ba2010-07-02 11:20:34 -070052void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070053 mMaxSize = maxSize;
54 while (mSize > mMaxSize) {
55 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070056 }
57}
58
Romain Guy121e2242010-07-01 18:26:52 -070059///////////////////////////////////////////////////////////////////////////////
60// Callbacks
61///////////////////////////////////////////////////////////////////////////////
62
Romain Guydda57022010-07-06 11:39:32 -070063void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) {
Romain Guy121e2242010-07-01 18:26:52 -070064 if (bitmap) {
Romain Guy7d139ba2010-07-02 11:20:34 -070065 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -070066 mSize -= size;
67 }
68
69 if (texture) {
70 glDeleteTextures(1, &texture->id);
71 delete texture;
72 }
73}
74
75///////////////////////////////////////////////////////////////////////////////
76// Caching
77///////////////////////////////////////////////////////////////////////////////
78
Romain Guyce0537b2010-06-29 21:05:21 -070079Texture* TextureCache::get(SkBitmap* bitmap) {
80 Texture* texture = mCache.get(bitmap);
81 if (!texture) {
Romain Guy7d139ba2010-07-02 11:20:34 -070082 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Romain Guy121e2242010-07-01 18:26:52 -070083 // Don't even try to cache a bitmap that's bigger than the cache
84 if (size < mMaxSize) {
85 while (mSize + size > mMaxSize) {
86 mCache.removeOldest();
87 }
88 }
89
Romain Guy364703c2010-06-30 15:51:03 -070090 texture = new Texture;
Romain Guyc1396e92010-06-30 17:56:19 -070091 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -070092
93 if (size < mMaxSize) {
94 mSize += size;
95 mCache.put(bitmap, texture);
Romain Guy22158e12010-08-06 11:18:34 -070096 } else {
97 texture->cleanup = true;
Romain Guy121e2242010-07-01 18:26:52 -070098 }
Romain Guyfe880942010-06-30 16:05:32 -070099 } else if (bitmap->getGenerationID() != texture->generation) {
100 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700101 }
Romain Guy22158e12010-08-06 11:18:34 -0700102
Romain Guyce0537b2010-06-29 21:05:21 -0700103 return texture;
104}
105
Romain Guy121e2242010-07-01 18:26:52 -0700106void TextureCache::remove(SkBitmap* bitmap) {
107 mCache.remove(bitmap);
Romain Guyce0537b2010-06-29 21:05:21 -0700108}
109
110void TextureCache::clear() {
111 mCache.clear();
112}
113
Romain Guyfe880942010-06-30 16:05:32 -0700114void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700115 SkAutoLockPixels alp(*bitmap);
116 if (!bitmap->readyToDraw()) {
117 LOGE("Cannot generate texture from bitmap");
118 return;
119 }
120
Romain Guyfe880942010-06-30 16:05:32 -0700121 if (!regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700122 texture->generation = bitmap->getGenerationID();
Romain Guyfe880942010-06-30 16:05:32 -0700123 texture->width = bitmap->width();
124 texture->height = bitmap->height();
Romain Guyce0537b2010-06-29 21:05:21 -0700125
Romain Guyfe880942010-06-30 16:05:32 -0700126 glGenTextures(1, &texture->id);
127 }
128
Romain Guyce0537b2010-06-29 21:05:21 -0700129 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700130 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
131
132 switch (bitmap->getConfig()) {
Romain Guybd0e6aa2010-07-22 18:50:12 -0700133 case SkBitmap::kA8_Config:
134 texture->blend = true;
135 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0,
136 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels());
137 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700138 case SkBitmap::kRGB_565_Config:
139 texture->blend = false;
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0,
141 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
142 break;
143 case SkBitmap::kARGB_8888_Config:
Romain Guy594f4062010-07-13 17:41:31 -0700144 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700145 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
146 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
147 break;
148 default:
149 break;
150 }
Romain Guyce0537b2010-06-29 21:05:21 -0700151
152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Romain Guy8ba548f2010-06-30 19:21:21 -0700154
Romain Guyce0537b2010-06-29 21:05:21 -0700155 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guyce0537b2010-06-29 21:05:21 -0700157}
158
159}; // namespace uirenderer
160}; // namespace android