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