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