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