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