blob: b8f2be111ecae7428e2f90808f4af61c2c936d20 [file] [log] [blame]
Alec Mouri492c85c2021-11-19 15:58:10 -08001/*
2 * Copyright 2021 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 <shaders/shaders.h>
18
19#include <tonemap/tonemap.h>
20
Alec Mouri5a493722022-01-26 16:43:02 -080021#include <cmath>
Alec Mouri492c85c2021-11-19 15:58:10 -080022#include <optional>
23
24#include <math/mat4.h>
25#include <system/graphics-base-v1.0.h>
26#include <ui/ColorSpace.h>
27
28namespace android::shaders {
29
Alec Mouri5a493722022-01-26 16:43:02 -080030namespace {
31
32aidl::android::hardware::graphics::common::Dataspace toAidlDataspace(ui::Dataspace dataspace) {
Alec Mouri492c85c2021-11-19 15:58:10 -080033 return static_cast<aidl::android::hardware::graphics::common::Dataspace>(dataspace);
34}
35
Alec Mouri5a493722022-01-26 16:43:02 -080036void generateXYZTransforms(std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -080037 shader.append(R"(
Alec Mouri8a186102023-04-25 00:34:30 +000038 uniform float3x3 in_rgbToXyz;
39 uniform float3x3 in_xyzToSrcRgb;
40 uniform float4x4 in_colorTransform;
Alec Mouri492c85c2021-11-19 15:58:10 -080041 float3 ToXYZ(float3 rgb) {
Alec Mouri8a186102023-04-25 00:34:30 +000042 return in_rgbToXyz * rgb;
Alec Mouri492c85c2021-11-19 15:58:10 -080043 }
44
Alec Mouri8a186102023-04-25 00:34:30 +000045 float3 ToSrcRGB(float3 xyz) {
46 return in_xyzToSrcRgb * xyz;
47 }
48
49 float3 ApplyColorTransform(float3 rgb) {
50 return (in_colorTransform * float4(rgb, 1.0)).rgb;
Alec Mouri492c85c2021-11-19 15:58:10 -080051 }
52 )");
53}
54
Alec Mouri8a186102023-04-25 00:34:30 +000055// Conversion from relative light to absolute light
56// Note that 1.0 == 203 nits.
57void generateLuminanceScalesForOOTF(ui::Dataspace inputDataspace, std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -080058 switch (inputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
Alec Mouri492c85c2021-11-19 15:58:10 -080059 case HAL_DATASPACE_TRANSFER_HLG:
Alec Mouri8a186102023-04-25 00:34:30 +000060 // BT. 2408 says that a signal level of 0.75 == 203 nits for HLG, but that's after
61 // applying OOTF. But we haven't applied OOTF yet, so we need to scale by a different
62 // constant instead.
Alec Mouri492c85c2021-11-19 15:58:10 -080063 shader.append(R"(
Alec Mouri8a186102023-04-25 00:34:30 +000064 float3 ScaleLuminance(float3 xyz) {
65 return xyz * 264.96;
66 }
67 )");
Alec Mouri492c85c2021-11-19 15:58:10 -080068 break;
69 default:
Alec Mouri8a186102023-04-25 00:34:30 +000070 shader.append(R"(
71 float3 ScaleLuminance(float3 xyz) {
72 return xyz * 203.0;
73 }
74 )");
75 break;
Alec Mouri492c85c2021-11-19 15:58:10 -080076 }
77}
78
79// Normalizes from absolute light back to relative light (maps from [0, maxNits] back to [0, 1])
Alec Mouri3e5965f2023-04-07 18:00:58 +000080static void generateLuminanceNormalizationForOOTF(ui::Dataspace inputDataspace,
81 ui::Dataspace outputDataspace,
Alec Mouri492c85c2021-11-19 15:58:10 -080082 std::string& shader) {
83 switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
84 case HAL_DATASPACE_TRANSFER_ST2084:
85 shader.append(R"(
Alec Mouri8a186102023-04-25 00:34:30 +000086 float3 NormalizeLuminance(float3 xyz) {
87 return xyz / 203.0;
88 }
89 )");
Alec Mouri492c85c2021-11-19 15:58:10 -080090 break;
91 case HAL_DATASPACE_TRANSFER_HLG:
Alec Mouri3e5965f2023-04-07 18:00:58 +000092 switch (inputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
93 case HAL_DATASPACE_TRANSFER_HLG:
94 shader.append(R"(
95 float3 NormalizeLuminance(float3 xyz) {
Alec Mouri8a186102023-04-25 00:34:30 +000096 return xyz / 264.96;
Alec Mouri3e5965f2023-04-07 18:00:58 +000097 }
98 )");
99 break;
100 default:
101 // Transcoding to HLG requires applying the inverse OOTF
102 // with the expectation that the OOTF is then applied during
103 // tonemapping downstream.
Alec Mouri8a186102023-04-25 00:34:30 +0000104 // BT. 2100-2 operates on normalized luminances, so renormalize to the input to
105 // correctly adjust gamma.
Alec Mouri3e5965f2023-04-07 18:00:58 +0000106 shader.append(R"(
107 float3 NormalizeLuminance(float3 xyz) {
Alec Mouri8a186102023-04-25 00:34:30 +0000108 float ootfGain = pow(xyz.y / 1000.0, -0.2 / 1.2);
109 return xyz * ootfGain / 203.0;
Alec Mouri3e5965f2023-04-07 18:00:58 +0000110 }
111 )");
112 break;
113 }
Alec Mouri492c85c2021-11-19 15:58:10 -0800114 break;
115 default:
Alec Mouri8a186102023-04-25 00:34:30 +0000116 switch (inputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
117 case HAL_DATASPACE_TRANSFER_HLG:
118 case HAL_DATASPACE_TRANSFER_ST2084:
119 // libtonemap outputs a range [0, in_libtonemap_displayMaxLuminance], so
120 // normalize back to [0, 1] when the output is SDR.
121 shader.append(R"(
122 float3 NormalizeLuminance(float3 xyz) {
123 return xyz / in_libtonemap_displayMaxLuminance;
124 }
125 )");
126 break;
127 default:
128 // Otherwise normalize back down to the range [0, 1]
129 // TODO: get this working for extended range outputs
130 shader.append(R"(
131 float3 NormalizeLuminance(float3 xyz) {
132 return xyz / 203.0;
133 }
134 )");
135 break;
136 }
Alec Mouri492c85c2021-11-19 15:58:10 -0800137 }
138}
139
Alec Mouri5a493722022-01-26 16:43:02 -0800140void generateOOTF(ui::Dataspace inputDataspace, ui::Dataspace outputDataspace,
141 std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800142 shader.append(tonemap::getToneMapper()
143 ->generateTonemapGainShaderSkSL(toAidlDataspace(inputDataspace),
144 toAidlDataspace(outputDataspace))
145 .c_str());
146
Alec Mouri8a186102023-04-25 00:34:30 +0000147 generateLuminanceScalesForOOTF(inputDataspace, shader);
Alec Mouri3e5965f2023-04-07 18:00:58 +0000148 generateLuminanceNormalizationForOOTF(inputDataspace, outputDataspace, shader);
Alec Mouri492c85c2021-11-19 15:58:10 -0800149
Alec Mouri8a186102023-04-25 00:34:30 +0000150 // Some tonemappers operate on CIE luminance, other tonemappers operate on linear rgb
151 // luminance in the source gamut.
Alec Mouri492c85c2021-11-19 15:58:10 -0800152 shader.append(R"(
Alec Mouri8a186102023-04-25 00:34:30 +0000153 float3 OOTF(float3 linearRGB) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800154 float3 scaledLinearRGB = ScaleLuminance(linearRGB);
Alec Mouri8a186102023-04-25 00:34:30 +0000155 float3 scaledXYZ = ToXYZ(scaledLinearRGB);
Alec Mouri492c85c2021-11-19 15:58:10 -0800156
Alec Mouri8a186102023-04-25 00:34:30 +0000157 float gain = libtonemap_LookupTonemapGain(ToSrcRGB(scaledXYZ), scaledXYZ);
Alec Mouri492c85c2021-11-19 15:58:10 -0800158
159 return NormalizeLuminance(scaledXYZ * gain);
160 }
161 )");
162}
163
Alec Mouri8a186102023-04-25 00:34:30 +0000164void generateOETF(std::string& shader) {
165 // Only support gamma 2.2 for now
166 shader.append(R"(
167 float OETF(float3 linear) {
168 return sign(linear) * pow(abs(linear), (1.0 / 2.2));
169 }
170 )");
Alec Mouri492c85c2021-11-19 15:58:10 -0800171}
172
Alec Mouri3c51bc52022-11-04 01:28:55 +0000173void generateEffectiveOOTF(bool undoPremultipliedAlpha, LinearEffect::SkSLType type,
Alec Mouri8a186102023-04-25 00:34:30 +0000174 bool needsCustomOETF, std::string& shader) {
Alec Mouri3c51bc52022-11-04 01:28:55 +0000175 switch (type) {
176 case LinearEffect::SkSLType::ColorFilter:
177 shader.append(R"(
178 half4 main(half4 inputColor) {
179 float4 c = float4(inputColor);
180 )");
181 break;
182 case LinearEffect::SkSLType::Shader:
183 shader.append(R"(
184 uniform shader child;
185 half4 main(float2 xy) {
186 float4 c = float4(child.eval(xy));
187 )");
188 break;
189 }
Alec Mouri492c85c2021-11-19 15:58:10 -0800190 if (undoPremultipliedAlpha) {
191 shader.append(R"(
192 c.rgb = c.rgb / (c.a + 0.0019);
193 )");
194 }
Alec Mouri8a186102023-04-25 00:34:30 +0000195 // We are using linear sRGB as a working space, with 1.0 == 203 nits
Alec Mouri492c85c2021-11-19 15:58:10 -0800196 shader.append(R"(
Alec Mouri8a186102023-04-25 00:34:30 +0000197 c.rgb = ApplyColorTransform(OOTF(toLinearSrgb(c.rgb)));
Alec Mouri492c85c2021-11-19 15:58:10 -0800198 )");
Alec Mouri8a186102023-04-25 00:34:30 +0000199 if (needsCustomOETF) {
200 shader.append(R"(
201 c.rgb = OETF(c.rgb);
202 )");
203 } else {
204 shader.append(R"(
205 c.rgb = fromLinearSrgb(c.rgb);
206 )");
207 }
Alec Mouri492c85c2021-11-19 15:58:10 -0800208 if (undoPremultipliedAlpha) {
209 shader.append(R"(
210 c.rgb = c.rgb * (c.a + 0.0019);
211 )");
212 }
213 shader.append(R"(
214 return c;
215 }
216 )");
217}
Sally Qi2019fd22021-11-22 10:19:04 -0800218
Alec Mouri5a493722022-01-26 16:43:02 -0800219template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true>
220std::vector<uint8_t> buildUniformValue(T value) {
221 std::vector<uint8_t> result;
222 result.resize(sizeof(value));
223 std::memcpy(result.data(), &value, sizeof(value));
224 return result;
225}
226
Alec Mouri5a493722022-01-26 16:43:02 -0800227} // namespace
228
Alec Mouri492c85c2021-11-19 15:58:10 -0800229std::string buildLinearEffectSkSL(const LinearEffect& linearEffect) {
230 std::string shaderString;
Alec Mouri492c85c2021-11-19 15:58:10 -0800231 generateXYZTransforms(shaderString);
232 generateOOTF(linearEffect.inputDataspace, linearEffect.outputDataspace, shaderString);
Alec Mouri8a186102023-04-25 00:34:30 +0000233
234 const bool needsCustomOETF = (linearEffect.fakeOutputDataspace & HAL_DATASPACE_TRANSFER_MASK) ==
235 HAL_DATASPACE_TRANSFER_GAMMA2_2;
236 if (needsCustomOETF) {
237 generateOETF(shaderString);
238 }
239 generateEffectiveOOTF(linearEffect.undoPremultipliedAlpha, linearEffect.type, needsCustomOETF,
240 shaderString);
Alec Mouri492c85c2021-11-19 15:58:10 -0800241 return shaderString;
242}
243
Alec Mouri8a186102023-04-25 00:34:30 +0000244ColorSpace toColorSpace(ui::Dataspace dataspace) {
245 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
246 case HAL_DATASPACE_STANDARD_BT709:
247 return ColorSpace::sRGB();
248 case HAL_DATASPACE_STANDARD_DCI_P3:
249 return ColorSpace::DisplayP3();
250 case HAL_DATASPACE_STANDARD_BT2020:
251 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
252 return ColorSpace::BT2020();
253 case HAL_DATASPACE_STANDARD_ADOBE_RGB:
254 return ColorSpace::AdobeRGB();
255 // TODO(b/208290320): BT601 format and variants return different primaries
256 case HAL_DATASPACE_STANDARD_BT601_625:
257 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
258 case HAL_DATASPACE_STANDARD_BT601_525:
259 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
260 // TODO(b/208290329): BT407M format returns different primaries
261 case HAL_DATASPACE_STANDARD_BT470M:
262 // TODO(b/208290904): FILM format returns different primaries
263 case HAL_DATASPACE_STANDARD_FILM:
264 case HAL_DATASPACE_STANDARD_UNSPECIFIED:
265 default:
266 return ColorSpace::sRGB();
267 }
268}
269
Alec Mouri492c85c2021-11-19 15:58:10 -0800270// Generates a list of uniforms to set on the LinearEffect shader above.
Alec Mourifcedb9c2022-04-11 20:02:17 +0000271std::vector<tonemap::ShaderUniform> buildLinearEffectUniforms(
272 const LinearEffect& linearEffect, const mat4& colorTransform, float maxDisplayLuminance,
273 float currentDisplayLuminanceNits, float maxLuminance, AHardwareBuffer* buffer,
274 aidl::android::hardware::graphics::composer3::RenderIntent renderIntent) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800275 std::vector<tonemap::ShaderUniform> uniforms;
Alec Mouri6ba7f2b2022-06-02 22:55:05 +0000276
Alec Mouri8a186102023-04-25 00:34:30 +0000277 auto inputColorSpace = toColorSpace(linearEffect.inputDataspace);
278 auto outputColorSpace = toColorSpace(linearEffect.outputDataspace);
Alec Mouri6ba7f2b2022-06-02 22:55:05 +0000279
Alec Mouri8a186102023-04-25 00:34:30 +0000280 uniforms.push_back(
281 {.name = "in_rgbToXyz",
282 .value = buildUniformValue<mat3>(ColorSpace::linearExtendedSRGB().getRGBtoXYZ())});
283 uniforms.push_back({.name = "in_xyzToSrcRgb",
284 .value = buildUniformValue<mat3>(inputColorSpace.getXYZtoRGB())});
285 // Transforms xyz colors to linear source colors, then applies the color transform, then
286 // transforms to linear extended RGB for skia to color manage.
287 uniforms.push_back({.name = "in_colorTransform",
288 .value = buildUniformValue<mat4>(
289 mat4(ColorSpace::linearExtendedSRGB().getXYZtoRGB()) *
290 // TODO: the color transform ideally should be applied
291 // in the source colorspace, but doing that breaks
292 // renderengine tests
293 mat4(outputColorSpace.getRGBtoXYZ()) * colorTransform *
294 mat4(outputColorSpace.getXYZtoRGB()))});
Alec Mouri492c85c2021-11-19 15:58:10 -0800295
296 tonemap::Metadata metadata{.displayMaxLuminance = maxDisplayLuminance,
Alec Mouri492c85c2021-11-19 15:58:10 -0800297 // If the input luminance is unknown, use display luminance (aka,
Alec Mouri3e5965f2023-04-07 18:00:58 +0000298 // no-op any luminance changes).
299 // This is expected to only be meaningful for PQ content
Alec Mouri492c85c2021-11-19 15:58:10 -0800300 .contentMaxLuminance =
Alec Mouri1a3c5452022-03-04 22:44:51 +0000301 maxLuminance > 0 ? maxLuminance : maxDisplayLuminance,
Alec Mouri7a577452022-03-04 23:41:38 +0000302 .currentDisplayLuminance = currentDisplayLuminanceNits > 0
303 ? currentDisplayLuminanceNits
304 : maxDisplayLuminance,
Alec Mourifcedb9c2022-04-11 20:02:17 +0000305 .buffer = buffer,
306 .renderIntent = renderIntent};
Alec Mouri492c85c2021-11-19 15:58:10 -0800307
308 for (const auto uniform : tonemap::getToneMapper()->generateShaderSkSLUniforms(metadata)) {
309 uniforms.push_back(uniform);
310 }
311
312 return uniforms;
313}
314
Alec Mouri1a3c5452022-03-04 22:44:51 +0000315} // namespace android::shaders