blob: b1fa3835d3c859b1ef992a9dfae36a85b3bfed82 [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 Guy121e22422010-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 Guy121e22422010-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>
23
Romain Guyca89e2a2013-03-08 17:44:20 -080024#include <utils/Mutex.h>
Romain Guy9aaa8262010-09-08 15:15:43 -070025
Romain Guy713e1bb2012-10-16 18:44:09 -070026#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040027#include "Texture.h"
Romain Guyce0537b2010-06-29 21:05:21 -070028#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070029#include "Properties.h"
Chris Craik70850ea2014-11-18 10:49:23 -080030#include "utils/TraceUtils.h"
Romain Guyce0537b2010-06-29 21:05:21 -070031
32namespace android {
33namespace uirenderer {
34
Romain Guy121e22422010-07-01 18:26:52 -070035///////////////////////////////////////////////////////////////////////////////
36// Constructors/destructor
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyfb8b7632010-08-23 21:05:08 -070039TextureCache::TextureCache():
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040040 mCache(LruCache<const SkPixelRef*, Texture*>::kUnlimitedCapacity),
Romain Guyeca0ca22011-11-04 15:12:29 -070041 mSize(0), mMaxSize(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
42 mFlushRate(DEFAULT_TEXTURE_CACHE_FLUSH_RATE) {
Romain Guyfb8b7632010-08-23 21:05:08 -070043 char property[PROPERTY_VALUE_MAX];
44 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080045 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070046 setMaxSize(MB(atof(property)));
47 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080048 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070049 }
50
Romain Guyeca0ca22011-11-04 15:12:29 -070051 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 Guyfb8b7632010-08-23 21:05:08 -070060 init();
61}
62
Romain Guy7d139ba2010-07-02 11:20:34 -070063TextureCache::TextureCache(uint32_t maxByteSize):
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040064 mCache(LruCache<const SkPixelRef*, Texture*>::kUnlimitedCapacity),
Romain Guy121e22422010-07-01 18:26:52 -070065 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070066 init();
Romain Guyce0537b2010-06-29 21:05:21 -070067}
68
69TextureCache::~TextureCache() {
70 mCache.clear();
71}
72
Romain Guyfb8b7632010-08-23 21:05:08 -070073void TextureCache::init() {
74 mCache.setOnEntryRemovedListener(this);
75
76 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080077 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080078
79 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070080}
81
Romain Guy121e22422010-07-01 18:26:52 -070082///////////////////////////////////////////////////////////////////////////////
83// Size management
84///////////////////////////////////////////////////////////////////////////////
85
Romain Guy7d139ba2010-07-02 11:20:34 -070086uint32_t TextureCache::getSize() {
Romain Guy121e22422010-07-01 18:26:52 -070087 return mSize;
88}
89
Romain Guy7d139ba2010-07-02 11:20:34 -070090uint32_t TextureCache::getMaxSize() {
Romain Guy121e22422010-07-01 18:26:52 -070091 return mMaxSize;
92}
93
Romain Guy7d139ba2010-07-02 11:20:34 -070094void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e22422010-07-01 18:26:52 -070095 mMaxSize = maxSize;
96 while (mSize > mMaxSize) {
97 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070098 }
99}
100
Romain Guyeca0ca22011-11-04 15:12:29 -0700101void TextureCache::setFlushRate(float flushRate) {
102 mFlushRate = fmaxf(0.0f, fminf(1.0f, flushRate));
103}
104
Romain Guy121e22422010-07-01 18:26:52 -0700105///////////////////////////////////////////////////////////////////////////////
106// Callbacks
107///////////////////////////////////////////////////////////////////////////////
108
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400109void TextureCache::operator()(const SkPixelRef*&, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700110 // This will be called already locked
Romain Guy121e22422010-07-01 18:26:52 -0700111 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700112 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800113 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
114 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800115 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000116 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800117 }
Romain Guybe1b1272013-06-06 14:02:54 -0700118 texture->deleteTexture();
Romain Guy121e22422010-07-01 18:26:52 -0700119 delete texture;
120 }
121}
122
123///////////////////////////////////////////////////////////////////////////////
124// Caching
125///////////////////////////////////////////////////////////////////////////////
126
John Reck860d1552014-04-11 19:15:05 -0700127void TextureCache::resetMarkInUse() {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400128 LruCache<const SkPixelRef*, Texture*>::Iterator iter(mCache);
John Reck860d1552014-04-11 19:15:05 -0700129 while (iter.next()) {
130 iter.value()->isInUse = false;
131 }
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)
145Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400146 Texture* texture = mCache.get(bitmap->pixelRef());
Romain Guya2341a92010-09-08 18:04:33 -0700147
Romain Guyce0537b2010-06-29 21:05:21 -0700148 if (!texture) {
John Reck860d1552014-04-11 19:15:05 -0700149 if (!canMakeTextureFromBitmap(bitmap)) {
Romain Guy9cccc2b92010-08-07 23:46:15 -0700150 return NULL;
151 }
152
Romain Guy7d139ba2010-07-02 11:20:34 -0700153 const uint32_t size = bitmap->rowBytes() * bitmap->height();
John Reck860d1552014-04-11 19:15:05 -0700154 bool canCache = size < mMaxSize;
Romain Guy121e22422010-07-01 18:26:52 -0700155 // Don't even try to cache a bitmap that's bigger than the cache
John Reck860d1552014-04-11 19:15:05 -0700156 while (canCache && mSize + size > mMaxSize) {
157 Texture* oldest = mCache.peekOldestValue();
158 if (oldest && !oldest->isInUse) {
Romain Guy121e22422010-07-01 18:26:52 -0700159 mCache.removeOldest();
John Reck860d1552014-04-11 19:15:05 -0700160 } else {
161 canCache = false;
Romain Guy121e22422010-07-01 18:26:52 -0700162 }
163 }
164
John Reck860d1552014-04-11 19:15:05 -0700165 if (canCache) {
166 texture = new Texture();
167 texture->bitmapSize = size;
168 generateTexture(bitmap, texture, false);
Romain Guy121e22422010-07-01 18:26:52 -0700169
Romain Guy121e22422010-07-01 18:26:52 -0700170 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800171 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
172 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800173 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000174 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800175 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400176 mCache.put(bitmap->pixelRef(), texture);
Romain Guy121e22422010-07-01 18:26:52 -0700177 }
John Reck860d1552014-04-11 19:15:05 -0700178 } 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 Guyfe880942010-06-30 16:05:32 -0700181 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700182 }
Romain Guy22158e12010-08-06 11:18:34 -0700183
Romain Guyce0537b2010-06-29 21:05:21 -0700184 return texture;
185}
186
John Reck860d1552014-04-11 19:15:05 -0700187bool TextureCache::prefetchAndMarkInUse(const SkBitmap* bitmap) {
188 Texture* texture = getCachedTexture(bitmap);
189 if (texture) {
190 texture->isInUse = true;
191 }
192 return texture;
193}
194
195Texture* 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 Craikd218a922014-01-02 17:13:34 -0800213Texture* TextureCache::getTransient(const SkBitmap* bitmap) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700214 Texture* texture = new Texture();
Romain Guye651cc62012-05-14 19:44:40 -0700215 texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
216 texture->cleanup = true;
217
218 generateTexture(bitmap, texture, false);
219
220 return texture;
221}
222
Chris Craikd218a922014-01-02 17:13:34 -0800223void TextureCache::remove(const SkBitmap* bitmap) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400224 mCache.remove(bitmap->pixelRef());
Romain Guyce0537b2010-06-29 21:05:21 -0700225}
226
Chris Craikd218a922014-01-02 17:13:34 -0800227void TextureCache::removeDeferred(const SkBitmap* bitmap) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700228 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800229 mGarbage.push(bitmap);
230}
231
232void TextureCache::clearGarbage() {
233 Mutex::Autolock _l(mLock);
234 size_t count = mGarbage.size();
235 for (size_t i = 0; i < count; i++) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900236 const SkBitmap* bitmap = mGarbage.itemAt(i);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400237 mCache.remove(bitmap->pixelRef());
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900238 delete bitmap;
Romain Guyfe48f652010-11-11 15:36:56 -0800239 }
240 mGarbage.clear();
241}
242
243void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700244 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700245 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700246}
247
Romain Guyeca0ca22011-11-04 15:12:29 -0700248void TextureCache::flush() {
249 if (mFlushRate >= 1.0f || mCache.size() == 0) return;
250 if (mFlushRate <= 0.0f) {
251 clear();
252 return;
253 }
254
255 uint32_t targetSize = uint32_t(mSize * mFlushRate);
256 TEXTURE_LOGD("TextureCache::flush: target size: %d", targetSize);
257
258 while (mSize > targetSize) {
259 mCache.removeOldest();
260 }
261}
262
Chris Craikd218a922014-01-02 17:13:34 -0800263void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700264 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700265
Romain Guyc1396e92010-06-30 17:56:19 -0700266 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000267 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700268 return;
269 }
270
Chris Craik70850ea2014-11-18 10:49:23 -0800271 ATRACE_FORMAT("Upload %ux%u Texture", bitmap->width(), bitmap->height());
John Reckec4cefc2014-07-29 09:49:13 -0700272
Romain Guy52439572012-10-17 12:14:11 -0700273 // We could also enable mipmapping if both bitmap dimensions are powers
274 // of 2 but we'd have to deal with size changes. Let's keep this simple
Romain Guy3bbacf22013-02-06 16:51:04 -0800275 const bool canMipMap = Extensions::getInstance().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700276
Romain Guy713e1bb2012-10-16 18:44:09 -0700277 // If the texture had mipmap enabled but not anymore,
278 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700279 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700280 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700281 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700282
Romain Guy8c749f82010-09-22 14:13:32 -0700283 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700284 glGenTextures(1, &texture->id);
285 }
286
Romain Guy8c749f82010-09-22 14:13:32 -0700287 texture->generation = bitmap->getGenerationID();
288 texture->width = bitmap->width();
289 texture->height = bitmap->height();
290
Romain Guy8aa195d2013-06-04 18:00:09 -0700291 Caches::getInstance().bindTexture(texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700292
Mike Reed1103b322014-07-08 12:36:44 -0400293 switch (bitmap->colorType()) {
294 case kAlpha_8_SkColorType:
Romain Guyd43b22d2012-10-16 11:25:06 -0700295 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800296 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700297 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy8c749f82010-09-22 14:13:32 -0700298 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700299 break;
Mike Reed1103b322014-07-08 12:36:44 -0400300 case kRGB_565_SkColorType:
Romain Guyd43b22d2012-10-16 11:25:06 -0700301 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800302 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700303 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700304 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700305 break;
Mike Reed1103b322014-07-08 12:36:44 -0400306 case kN32_SkColorType:
Romain Guyd43b22d2012-10-16 11:25:06 -0700307 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800308 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700309 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700310 // Do this after calling getPixels() to make sure Skia's deferred
311 // decoding happened
312 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700313 break;
Mike Reed1103b322014-07-08 12:36:44 -0400314 case kARGB_4444_SkColorType:
315 case kIndex_8_SkColorType:
Romain Guyd43b22d2012-10-16 11:25:06 -0700316 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
Romain Guy5b3b3522010-10-27 18:57:51 -0700317 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800318 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700319 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700320 default:
Mike Reed1103b322014-07-08 12:36:44 -0400321 ALOGW("Unsupported bitmap colorType: %d", bitmap->colorType());
Romain Guyc1396e92010-06-30 17:56:19 -0700322 break;
323 }
Romain Guyce0537b2010-06-29 21:05:21 -0700324
Romain Guy52439572012-10-17 12:14:11 -0700325 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700326 texture->mipMap = bitmap->hasHardwareMipMap();
327 if (texture->mipMap) {
328 glGenerateMipmap(GL_TEXTURE_2D);
329 }
330 }
331
Romain Guyd21b6e12011-11-30 20:21:23 -0800332 if (!regenerate) {
333 texture->setFilter(GL_NEAREST);
334 texture->setWrap(GL_CLAMP_TO_EDGE);
335 }
Romain Guyce0537b2010-06-29 21:05:21 -0700336}
337
Chris Craikd218a922014-01-02 17:13:34 -0800338void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700339 uint32_t width, uint32_t height) {
340 SkBitmap rgbaBitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400341 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(width, height, bitmap->alphaType()));
Romain Guy7adaf3d2010-10-05 14:58:09 -0700342 rgbaBitmap.eraseColor(0);
343
344 SkCanvas canvas(rgbaBitmap);
345 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL);
346
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800347 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), rgbaBitmap.bytesPerPixel(),
348 width, height, GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700349}
350
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800351void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
Romain Guy318ae7b2013-09-24 18:44:54 -0700352 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) {
Romain Guy318ae7b2013-09-24 18:44:54 -0700353 const bool useStride = stride != width && Extensions::getInstance().hasUnpackRowLength();
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800354 if ((stride == width) || useStride) {
355 if (useStride) {
356 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
357 }
Romain Guy318ae7b2013-09-24 18:44:54 -0700358
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800359 if (resize) {
360 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
361 } else {
362 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
363 }
364
365 if (useStride) {
366 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
367 }
Romain Guy8c749f82010-09-22 14:13:32 -0700368 } else {
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800369 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
370 // if the stride doesn't match the width
Romain Guy318ae7b2013-09-24 18:44:54 -0700371
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800372 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
373 if (!temp) return;
374
375 uint8_t * pDst = (uint8_t *)temp;
376 uint8_t * pSrc = (uint8_t *)data;
377 for (GLsizei i = 0; i < height; i++) {
378 memcpy(pDst, pSrc, width * bpp);
379 pDst += width * bpp;
380 pSrc += stride * bpp;
381 }
382
383 if (resize) {
384 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
385 } else {
386 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
387 }
388
389 free(temp);
Romain Guy318ae7b2013-09-24 18:44:54 -0700390 }
Romain Guy8c749f82010-09-22 14:13:32 -0700391}
392
Romain Guyce0537b2010-06-29 21:05:21 -0700393}; // namespace uirenderer
394}; // namespace android