Texture.cpp revision 975591a7af883d866d86ab819e164c6004694744
18aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy/* 28aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * Copyright (C) 2013 The Android Open Source Project 38aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * 48aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 58aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * you may not use this file except in compliance with the License. 68aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * You may obtain a copy of the License at 78aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * 88aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * http://www.apache.org/licenses/LICENSE-2.0 98aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * 108aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * Unless required by applicable law or agreed to in writing, software 118aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * distributed under the License is distributed on an "AS IS" BASIS, 128aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * See the License for the specific language governing permissions and 148aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy * limitations under the License. 158aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy */ 168aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 178aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy#include "Caches.h" 188aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy#include "Texture.h" 199372ac3621848085e77b867f220c0b5ffce4010dJohn Reck#include "utils/GLUtils.h" 2038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include "utils/TraceUtils.h" 2138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 2238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <utils/Log.h> 2338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 2438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <SkCanvas.h> 258aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 268aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guynamespace android { 278aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guynamespace uirenderer { 288aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 2938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic int bytesPerPixel(GLint glFormat) { 3038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck switch (glFormat) { 3138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case GL_ALPHA: 3238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return 1; 3338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case GL_RGB: 3438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return 3; 3538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case GL_RGBA: 3638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck default: 3738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return 4; 3838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 3938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 4038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 418aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force, 428aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy GLenum renderTarget) { 438aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 4448247a2956f34d5d709660869273e0f7356e42b6John Reck if (force || wrapS != mWrapS || wrapT != mWrapT) { 458aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mWrapS = wrapS; 468aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mWrapT = wrapT; 478aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 488aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy if (bindTexture) { 4938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(renderTarget, mId); 508aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 518aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 528aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS); 538aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT); 548aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 558aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy} 568aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 578aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force, 588aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy GLenum renderTarget) { 598aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 6048247a2956f34d5d709660869273e0f7356e42b6John Reck if (force || min != mMinFilter || mag != mMagFilter) { 618aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mMinFilter = min; 628aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mMagFilter = mag; 638aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 648aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy if (bindTexture) { 6538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(renderTarget, mId); 668aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 678aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 688aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR; 698aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 708aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min); 718aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag); 728aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 738aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy} 748aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 7538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::deleteTexture() { 7638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().deleteTexture(mId); 7738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mId = 0; 7838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 7938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 8038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckbool Texture::updateSize(uint32_t width, uint32_t height, GLint format) { 8138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (mWidth == width && mHeight == height && mFormat == format) { 8238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return false; 8338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 8438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mWidth = width; 8538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mHeight = height; 8638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mFormat = format; 8738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck notifySizeChanged(mWidth * mHeight * bytesPerPixel(mFormat)); 8838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return true; 8938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 9038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 9148247a2956f34d5d709660869273e0f7356e42b6John Reckvoid Texture::resetCachedParams() { 9248247a2956f34d5d709660869273e0f7356e42b6John Reck mWrapS = GL_REPEAT; 9348247a2956f34d5d709660869273e0f7356e42b6John Reck mWrapT = GL_REPEAT; 9448247a2956f34d5d709660869273e0f7356e42b6John Reck mMinFilter = GL_NEAREST_MIPMAP_LINEAR; 9548247a2956f34d5d709660869273e0f7356e42b6John Reck mMagFilter = GL_LINEAR; 9648247a2956f34d5d709660869273e0f7356e42b6John Reck} 9748247a2956f34d5d709660869273e0f7356e42b6John Reck 9838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::upload(GLint internalformat, uint32_t width, uint32_t height, 9938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLenum format, GLenum type, const void* pixels) { 100975591a7af883d866d86ab819e164c6004694744John Reck GL_CHECKPOINT(MODERATE); 10166f65cb345e5f4e4bdb022e3e8a6c90a1575168eJohn Reck bool needsAlloc = updateSize(width, height, internalformat); 10238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!mId) { 10338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glGenTextures(1, &mId); 10438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck needsAlloc = true; 10548247a2956f34d5d709660869273e0f7356e42b6John Reck resetCachedParams(); 10638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 10738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId); 10838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (needsAlloc) { 10938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, 11038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck format, type, pixels); 11166f65cb345e5f4e4bdb022e3e8a6c90a1575168eJohn Reck } else if (pixels) { 11238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, 11338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck format, type, pixels); 11438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 115975591a7af883d866d86ab819e164c6004694744John Reck GL_CHECKPOINT(MODERATE); 11638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 11738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 11838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp, 11938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLsizei width, GLsizei height, const GLvoid * data) { 12038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 12138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck const bool useStride = stride != width 12238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck && Caches::getInstance().extensions().hasUnpackRowLength(); 12338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if ((stride == width) || useStride) { 12438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (useStride) { 12538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); 12638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 12738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 12838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (resize) { 12938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); 13038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 13138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); 13238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 13338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 13438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (useStride) { 13538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 13638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 13738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 13838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer 13938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // if the stride doesn't match the width 14038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 14138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLvoid * temp = (GLvoid *) malloc(width * height * bpp); 14238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!temp) return; 14338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 14438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uint8_t * pDst = (uint8_t *)temp; 14538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uint8_t * pSrc = (uint8_t *)data; 14638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck for (GLsizei i = 0; i < height; i++) { 14738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck memcpy(pDst, pSrc, width * bpp); 14838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck pDst += width * bpp; 14938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck pSrc += stride * bpp; 15038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 15138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 15238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (resize) { 15338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp); 15438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 15538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp); 15638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 15738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 15838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck free(temp); 15938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 16038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 16138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 16238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void uploadSkBitmapToTexture(const SkBitmap& bitmap, 16338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bool resize, GLenum format, GLenum type) { 16438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(), 16538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bitmap.width(), bitmap.height(), bitmap.getPixels()); 16638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 16738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 16838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void colorTypeToGlFormatAndType(SkColorType colorType, 16938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLint* outFormat, GLint* outType) { 17038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck switch (colorType) { 17138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kAlpha_8_SkColorType: 17238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outFormat = GL_ALPHA; 17338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outType = GL_UNSIGNED_BYTE; 17438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 17538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kRGB_565_SkColorType: 17638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outFormat = GL_RGB; 17738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outType = GL_UNSIGNED_SHORT_5_6_5; 17838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 17938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // ARGB_4444 and Index_8 are both upconverted to RGBA_8888 18038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kARGB_4444_SkColorType: 18138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kIndex_8_SkColorType: 18238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kN32_SkColorType: 18338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outFormat = GL_RGBA; 18438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outType = GL_UNSIGNED_BYTE; 18538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 18688d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger case kGray_8_SkColorType: 18788d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger *outFormat = GL_LUMINANCE; 18888d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger *outType = GL_UNSIGNED_BYTE; 18988d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger break; 19038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck default: 19138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType); 19238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 19338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 19438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 19538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 19638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::upload(const SkBitmap& bitmap) { 19738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck SkAutoLockPixels alp(bitmap); 19838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 19938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!bitmap.readyToDraw()) { 20038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck ALOGE("Cannot generate texture from bitmap"); 20138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return; 20238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 20338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 20438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height()); 20538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 20638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // We could also enable mipmapping if both bitmap dimensions are powers 20738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // of 2 but we'd have to deal with size changes. Let's keep this simple 20838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck const bool canMipMap = mCaches.extensions().hasNPot(); 20938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 21038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // If the texture had mipmap enabled but not anymore, 21138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // force a glTexImage2D to discard the mipmap levels 21238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap(); 21348247a2956f34d5d709660869273e0f7356e42b6John Reck bool setDefaultParams = false; 21438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 21538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!mId) { 21638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glGenTextures(1, &mId); 21738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck needsAlloc = true; 21848247a2956f34d5d709660869273e0f7356e42b6John Reck setDefaultParams = true; 21938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 22038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 22138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLint format, type; 22238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type); 22338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 22438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (updateSize(bitmap.width(), bitmap.height(), format)) { 22538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck needsAlloc = true; 22638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 22738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 22838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck blend = !bitmap.isOpaque(); 22938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(mId); 23038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 23138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType 23238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck || bitmap.colorType() == kIndex_8_SkColorType)) { 23338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck SkBitmap rgbaBitmap; 23438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight, 23538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bitmap.alphaType())); 23638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck rgbaBitmap.eraseColor(0); 23738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 23838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck SkCanvas canvas(rgbaBitmap); 23938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); 24038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 24138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type); 24238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 24338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uploadSkBitmapToTexture(bitmap, needsAlloc, format, type); 24438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 24538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 24638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (canMipMap) { 24738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mipMap = bitmap.hasHardwareMipMap(); 24838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (mipMap) { 24938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glGenerateMipmap(GL_TEXTURE_2D); 25038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 25138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 25238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 25348247a2956f34d5d709660869273e0f7356e42b6John Reck if (setDefaultParams) { 25438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck setFilter(GL_NEAREST); 25538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck setWrap(GL_CLAMP_TO_EDGE); 25638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 25738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 25838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 25938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) { 26038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mId = id; 26138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mWidth = width; 26238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mHeight = height; 26338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mFormat = format; 26438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // We're wrapping an existing texture, so don't double count this memory 26538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck notifySizeChanged(0); 266be1b127c7bec252e0c6ab0e06ed6babed07d496fRomain Guy} 267be1b127c7bec252e0c6ab0e06ed6babed07d496fRomain Guy 2688aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}; // namespace uirenderer 2698aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}; // namespace android 270