blob: fe8fb5bd8e35f993717ff2ebdc6b855a003e460b [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
Romain Guyfb8b7632010-08-23 21:05:08 -070041TextureCache::TextureCache():
John Reck71d08a02014-11-24 15:21:28 -080042 mCache(LruCache<uint32_t, Texture*>::kUnlimitedCapacity),
Romain Guyeca0ca22011-11-04 15:12:29 -070043 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Chris Craikd41c4d82015-01-05 15:51:13 -080044 mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE), mAssetAtlas(nullptr) {
Romain Guyfb8b7632010-08-23 21:05:08 -070045 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -080046 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080047 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070048 setMaxSize(MB(atof(property)));
49 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080050 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070051 }
52
Chris Craikd41c4d82015-01-05 15:51:13 -080053 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, nullptr) > 0) {
Romain Guyeca0ca22011-11-04 15:12:29 -070054 float flushRate = atof(property);
55 INIT_LOGD(" Setting texture cache flush rate to %.2f%%", flushRate * 100.0f);
56 setFlushRate(flushRate);
57 } else {
58 INIT_LOGD(" Using default texture cache flush rate of %.2f%%",
59 DEFAULT_TEXTURE_CACHE_FLUSH_RATE * 100.0f);
60 }
61
Romain Guyfb8b7632010-08-23 21:05:08 -070062 init();
63}
64
Romain Guy7d139ba2010-07-02 11:20:34 -070065TextureCache::TextureCache(uint32_t maxByteSize):
John Reck71d08a02014-11-24 15:21:28 -080066 mCache(LruCache<uint32_t, Texture*>::kUnlimitedCapacity),
Chris Craikd41c4d82015-01-05 15:51:13 -080067 mSize(0), mMaxSize(maxByteSize), mAssetAtlas(nullptr) {
Romain Guyfb8b7632010-08-23 21:05:08 -070068 init();
Romain Guyce0537b2010-06-29 21:05:21 -070069}
70
71TextureCache::~TextureCache() {
72 mCache.clear();
73}
74
Romain Guyfb8b7632010-08-23 21:05:08 -070075void TextureCache::init() {
76 mCache.setOnEntryRemovedListener(this);
77
78 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080079 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080080
81 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070082}
83
Romain Guy121e2242010-07-01 18:26:52 -070084///////////////////////////////////////////////////////////////////////////////
85// Size management
86///////////////////////////////////////////////////////////////////////////////
87
Romain Guy7d139ba2010-07-02 11:20:34 -070088uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070089 return mSize;
90}
91
Romain Guy7d139ba2010-07-02 11:20:34 -070092uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070093 return mMaxSize;
94}
95
Romain Guy7d139ba2010-07-02 11:20:34 -070096void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070097 mMaxSize = maxSize;
98 while (mSize > mMaxSize) {
99 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -0700100 }
101}
102
Romain Guyeca0ca22011-11-04 15:12:29 -0700103void TextureCache::setFlushRate(float flushRate) {
104 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate));
105}
106
Romain Guy121e2242010-07-01 18:26:52 -0700107///////////////////////////////////////////////////////////////////////////////
108// Callbacks
109///////////////////////////////////////////////////////////////////////////////
110
John Reck71d08a02014-11-24 15:21:28 -0800111void TextureCache::operator()(uint32_t&, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700112 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -0700113 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700114 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800115 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
116 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800117 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000118 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800119 }
Romain Guybe1b1272013-06-06 14:02:54 -0700120 texture->deleteTexture();
Romain Guy121e2242010-07-01 18:26:52 -0700121 delete texture;
122 }
123}
124
125///////////////////////////////////////////////////////////////////////////////
126// Caching
127///////////////////////////////////////////////////////////////////////////////
128
John Reckebd52612014-12-10 16:47:36 -0800129void TextureCache::setAssetAtlas(AssetAtlas* assetAtlas) {
130 mAssetAtlas = assetAtlas;
131}
132
John Reck860d1552014-04-11 19:15:05 -0700133void TextureCache::resetMarkInUse() {
John Reck71d08a02014-11-24 15:21:28 -0800134 LruCache<uint32_t, Texture*>::Iterator iter(mCache);
John Reck860d1552014-04-11 19:15:05 -0700135 while (iter.next()) {
136 iter.value()->isInUse = false;
137 }
138}
139
140bool TextureCache::canMakeTextureFromBitmap(const SkBitmap* bitmap) {
141 if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) {
142 ALOGW("Bitmap too large to be uploaded into a texture (%dx%d, max=%dx%d)",
143 bitmap->width(), bitmap->height(), mMaxTextureSize, mMaxTextureSize);
144 return false;
145 }
146 return true;
147}
148
149// Returns a prepared Texture* that either is already in the cache or can fit
150// in the cache (and is thus added to the cache)
151Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -0800152 if (CC_LIKELY(mAssetAtlas)) {
153 AssetAtlas::Entry* entry = mAssetAtlas->getEntry(bitmap);
154 if (CC_UNLIKELY(entry)) {
155 return entry->texture;
156 }
157 }
158
John Reck71d08a02014-11-24 15:21:28 -0800159 Texture* texture = mCache.get(bitmap->pixelRef()->getStableID());
Romain Guya2341a92010-09-08 18:04:33 -0700160
Romain Guyce0537b2010-06-29 21:05:21 -0700161 if (!texture) {
John Reck860d1552014-04-11 19:15:05 -0700162 if (!canMakeTextureFromBitmap(bitmap)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800163 return nullptr;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700164 }
165
Romain Guy7d139ba2010-07-02 11:20:34 -0700166 const uint32_t size = bitmap->rowBytes() * bitmap->height();
John Reck860d1552014-04-11 19:15:05 -0700167 bool canCache = size < mMaxSize;
Romain Guy121e2242010-07-01 18:26:52 -0700168 // Don't even try to cache a bitmap that's bigger than the cache
John Reck860d1552014-04-11 19:15:05 -0700169 while (canCache && mSize + size > mMaxSize) {
170 Texture* oldest = mCache.peekOldestValue();
171 if (oldest && !oldest->isInUse) {
Romain Guy121e2242010-07-01 18:26:52 -0700172 mCache.removeOldest();
John Reck860d1552014-04-11 19:15:05 -0700173 } else {
174 canCache = false;
Romain Guy121e2242010-07-01 18:26:52 -0700175 }
176 }
177
John Reck860d1552014-04-11 19:15:05 -0700178 if (canCache) {
179 texture = new Texture();
180 texture->bitmapSize = size;
181 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700182
Romain Guy121e2242010-07-01 18:26:52 -0700183 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800184 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
185 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800186 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000187 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800188 }
John Reck71d08a02014-11-24 15:21:28 -0800189 mCache.put(bitmap->pixelRef()->getStableID(), texture);
Romain Guy121e2242010-07-01 18:26:52 -0700190 }
John Reck860d1552014-04-11 19:15:05 -0700191 } else if (!texture->isInUse && bitmap->getGenerationID() != texture->generation) {
192 // Texture was in the cache but is dirty, re-upload
193 // TODO: Re-adjust the cache size if the bitmap's dimensions have changed
Romain Guyfe880942010-06-30 16:05:32 -0700194 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700195 }
Romain Guy22158e12010-08-06 11:18:34 -0700196
Romain Guyce0537b2010-06-29 21:05:21 -0700197 return texture;
198}
199
John Reck860d1552014-04-11 19:15:05 -0700200bool TextureCache::prefetchAndMarkInUse(const SkBitmap* bitmap) {
201 Texture* texture = getCachedTexture(bitmap);
202 if (texture) {
203 texture->isInUse = true;
204 }
205 return texture;
206}
207
208Texture* TextureCache::get(const SkBitmap* bitmap) {
209 Texture* texture = getCachedTexture(bitmap);
210
211 if (!texture) {
212 if (!canMakeTextureFromBitmap(bitmap)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800213 return nullptr;
John Reck860d1552014-04-11 19:15:05 -0700214 }
215
216 const uint32_t size = bitmap->rowBytes() * bitmap->height();
217 texture = new Texture();
218 texture->bitmapSize = size;
219 generateTexture(bitmap, texture, false);
220 texture->cleanup = true;
221 }
222
223 return texture;
224}
225
Chris Craikd218a922014-01-02 17:13:34 -0800226Texture* TextureCache::getTransient(const SkBitmap* bitmap) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700227 Texture* texture = new Texture();
Romain Guye651cc62012-05-14 19:44:40 -0700228 texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
229 texture->cleanup = true;
230
231 generateTexture(bitmap, texture, false);
232
233 return texture;
234}
235
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500236void TextureCache::releaseTexture(uint32_t pixelRefStableID) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700237 Mutex::Autolock _l(mLock);
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500238 mGarbage.push(pixelRefStableID);
Romain Guyfe48f652010-11-11 15:36:56 -0800239}
240
241void TextureCache::clearGarbage() {
242 Mutex::Autolock _l(mLock);
243 size_t count = mGarbage.size();
244 for (size_t i = 0; i < count; i++) {
John Reck71d08a02014-11-24 15:21:28 -0800245 uint32_t pixelRefId = mGarbage.itemAt(i);
246 mCache.remove(pixelRefId);
Romain Guyfe48f652010-11-11 15:36:56 -0800247 }
248 mGarbage.clear();
249}
250
251void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700252 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700253 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700254}
255
Romain Guyeca0ca22011-11-04 15:12:29 -0700256void TextureCache::flush() {
257 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
258 if (mFlushRate <= 0.0f) {
259 clear();
260 return;
261 }
262
263 uint32_t targetSize = uint32_t(mSize * mFlushRate);
264 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
265
266 while (mSize > targetSize) {
267 mCache.removeOldest();
268 }
269}
270
Chris Craikd218a922014-01-02 17:13:34 -0800271void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700272 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700273
Romain Guyc1396e92010-06-30 17:56:19 -0700274 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000275 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700276 return;
277 }
278
Chris Craik70850ea2014-11-18 10:49:23 -0800279 ATRACE_FORMAT("Upload %ux%u Texture", bitmap->width(), bitmap->height());
John Reckec4cefc2014-07-29 09:49:13 -0700280
Romain Guy52439572012-10-17 12:14:11 -0700281 // We could also enable mipmapping if both bitmap dimensions are powers
282 // of 2 but we'd have to deal with size changes. Let's keep this simple
Romain Guy3bbacf22013-02-06 16:51:04 -0800283 const bool canMipMap = Extensions::getInstance().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700284
Romain Guy713e1bb2012-10-16 18:44:09 -0700285 // If the texture had mipmap enabled but not anymore,
286 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700287 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700288 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700289 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700290
Romain Guy8c749f82010-09-22 14:13:32 -0700291 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700292 glGenTextures(1, &texture->id);
293 }
294
Romain Guy8c749f82010-09-22 14:13:32 -0700295 texture->generation = bitmap->getGenerationID();
296 texture->width = bitmap->width();
297 texture->height = bitmap->height();
298
Chris Craik44eb2c02015-01-29 09:45:09 -0800299 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700300
Mike Reed1103b322014-07-08 12:36:44 -0400301 switch (bitmap->colorType()) {
302 case kAlpha_8_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800303 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700304 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy8c749f82010-09-22 14:13:32 -0700305 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700306 break;
Mike Reed1103b322014-07-08 12:36:44 -0400307 case kRGB_565_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800308 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700309 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700310 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700311 break;
Mike Reed1103b322014-07-08 12:36:44 -0400312 case kN32_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800313 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700314 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700315 // Do this after calling getPixels() to make sure Skia's deferred
316 // decoding happened
317 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700318 break;
Mike Reed1103b322014-07-08 12:36:44 -0400319 case kARGB_4444_SkColorType:
320 case kIndex_8_SkColorType:
Romain Guy5b3b3522010-10-27 18:57:51 -0700321 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800322 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700323 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700324 default:
Mike Reed1103b322014-07-08 12:36:44 -0400325 ALOGW("Unsupported bitmap colorType: %d", bitmap->colorType());
Romain Guyc1396e92010-06-30 17:56:19 -0700326 break;
327 }
Romain Guyce0537b2010-06-29 21:05:21 -0700328
Romain Guy52439572012-10-17 12:14:11 -0700329 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700330 texture->mipMap = bitmap->hasHardwareMipMap();
331 if (texture->mipMap) {
332 glGenerateMipmap(GL_TEXTURE_2D);
333 }
334 }
335
Romain Guyd21b6e12011-11-30 20:21:23 -0800336 if (!regenerate) {
337 texture->setFilter(GL_NEAREST);
338 texture->setWrap(GL_CLAMP_TO_EDGE);
339 }
Romain Guyce0537b2010-06-29 21:05:21 -0700340}
341
Chris Craikd218a922014-01-02 17:13:34 -0800342void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700343 uint32_t width, uint32_t height) {
344 SkBitmap rgbaBitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400345 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(width, height, bitmap->alphaType()));
Romain Guy7adaf3d2010-10-05 14:58:09 -0700346 rgbaBitmap.eraseColor(0);
347
348 SkCanvas canvas(rgbaBitmap);
Chris Craikd41c4d82015-01-05 15:51:13 -0800349 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, nullptr);
Romain Guy7adaf3d2010-10-05 14:58:09 -0700350
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800351 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), rgbaBitmap.bytesPerPixel(),
352 width, height, GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700353}
354
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800355void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
Romain Guy318ae7b2013-09-24 18:44:54 -0700356 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) {
Chris Craik37424b32015-01-20 14:15:51 -0800357 glPixelStorei(GL_UNPACK_ALIGNMENT, bpp);
Romain Guy318ae7b2013-09-24 18:44:54 -0700358 const bool useStride = stride != width && Extensions::getInstance().hasUnpackRowLength();
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800359 if ((stride == width) || useStride) {
360 if (useStride) {
361 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
362 }
Romain Guy318ae7b2013-09-24 18:44:54 -0700363
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800364 if (resize) {
365 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
366 } else {
367 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
368 }
369
370 if (useStride) {
371 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
372 }
Romain Guy8c749f82010-09-22 14:13:32 -0700373 } else {
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800374 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
375 // if the stride doesn't match the width
Romain Guy318ae7b2013-09-24 18:44:54 -0700376
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800377 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
378 if (!temp) return;
379
380 uint8_t * pDst = (uint8_t *)temp;
381 uint8_t * pSrc = (uint8_t *)data;
382 for (GLsizei i = 0; i < height; i++) {
383 memcpy(pDst, pSrc, width * bpp);
384 pDst += width * bpp;
385 pSrc += stride * bpp;
386 }
387
388 if (resize) {
389 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
390 } else {
391 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
392 }
393
394 free(temp);
Romain Guy318ae7b2013-09-24 18:44:54 -0700395 }
Romain Guy8c749f82010-09-22 14:13:32 -0700396}
397
Romain Guyce0537b2010-06-29 21:05:21 -0700398}; // namespace uirenderer
399}; // namespace android