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 | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 23 | #include <utils/Mutex.h> |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 24 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 25 | #include "Caches.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 26 | #include "TextureCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 27 | #include "Properties.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | // Constructors/destructor |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 36 | TextureCache::TextureCache(): |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 37 | mCache(LruCache<const SkBitmap*, Texture*>::kUnlimitedCapacity), |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 38 | mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)), |
| 39 | mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 40 | char property[PROPERTY_VALUE_MAX]; |
| 41 | if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 42 | INIT_LOGD(" Setting texture cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 43 | setMaxSize(MB(atof(property))); |
| 44 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 45 | INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 48 | if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, NULL) > 0) { |
| 49 | float flushRate = atof(property); |
| 50 | INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f); |
| 51 | setFlushRate(flushRate); |
| 52 | } else { |
| 53 | INIT_LOGD(" Using default texture cache flush rate of %.2f%%", |
| 54 | DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f); |
| 55 | } |
| 56 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 57 | init(); |
| 58 | } |
| 59 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 60 | TextureCache::TextureCache(uint32_t maxByteSize): |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 61 | mCache(LruCache<const SkBitmap*, Texture*>::kUnlimitedCapacity), |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 62 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 63 | init(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TextureCache::~TextureCache() { |
| 67 | mCache.clear(); |
| 68 | } |
| 69 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 70 | void TextureCache::init() { |
| 71 | mCache.setOnEntryRemovedListener(this); |
| 72 | |
| 73 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 74 | INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 75 | |
| 76 | mDebugEnabled = readDebugLevel() & kDebugCaches; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | // Size management |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 83 | uint32_t TextureCache::getSize() { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 84 | return mSize; |
| 85 | } |
| 86 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 87 | uint32_t TextureCache::getMaxSize() { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 88 | return mMaxSize; |
| 89 | } |
| 90 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 91 | void TextureCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 92 | mMaxSize = maxSize; |
| 93 | while (mSize > mMaxSize) { |
| 94 | mCache.removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 98 | void TextureCache::setFlushRate(float flushRate) { |
| 99 | mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate)); |
| 100 | } |
| 101 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | // Callbacks |
| 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 106 | void TextureCache::operator()(const SkBitmap*&, Texture*& texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 107 | // This will be called already locked |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 108 | if (texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 109 | mSize -= texture->bitmapSize; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 110 | TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d", |
| 111 | texture->id, texture->bitmapSize, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 112 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 113 | ALOGD("Texture deleted, size = %d", texture->bitmapSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 114 | } |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 115 | texture->deleteTexture(); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 116 | delete texture; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | // Caching |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 124 | void TextureCache::resetMarkInUse() { |
| 125 | LruCache<const SkBitmap*, Texture*>::Iterator iter(mCache); |
| 126 | while (iter.next()) { |
| 127 | iter.value()->isInUse = false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | bool TextureCache::canMakeTextureFromBitmap(const SkBitmap* bitmap) { |
| 132 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
| 133 | ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)", |
| 134 | bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize); |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // Returns a prepared Texture* that either is already in the cache or can fit |
| 141 | // in the cache (and is thus added to the cache) |
| 142 | Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 143 | Texture* texture = mCache.get(bitmap); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 144 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 145 | if (!texture) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 146 | if (!canMakeTextureFromBitmap(bitmap)) { |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 147 | return NULL; |
| 148 | } |
| 149 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 150 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 151 | bool canCache = size < mMaxSize; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 152 | // Don't even try to cache a bitmap that's bigger than the cache |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 153 | while (canCache && mSize + size > mMaxSize) { |
| 154 | Texture* oldest = mCache.peekOldestValue(); |
| 155 | if (oldest && !oldest->isInUse) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 156 | mCache.removeOldest(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 157 | } else { |
| 158 | canCache = false; |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 162 | if (canCache) { |
| 163 | texture = new Texture(); |
| 164 | texture->bitmapSize = size; |
| 165 | generateTexture(bitmap, texture, false); |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 166 | |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 167 | mSize += size; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 168 | TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", |
| 169 | bitmap, texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 170 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 171 | ALOGD("Texture created, size = %d", size); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 172 | } |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 173 | mCache.put(bitmap, texture); |
| 174 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 175 | } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) { |
| 176 | // Texture was in the cache but is dirty, re-upload |
| 177 | // TODO: Re-adjust the cache size if the bitmap's dimensions have changed |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 178 | generateTexture(bitmap, texture, true); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 179 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 180 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 181 | return texture; |
| 182 | } |
| 183 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame^] | 184 | bool TextureCache::prefetchAndMarkInUse(const SkBitmap* bitmap) { |
| 185 | Texture* texture = getCachedTexture(bitmap); |
| 186 | if (texture) { |
| 187 | texture->isInUse = true; |
| 188 | } |
| 189 | return texture; |
| 190 | } |
| 191 | |
| 192 | Texture* TextureCache::get(const SkBitmap* bitmap) { |
| 193 | Texture* texture = getCachedTexture(bitmap); |
| 194 | |
| 195 | if (!texture) { |
| 196 | if (!canMakeTextureFromBitmap(bitmap)) { |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
| 201 | texture = new Texture(); |
| 202 | texture->bitmapSize = size; |
| 203 | generateTexture(bitmap, texture, false); |
| 204 | texture->cleanup = true; |
| 205 | } |
| 206 | |
| 207 | return texture; |
| 208 | } |
| 209 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 210 | Texture* TextureCache::getTransient(const SkBitmap* bitmap) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 211 | Texture* texture = new Texture(); |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 212 | texture->bitmapSize = bitmap->rowBytes() * bitmap->height(); |
| 213 | texture->cleanup = true; |
| 214 | |
| 215 | generateTexture(bitmap, texture, false); |
| 216 | |
| 217 | return texture; |
| 218 | } |
| 219 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 220 | void TextureCache::remove(const SkBitmap* bitmap) { |
Romain Guy | 121e2242 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 221 | mCache.remove(bitmap); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 224 | void TextureCache::removeDeferred(const SkBitmap* bitmap) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 225 | Mutex::Autolock _l(mLock); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 226 | mGarbage.push(bitmap); |
| 227 | } |
| 228 | |
| 229 | void TextureCache::clearGarbage() { |
| 230 | Mutex::Autolock _l(mLock); |
| 231 | size_t count = mGarbage.size(); |
| 232 | for (size_t i = 0; i < count; i++) { |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 233 | const SkBitmap* bitmap = mGarbage.itemAt(i); |
| 234 | mCache.remove(bitmap); |
| 235 | delete bitmap; |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 236 | } |
| 237 | mGarbage.clear(); |
| 238 | } |
| 239 | |
| 240 | void TextureCache::clear() { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 241 | mCache.clear(); |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 242 | TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 245 | void TextureCache::flush() { |
| 246 | if (mFlushRate >= 1.0f || mCache.size() == 0) return; |
| 247 | if (mFlushRate <= 0.0f) { |
| 248 | clear(); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | uint32_t targetSize = uint32_t(mSize * mFlushRate); |
| 253 | TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize); |
| 254 | |
| 255 | while (mSize > targetSize) { |
| 256 | mCache.removeOldest(); |
| 257 | } |
| 258 | } |
| 259 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 260 | void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 261 | SkAutoLockPixels alp(*bitmap); |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 262 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 263 | if (!bitmap->readyToDraw()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 264 | ALOGE("Cannot generate texture from bitmap"); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Romain Guy | 5243957 | 2012-10-17 12:14:11 -0700 | [diff] [blame] | 268 | // We could also enable mipmapping if both bitmap dimensions are powers |
| 269 | // of 2 but we'd have to deal with size changes. Let's keep this simple |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 270 | const bool canMipMap = Extensions::getInstance().hasNPot(); |
Romain Guy | 5243957 | 2012-10-17 12:14:11 -0700 | [diff] [blame] | 271 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 272 | // If the texture had mipmap enabled but not anymore, |
| 273 | // force a glTexImage2D to discard the mipmap levels |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 274 | const bool resize = !regenerate || bitmap->width() != int(texture->width) || |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 275 | bitmap->height() != int(texture->height) || |
Romain Guy | 5243957 | 2012-10-17 12:14:11 -0700 | [diff] [blame] | 276 | (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap()); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 277 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 278 | if (!regenerate) { |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 279 | glGenTextures(1, &texture->id); |
| 280 | } |
| 281 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 282 | texture->generation = bitmap->getGenerationID(); |
| 283 | texture->width = bitmap->width(); |
| 284 | texture->height = bitmap->height(); |
| 285 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 286 | Caches::getInstance().bindTexture(texture->id); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 287 | |
Leon Scroggins III | 4b9a19a | 2013-12-03 15:06:30 -0500 | [diff] [blame] | 288 | switch (bitmap->config()) { |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 289 | case SkBitmap::kA8_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 290 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 291 | uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), |
| 292 | texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 293 | texture->blend = true; |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 294 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 295 | case SkBitmap::kRGB_565_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 296 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 297 | uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), |
| 298 | texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 299 | texture->blend = false; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 300 | break; |
| 301 | case SkBitmap::kARGB_8888_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 302 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 303 | uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), |
| 304 | texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
Romain Guy | e9e7fd0 | 2010-08-19 14:45:42 -0700 | [diff] [blame] | 305 | // Do this after calling getPixels() to make sure Skia's deferred |
| 306 | // decoding happened |
| 307 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 308 | break; |
Romain Guy | b37cbec | 2011-02-24 17:21:29 -0800 | [diff] [blame] | 309 | case SkBitmap::kARGB_4444_Config: |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 310 | case SkBitmap::kIndex8_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 311 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 312 | uploadLoFiTexture(resize, bitmap, texture->width, texture->height); |
Romain Guy | b37cbec | 2011-02-24 17:21:29 -0800 | [diff] [blame] | 313 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 314 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 315 | default: |
Leon Scroggins III | 4b9a19a | 2013-12-03 15:06:30 -0500 | [diff] [blame] | 316 | ALOGW("Unsupported bitmap config: %d", bitmap->config()); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 317 | break; |
| 318 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 319 | |
Romain Guy | 5243957 | 2012-10-17 12:14:11 -0700 | [diff] [blame] | 320 | if (canMipMap) { |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 321 | texture->mipMap = bitmap->hasHardwareMipMap(); |
| 322 | if (texture->mipMap) { |
| 323 | glGenerateMipmap(GL_TEXTURE_2D); |
| 324 | } |
| 325 | } |
| 326 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 327 | if (!regenerate) { |
| 328 | texture->setFilter(GL_NEAREST); |
| 329 | texture->setWrap(GL_CLAMP_TO_EDGE); |
| 330 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 333 | void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap, |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 334 | uint32_t width, uint32_t height) { |
| 335 | SkBitmap rgbaBitmap; |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 336 | rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, bitmap->alphaType()); |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 337 | rgbaBitmap.allocPixels(); |
| 338 | rgbaBitmap.eraseColor(0); |
| 339 | |
| 340 | SkCanvas canvas(rgbaBitmap); |
| 341 | canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL); |
| 342 | |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 343 | uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), width, height, |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 344 | GL_UNSIGNED_BYTE, rgbaBitmap.getPixels()); |
| 345 | } |
| 346 | |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 347 | void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride, |
| 348 | GLsizei width, GLsizei height, GLenum type, const GLvoid * data) { |
| 349 | // TODO: With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer |
| 350 | // if the stride doesn't match the width |
| 351 | const bool useStride = stride != width && Extensions::getInstance().hasUnpackRowLength(); |
| 352 | if (useStride) { |
| 353 | glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); |
| 354 | } |
| 355 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 356 | if (resize) { |
| 357 | glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); |
| 358 | } else { |
| 359 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); |
| 360 | } |
Romain Guy | 318ae7b | 2013-09-24 18:44:54 -0700 | [diff] [blame] | 361 | |
| 362 | if (useStride) { |
| 363 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); |
| 364 | } |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 367 | }; // namespace uirenderer |
| 368 | }; // namespace android |