Texture.cpp revision 38e0c32852e3b9d8ca4a9d3791577f52536419cb
1/* 2 * Copyright (C) 2013 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 "Caches.h" 18#include "Texture.h" 19#include "utils/TraceUtils.h" 20 21#include <utils/Log.h> 22 23#include <SkCanvas.h> 24 25namespace android { 26namespace uirenderer { 27 28static int bytesPerPixel(GLint glFormat) { 29 switch (glFormat) { 30 case GL_ALPHA: 31 return 1; 32 case GL_RGB: 33 return 3; 34 case GL_RGBA: 35 default: 36 return 4; 37 } 38} 39 40void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force, 41 GLenum renderTarget) { 42 43 if (mFirstWrap || force || wrapS != mWrapS || wrapT != mWrapT) { 44 mFirstWrap = false; 45 46 mWrapS = wrapS; 47 mWrapT = wrapT; 48 49 if (bindTexture) { 50 mCaches.textureState().bindTexture(renderTarget, mId); 51 } 52 53 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS); 54 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT); 55 } 56} 57 58void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force, 59 GLenum renderTarget) { 60 61 if (mFirstFilter || force || min != mMinFilter || mag != mMagFilter) { 62 mFirstFilter = false; 63 64 mMinFilter = min; 65 mMagFilter = mag; 66 67 if (bindTexture) { 68 mCaches.textureState().bindTexture(renderTarget, mId); 69 } 70 71 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR; 72 73 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min); 74 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag); 75 } 76} 77 78void Texture::deleteTexture() { 79 mCaches.textureState().deleteTexture(mId); 80 mId = 0; 81} 82 83bool Texture::updateSize(uint32_t width, uint32_t height, GLint format) { 84 if (mWidth == width && mHeight == height && mFormat == format) { 85 return false; 86 } 87 mWidth = width; 88 mHeight = height; 89 mFormat = format; 90 notifySizeChanged(mWidth * mHeight * bytesPerPixel(mFormat)); 91 return true; 92} 93 94void Texture::upload(GLint internalformat, uint32_t width, uint32_t height, 95 GLenum format, GLenum type, const void* pixels) { 96 bool needsAlloc = updateSize(width, height, internalformat); 97 if (!needsAlloc && !pixels) { 98 return; 99 } 100 mCaches.textureState().activateTexture(0); 101 if (!mId) { 102 glGenTextures(1, &mId); 103 needsAlloc = true; 104 } 105 mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId); 106 if (needsAlloc) { 107 glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, 108 format, type, pixels); 109 } else { 110 glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, 111 format, type, pixels); 112 } 113} 114 115static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp, 116 GLsizei width, GLsizei height, const GLvoid * data) { 117 118 glPixelStorei(GL_UNPACK_ALIGNMENT, bpp); 119 const bool useStride = stride != width 120 && Caches::getInstance().extensions().hasUnpackRowLength(); 121 if ((stride == width) || useStride) { 122 if (useStride) { 123 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); 124 } 125 126 if (resize) { 127 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data); 128 } else { 129 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); 130 } 131 132 if (useStride) { 133 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 134 } 135 } else { 136 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer 137 // if the stride doesn't match the width 138 139 GLvoid * temp = (GLvoid *) malloc(width * height * bpp); 140 if (!temp) return; 141 142 uint8_t * pDst = (uint8_t *)temp; 143 uint8_t * pSrc = (uint8_t *)data; 144 for (GLsizei i = 0; i < height; i++) { 145 memcpy(pDst, pSrc, width * bpp); 146 pDst += width * bpp; 147 pSrc += stride * bpp; 148 } 149 150 if (resize) { 151 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp); 152 } else { 153 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp); 154 } 155 156 free(temp); 157 } 158} 159 160static void uploadSkBitmapToTexture(const SkBitmap& bitmap, 161 bool resize, GLenum format, GLenum type) { 162 uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(), 163 bitmap.width(), bitmap.height(), bitmap.getPixels()); 164} 165 166static void colorTypeToGlFormatAndType(SkColorType colorType, 167 GLint* outFormat, GLint* outType) { 168 switch (colorType) { 169 case kAlpha_8_SkColorType: 170 *outFormat = GL_ALPHA; 171 *outType = GL_UNSIGNED_BYTE; 172 break; 173 case kRGB_565_SkColorType: 174 *outFormat = GL_RGB; 175 *outType = GL_UNSIGNED_SHORT_5_6_5; 176 break; 177 // ARGB_4444 and Index_8 are both upconverted to RGBA_8888 178 case kARGB_4444_SkColorType: 179 case kIndex_8_SkColorType: 180 case kN32_SkColorType: 181 *outFormat = GL_RGBA; 182 *outType = GL_UNSIGNED_BYTE; 183 break; 184 default: 185 LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType); 186 break; 187 } 188} 189 190void Texture::upload(const SkBitmap& bitmap) { 191 SkAutoLockPixels alp(bitmap); 192 193 if (!bitmap.readyToDraw()) { 194 ALOGE("Cannot generate texture from bitmap"); 195 return; 196 } 197 198 ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height()); 199 200 // We could also enable mipmapping if both bitmap dimensions are powers 201 // of 2 but we'd have to deal with size changes. Let's keep this simple 202 const bool canMipMap = mCaches.extensions().hasNPot(); 203 204 // If the texture had mipmap enabled but not anymore, 205 // force a glTexImage2D to discard the mipmap levels 206 bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap(); 207 208 if (!mId) { 209 glGenTextures(1, &mId); 210 needsAlloc = true; 211 } 212 213 GLint format, type; 214 colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type); 215 216 if (updateSize(bitmap.width(), bitmap.height(), format)) { 217 needsAlloc = true; 218 } 219 220 blend = !bitmap.isOpaque(); 221 mCaches.textureState().bindTexture(mId); 222 223 if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType 224 || bitmap.colorType() == kIndex_8_SkColorType)) { 225 SkBitmap rgbaBitmap; 226 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight, 227 bitmap.alphaType())); 228 rgbaBitmap.eraseColor(0); 229 230 SkCanvas canvas(rgbaBitmap); 231 canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); 232 233 uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type); 234 } else { 235 uploadSkBitmapToTexture(bitmap, needsAlloc, format, type); 236 } 237 238 if (canMipMap) { 239 mipMap = bitmap.hasHardwareMipMap(); 240 if (mipMap) { 241 glGenerateMipmap(GL_TEXTURE_2D); 242 } 243 } 244 245 if (mFirstFilter) { 246 setFilter(GL_NEAREST); 247 } 248 249 if (mFirstWrap) { 250 setWrap(GL_CLAMP_TO_EDGE); 251 } 252} 253 254void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) { 255 mId = id; 256 mWidth = width; 257 mHeight = height; 258 mFormat = format; 259 // We're wrapping an existing texture, so don't double count this memory 260 notifySizeChanged(0); 261} 262 263}; // namespace uirenderer 264}; // namespace android 265