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