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 "Caches.h" |
| 18 | #include "Texture.h" |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 19 | #include "utils/GLUtils.h" |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 20 | #include "utils/TraceUtils.h" |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <SkCanvas.h> |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 29 | static 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 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 41 | void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force, |
| 42 | GLenum renderTarget) { |
| 43 | |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 44 | if (force || wrapS != mWrapS || wrapT != mWrapT) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 45 | mWrapS = wrapS; |
| 46 | mWrapT = wrapT; |
| 47 | |
| 48 | if (bindTexture) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 49 | mCaches.textureState().bindTexture(renderTarget, mId); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS); |
| 53 | glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force, |
| 58 | GLenum renderTarget) { |
| 59 | |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 60 | if (force || min != mMinFilter || mag != mMagFilter) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 61 | mMinFilter = min; |
| 62 | mMagFilter = mag; |
| 63 | |
| 64 | if (bindTexture) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 65 | mCaches.textureState().bindTexture(renderTarget, mId); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 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 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 75 | void Texture::deleteTexture() { |
| 76 | mCaches.textureState().deleteTexture(mId); |
| 77 | mId = 0; |
| 78 | } |
| 79 | |
| 80 | bool 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 | |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 91 | void Texture::resetCachedParams() { |
| 92 | mWrapS = GL_REPEAT; |
| 93 | mWrapT = GL_REPEAT; |
| 94 | mMinFilter = GL_NEAREST_MIPMAP_LINEAR; |
| 95 | mMagFilter = GL_LINEAR; |
| 96 | } |
| 97 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 98 | void Texture::upload(GLint internalformat, uint32_t width, uint32_t height, |
| 99 | GLenum format, GLenum type, const void* pixels) { |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 100 | GL_CHECKPOINT(); |
John Reck | 66f65cb | 2016-01-21 09:08:42 -0800 | [diff] [blame] | 101 | bool needsAlloc = updateSize(width, height, internalformat); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 102 | if (!mId) { |
| 103 | glGenTextures(1, &mId); |
| 104 | needsAlloc = true; |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 105 | resetCachedParams(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 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); |
John Reck | 66f65cb | 2016-01-21 09:08:42 -0800 | [diff] [blame] | 111 | } else if (pixels) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 112 | glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0, |
| 113 | format, type, pixels); |
| 114 | } |
John Reck | 66f65cb | 2016-01-21 09:08:42 -0800 | [diff] [blame] | 115 | GL_CHECKPOINT(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp, |
| 119 | GLsizei width, GLsizei height, const GLvoid * data) { |
| 120 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 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 | |
| 162 | static 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 | |
| 168 | static 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; |
Derek Sollenberger | 88d842f | 2016-01-20 10:37:30 -0500 | [diff] [blame] | 186 | case kGray_8_SkColorType: |
| 187 | *outFormat = GL_LUMINANCE; |
| 188 | *outType = GL_UNSIGNED_BYTE; |
| 189 | break; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 190 | default: |
| 191 | LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void 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(); |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 213 | bool setDefaultParams = false; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 214 | |
| 215 | if (!mId) { |
| 216 | glGenTextures(1, &mId); |
| 217 | needsAlloc = true; |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 218 | setDefaultParams = true; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 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 | |
John Reck | 48247a2 | 2016-01-22 10:55:32 -0800 | [diff] [blame] | 253 | if (setDefaultParams) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 254 | setFilter(GL_NEAREST); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 255 | setWrap(GL_CLAMP_TO_EDGE); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void 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); |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 268 | }; // namespace uirenderer |
| 269 | }; // namespace android |