diff options
author | Romain Guy <romainguy@google.com> | 2017-03-27 00:40:21 -0700 |
---|---|---|
committer | Romain Guy <romainguy@google.com> | 2017-03-28 18:35:49 -0700 |
commit | caaaa66e57293e4a6f312649bf472eab84d5c7fe (patch) | |
tree | 66beeca493da1046b482736293441a70f29474b3 /libs/hwui/utils | |
parent | b7980a3bbee067eae4665c8abbe8d39aefb2d36a (diff) |
Convert bitmaps to sRGB/scRGB when they have a color profile
This change also fixes an issue with RGBA16F bitmaps when modulated
with a color (for instance by setting an alpha on the Paint object).
The color space conversion is currently done entirely in the shader,
by doing these operations in order:
1. Sample the texture
2. Un-premultiply alpha
3. Apply the EOTF
4. Multiply by the 3x3 color space matrix
5. Apply the OETF
6. Premultiply alpha
Optimizations:
- Steps 2 & 6 are skipped for opaque (common) bitmaps
- Step 3 is skipped when the color space's EOTF is close
to sRGB (Display P3 for instance). Instead, we use
a hardware sRGB fetch (when the GPU supports it)
- When step 3 is necessary, we use one of four standard
EOTF implementations, to save cycles when possible:
+ Linear (doesn't do anything)
+ Full parametric (ICC parametric curve type 4 as defined
in ICC.1:2004-10, section 10.15)
+ Limited parametric (ICC parametric curve type 3)
+ Gamma (ICC parametric curve type 0)
Color space conversion could be done using texture samplers
instead, for instance 3D LUTs, with or without transfer
functions baked in, or 1D LUTs for transfer functions. This
would result in dependent texture fetches which may or may
not be an advantage over an ALU based implementation. The
current solution favor the use of ALUs to save precious
bandwidth.
Test: CtsUiRenderingTests, CtsGraphicsTests
Bug: 32984164
Change-Id: I10bc3db515e13973b45220f129c66b23f0f7f8fe
Diffstat (limited to 'libs/hwui/utils')
-rw-r--r-- | libs/hwui/utils/Color.cpp | 58 | ||||
-rw-r--r-- | libs/hwui/utils/Color.h | 13 |
2 files changed, 71 insertions, 0 deletions
diff --git a/libs/hwui/utils/Color.cpp b/libs/hwui/utils/Color.cpp new file mode 100644 index 000000000000..7d234b06b8ca --- /dev/null +++ b/libs/hwui/utils/Color.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 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 "Color.h" + +#include <cmath> + +namespace android { +namespace uirenderer { + +static inline bool almostEqual(float a, float b) { + return std::abs(a - b) < 1e-2f; +} + +bool transferFunctionCloseToSRGB(const SkColorSpace* colorSpace) { + if (colorSpace == nullptr) return true; + if (colorSpace->isSRGB()) return true; + + SkColorSpaceTransferFn transferFunction; + if (colorSpace->isNumericalTransferFn(&transferFunction)) { + // sRGB transfer function params: + const float sRGBParamA = 1 / 1.055f; + const float sRGBParamB = 0.055f / 1.055f; + const float sRGBParamC = 1 / 12.92f; + const float sRGBParamD = 0.04045f; + const float sRGBParamE = 0.0f; + const float sRGBParamF = 0.0f; + const float sRGBParamG = 2.4f; + + // This comparison will catch Display P3 + return + almostEqual(sRGBParamA, transferFunction.fA) + && almostEqual(sRGBParamB, transferFunction.fB) + && almostEqual(sRGBParamC, transferFunction.fC) + && almostEqual(sRGBParamD, transferFunction.fD) + && almostEqual(sRGBParamE, transferFunction.fE) + && almostEqual(sRGBParamF, transferFunction.fF) + && almostEqual(sRGBParamG, transferFunction.fG); + } + + return false; +} + +}; // namespace uirenderer +}; // namespace android diff --git a/libs/hwui/utils/Color.h b/libs/hwui/utils/Color.h index 4a27ca2f327a..9c096601c826 100644 --- a/libs/hwui/utils/Color.h +++ b/libs/hwui/utils/Color.h @@ -19,6 +19,7 @@ #include <math.h> #include <SkColor.h> +#include <SkColorSpace.h> namespace android { namespace uirenderer { @@ -82,6 +83,13 @@ namespace uirenderer { }; static constexpr int BrightColorsCount = sizeof(BrightColors) / sizeof(Color::Color); + enum class TransferFunctionType : int8_t { + None = 0, + Full, + Limited, + Gamma + }; + // Opto-electronic conversion function for the sRGB color space // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value static constexpr float OECF_sRGB(float linear) { @@ -118,6 +126,11 @@ namespace uirenderer { return srgb; #endif } + + // Returns whether the specified color space's transfer function can be + // approximated with the native sRGB transfer function. This method + // returns true for sRGB, gamma 2.2 and Display P3 for instance + bool transferFunctionCloseToSRGB(const SkColorSpace* colorSpace); } /* namespace uirenderer */ } /* namespace android */ |