Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 19 | #include <GLES2/gl2.h> |
| 20 | |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 21 | #include <SkCanvas.h> |
| 22 | |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 23 | #include <utils/threads.h> |
| 24 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 25 | #include "TextureCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 26 | #include "Properties.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // Constructors/destructor |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 35 | TextureCache::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 Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 49 | TextureCache::TextureCache(uint32_t maxByteSize): |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 50 | mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 51 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 52 | init(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TextureCache::~TextureCache() { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 56 | Mutex::Autolock _l(mLock); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 57 | mCache.clear(); |
| 58 | } |
| 59 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 60 | void TextureCache::init() { |
| 61 | mCache.setOnEntryRemovedListener(this); |
| 62 | |
| 63 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 64 | LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame^] | 65 | |
| 66 | mDebugEnabled = readDebugLevel() & kDebugCaches; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // Size management |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 73 | uint32_t TextureCache::getSize() { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 74 | Mutex::Autolock _l(mLock); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 75 | return mSize; |
| 76 | } |
| 77 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 78 | uint32_t TextureCache::getMaxSize() { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 79 | Mutex::Autolock _l(mLock); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 80 | return mMaxSize; |
| 81 | } |
| 82 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 83 | void TextureCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 84 | Mutex::Autolock _l(mLock); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 85 | mMaxSize = maxSize; |
| 86 | while (mSize > mMaxSize) { |
| 87 | mCache.removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 91 | /////////////////////////////////////////////////////////////////////////////// |
| 92 | // Callbacks |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 95 | void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 96 | // This will be called already locked |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 97 | if (texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 98 | mSize -= texture->bitmapSize; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 99 | TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", |
| 100 | texture->id, texture->bitmapSize, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame^] | 101 | if (mDebugEnabled) { |
| 102 | LOGD("Texture deleted, size = %d", texture->bitmapSize); |
| 103 | } |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 104 | glDeleteTextures(1, &texture->id); |
| 105 | delete texture; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | // Caching |
| 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 113 | Texture* TextureCache::get(SkBitmap* bitmap) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 114 | mLock.lock(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 115 | Texture* texture = mCache.get(bitmap); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 116 | mLock.unlock(); |
| 117 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 118 | if (!texture) { |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 119 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
| 120 | LOGW("Bitmap too large to be uploaded into a texture"); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 124 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 125 | // Don't even try to cache a bitmap that's bigger than the cache |
| 126 | if (size < mMaxSize) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 127 | mLock.lock(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 128 | while (mSize + size > mMaxSize) { |
| 129 | mCache.removeOldest(); |
| 130 | } |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 131 | mLock.unlock(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Romain Guy | 364703c | 2010-06-30 15:51:03 -0700 | [diff] [blame] | 134 | texture = new Texture; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 135 | texture->bitmapSize = size; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 136 | generateTexture(bitmap, texture, false); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 137 | |
| 138 | if (size < mMaxSize) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 139 | mLock.lock(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 140 | mSize += size; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 141 | TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", |
| 142 | bitmap, texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame^] | 143 | if (mDebugEnabled) { |
| 144 | LOGD("Texture created, size = %d", size); |
| 145 | } |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 146 | mCache.put(bitmap, texture); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 147 | mLock.unlock(); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 148 | } else { |
| 149 | texture->cleanup = true; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 150 | } |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 151 | } else if (bitmap->getGenerationID() != texture->generation) { |
| 152 | generateTexture(bitmap, texture, true); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 153 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 154 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 155 | return texture; |
| 156 | } |
| 157 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 158 | void TextureCache::remove(SkBitmap* bitmap) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 159 | Mutex::Autolock _l(mLock); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 160 | mCache.remove(bitmap); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void TextureCache::clear() { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 164 | Mutex::Autolock _l(mLock); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 165 | mCache.clear(); |
Chet Haase | d98aa2d | 2010-10-25 15:47:32 -0700 | [diff] [blame] | 166 | TEXTURE_LOGD("TextureCache:clear(), miSize = %d", mSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 169 | void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 170 | SkAutoLockPixels alp(*bitmap); |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 171 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 172 | if (!bitmap->readyToDraw()) { |
| 173 | LOGE("Cannot generate texture from bitmap"); |
| 174 | return; |
| 175 | } |
| 176 | |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 177 | const bool resize = !regenerate || bitmap->width() != int(texture->width) || |
| 178 | bitmap->height() != int(texture->height); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 179 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 180 | if (!regenerate) { |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 181 | glGenTextures(1, &texture->id); |
| 182 | } |
| 183 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 184 | texture->generation = bitmap->getGenerationID(); |
| 185 | texture->width = bitmap->width(); |
| 186 | texture->height = bitmap->height(); |
| 187 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 188 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 189 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
| 190 | |
| 191 | switch (bitmap->getConfig()) { |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 192 | case SkBitmap::kA8_Config: |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 193 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 194 | uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, |
| 195 | GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 196 | texture->blend = true; |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 197 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 198 | case SkBitmap::kRGB_565_Config: |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 199 | uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, |
| 200 | GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 201 | texture->blend = false; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 202 | break; |
| 203 | case SkBitmap::kARGB_8888_Config: |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 204 | uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, |
| 205 | GL_UNSIGNED_BYTE, bitmap->getPixels()); |
Romain Guy | e9e7fd0 | 2010-08-19 14:45:42 -0700 | [diff] [blame] | 206 | // Do this after calling getPixels() to make sure Skia's deferred |
| 207 | // decoding happened |
| 208 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 209 | break; |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 210 | case SkBitmap::kIndex8_Config: |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 211 | uploadLoFiTexture(resize, bitmap, texture->width, texture->height); |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 212 | texture->blend = false; |
| 213 | break; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 214 | case SkBitmap::kARGB_4444_Config: |
| 215 | uploadLoFiTexture(resize, bitmap, texture->width, texture->height); |
| 216 | texture->blend = true; |
| 217 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 218 | default: |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 219 | LOGW("Unsupported bitmap config: %d", bitmap->getConfig()); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 220 | break; |
| 221 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 222 | |
| 223 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 224 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 225 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 226 | 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 Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 230 | void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap, |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 231 | 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 Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 244 | void 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 Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 253 | }; // namespace uirenderer |
| 254 | }; // namespace android |