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 "Texture.h"
18#include "Caches.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, GLint format,
96                           GLenum target) {
97    if (mWidth == width && mHeight == height && mFormat == format &&
98        mInternalFormat == internalFormat && mTarget == target) {
99        return false;
100    }
101    mWidth = width;
102    mHeight = height;
103    mFormat = format;
104    mInternalFormat = internalFormat;
105    mTarget = target;
106    notifySizeChanged(mWidth * mHeight * bytesPerPixel(internalFormat));
107    return true;
108}
109
110void Texture::resetCachedParams() {
111    mWrapS = GL_REPEAT;
112    mWrapT = GL_REPEAT;
113    mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
114    mMagFilter = GL_LINEAR;
115}
116
117void Texture::upload(GLint internalFormat, uint32_t width, uint32_t height, GLenum format,
118                     GLenum type, const void* pixels) {
119    GL_CHECKPOINT(MODERATE);
120
121    // We don't have color space information, we assume the data is gamma encoded
122    mIsLinear = false;
123
124    bool needsAlloc = updateLayout(width, height, internalFormat, format, GL_TEXTURE_2D);
125    if (!mId) {
126        glGenTextures(1, &mId);
127        needsAlloc = true;
128        resetCachedParams();
129    }
130    mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
131    if (needsAlloc) {
132        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, format, type, pixels);
133    } else if (pixels) {
134        glTexSubImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, format, type, pixels);
135    }
136    GL_CHECKPOINT(MODERATE);
137}
138
139void 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,
146                                        buffer->getNativeBuffer(), 0);
147    glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, mEglImageHandle);
148}
149
150static void uploadToTexture(bool resize, GLint internalFormat, GLenum format, GLenum type,
151                            GLsizei stride, GLsizei bpp, GLsizei width, GLsizei height,
152                            const GLvoid* data) {
153    const bool useStride =
154            stride != width && Caches::getInstance().extensions().hasUnpackRowLength();
155    if ((stride == width) || useStride) {
156        if (useStride) {
157            glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
158        }
159
160        if (resize) {
161            glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data);
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
173        GLvoid* temp = (GLvoid*)malloc(width * height * bpp);
174        if (!temp) return;
175
176        uint8_t* pDst = (uint8_t*)temp;
177        uint8_t* pSrc = (uint8_t*)data;
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) {
185            glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, temp);
186        } else {
187            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
188        }
189
190        free(temp);
191    }
192}
193
194void Texture::colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType, bool needSRGB,
195                                         GLint* outInternalFormat, GLint* outFormat,
196                                         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 is upconverted to RGBA_8888
217        case kARGB_4444_SkColorType:
218        case kN32_SkColorType:
219            *outFormat = GL_RGBA;
220            *outInternalFormat = caches.rgbaInternalFormat(needSRGB);
221            *outType = GL_UNSIGNED_BYTE;
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;
243    }
244}
245
246SkBitmap Texture::uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending,
247                              sk_sp<SkColorSpace> sRGB) {
248    SkBitmap rgbaBitmap;
249    rgbaBitmap.allocPixels(SkImageInfo::MakeN32(bitmap.width(), bitmap.height(),
250                                                bitmap.info().alphaType(),
251                                                hasLinearBlending ? sRGB : nullptr));
252    rgbaBitmap.eraseColor(0);
253
254    if (bitmap.colorType() == kRGBA_F16_SkColorType) {
255        // Drawing RGBA_F16 onto ARGB_8888 is not supported
256        bitmap.readPixels(rgbaBitmap.info().makeColorSpace(SkColorSpace::MakeSRGB()),
257                          rgbaBitmap.getPixels(), rgbaBitmap.rowBytes(), 0, 0);
258    } else {
259        SkCanvas canvas(rgbaBitmap);
260        canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
261    }
262
263    return rgbaBitmap;
264}
265
266bool Texture::hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending) {
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);
272}
273
274void Texture::upload(Bitmap& bitmap) {
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();
284    bool setDefaultParams = false;
285
286    if (!mId) {
287        glGenTextures(1, &mId);
288        needsAlloc = true;
289        setDefaultParams = true;
290    }
291
292    bool hasLinearBlending = mCaches.extensions().hasLinearBlending();
293    bool needSRGB = transferFunctionCloseToSRGB(bitmap.info().colorSpace());
294
295    GLint internalFormat, format, type;
296    colorTypeToGlFormatAndType(mCaches, bitmap.colorType(), needSRGB && hasLinearBlending,
297                               &internalFormat, &format, &type);
298
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
301    bool rgba16fNeedsConversion =
302            bitmap.colorType() == kRGBA_F16_SkColorType && internalFormat != GL_RGBA16F;
303
304    // RGBA16F is always linear extended sRGB
305    if (internalFormat == GL_RGBA16F) {
306        mIsLinear = true;
307    }
308
309    mConnector.reset();
310
311    // Alpha masks don't have color profiles
312    // If an RGBA16F bitmap needs conversion, we know the target will be sRGB
313    if (!mIsLinear && internalFormat != GL_ALPHA && !rgba16fNeedsConversion) {
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
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);
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
336                    if (needSRGB && internalFormat == GL_RGBA && mCaches.extensions().hasSRGB() &&
337                        !bitmap.isHardware()) {
338                        internalFormat = GL_SRGB8_ALPHA8;
339                    }
340                }
341            }
342        }
343    }
344
345    GLenum target = bitmap.isHardware() ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
346    needsAlloc |= updateLayout(bitmap.width(), bitmap.height(), internalFormat, format, target);
347
348    blend = !bitmap.isOpaque();
349    mCaches.textureState().bindTexture(mTarget, mId);
350
351    // TODO: Handle sRGB gray bitmaps
352    if (CC_UNLIKELY(hasUnsupportedColorType(bitmap.info(), hasLinearBlending))) {
353        SkBitmap skBitmap;
354        bitmap.getSkBitmap(&skBitmap);
355        sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB();
356        SkBitmap rgbaBitmap = uploadToN32(skBitmap, hasLinearBlending, std::move(sRGB));
357        uploadToTexture(needsAlloc, internalFormat, format, type, rgbaBitmap.rowBytesAsPixels(),
358                        rgbaBitmap.bytesPerPixel(), rgbaBitmap.width(), rgbaBitmap.height(),
359                        rgbaBitmap.getPixels());
360    } else if (bitmap.isHardware()) {
361        uploadHardwareBitmapToTexture(bitmap.graphicBuffer());
362    } else {
363        uploadToTexture(needsAlloc, internalFormat, format, type, bitmap.rowBytesAsPixels(),
364                        bitmap.info().bytesPerPixel(), bitmap.width(), bitmap.height(),
365                        bitmap.pixels());
366    }
367
368    if (canMipMap) {
369        mipMap = bitmap.hasHardwareMipMap();
370        if (mipMap) {
371            glGenerateMipmap(GL_TEXTURE_2D);
372        }
373    }
374
375    if (setDefaultParams) {
376        setFilter(GL_NEAREST);
377        setWrap(GL_CLAMP_TO_EDGE);
378    }
379}
380
381void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, GLint format,
382                   GLenum target) {
383    mId = id;
384    mWidth = width;
385    mHeight = height;
386    mFormat = format;
387    mInternalFormat = internalFormat;
388    mTarget = target;
389    mConnector.reset();
390    // We're wrapping an existing texture, so don't double count this memory
391    notifySizeChanged(0);
392}
393
394TransferFunctionType 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)) {
398            if (MathUtils::areEqual(p.a, 1.0f) && MathUtils::isZero(p.b) &&
399                MathUtils::isZero(p.c) && MathUtils::isZero(p.d)) {
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
412};  // namespace uirenderer
413};  // namespace android
414