blob: 16f6f4ba4bb6268b5455865ce166b28133794a7b [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
17#include <GLES2/gl2.h>
18
Romain Guy7adaf3d2010-10-05 14:58:09 -070019#include <SkCanvas.h>
John Reck71d08a02014-11-24 15:21:28 -080020#include <SkPixelRef.h>
Romain Guy7adaf3d2010-10-05 14:58:09 -070021
Romain Guyca89e2a2013-03-08 17:44:20 -080022#include <utils/Mutex.h>
Romain Guy9aaa8262010-09-08 15:15:43 -070023
John Reckebd52612014-12-10 16:47:36 -080024#include "AssetAtlas.h"
Romain Guy713e1bb2012-10-16 18:44:09 -070025#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040026#include "Texture.h"
Romain Guyce0537b2010-06-29 21:05:21 -070027#include "TextureCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070028#include "Properties.h"
Chris Craik70850ea2014-11-18 10:49:23 -080029#include "utils/TraceUtils.h"
Romain Guyce0537b2010-06-29 21:05:21 -070030
31namespace android {
32namespace uirenderer {
33
Romain Guy121e2242010-07-01 18:26:52 -070034///////////////////////////////////////////////////////////////////////////////
35// Constructors/destructor
36///////////////////////////////////////////////////////////////////////////////
37
Chris Craik117bdbc2015-02-05 10:12:38 -080038TextureCache::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 Guyfb8b7632010-08-23 21:05:08 -070044 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -080045 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080046 INIT_LOGD(" Setting texture cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070047 setMaxSize(MB(atof(property)));
48 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080049 INIT_LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070050 }
51
Chris Craikd41c4d82015-01-05 15:51:13 -080052 if (property_get(PROPERTY_TEXTURE_CACHE_FLUSH_RATE, property, nullptr) > 0) {
Romain Guyeca0ca22011-11-04 15:12:29 -070053 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 Guyfb8b7632010-08-23 21:05:08 -070061 mCache.setOnEntryRemovedListener(this);
62
63 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guyf6834472011-01-23 13:32:12 -080064 INIT_LOGD(" Maximum texture dimension is %d pixels", mMaxTextureSize);
Romain Guye190aa62010-11-10 19:01:29 -080065
Chris Craik2507c342015-05-04 14:36:49 -070066 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyfb8b7632010-08-23 21:05:08 -070067}
68
Chris Craik117bdbc2015-02-05 10:12:38 -080069TextureCache::~TextureCache() {
70 mCache.clear();
71}
72
Romain Guy121e2242010-07-01 18:26:52 -070073///////////////////////////////////////////////////////////////////////////////
74// Size management
75///////////////////////////////////////////////////////////////////////////////
76
Romain Guy7d139ba2010-07-02 11:20:34 -070077uint32_t TextureCache::getSize() {
Romain Guy121e2242010-07-01 18:26:52 -070078 return mSize;
79}
80
Romain Guy7d139ba2010-07-02 11:20:34 -070081uint32_t TextureCache::getMaxSize() {
Romain Guy121e2242010-07-01 18:26:52 -070082 return mMaxSize;
83}
84
Romain Guy7d139ba2010-07-02 11:20:34 -070085void TextureCache::setMaxSize(uint32_t maxSize) {
Romain Guy121e2242010-07-01 18:26:52 -070086 mMaxSize = maxSize;
87 while (mSize > mMaxSize) {
88 mCache.removeOldest();
Romain Guyce0537b2010-06-29 21:05:21 -070089 }
90}
91
Romain Guyeca0ca22011-11-04 15:12:29 -070092void TextureCache::setFlushRate(float flushRate) {
Chris Craikdf72b632015-06-30 17:56:13 -070093 mFlushRate = std::max(0.0f, std::min(1.0f, flushRate));
Romain Guyeca0ca22011-11-04 15:12:29 -070094}
95
Romain Guy121e2242010-07-01 18:26:52 -070096///////////////////////////////////////////////////////////////////////////////
97// Callbacks
98///////////////////////////////////////////////////////////////////////////////
99
John Reck71d08a02014-11-24 15:21:28 -0800100void TextureCache::operator()(uint32_t&, Texture*& texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700101 // This will be called already locked
Romain Guy121e2242010-07-01 18:26:52 -0700102 if (texture) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700103 mSize -= texture->bitmapSize;
Romain Guy9e108412010-11-09 14:35:20 -0800104 TEXTURE_LOGD("TextureCache::callback: name, removed size, mSize = %d, %d, %d",
105 texture->id, texture->bitmapSize, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800106 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000107 ALOGD("Texture deleted, size = %d", texture->bitmapSize);
Romain Guye190aa62010-11-10 19:01:29 -0800108 }
Romain Guybe1b1272013-06-06 14:02:54 -0700109 texture->deleteTexture();
Romain Guy121e2242010-07-01 18:26:52 -0700110 delete texture;
111 }
112}
113
114///////////////////////////////////////////////////////////////////////////////
115// Caching
116///////////////////////////////////////////////////////////////////////////////
117
John Reckebd52612014-12-10 16:47:36 -0800118void TextureCache::setAssetAtlas(AssetAtlas* assetAtlas) {
119 mAssetAtlas = assetAtlas;
120}
121
John Reck00e79c92015-07-21 10:23:59 -0700122void TextureCache::resetMarkInUse(void* ownerToken) {
John Reck71d08a02014-11-24 15:21:28 -0800123 LruCache<uint32_t, Texture*>::Iterator iter(mCache);
John Reck860d1552014-04-11 19:15:05 -0700124 while (iter.next()) {
John Reck00e79c92015-07-21 10:23:59 -0700125 if (iter.value()->isInUse == ownerToken) {
126 iter.value()->isInUse = nullptr;
127 }
John Reck860d1552014-04-11 19:15:05 -0700128 }
129}
130
131bool 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 Craik6ad690e2015-07-17 15:51:36 -0700142Texture* TextureCache::getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
143 if (CC_LIKELY(mAssetAtlas != nullptr) && atlasUsageType == AtlasUsageType::Use) {
John Reckebd52612014-12-10 16:47:36 -0800144 AssetAtlas::Entry* entry = mAssetAtlas->getEntry(bitmap);
145 if (CC_UNLIKELY(entry)) {
146 return entry->texture;
147 }
148 }
149
John Reck71d08a02014-11-24 15:21:28 -0800150 Texture* texture = mCache.get(bitmap->pixelRef()->getStableID());
Romain Guya2341a92010-09-08 18:04:33 -0700151
Romain Guyce0537b2010-06-29 21:05:21 -0700152 if (!texture) {
John Reck860d1552014-04-11 19:15:05 -0700153 if (!canMakeTextureFromBitmap(bitmap)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800154 return nullptr;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700155 }
156
Romain Guy7d139ba2010-07-02 11:20:34 -0700157 const uint32_t size = bitmap->rowBytes() * bitmap->height();
John Reck860d1552014-04-11 19:15:05 -0700158 bool canCache = size < mMaxSize;
Romain Guy121e2242010-07-01 18:26:52 -0700159 // Don't even try to cache a bitmap that's bigger than the cache
John Reck860d1552014-04-11 19:15:05 -0700160 while (canCache && mSize + size > mMaxSize) {
161 Texture* oldest = mCache.peekOldestValue();
162 if (oldest && !oldest->isInUse) {
Romain Guy121e2242010-07-01 18:26:52 -0700163 mCache.removeOldest();
John Reck860d1552014-04-11 19:15:05 -0700164 } else {
165 canCache = false;
Romain Guy121e2242010-07-01 18:26:52 -0700166 }
167 }
168
John Reck860d1552014-04-11 19:15:05 -0700169 if (canCache) {
Chris Craik8e93a7c2015-02-23 13:07:57 -0800170 texture = new Texture(Caches::getInstance());
John Reck860d1552014-04-11 19:15:05 -0700171 texture->bitmapSize = size;
172 generateTexture(bitmap, texture, false);
Romain Guy121e2242010-07-01 18:26:52 -0700173
Romain Guy121e2242010-07-01 18:26:52 -0700174 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800175 TEXTURE_LOGD("TextureCache::get: create texture(%p): name, size, mSize = %d, %d, %d",
176 bitmap, texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800177 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000178 ALOGD("Texture created, size = %d", size);
Romain Guye190aa62010-11-10 19:01:29 -0800179 }
John Reck71d08a02014-11-24 15:21:28 -0800180 mCache.put(bitmap->pixelRef()->getStableID(), texture);
Romain Guy121e2242010-07-01 18:26:52 -0700181 }
John Reck860d1552014-04-11 19:15:05 -0700182 } 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 Guyfe880942010-06-30 16:05:32 -0700185 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700186 }
Romain Guy22158e12010-08-06 11:18:34 -0700187
Romain Guyce0537b2010-06-29 21:05:21 -0700188 return texture;
189}
190
John Reck00e79c92015-07-21 10:23:59 -0700191bool TextureCache::prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap) {
Chris Craik6ad690e2015-07-17 15:51:36 -0700192 Texture* texture = getCachedTexture(bitmap, AtlasUsageType::Use);
John Reck860d1552014-04-11 19:15:05 -0700193 if (texture) {
John Reck00e79c92015-07-21 10:23:59 -0700194 texture->isInUse = ownerToken;
John Reck860d1552014-04-11 19:15:05 -0700195 }
196 return texture;
197}
198
Chris Craik6ad690e2015-07-17 15:51:36 -0700199Texture* TextureCache::get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
200 Texture* texture = getCachedTexture(bitmap, atlasUsageType);
John Reck860d1552014-04-11 19:15:05 -0700201
202 if (!texture) {
203 if (!canMakeTextureFromBitmap(bitmap)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800204 return nullptr;
John Reck860d1552014-04-11 19:15:05 -0700205 }
206
207 const uint32_t size = bitmap->rowBytes() * bitmap->height();
Chris Craik8e93a7c2015-02-23 13:07:57 -0800208 texture = new Texture(Caches::getInstance());
John Reck860d1552014-04-11 19:15:05 -0700209 texture->bitmapSize = size;
210 generateTexture(bitmap, texture, false);
211 texture->cleanup = true;
212 }
213
214 return texture;
215}
216
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500217void TextureCache::releaseTexture(uint32_t pixelRefStableID) {
Romain Guy9aaa8262010-09-08 15:15:43 -0700218 Mutex::Autolock _l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700219 mGarbage.push_back(pixelRefStableID);
Romain Guyfe48f652010-11-11 15:36:56 -0800220}
221
222void TextureCache::clearGarbage() {
223 Mutex::Autolock _l(mLock);
224 size_t count = mGarbage.size();
225 for (size_t i = 0; i < count; i++) {
John Reck272a6852015-07-29 16:48:58 -0700226 uint32_t pixelRefId = mGarbage[i];
John Reck71d08a02014-11-24 15:21:28 -0800227 mCache.remove(pixelRefId);
Romain Guyfe48f652010-11-11 15:36:56 -0800228 }
229 mGarbage.clear();
230}
231
232void TextureCache::clear() {
Romain Guyce0537b2010-06-29 21:05:21 -0700233 mCache.clear();
Romain Guy912a7b32011-07-26 18:57:28 -0700234 TEXTURE_LOGD("TextureCache:clear(), mSize = %d", mSize);
Romain Guyce0537b2010-06-29 21:05:21 -0700235}
236
Romain Guyeca0ca22011-11-04 15:12:29 -0700237void 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 Craikd218a922014-01-02 17:13:34 -0800252void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) {
Romain Guyc1396e92010-06-30 17:56:19 -0700253 SkAutoLockPixels alp(*bitmap);
Romain Guy9aaa8262010-09-08 15:15:43 -0700254
Romain Guyc1396e92010-06-30 17:56:19 -0700255 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000256 ALOGE("Cannot generate texture from bitmap");
Romain Guyc1396e92010-06-30 17:56:19 -0700257 return;
258 }
259
Chris Craik70850ea2014-11-18 10:49:23 -0800260 ATRACE_FORMAT("Upload %ux%u Texture", bitmap->width(), bitmap->height());
John Reckec4cefc2014-07-29 09:49:13 -0700261
Romain Guy52439572012-10-17 12:14:11 -0700262 // 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 Craik117bdbc2015-02-05 10:12:38 -0800264 const bool canMipMap = Caches::getInstance().extensions().hasNPot();
Romain Guy52439572012-10-17 12:14:11 -0700265
Romain Guy713e1bb2012-10-16 18:44:09 -0700266 // If the texture had mipmap enabled but not anymore,
267 // force a glTexImage2D to discard the mipmap levels
Romain Guy29d89972010-09-22 16:10:57 -0700268 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
Romain Guy713e1bb2012-10-16 18:44:09 -0700269 bitmap->height() != int(texture->height) ||
Romain Guy52439572012-10-17 12:14:11 -0700270 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
Romain Guyce0537b2010-06-29 21:05:21 -0700271
Romain Guy8c749f82010-09-22 14:13:32 -0700272 if (!regenerate) {
Romain Guyfe880942010-06-30 16:05:32 -0700273 glGenTextures(1, &texture->id);
274 }
275
Romain Guy8c749f82010-09-22 14:13:32 -0700276 texture->generation = bitmap->getGenerationID();
277 texture->width = bitmap->width();
278 texture->height = bitmap->height();
279
Chris Craik44eb2c02015-01-29 09:45:09 -0800280 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc1396e92010-06-30 17:56:19 -0700281
Mike Reed1103b322014-07-08 12:36:44 -0400282 switch (bitmap->colorType()) {
283 case kAlpha_8_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800284 uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700285 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy8c749f82010-09-22 14:13:32 -0700286 texture->blend = true;
Romain Guybd0e6aa2010-07-22 18:50:12 -0700287 break;
Mike Reed1103b322014-07-08 12:36:44 -0400288 case kRGB_565_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800289 uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700290 texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
Romain Guyc1396e92010-06-30 17:56:19 -0700291 texture->blend = false;
Romain Guyc1396e92010-06-30 17:56:19 -0700292 break;
Mike Reed1103b322014-07-08 12:36:44 -0400293 case kN32_SkColorType:
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800294 uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(),
Romain Guy318ae7b2013-09-24 18:44:54 -0700295 texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guye9e7fd02010-08-19 14:45:42 -0700296 // Do this after calling getPixels() to make sure Skia's deferred
297 // decoding happened
298 texture->blend = !bitmap->isOpaque();
Romain Guyc1396e92010-06-30 17:56:19 -0700299 break;
Mike Reed1103b322014-07-08 12:36:44 -0400300 case kARGB_4444_SkColorType:
301 case kIndex_8_SkColorType:
Romain Guy5b3b3522010-10-27 18:57:51 -0700302 uploadLoFiTexture(resize, bitmap, texture->width, texture->height);
Romain Guyb37cbec2011-02-24 17:21:29 -0800303 texture->blend = !bitmap->isOpaque();
Romain Guy5b3b3522010-10-27 18:57:51 -0700304 break;
Romain Guyc1396e92010-06-30 17:56:19 -0700305 default:
Mike Reed1103b322014-07-08 12:36:44 -0400306 ALOGW("Unsupported bitmap colorType: %d", bitmap->colorType());
Romain Guyc1396e92010-06-30 17:56:19 -0700307 break;
308 }
Romain Guyce0537b2010-06-29 21:05:21 -0700309
Romain Guy52439572012-10-17 12:14:11 -0700310 if (canMipMap) {
Romain Guy713e1bb2012-10-16 18:44:09 -0700311 texture->mipMap = bitmap->hasHardwareMipMap();
312 if (texture->mipMap) {
313 glGenerateMipmap(GL_TEXTURE_2D);
314 }
315 }
316
Romain Guyd21b6e12011-11-30 20:21:23 -0800317 if (!regenerate) {
318 texture->setFilter(GL_NEAREST);
319 texture->setWrap(GL_CLAMP_TO_EDGE);
320 }
Romain Guyce0537b2010-06-29 21:05:21 -0700321}
322
Chris Craikd218a922014-01-02 17:13:34 -0800323void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap,
Romain Guy7adaf3d2010-10-05 14:58:09 -0700324 uint32_t width, uint32_t height) {
325 SkBitmap rgbaBitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400326 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(width, height, bitmap->alphaType()));
Romain Guy7adaf3d2010-10-05 14:58:09 -0700327 rgbaBitmap.eraseColor(0);
328
329 SkCanvas canvas(rgbaBitmap);
Chris Craikd41c4d82015-01-05 15:51:13 -0800330 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, nullptr);
Romain Guy7adaf3d2010-10-05 14:58:09 -0700331
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800332 uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), rgbaBitmap.bytesPerPixel(),
333 width, height, GL_UNSIGNED_BYTE, rgbaBitmap.getPixels());
Romain Guy7adaf3d2010-10-05 14:58:09 -0700334}
335
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800336void TextureCache::uploadToTexture(bool resize, GLenum format, GLsizei stride, GLsizei bpp,
Romain Guy318ae7b2013-09-24 18:44:54 -0700337 GLsizei width, GLsizei height, GLenum type, const GLvoid * data) {
Chris Craik37424b32015-01-20 14:15:51 -0800338 glPixelStorei(GL_UNPACK_ALIGNMENT, bpp);
Chris Craik117bdbc2015-02-05 10:12:38 -0800339 const bool useStride = stride != width
340 && Caches::getInstance().extensions().hasUnpackRowLength();
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800341 if ((stride == width) || useStride) {
342 if (useStride) {
343 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
344 }
Romain Guy318ae7b2013-09-24 18:44:54 -0700345
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800346 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 Guy8c749f82010-09-22 14:13:32 -0700355 } else {
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800356 // 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 Guy318ae7b2013-09-24 18:44:54 -0700358
Lu, Shenghuac5e0a292013-11-27 20:16:43 +0800359 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 Guy318ae7b2013-09-24 18:44:54 -0700377 }
Romain Guy8c749f82010-09-22 14:13:32 -0700378}
379
Romain Guyce0537b2010-06-29 21:05:21 -0700380}; // namespace uirenderer
381}; // namespace android