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) { 31623d22319374074cdf1f1339e7cda4a1f7527f64John Reck // The wrapped-texture case, usually means a SurfaceTexture 32623d22319374074cdf1f1339e7cda4a1f7527f64John Reck case 0: 33623d22319374074cdf1f1339e7cda4a1f7527f64John Reck return 0; 3438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case GL_ALPHA: 3538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return 1; 3638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case GL_RGB: 3738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return 3; 3838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case GL_RGBA: 3938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return 4; 401d4e6a0901e5d26f4319ed173b4aa7b907350d93John Reck case GL_RGBA16F: 411d4e6a0901e5d26f4319ed173b4aa7b907350d93John Reck return 16; 421d4e6a0901e5d26f4319ed173b4aa7b907350d93John Reck default: 431d4e6a0901e5d26f4319ed173b4aa7b907350d93John Reck LOG_ALWAYS_FATAL("UNKNOWN FORMAT %d", glFormat); 4438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 4538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 4638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 478aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force, 488aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy GLenum renderTarget) { 498aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 5048247a2956f34d5d709660869273e0f7356e42b6John Reck if (force || wrapS != mWrapS || wrapT != mWrapT) { 518aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mWrapS = wrapS; 528aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mWrapT = wrapT; 538aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 548aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy if (bindTexture) { 5538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(renderTarget, mId); 568aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 578aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 588aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS); 598aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT); 608aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 618aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy} 628aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 638aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force, 648aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy GLenum renderTarget) { 658aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 6648247a2956f34d5d709660869273e0f7356e42b6John Reck if (force || min != mMinFilter || mag != mMagFilter) { 678aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mMinFilter = min; 688aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy mMagFilter = mag; 698aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 708aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy if (bindTexture) { 7138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(renderTarget, mId); 728aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 738aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 748aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR; 758aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 768aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min); 778aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag); 788aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy } 798aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy} 808aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy 8138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::deleteTexture() { 8238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().deleteTexture(mId); 8338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mId = 0; 8438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 8538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 8638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckbool Texture::updateSize(uint32_t width, uint32_t height, GLint format) { 8738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (mWidth == width && mHeight == height && mFormat == format) { 8838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return false; 8938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 9038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mWidth = width; 9138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mHeight = height; 9238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mFormat = format; 9338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck notifySizeChanged(mWidth * mHeight * bytesPerPixel(mFormat)); 9438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return true; 9538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 9638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 9748247a2956f34d5d709660869273e0f7356e42b6John Reckvoid Texture::resetCachedParams() { 9848247a2956f34d5d709660869273e0f7356e42b6John Reck mWrapS = GL_REPEAT; 9948247a2956f34d5d709660869273e0f7356e42b6John Reck mWrapT = GL_REPEAT; 10048247a2956f34d5d709660869273e0f7356e42b6John Reck mMinFilter = GL_NEAREST_MIPMAP_LINEAR; 10148247a2956f34d5d709660869273e0f7356e42b6John Reck mMagFilter = GL_LINEAR; 10248247a2956f34d5d709660869273e0f7356e42b6John Reck} 10348247a2956f34d5d709660869273e0f7356e42b6John Reck 10438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::upload(GLint internalformat, uint32_t width, uint32_t height, 10538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLenum format, GLenum type, const void* pixels) { 106975591a7af883d866d86ab819e164c6004694744John Reck GL_CHECKPOINT(MODERATE); 10766f65cb345e5f4e4bdb022e3e8a6c90a1575168eJohn Reck bool needsAlloc = updateSize(width, height, internalformat); 10838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!mId) { 10938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glGenTextures(1, &mId); 11038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck needsAlloc = true; 11148247a2956f34d5d709660869273e0f7356e42b6John Reck resetCachedParams(); 11238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 11338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId); 11438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (needsAlloc) { 11538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, 11638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck format, type, pixels); 11766f65cb345e5f4e4bdb022e3e8a6c90a1575168eJohn Reck } else if (pixels) { 11838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, 11938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck format, type, pixels); 12038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 121975591a7af883d866d86ab819e164c6004694744John Reck GL_CHECKPOINT(MODERATE); 12238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 12338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 12438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp, 12538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLsizei width, GLsizei height, const GLvoid * data) { 12638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 12738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck const bool useStride = stride != width 12838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck && Caches::getInstance().extensions().hasUnpackRowLength(); 12938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if ((stride == width) || useStride) { 13038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (useStride) { 13138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); 13238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 13338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 13438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (resize) { 13538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); 13638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 13738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); 13838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 13938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 14038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (useStride) { 14138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 14238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 14338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 14438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer 14538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // if the stride doesn't match the width 14638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 14738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLvoid * temp = (GLvoid *) malloc(width * height * bpp); 14838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!temp) return; 14938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 15038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uint8_t * pDst = (uint8_t *)temp; 15138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uint8_t * pSrc = (uint8_t *)data; 15238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck for (GLsizei i = 0; i < height; i++) { 15338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck memcpy(pDst, pSrc, width * bpp); 15438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck pDst += width * bpp; 15538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck pSrc += stride * bpp; 15638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 15738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 15838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (resize) { 15938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp); 16038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 16138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp); 16238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 16338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 16438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck free(temp); 16538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 16638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 16738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 16838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void uploadSkBitmapToTexture(const SkBitmap& bitmap, 16938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bool resize, GLenum format, GLenum type) { 17038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(), 17138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bitmap.width(), bitmap.height(), bitmap.getPixels()); 17238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 17338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 17438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstatic void colorTypeToGlFormatAndType(SkColorType colorType, 17538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLint* outFormat, GLint* outType) { 17638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck switch (colorType) { 17738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kAlpha_8_SkColorType: 17838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outFormat = GL_ALPHA; 17938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outType = GL_UNSIGNED_BYTE; 18038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 18138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kRGB_565_SkColorType: 18238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outFormat = GL_RGB; 18338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outType = GL_UNSIGNED_SHORT_5_6_5; 18438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 18538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // ARGB_4444 and Index_8 are both upconverted to RGBA_8888 18638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kARGB_4444_SkColorType: 18738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kIndex_8_SkColorType: 18838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck case kN32_SkColorType: 18938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outFormat = GL_RGBA; 19038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck *outType = GL_UNSIGNED_BYTE; 19138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 19288d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger case kGray_8_SkColorType: 19388d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger *outFormat = GL_LUMINANCE; 19488d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger *outType = GL_UNSIGNED_BYTE; 19588d842fedf9a6c03dee1f2c91bc7a1f51c8438daDerek Sollenberger break; 19638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck default: 19738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType); 19838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck break; 19938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 20038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 20138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 20238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::upload(const SkBitmap& bitmap) { 20338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck SkAutoLockPixels alp(bitmap); 20438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 20538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!bitmap.readyToDraw()) { 20638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck ALOGE("Cannot generate texture from bitmap"); 20738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck return; 20838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 20938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 21038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height()); 21138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 21238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // We could also enable mipmapping if both bitmap dimensions are powers 21338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // of 2 but we'd have to deal with size changes. Let's keep this simple 21438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck const bool canMipMap = mCaches.extensions().hasNPot(); 21538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 21638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // If the texture had mipmap enabled but not anymore, 21738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // force a glTexImage2D to discard the mipmap levels 21838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap(); 21948247a2956f34d5d709660869273e0f7356e42b6John Reck bool setDefaultParams = false; 22038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 22138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (!mId) { 22238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glGenTextures(1, &mId); 22338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck needsAlloc = true; 22448247a2956f34d5d709660869273e0f7356e42b6John Reck setDefaultParams = true; 22538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 22638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 22738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck GLint format, type; 22838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type); 22938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 23038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (updateSize(bitmap.width(), bitmap.height(), format)) { 23138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck needsAlloc = true; 23238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 23338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 23438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck blend = !bitmap.isOpaque(); 23538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mCaches.textureState().bindTexture(mId); 23638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 23738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType 23838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck || bitmap.colorType() == kIndex_8_SkColorType)) { 23938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck SkBitmap rgbaBitmap; 24038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight, 24138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck bitmap.alphaType())); 24238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck rgbaBitmap.eraseColor(0); 24338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 24438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck SkCanvas canvas(rgbaBitmap); 24538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); 24638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 24738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type); 24838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } else { 24938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck uploadSkBitmapToTexture(bitmap, needsAlloc, format, type); 25038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 25138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 25238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (canMipMap) { 25338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mipMap = bitmap.hasHardwareMipMap(); 25438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck if (mipMap) { 25538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck glGenerateMipmap(GL_TEXTURE_2D); 25638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 25738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 25838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 25948247a2956f34d5d709660869273e0f7356e42b6John Reck if (setDefaultParams) { 26038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck setFilter(GL_NEAREST); 26138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck setWrap(GL_CLAMP_TO_EDGE); 26238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck } 26338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck} 26438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck 26538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckvoid Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) { 26638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mId = id; 26738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mWidth = width; 26838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mHeight = height; 26938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck mFormat = format; 27038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck // We're wrapping an existing texture, so don't double count this memory 27138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck notifySizeChanged(0); 272be1b127c7bec252e0c6ab0e06ed6babed07d496fRomain Guy} 273be1b127c7bec252e0c6ab0e06ed6babed07d496fRomain Guy 2748aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}; // namespace uirenderer 2758aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}; // namespace android 276