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        if (caches.extensions().getMajorGlVersion() >= 3) {
231            // This format is always linear
232            *outFormat = GL_RGBA;
233            *outInternalFormat = GL_RGBA16F;
234            *outType = GL_HALF_FLOAT;
235        } else {
236            *outFormat = GL_RGBA;
237            *outInternalFormat = caches.rgbaInternalFormat(true);
238            *outType = GL_UNSIGNED_BYTE;
239        }
240        break;
241    default:
242        LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
243        break;
244    }
245}
246
247SkBitmap Texture::uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending,
248        sk_sp<SkColorSpace> sRGB) {
249    SkBitmap rgbaBitmap;
250    rgbaBitmap.allocPixels(SkImageInfo::MakeN32(bitmap.width(), bitmap.height(),
251            bitmap.info().alphaType(), 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()
257                .makeColorSpace(SkColorSpace::MakeSRGB()),
258                rgbaBitmap.getPixels(), rgbaBitmap.rowBytes(), 0, 0);
259    } else {
260        SkCanvas canvas(rgbaBitmap);
261        canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
262    }
263
264    return rgbaBitmap;
265}
266
267bool Texture::hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending) {
268    return info.colorType() == kARGB_4444_SkColorType
269        || info.colorType() == kIndex_8_SkColorType
270        || (info.colorType() == kRGB_565_SkColorType
271                && hasLinearBlending
272                && info.colorSpace()->isSRGB())
273        || (info.colorType() == kRGBA_F16_SkColorType
274                && Caches::getInstance().extensions().getMajorGlVersion() < 3);
275}
276
277void Texture::upload(Bitmap& bitmap) {
278    if (!bitmap.readyToDraw()) {
279        ALOGE("Cannot generate texture from bitmap");
280        return;
281    }
282
283    ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
284
285    // We could also enable mipmapping if both bitmap dimensions are powers
286    // of 2 but we'd have to deal with size changes. Let's keep this simple
287    const bool canMipMap = mCaches.extensions().hasNPot();
288
289    // If the texture had mipmap enabled but not anymore,
290    // force a glTexImage2D to discard the mipmap levels
291    bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
292    bool setDefaultParams = false;
293
294    if (!mId) {
295        glGenTextures(1, &mId);
296        needsAlloc = true;
297        setDefaultParams = true;
298    }
299
300    bool hasLinearBlending = mCaches.extensions().hasLinearBlending();
301    bool needSRGB = transferFunctionCloseToSRGB(bitmap.info().colorSpace());
302
303    GLint internalFormat, format, type;
304    colorTypeToGlFormatAndType(mCaches, bitmap.colorType(),
305            needSRGB && hasLinearBlending, &internalFormat, &format, &type);
306
307    // Some devices don't support GL_RGBA16F, so we need to compare the color type
308    // and internal GL format to decide what to do with 16 bit bitmaps
309    bool rgba16fNeedsConversion = bitmap.colorType() == kRGBA_F16_SkColorType
310            && internalFormat != GL_RGBA16F;
311
312    mConnector.reset();
313
314    // RGBA16F is always extended sRGB, alpha masks don't have color profiles
315    // If an RGBA16F bitmap needs conversion, we know the target will be sRGB
316    if (internalFormat != GL_RGBA16F && internalFormat != GL_ALPHA && !rgba16fNeedsConversion) {
317        SkColorSpace* colorSpace = bitmap.info().colorSpace();
318        // If the bitmap is sRGB we don't need conversion
319        if (colorSpace != nullptr && !colorSpace->isSRGB()) {
320            SkMatrix44 xyzMatrix(SkMatrix44::kUninitialized_Constructor);
321            if (!colorSpace->toXYZD50(&xyzMatrix)) {
322                ALOGW("Incompatible color space!");
323            } else {
324                SkColorSpaceTransferFn fn;
325                if (!colorSpace->isNumericalTransferFn(&fn)) {
326                    ALOGW("Incompatible color space, no numerical transfer function!");
327                } else {
328                    float data[16];
329                    xyzMatrix.asColMajorf(data);
330
331                    ColorSpace::TransferParameters p =
332                            {fn.fG, fn.fA, fn.fB, fn.fC, fn.fD, fn.fE, fn.fF};
333                    ColorSpace src("Unnamed", mat4f((const float*) &data[0]).upperLeft(), p);
334                    mConnector.reset(new ColorSpaceConnector(src, ColorSpace::sRGB()));
335
336                    // A non-sRGB color space might have a transfer function close enough to sRGB
337                    // that we can save shader instructions by using an sRGB sampler
338                    // This is only possible if we have hardware support for sRGB textures
339                    if (needSRGB && internalFormat == GL_RGBA
340                            && mCaches.extensions().hasSRGB() && !bitmap.isHardware()) {
341                        internalFormat = GL_SRGB8_ALPHA8;
342                    }
343                }
344            }
345        }
346    }
347
348    GLenum target = bitmap.isHardware() ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
349    needsAlloc |= updateLayout(bitmap.width(), bitmap.height(), internalFormat, format, target);
350
351    blend = !bitmap.isOpaque();
352    mCaches.textureState().bindTexture(mTarget, mId);
353
354    // TODO: Handle sRGB gray bitmaps
355    if (CC_UNLIKELY(hasUnsupportedColorType(bitmap.info(), hasLinearBlending))) {
356        SkBitmap skBitmap;
357        bitmap.getSkBitmap(&skBitmap);
358        sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB();
359        SkBitmap rgbaBitmap = uploadToN32(skBitmap, hasLinearBlending, std::move(sRGB));
360        uploadToTexture(needsAlloc, internalFormat, format, type, rgbaBitmap.rowBytesAsPixels(),
361                rgbaBitmap.bytesPerPixel(), rgbaBitmap.width(),
362                rgbaBitmap.height(), rgbaBitmap.getPixels());
363    } else if (bitmap.isHardware()) {
364        uploadHardwareBitmapToTexture(bitmap.graphicBuffer());
365    } else {
366        uploadToTexture(needsAlloc, internalFormat, format, type, bitmap.rowBytesAsPixels(),
367                bitmap.info().bytesPerPixel(), bitmap.width(), bitmap.height(), bitmap.pixels());
368    }
369
370    if (canMipMap) {
371        mipMap = bitmap.hasHardwareMipMap();
372        if (mipMap) {
373            glGenerateMipmap(GL_TEXTURE_2D);
374        }
375    }
376
377    if (setDefaultParams) {
378        setFilter(GL_NEAREST);
379        setWrap(GL_CLAMP_TO_EDGE);
380    }
381}
382
383void Texture::wrap(GLuint id, uint32_t width, uint32_t height,
384        GLint internalFormat, GLint format, GLenum target) {
385    mId = id;
386    mWidth = width;
387    mHeight = height;
388    mFormat = format;
389    mInternalFormat = internalFormat;
390    mTarget = target;
391    mConnector.reset();
392    // We're wrapping an existing texture, so don't double count this memory
393    notifySizeChanged(0);
394}
395
396TransferFunctionType Texture::getTransferFunctionType() const {
397    if (mConnector.get() != nullptr && mInternalFormat != GL_SRGB8_ALPHA8) {
398        const ColorSpace::TransferParameters& p = mConnector->getSource().getTransferParameters();
399        if (MathUtils::isZero(p.e) && MathUtils::isZero(p.f)) {
400            if (MathUtils::areEqual(p.a, 1.0f) && MathUtils::isZero(p.b)
401                    && MathUtils::isZero(p.c) && MathUtils::isZero(p.d)) {
402                if (MathUtils::areEqual(p.g, 1.0f)) {
403                    return TransferFunctionType::None;
404                }
405                return TransferFunctionType::Gamma;
406            }
407            return TransferFunctionType::Limited;
408        }
409        return TransferFunctionType::Full;
410    }
411    return TransferFunctionType::None;
412}
413
414}; // namespace uirenderer
415}; // namespace android
416