Romain Guy 636afc1877 Apply transfer function when rendering with linear textures
RGBA16F bitmaps are always encoded in linear space, which means we must
apply the opto-electronic transfer function before we can render them
in the framebuffer.

Since our linear bitmaps are assumed to be scRGB, values can be negative.
The OETF is a slightly modified sRGB OETF:

sign(x) * OETF_sRGB(abs(x))

This effectively mirrors the OETF over the negative domain.

This CL also removes the "optimized" shader generation path. With
current compilers, the optimized path doesn't do anything of value
and makes ProgramCache difficult to maintain. Shader compilers inline
everything and are really good at folding expressions and removing
unused code.

Bug: 32984164
Test: CtsUiRenderingTestCases
Change-Id: Ieb458ad53574e3a8959aa6bccbbd2d1fe203cbc5
2017-02-07 22:30:34 +00:00

340 lines
11 KiB
C++

/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Caches.h"
#include "Texture.h"
#include "utils/GLUtils.h"
#include "utils/TraceUtils.h"
#include <utils/Log.h>
#include <SkCanvas.h>
namespace android {
namespace uirenderer {
// Number of bytes used by a texture in the given format
static int bytesPerPixel(GLint glFormat) {
switch (glFormat) {
// The wrapped-texture case, usually means a SurfaceTexture
case 0:
return 0;
case GL_LUMINANCE:
case GL_ALPHA:
return 1;
case GL_SRGB8:
case GL_RGB:
return 3;
case GL_SRGB8_ALPHA8:
case GL_RGBA:
return 4;
case GL_RGBA16F:
return 8;
default:
LOG_ALWAYS_FATAL("UNKNOWN FORMAT 0x%x", glFormat);
}
}
bool Texture::isLinear() const {
return mInternalFormat == GL_RGBA16F;
}
void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force) {
if (force || wrapS != mWrapS || wrapT != mWrapT) {
mWrapS = wrapS;
mWrapT = wrapT;
if (bindTexture) {
mCaches.textureState().bindTexture(mTarget, mId);
}
glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, wrapS);
glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, wrapT);
}
}
void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force) {
if (force || min != mMinFilter || mag != mMagFilter) {
mMinFilter = min;
mMagFilter = mag;
if (bindTexture) {
mCaches.textureState().bindTexture(mTarget, mId);
}
if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
glTexParameteri(mTarget, GL_TEXTURE_MIN_FILTER, min);
glTexParameteri(mTarget, GL_TEXTURE_MAG_FILTER, mag);
}
}
void Texture::deleteTexture() {
mCaches.textureState().deleteTexture(mId);
mId = 0;
mTarget = GL_NONE;
if (mEglImageHandle != EGL_NO_IMAGE_KHR) {
EGLDisplay eglDisplayHandle = eglGetCurrentDisplay();
eglDestroyImageKHR(eglDisplayHandle, mEglImageHandle);
mEglImageHandle = EGL_NO_IMAGE_KHR;
}
}
bool Texture::updateSize(uint32_t width, uint32_t height, GLint internalFormat,
GLint format, GLenum target) {
if (mWidth == width
&& mHeight == height
&& mFormat == format
&& mInternalFormat == internalFormat
&& mTarget == target) {
return false;
}
mWidth = width;
mHeight = height;
mFormat = format;
mInternalFormat = internalFormat;
mTarget = target;
notifySizeChanged(mWidth * mHeight * bytesPerPixel(internalFormat));
return true;
}
void Texture::resetCachedParams() {
mWrapS = GL_REPEAT;
mWrapT = GL_REPEAT;
mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
mMagFilter = GL_LINEAR;
}
void Texture::upload(GLint internalFormat, uint32_t width, uint32_t height,
GLenum format, GLenum type, const void* pixels) {
GL_CHECKPOINT(MODERATE);
bool needsAlloc = updateSize(width, height, internalFormat, format, GL_TEXTURE_2D);
if (!mId) {
glGenTextures(1, &mId);
needsAlloc = true;
resetCachedParams();
}
mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
if (needsAlloc) {
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0,
format, type, pixels);
} else if (pixels) {
glTexSubImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0,
format, type, pixels);
}
GL_CHECKPOINT(MODERATE);
}
void Texture::uploadHardwareBitmapToTexture(GraphicBuffer* buffer) {
EGLDisplay eglDisplayHandle = eglGetCurrentDisplay();
if (mEglImageHandle != EGL_NO_IMAGE_KHR) {
eglDestroyImageKHR(eglDisplayHandle, mEglImageHandle);
mEglImageHandle = EGL_NO_IMAGE_KHR;
}
mEglImageHandle = eglCreateImageKHR(eglDisplayHandle, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
buffer->getNativeBuffer(), 0);
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, mEglImageHandle);
}
static void uploadToTexture(bool resize, GLint internalFormat, GLenum format, GLenum type,
GLsizei stride, GLsizei bpp, GLsizei width, GLsizei height, const GLvoid * data) {
const bool useStride = stride != width
&& Caches::getInstance().extensions().hasUnpackRowLength();
if ((stride == width) || useStride) {
if (useStride) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
}
if (resize) {
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data);
} else {
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
}
if (useStride) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
} else {
// With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
// if the stride doesn't match the width
GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
if (!temp) return;
uint8_t * pDst = (uint8_t *)temp;
uint8_t * pSrc = (uint8_t *)data;
for (GLsizei i = 0; i < height; i++) {
memcpy(pDst, pSrc, width * bpp);
pDst += width * bpp;
pSrc += stride * bpp;
}
if (resize) {
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, temp);
} else {
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
}
free(temp);
}
}
void Texture::colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType,
bool needSRGB, GLint* outInternalFormat, GLint* outFormat, GLint* outType) {
switch (colorType) {
case kAlpha_8_SkColorType:
*outFormat = GL_ALPHA;
*outInternalFormat = GL_ALPHA;
*outType = GL_UNSIGNED_BYTE;
break;
case kRGB_565_SkColorType:
if (needSRGB) {
// We would ideally use a GL_RGB/GL_SRGB8 texture but the
// intermediate Skia bitmap needs to be ARGB_8888
*outFormat = GL_RGBA;
*outInternalFormat = caches.rgbaInternalFormat();
*outType = GL_UNSIGNED_BYTE;
} else {
*outFormat = GL_RGB;
*outInternalFormat = GL_RGB;
*outType = GL_UNSIGNED_SHORT_5_6_5;
}
break;
// ARGB_4444 and Index_8 are both upconverted to RGBA_8888
case kARGB_4444_SkColorType:
case kIndex_8_SkColorType:
case kN32_SkColorType:
*outFormat = GL_RGBA;
*outInternalFormat = caches.rgbaInternalFormat(needSRGB);
*outType = GL_UNSIGNED_BYTE;
break;
case kGray_8_SkColorType:
// TODO: Handle sRGB
*outFormat = GL_LUMINANCE;
*outInternalFormat = GL_LUMINANCE;
*outType = GL_UNSIGNED_BYTE;
break;
case kRGBA_F16_SkColorType:
// This format is always linear
*outFormat = GL_RGBA;
*outInternalFormat = GL_RGBA16F;
*outType = GL_HALF_FLOAT;
break;
default:
LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
break;
}
}
SkBitmap Texture::uploadToN32(const SkBitmap& bitmap, bool hasSRGB, sk_sp<SkColorSpace> sRGB) {
SkBitmap rgbaBitmap;
rgbaBitmap.allocPixels(SkImageInfo::MakeN32(bitmap.width(), bitmap.height(),
bitmap.info().alphaType(), hasSRGB ? sRGB : nullptr));
rgbaBitmap.eraseColor(0);
SkCanvas canvas(rgbaBitmap);
canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
return rgbaBitmap;
}
bool Texture::hasUnsupportedColorType(const SkImageInfo& info, bool hasSRGB, SkColorSpace* sRGB) {
bool needSRGB = info.colorSpace() == sRGB;
return info.colorType() == kARGB_4444_SkColorType
|| info.colorType() == kIndex_8_SkColorType
|| (info.colorType() == kRGB_565_SkColorType && hasSRGB && needSRGB);
}
void Texture::upload(Bitmap& bitmap) {
if (!bitmap.readyToDraw()) {
ALOGE("Cannot generate texture from bitmap");
return;
}
ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
// We could also enable mipmapping if both bitmap dimensions are powers
// of 2 but we'd have to deal with size changes. Let's keep this simple
const bool canMipMap = mCaches.extensions().hasNPot();
// If the texture had mipmap enabled but not anymore,
// force a glTexImage2D to discard the mipmap levels
bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
bool setDefaultParams = false;
if (!mId) {
glGenTextures(1, &mId);
needsAlloc = true;
setDefaultParams = true;
}
sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
bool needSRGB = bitmap.info().colorSpace() == sRGB.get();
GLint internalFormat, format, type;
colorTypeToGlFormatAndType(mCaches, bitmap.colorType(), needSRGB, &internalFormat, &format, &type);
GLenum target = bitmap.isHardware() ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
needsAlloc |= updateSize(bitmap.width(), bitmap.height(), internalFormat, format, target);
blend = !bitmap.isOpaque();
mCaches.textureState().bindTexture(mTarget, mId);
// TODO: Handle sRGB gray bitmaps
bool hasSRGB = mCaches.extensions().hasSRGB();
if (CC_UNLIKELY(hasUnsupportedColorType(bitmap.info(), hasSRGB, sRGB.get()))) {
SkBitmap skBitmap;
bitmap.getSkBitmap(&skBitmap);
SkBitmap rgbaBitmap = uploadToN32(skBitmap, hasSRGB, std::move(sRGB));
uploadToTexture(needsAlloc, internalFormat, format, type, rgbaBitmap.rowBytesAsPixels(),
rgbaBitmap.bytesPerPixel(), rgbaBitmap.width(),
rgbaBitmap.height(), rgbaBitmap.getPixels());
} else if (bitmap.isHardware()) {
uploadHardwareBitmapToTexture(bitmap.graphicBuffer());
} else {
uploadToTexture(needsAlloc, internalFormat, format, type, bitmap.rowBytesAsPixels(),
bitmap.info().bytesPerPixel(), bitmap.width(), bitmap.height(), bitmap.pixels());
}
if (canMipMap) {
mipMap = bitmap.hasHardwareMipMap();
if (mipMap) {
glGenerateMipmap(GL_TEXTURE_2D);
}
}
if (setDefaultParams) {
setFilter(GL_NEAREST);
setWrap(GL_CLAMP_TO_EDGE);
}
}
void Texture::wrap(GLuint id, uint32_t width, uint32_t height,
GLint internalFormat, GLint format, GLenum target) {
mId = id;
mWidth = width;
mHeight = height;
mFormat = format;
mInternalFormat = internalFormat;
mTarget = target;
// We're wrapping an existing texture, so don't double count this memory
notifySizeChanged(0);
}
}; // namespace uirenderer
}; // namespace android