blob: fda009108abac31ceb5f7dd2f05b46c56405f841 [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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 Guy121e2242010-07-01 18:26:52 -070017#define LOG_TAG "OpenGLRenderer"
John Reckec4cefc2014-07-29 09:49:13 -070018#define ATRACE_TAG ATRACE_TAG_VIEW
Romain Guy121e2242010-07-01 18:26:52 -070019
Romain Guyce0537b2010-06-29 21:05:21 -070020#include <GLES2/gl2.h>
21
Romain Guy7adaf3d2010-10-05 14:58:09 -070022#include <SkCanvas.h>
John Reck71d08a02014-11-24 15:21:28 -080023#include <SkPixelRef.h>
Romain Guy7adaf3d2010-10-05 14:58:09 -070024
Romain Guyca89e2a2013-03-08 17:44:20 -080025#include <utils/Mutex.h>
Romain Guy9aaa8262010-09-08 15:15:43 -070026
John Reckebd52612014-12-10 16:47:36 -080027#include "AssetAtlas.h"
Romain Guy713e1bb2012-10-16 18:44:09 -070028#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040029#include "Texture.h"
Romain Guyce0537b2010-06-29 21:05:21 -070030#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070031#include "Properties.h"
Chris Craik70850ea2014-11-18 10:49:23 -080032#include "utils/TraceUtils.h"
Romain Guyce0537b2010-06-29 21:05:21 -070033
34namespace android {
35namespace uirenderer {
36
Romain Guy121e2242010-07-01 18:26:52 -070037///////////////////////////////////////////////////////////////////////////////
38// Constructors/destructor
39///////////////////////////////////////////////////////////////////////////////
40
Chris Craik117bdbc2015-02-05 10:12:38 -080041TextureCache::TextureCache()
42 : mCache(LruCache<uint32_t, Texture*>::kUnlimitedCapacity)
43 , mSize(0)
44 , mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE))
45 , mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE)
46 , mAssetAtlas(nullptr) {
Romain Guyfb8b7632010-08-23 21:05:08 -070047 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -080048 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080049 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070050 setMaxSize(MB(atof(property)));
51 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080052 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070053 }
54
Chris Craikd41c4d82015-01-05 15:51:13 -080055 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, nullptr) > 0) {
Romain Guyeca0ca22011-11-04 15:12:29 -070056 float flushRate = atof(property);
57 INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f);
58 setFlushRate(flushRate);
59 } else {
60 INIT_LOGD(" Using default texture cache flush rate of %.2f%%",
61 DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f);
62 }
63
Romain Guyfb8b7632010-08-23 21:05:08 -070064 mCache.setOnEntryRemovedListener(this);
65
66 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080067 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080068
Chris Craik2507c342015-05-04 14:36:49 -070069 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070070}
71
Chris Craik117bdbc2015-02-05 10:12:38 -080072TextureCache::~TextureCache() {
73 mCache.clear();
74}
75
Romain Guy121e2242010-07-01 18:26:52 -070076///////////////////////////////////////////////////////////////////////////////
77// Size management
78///////////////////////////////////////////////////////////////////////////////
79
Romain Guy7d139ba2010-07-02 11:20:34 -070080uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070081 return mSize;
82}
83
Romain Guy7d139ba2010-07-02 11:20:34 -070084uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070085 return mMaxSize;
86}
87
Romain Guy7d139ba2010-07-02 11:20:34 -070088void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070089 mMaxSize = maxSize;
90 while (mSize > mMaxSize) {
91 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070092 }
93}
94
Romain Guyeca0ca22011-11-04 15:12:29 -070095void TextureCache::setFlushRate(float flushRate) {
Chris Craikdf72b632015-06-30 17:56:13 -070096 mFlushRate = std::max(0.0f, std::min(1.0f, flushRate));
Romain Guyeca0ca22011-11-04 15:12:29 -070097}
98
Romain Guy121e2242010-07-01 18:26:52 -070099///////////////////////////////////////////////////////////////////////////////
100// Callbacks
101///////////////////////////////////////////////////////////////////////////////
102
John Reck71d08a02014-11-24 15:21:28 -0800103void TextureCache::operator()(uint32_t&, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700104 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -0700105 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700106 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800107 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
108 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800109 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000110 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800111 }
Romain Guybe1b1272013-06-06 14:02:54 -0700112 texture->deleteTexture();
Romain Guy121e2242010-07-01 18:26:52 -0700113 delete texture;
114 }
115}
116
117///////////////////////////////////////////////////////////////////////////////
118// Caching
119///////////////////////////////////////////////////////////////////////////////
120
John Reckebd52612014-12-10 16:47:36 -0800121void TextureCache::setAssetAtlas(AssetAtlas* assetAtlas) {
122 mAssetAtlas = assetAtlas;
123}
124
John Reck00e79c92015-07-21 10:23:59 -0700125void TextureCache::resetMarkInUse(void* ownerToken) {
John Reck71d08a02014-11-24 15:21:28 -0800126 LruCache<uint32_t, Texture*>::Iterator iter(mCache);
John Reck860d1552014-04-11 19:15:05 -0700127 while (iter.next()) {
John Reck00e79c92015-07-21 10:23:59 -0700128 if (iter.value()->isInUse == ownerToken) {
129 iter.value()->isInUse = nullptr;
130 }
John Reck860d1552014-04-11 19:15:05 -0700131 }
132}
133
134bool 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)
Chris Craik6ad690e2015-07-17 15:51:36 -0700145Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
146 if (CC_LIKELY(mAssetAtlas != nullptr) && atlasUsageType == AtlasUsageType::Use) {
John Reckebd52612014-12-10 16:47:36 -0800147 AssetAtlas::Entry* entry = mAssetAtlas->getEntry(bitmap);
148 if (CC_UNLIKELY(entry)) {
149 return entry->texture;
150 }
151 }
152
John Reck71d08a02014-11-24 15:21:28 -0800153 Texture* texture = mCache.get(bitmap->pixelRef()->getStableID());
Romain Guya2341a92010-09-08 18:04:33 -0700154
Romain Guyce0537b2010-06-29 21:05:21 -0700155 if (!texture) {
John Reck860d1552014-04-11 19:15:05 -0700156 if (!canMakeTextureFromBitmap(bitmap)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800157 return nullptr;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700158 }
159
Romain Guy7d139ba2010-07-02 11:20:34 -0700160 const uint32_t size = bitmap->rowBytes() * bitmap->height();
John Reck860d1552014-04-11 19:15:05 -0700161 bool canCache = size < mMaxSize;
Romain Guy121e2242010-07-01 18:26:52 -0700162 // Don't even try to cache a bitmap that's bigger than the cache
John Reck860d1552014-04-11 19:15:05 -0700163 while (canCache && mSize + size > mMaxSize) {
164 Texture* oldest = mCache.peekOldestValue();
165 if (oldest && !oldest->isInUse) {
Romain Guy121e2242010-07-01 18:26:52 -0700166 mCache.removeOldest();
John Reck860d1552014-04-11 19:15:05 -0700167 } else {
168 canCache = false;
Romain Guy121e2242010-07-01 18:26:52 -0700169 }
170 }
171
John Reck860d1552014-04-11 19:15:05 -0700172 if (canCache) {
Chris Craik8e93a7c2015-02-23 13:07:57 -0800173 texture = new Texture(Caches::getInstance());
John Reck860d1552014-04-11 19:15:05 -0700174 texture->bitmapSize = size;
175 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700176
Romain Guy121e2242010-07-01 18:26:52 -0700177 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800178 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
179 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800180 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000181 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800182 }
John Reck71d08a02014-11-24 15:21:28 -0800183 mCache.put(bitmap->pixelRef()->getStableID(), texture);
Romain Guy121e2242010-07-01 18:26:52 -0700184 }
John Reck860d1552014-04-11 19:15:05 -0700185 } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) {
186 // Texture was in the cache but is dirty, re-upload
187 // TODO: Re-adjust the cache size if the bitmap's dimensions have changed
Romain Guyfe880942010-06-30 16:05:32 -0700188 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700189 }
Romain Guy22158e12010-08-06 11:18:34 -0700190
Romain Guyce0537b2010-06-29 21:05:21 -0700191 return texture;
192}
193
John Reck00e79c92015-07-21 10:23:59 -0700194bool TextureCache::prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap) {
Chris Craik6ad690e2015-07-17 15:51:36 -0700195 Texture* texture = getCachedTexture(bitmap, AtlasUsageType::Use);
John Reck860d1552014-04-11 19:15:05 -0700196 if (texture) {
John Reck00e79c92015-07-21 10:23:59 -0700197 texture->isInUse = ownerToken;
John Reck860d1552014-04-11 19:15:05 -0700198 }
199 return texture;
200}
201
Chris Craik6ad690e2015-07-17 15:51:36 -0700202Texture* TextureCache::get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
203 Texture* texture = getCachedTexture(bitmap, atlasUsageType);
John Reck860d1552014-04-11 19:15:05 -0700204
205 if (!texture) {
206 if (!canMakeTextureFromBitmap(bitmap)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800207 return nullptr;
John Reck860d1552014-04-11 19:15:05 -0700208 }
209
210 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Chris Craik8e93a7c2015-02-23 13:07:57 -0800211 texture = new Texture(Caches::getInstance());
John Reck860d1552014-04-11 19:15:05 -0700212 texture->bitmapSize = size;
213 generateTexture(bitmap, texture, false);
214 texture->cleanup = true;
215 }
216
217 return texture;
218}
219
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500220void TextureCache::releaseTexture(uint32_t pixelRefStableID) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700221 Mutex::Autolock _l(mLock);
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500222 mGarbage.push(pixelRefStableID);
Romain Guyfe48f652010-11-11 15:36:56 -0800223}
224
225void TextureCache::clearGarbage() {
226 Mutex::Autolock _l(mLock);
227 size_t count = mGarbage.size();
228 for (size_t i = 0; i < count; i++) {
John Reck71d08a02014-11-24 15:21:28 -0800229 uint32_t pixelRefId = mGarbage.itemAt(i);
230 mCache.remove(pixelRefId);
Romain Guyfe48f652010-11-11 15:36:56 -0800231 }
232 mGarbage.clear();
233}
234
235void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700236 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700237 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700238}
239
Romain Guyeca0ca22011-11-04 15:12:29 -0700240void TextureCache::flush() {
241 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
242 if (mFlushRate <= 0.0f) {
243 clear();
244 return;
245 }
246
247 uint32_t targetSize = uint32_t(mSize * mFlushRate);
248 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
249
250 while (mSize > targetSize) {
251 mCache.removeOldest();
252 }
253}
254
Chris Craikd218a922014-01-02 17:13:34 -0800255void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700256 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700257
Romain Guyc1396e92010-06-30 17:56:19 -0700258 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000259 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700260 return;
261 }
262
Chris Craik70850ea2014-11-18 10:49:23 -0800263 ATRACE_FORMAT("Upload %ux%u Texture", bitmap->width(), bitmap->height());
John Reckec4cefc2014-07-29 09:49:13 -0700264
Romain Guy52439572012-10-17 12:14:11 -0700265 // We could also enable mipmapping if both bitmap dimensions are powers
266 // of 2 but we'd have to deal with size changes. Let's keep this simple
Chris Craik117bdbc2015-02-05 10:12:38 -0800267 const bool canMipMap = Caches::getInstance().extensions().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700268
Romain Guy713e1bb2012-10-16 18:44:09 -0700269 // If the texture had mipmap enabled but not anymore,
270 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700271 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700272 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700273 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700274
Romain Guy8c749f82010-09-22 14:13:32 -0700275 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700276 glGenTextures(1, &texture->id);
277 }
278
Romain Guy8c749f82010-09-22 14:13:32 -0700279 texture->generation = bitmap->getGenerationID();
280 texture->width = bitmap->width();
281 texture->height = bitmap->height();
282
Chris Craik44eb2c02015-01-29 09:45:09 -0800283 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700284
Mike Reed1103b322014-07-08 12:36:44 -0400285 switch (bitmap->colorType()) {
286 case kAlpha_8_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800287 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700288 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy8c749f82010-09-22 14:13:32 -0700289 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700290 break;
Mike Reed1103b322014-07-08 12:36:44 -0400291 case kRGB_565_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800292 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700293 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700294 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700295 break;
Mike Reed1103b322014-07-08 12:36:44 -0400296 case kN32_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800297 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700298 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700299 // Do this after calling getPixels() to make sure Skia's deferred
300 // decoding happened
301 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700302 break;
Mike Reed1103b322014-07-08 12:36:44 -0400303 case kARGB_4444_SkColorType:
304 case kIndex_8_SkColorType:
Romain Guy5b3b3522010-10-27 18:57:51 -0700305 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800306 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700307 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700308 default:
Mike Reed1103b322014-07-08 12:36:44 -0400309 ALOGW("Unsupported bitmap colorType: %d", bitmap->colorType());
Romain Guyc1396e92010-06-30 17:56:19 -0700310 break;
311 }
Romain Guyce0537b2010-06-29 21:05:21 -0700312
Romain Guy52439572012-10-17 12:14:11 -0700313 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700314 texture->mipMap = bitmap->hasHardwareMipMap();
315 if (texture->mipMap) {
316 glGenerateMipmap(GL_TEXTURE_2D);
317 }
318 }
319
Romain Guyd21b6e12011-11-30 20:21:23 -0800320 if (!regenerate) {
321 texture->setFilter(GL_NEAREST);
322 texture->setWrap(GL_CLAMP_TO_EDGE);
323 }
Romain Guyce0537b2010-06-29 21:05:21 -0700324}
325
Chris Craikd218a922014-01-02 17:13:34 -0800326void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700327 uint32_t width, uint32_t height) {
328 SkBitmap rgbaBitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400329 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(width, height, bitmap->alphaType()));
Romain Guy7adaf3d2010-10-05 14:58:09 -0700330 rgbaBitmap.eraseColor(0);
331
332 SkCanvas canvas(rgbaBitmap);
Chris Craikd41c4d82015-01-05 15:51:13 -0800333 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, nullptr);
Romain Guy7adaf3d2010-10-05 14:58:09 -0700334
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800335 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), rgbaBitmap.bytesPerPixel(),
336 width, height, GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700337}
338
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800339void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
Romain Guy318ae7b2013-09-24 18:44:54 -0700340 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) {
Chris Craik37424b32015-01-20 14:15:51 -0800341 glPixelStorei(GL_UNPACK_ALIGNMENT, bpp);
Chris Craik117bdbc2015-02-05 10:12:38 -0800342 const bool useStride = stride != width
343 && Caches::getInstance().extensions().hasUnpackRowLength();
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800344 if ((stride == width) || useStride) {
345 if (useStride) {
346 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
347 }
Romain Guy318ae7b2013-09-24 18:44:54 -0700348
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800349 if (resize) {
350 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
351 } else {
352 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
353 }
354
355 if (useStride) {
356 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
357 }
Romain Guy8c749f82010-09-22 14:13:32 -0700358 } else {
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800359 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
360 // if the stride doesn't match the width
Romain Guy318ae7b2013-09-24 18:44:54 -0700361
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800362 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
363 if (!temp) return;
364
365 uint8_t * pDst = (uint8_t *)temp;
366 uint8_t * pSrc = (uint8_t *)data;
367 for (GLsizei i = 0; i < height; i++) {
368 memcpy(pDst, pSrc, width * bpp);
369 pDst += width * bpp;
370 pSrc += stride * bpp;
371 }
372
373 if (resize) {
374 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
375 } else {
376 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
377 }
378
379 free(temp);
Romain Guy318ae7b2013-09-24 18:44:54 -0700380 }
Romain Guy8c749f82010-09-22 14:13:32 -0700381}
382
Romain Guyce0537b2010-06-29 21:05:21 -0700383}; // namespace uirenderer
384}; // namespace android