Texture.cpp revision caaaa66e57293e4a6f312649bf472eab84d5c7fe
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/MathUtils.h" 21#include "utils/TraceUtils.h" 22 23#include <utils/Log.h> 24 25#include <math/mat4.h> 26 27#include <SkCanvas.h> 28 29namespace android { 30namespace uirenderer { 31 32// Number of bytes used by a texture in the given format 33static int bytesPerPixel(GLint glFormat) { 34 switch (glFormat) { 35 // The wrapped-texture case, usually means a SurfaceTexture 36 case 0: 37 return 0; 38 case GL_LUMINANCE: 39 case GL_ALPHA: 40 return 1; 41 case GL_SRGB8: 42 case GL_RGB: 43 return 3; 44 case GL_SRGB8_ALPHA8: 45 case GL_RGBA: 46 return 4; 47 case GL_RGBA16F: 48 return 8; 49 default: 50 LOG_ALWAYS_FATAL("UNKNOWN FORMAT 0x%x", glFormat); 51 } 52} 53 54void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force) { 55 if (force || wrapS != mWrapS || wrapT != mWrapT) { 56 mWrapS = wrapS; 57 mWrapT = wrapT; 58 59 if (bindTexture) { 60 mCaches.textureState().bindTexture(mTarget, mId); 61 } 62 63 glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, wrapS); 64 glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, wrapT); 65 } 66} 67 68void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force) { 69 if (force || min != mMinFilter || mag != mMagFilter) { 70 mMinFilter = min; 71 mMagFilter = mag; 72 73 if (bindTexture) { 74 mCaches.textureState().bindTexture(mTarget, mId); 75 } 76 77 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR; 78 79 glTexParameteri(mTarget, GL_TEXTURE_MIN_FILTER, min); 80 glTexParameteri(mTarget, GL_TEXTURE_MAG_FILTER, mag); 81 } 82} 83 84void Texture::deleteTexture() { 85 mCaches.textureState().deleteTexture(mId); 86 mId = 0; 87 mTarget = GL_NONE; 88 if (mEglImageHandle != EGL_NO_IMAGE_KHR) { 89 EGLDisplay eglDisplayHandle = eglGetCurrentDisplay(); 90 eglDestroyImageKHR(eglDisplayHandle, mEglImageHandle); 91 mEglImageHandle = EGL_NO_IMAGE_KHR; 92 } 93} 94 95bool Texture::updateLayout(uint32_t width, uint32_t height, GLint internalFormat, 96 GLint format, GLenum target) { 97 if (mWidth == width 98 && mHeight == height 99 && mFormat == format 100 && mInternalFormat == internalFormat 101 && mTarget == target) { 102 return false; 103 } 104 mWidth = width; 105 mHeight = height; 106 mFormat = format; 107 mInternalFormat = internalFormat; 108 mTarget = target; 109 notifySizeChanged(mWidth * mHeight * bytesPerPixel(internalFormat)); 110 return true; 111} 112 113void Texture::resetCachedParams() { 114 mWrapS = GL_REPEAT; 115 mWrapT = GL_REPEAT; 116 mMinFilter = GL_NEAREST_MIPMAP_LINEAR; 117 mMagFilter = GL_LINEAR; 118} 119 120void Texture::upload(GLint internalFormat, uint32_t width, uint32_t height, 121 GLenum format, GLenum type, const void* pixels) { 122 GL_CHECKPOINT(MODERATE); 123 bool needsAlloc = updateLayout(width, height, internalFormat, format, GL_TEXTURE_2D); 124 if (!mId) { 125 glGenTextures(1, &mId); 126 needsAlloc = true; 127 resetCachedParams(); 128 } 129 mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId); 130 if (needsAlloc) { 131 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, 132 format, type, pixels); 133 } else if (pixels) { 134 glTexSubImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, 135 format, type, pixels); 136 } 137 GL_CHECKPOINT(MODERATE); 138} 139 140void Texture::uploadHardwareBitmapToTexture(GraphicBuffer* buffer) { 141 EGLDisplay eglDisplayHandle = eglGetCurrentDisplay(); 142 if (mEglImageHandle != EGL_NO_IMAGE_KHR) { 143 eglDestroyImageKHR(eglDisplayHandle, mEglImageHandle); 144 mEglImageHandle = EGL_NO_IMAGE_KHR; 145 } 146 mEglImageHandle = eglCreateImageKHR(eglDisplayHandle, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, 147 buffer->getNativeBuffer(), 0); 148 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, mEglImageHandle); 149} 150 151static void uploadToTexture(bool resize, GLint internalFormat, GLenum format, GLenum type, 152 GLsizei stride, GLsizei bpp, GLsizei width, GLsizei height, const GLvoid * data) { 153 154 const bool useStride = stride != width 155 && Caches::getInstance().extensions().hasUnpackRowLength(); 156 if ((stride == width) || useStride) { 157 if (useStride) { 158 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); 159 } 160 161 if (resize) { 162 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data); 163 } else { 164 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data); 165 } 166 167 if (useStride) { 168 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 169 } 170 } else { 171 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer 172 // if the stride doesn't match the width 173 174 GLvoid * temp = (GLvoid *) malloc(width * height * bpp); 175 if (!temp) return; 176 177 uint8_t * pDst = (uint8_t *)temp; 178 uint8_t * pSrc = (uint8_t *)data; 179 for (GLsizei i = 0; i < height; i++) { 180 memcpy(pDst, pSrc, width * bpp); 181 pDst += width * bpp; 182 pSrc += stride * bpp; 183 } 184 185 if (resize) { 186 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, temp); 187 } else { 188 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp); 189 } 190 191 free(temp); 192 } 193} 194 195void Texture::colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType, 196 bool needSRGB, GLint* outInternalFormat, GLint* outFormat, GLint* outType) { 197 switch (colorType) { 198 case kAlpha_8_SkColorType: 199 *outFormat = GL_ALPHA; 200 *outInternalFormat = GL_ALPHA; 201 *outType = GL_UNSIGNED_BYTE; 202 break; 203 case kRGB_565_SkColorType: 204 if (needSRGB) { 205 // We would ideally use a GL_RGB/GL_SRGB8 texture but the 206 // intermediate Skia bitmap needs to be ARGB_8888 207 *outFormat = GL_RGBA; 208 *outInternalFormat = caches.rgbaInternalFormat(); 209 *outType = GL_UNSIGNED_BYTE; 210 } else { 211 *outFormat = GL_RGB; 212 *outInternalFormat = GL_RGB; 213 *outType = GL_UNSIGNED_SHORT_5_6_5; 214 } 215 break; 216 // ARGB_4444 and Index_8 are both upconverted to RGBA_8888 217 case kARGB_4444_SkColorType: 218 case kIndex_8_SkColorType: 219 case kN32_SkColorType: 220 *outFormat = GL_RGBA; 221 *outInternalFormat = caches.rgbaInternalFormat(needSRGB); 222 *outType = GL_UNSIGNED_BYTE; 223 break; 224 case kGray_8_SkColorType: 225 *outFormat = GL_LUMINANCE; 226 *outInternalFormat = GL_LUMINANCE; 227 *outType = GL_UNSIGNED_BYTE; 228 break; 229 case kRGBA_F16_SkColorType: 230 // This format is always linear 231 *outFormat = GL_RGBA; 232 *outInternalFormat = GL_RGBA16F; 233 *outType = GL_HALF_FLOAT; 234 break; 235 default: 236 LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType); 237 break; 238 } 239} 240 241SkBitmap Texture::uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending, 242 sk_sp<SkColorSpace> sRGB) { 243 SkBitmap rgbaBitmap; 244 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(bitmap.width(), bitmap.height(), 245 bitmap.info().alphaType(), hasLinearBlending ? sRGB : nullptr)); 246 rgbaBitmap.eraseColor(0); 247 SkCanvas canvas(rgbaBitmap); 248 canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); 249 return rgbaBitmap; 250} 251 252bool Texture::hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending) { 253 return info.colorType() == kARGB_4444_SkColorType 254 || info.colorType() == kIndex_8_SkColorType 255 || (info.colorType() == kRGB_565_SkColorType 256 && hasLinearBlending 257 && info.colorSpace()->isSRGB()); 258} 259 260void Texture::upload(Bitmap& bitmap) { 261 if (!bitmap.readyToDraw()) { 262 ALOGE("Cannot generate texture from bitmap"); 263 return; 264 } 265 266 ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height()); 267 268 // We could also enable mipmapping if both bitmap dimensions are powers 269 // of 2 but we'd have to deal with size changes. Let's keep this simple 270 const bool canMipMap = mCaches.extensions().hasNPot(); 271 272 // If the texture had mipmap enabled but not anymore, 273 // force a glTexImage2D to discard the mipmap levels 274 bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap(); 275 bool setDefaultParams = false; 276 277 if (!mId) { 278 glGenTextures(1, &mId); 279 needsAlloc = true; 280 setDefaultParams = true; 281 } 282 283 bool hasLinearBlending = mCaches.extensions().hasLinearBlending(); 284 bool needSRGB = transferFunctionCloseToSRGB(bitmap.info().colorSpace()); 285 286 GLint internalFormat, format, type; 287 colorTypeToGlFormatAndType(mCaches, bitmap.colorType(), 288 needSRGB && hasLinearBlending, &internalFormat, &format, &type); 289 290 mConnector.reset(); 291 292 // RGBA16F is always extended sRGB, alpha masks don't have color profiles 293 if (internalFormat != GL_RGBA16F && internalFormat != GL_ALPHA) { 294 SkColorSpace* colorSpace = bitmap.info().colorSpace(); 295 // If the bitmap is sRGB we don't need conversion 296 if (colorSpace != nullptr && !colorSpace->isSRGB()) { 297 SkMatrix44 xyzMatrix(SkMatrix44::kUninitialized_Constructor); 298 if (!colorSpace->toXYZD50(&xyzMatrix)) { 299 ALOGW("Incompatible color space!"); 300 } else { 301 SkColorSpaceTransferFn fn; 302 if (!colorSpace->isNumericalTransferFn(&fn)) { 303 ALOGW("Incompatible color space, no numerical transfer function!"); 304 } else { 305 float data[16]; 306 xyzMatrix.asColMajorf(data); 307 308 ColorSpace::TransferParameters p = 309 {fn.fG, fn.fA, fn.fB, fn.fC, fn.fD, fn.fE, fn.fF}; 310 ColorSpace src("Unnamed", mat4f((const float*) &data[0]).upperLeft(), p); 311 mConnector.reset(new ColorSpaceConnector(src, ColorSpace::sRGB())); 312 313 // A non-sRGB color space might have a transfer function close enough to sRGB 314 // that we can save shader instructions by using an sRGB sampler 315 // This is only possible if we have hardware support for sRGB textures 316 if (needSRGB && internalFormat == GL_RGBA 317 && mCaches.extensions().hasSRGB() && !bitmap.isHardware()) { 318 internalFormat = GL_SRGB8_ALPHA8; 319 } 320 } 321 } 322 } 323 } 324 325 GLenum target = bitmap.isHardware() ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D; 326 needsAlloc |= updateLayout(bitmap.width(), bitmap.height(), internalFormat, format, target); 327 328 blend = !bitmap.isOpaque(); 329 mCaches.textureState().bindTexture(mTarget, mId); 330 331 // TODO: Handle sRGB gray bitmaps 332 if (CC_UNLIKELY(hasUnsupportedColorType(bitmap.info(), hasLinearBlending))) { 333 SkBitmap skBitmap; 334 bitmap.getSkBitmap(&skBitmap); 335 sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(); 336 SkBitmap rgbaBitmap = uploadToN32(skBitmap, hasLinearBlending, std::move(sRGB)); 337 uploadToTexture(needsAlloc, internalFormat, format, type, rgbaBitmap.rowBytesAsPixels(), 338 rgbaBitmap.bytesPerPixel(), rgbaBitmap.width(), 339 rgbaBitmap.height(), rgbaBitmap.getPixels()); 340 } else if (bitmap.isHardware()) { 341 uploadHardwareBitmapToTexture(bitmap.graphicBuffer()); 342 } else { 343 uploadToTexture(needsAlloc, internalFormat, format, type, bitmap.rowBytesAsPixels(), 344 bitmap.info().bytesPerPixel(), bitmap.width(), bitmap.height(), bitmap.pixels()); 345 } 346 347 if (canMipMap) { 348 mipMap = bitmap.hasHardwareMipMap(); 349 if (mipMap) { 350 glGenerateMipmap(GL_TEXTURE_2D); 351 } 352 } 353 354 if (setDefaultParams) { 355 setFilter(GL_NEAREST); 356 setWrap(GL_CLAMP_TO_EDGE); 357 } 358} 359 360void Texture::wrap(GLuint id, uint32_t width, uint32_t height, 361 GLint internalFormat, GLint format, GLenum target) { 362 mId = id; 363 mWidth = width; 364 mHeight = height; 365 mFormat = format; 366 mInternalFormat = internalFormat; 367 mTarget = target; 368 mConnector.reset(); 369 // We're wrapping an existing texture, so don't double count this memory 370 notifySizeChanged(0); 371} 372 373TransferFunctionType Texture::getTransferFunctionType() const { 374 if (mConnector.get() != nullptr && mInternalFormat != GL_SRGB8_ALPHA8) { 375 const ColorSpace::TransferParameters& p = mConnector->getSource().getTransferParameters(); 376 if (MathUtils::isZero(p.e) && MathUtils::isZero(p.f)) { 377 if (MathUtils::areEqual(p.a, 1.0f) && MathUtils::isZero(p.b) 378 && MathUtils::isZero(p.c) && MathUtils::isZero(p.d)) { 379 if (MathUtils::areEqual(p.g, 1.0f)) { 380 return TransferFunctionType::None; 381 } 382 return TransferFunctionType::Gamma; 383 } 384 return TransferFunctionType::Limited; 385 } 386 return TransferFunctionType::Full; 387 } 388 return TransferFunctionType::None; 389} 390 391}; // namespace uirenderer 392}; // namespace android 393