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" |
| 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 | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 23 | #include <utils/threads.h> |
| 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 | 121e224 | 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(): |
| 37 | mCache(GenerationCache<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): |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 61 | mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), |
Romain Guy | 121e224 | 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 | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame^] | 77 | |
| 78 | mHasNPot = Caches::getInstance().extensions.hasNPot(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Romain Guy | 121e224 | 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 | 121e224 | 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 | 121e224 | 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 | 121e224 | 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 | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | // Callbacks |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 108 | void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 109 | // This will be called already locked |
Romain Guy | 121e224 | 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 | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 117 | glDeleteTextures(1, &texture->id); |
| 118 | delete texture; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | // Caching |
| 124 | /////////////////////////////////////////////////////////////////////////////// |
| 125 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 126 | Texture* TextureCache::get(SkBitmap* bitmap) { |
| 127 | Texture* texture = mCache.get(bitmap); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 128 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 129 | if (!texture) { |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 130 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 131 | ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)", |
Romain Guy | 8f9a9f6 | 2011-12-05 11:53:26 -0800 | [diff] [blame] | 132 | bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 136 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 137 | // Don't even try to cache a bitmap that's bigger than the cache |
| 138 | if (size < mMaxSize) { |
| 139 | while (mSize + size > mMaxSize) { |
| 140 | mCache.removeOldest(); |
| 141 | } |
| 142 | } |
| 143 | |
Romain Guy | 364703c | 2010-06-30 15:51:03 -0700 | [diff] [blame] | 144 | texture = new Texture; |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 145 | texture->bitmapSize = size; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 146 | generateTexture(bitmap, texture, false); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 147 | |
| 148 | if (size < mMaxSize) { |
| 149 | mSize += size; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 150 | TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d", |
| 151 | bitmap, texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 152 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 153 | ALOGD("Texture created, size = %d", size); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 154 | } |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 155 | mCache.put(bitmap, texture); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 156 | } else { |
| 157 | texture->cleanup = true; |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 158 | } |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 159 | } else if (bitmap->getGenerationID() != texture->generation) { |
| 160 | generateTexture(bitmap, texture, true); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 161 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 162 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 163 | return texture; |
| 164 | } |
| 165 | |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 166 | Texture* TextureCache::getTransient(SkBitmap* bitmap) { |
| 167 | Texture* texture = new Texture; |
| 168 | texture->bitmapSize = bitmap->rowBytes() * bitmap->height(); |
| 169 | texture->cleanup = true; |
| 170 | |
| 171 | generateTexture(bitmap, texture, false); |
| 172 | |
| 173 | return texture; |
| 174 | } |
| 175 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 176 | void TextureCache::remove(SkBitmap* bitmap) { |
| 177 | mCache.remove(bitmap); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 180 | void TextureCache::removeDeferred(SkBitmap* bitmap) { |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 181 | Mutex::Autolock _l(mLock); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 182 | mGarbage.push(bitmap); |
| 183 | } |
| 184 | |
| 185 | void TextureCache::clearGarbage() { |
| 186 | Mutex::Autolock _l(mLock); |
| 187 | size_t count = mGarbage.size(); |
| 188 | for (size_t i = 0; i < count; i++) { |
| 189 | mCache.remove(mGarbage.itemAt(i)); |
| 190 | } |
| 191 | mGarbage.clear(); |
| 192 | } |
| 193 | |
| 194 | void TextureCache::clear() { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 195 | mCache.clear(); |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 196 | TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Romain Guy | eca0ca2 | 2011-11-04 15:12:29 -0700 | [diff] [blame] | 199 | void TextureCache::flush() { |
| 200 | if (mFlushRate >= 1.0f || mCache.size() == 0) return; |
| 201 | if (mFlushRate <= 0.0f) { |
| 202 | clear(); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | uint32_t targetSize = uint32_t(mSize * mFlushRate); |
| 207 | TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize); |
| 208 | |
| 209 | while (mSize > targetSize) { |
| 210 | mCache.removeOldest(); |
| 211 | } |
| 212 | } |
| 213 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 214 | void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 215 | SkAutoLockPixels alp(*bitmap); |
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 216 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 217 | if (!bitmap->readyToDraw()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 218 | ALOGE("Cannot generate texture from bitmap"); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame^] | 222 | // If the texture had mipmap enabled but not anymore, |
| 223 | // force a glTexImage2D to discard the mipmap levels |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 224 | const bool resize = !regenerate || bitmap->width() != int(texture->width) || |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame^] | 225 | bitmap->height() != int(texture->height) || |
| 226 | (regenerate && mHasNPot && texture->mipMap && !bitmap->hasHardwareMipMap()); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 227 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 228 | if (!regenerate) { |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 229 | glGenTextures(1, &texture->id); |
| 230 | } |
| 231 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 232 | texture->generation = bitmap->getGenerationID(); |
| 233 | texture->width = bitmap->width(); |
| 234 | texture->height = bitmap->height(); |
| 235 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 236 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 237 | |
| 238 | switch (bitmap->getConfig()) { |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 239 | case SkBitmap::kA8_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 240 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 241 | uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, |
| 242 | GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 243 | texture->blend = true; |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 244 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 245 | case SkBitmap::kRGB_565_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 246 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 247 | uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, |
| 248 | GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 249 | texture->blend = false; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 250 | break; |
| 251 | case SkBitmap::kARGB_8888_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 252 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 253 | uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, |
| 254 | GL_UNSIGNED_BYTE, bitmap->getPixels()); |
Romain Guy | e9e7fd0 | 2010-08-19 14:45:42 -0700 | [diff] [blame] | 255 | // Do this after calling getPixels() to make sure Skia's deferred |
| 256 | // decoding happened |
| 257 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 258 | break; |
Romain Guy | b37cbec | 2011-02-24 17:21:29 -0800 | [diff] [blame] | 259 | case SkBitmap::kARGB_4444_Config: |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 260 | case SkBitmap::kIndex8_Config: |
Romain Guy | d43b22d | 2012-10-16 11:25:06 -0700 | [diff] [blame] | 261 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 262 | uploadLoFiTexture(resize, bitmap, texture->width, texture->height); |
Romain Guy | b37cbec | 2011-02-24 17:21:29 -0800 | [diff] [blame] | 263 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 264 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 265 | default: |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 266 | ALOGW("Unsupported bitmap config: %d", bitmap->getConfig()); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 267 | break; |
| 268 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 269 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame^] | 270 | if (mHasNPot) { |
| 271 | texture->mipMap = bitmap->hasHardwareMipMap(); |
| 272 | if (texture->mipMap) { |
| 273 | glGenerateMipmap(GL_TEXTURE_2D); |
| 274 | } |
| 275 | } |
| 276 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 277 | if (!regenerate) { |
| 278 | texture->setFilter(GL_NEAREST); |
| 279 | texture->setWrap(GL_CLAMP_TO_EDGE); |
| 280 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 283 | void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap, |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 284 | uint32_t width, uint32_t height) { |
| 285 | SkBitmap rgbaBitmap; |
| 286 | rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 287 | rgbaBitmap.allocPixels(); |
| 288 | rgbaBitmap.eraseColor(0); |
Romain Guy | b37cbec | 2011-02-24 17:21:29 -0800 | [diff] [blame] | 289 | rgbaBitmap.setIsOpaque(bitmap->isOpaque()); |
Romain Guy | 7adaf3d | 2010-10-05 14:58:09 -0700 | [diff] [blame] | 290 | |
| 291 | SkCanvas canvas(rgbaBitmap); |
| 292 | canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL); |
| 293 | |
| 294 | uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), height, |
| 295 | GL_UNSIGNED_BYTE, rgbaBitmap.getPixels()); |
| 296 | } |
| 297 | |
Romain Guy | 8c749f8 | 2010-09-22 14:13:32 -0700 | [diff] [blame] | 298 | void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height, |
| 299 | GLenum type, const GLvoid * data) { |
| 300 | if (resize) { |
| 301 | glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); |
| 302 | } else { |
| 303 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); |
| 304 | } |
| 305 | } |
| 306 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 307 | }; // namespace uirenderer |
| 308 | }; // namespace android |