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