blob: 0d77519ab79a04dd50a9f5d6666652db9e4e935e [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 generateEOTF(ui::Dataspace dataspace, std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -080037 switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) {
38 case HAL_DATASPACE_TRANSFER_ST2084:
39 shader.append(R"(
40
41 float3 EOTF(float3 color) {
42 float m1 = (2610.0 / 4096.0) / 4.0;
43 float m2 = (2523.0 / 4096.0) * 128.0;
44 float c1 = (3424.0 / 4096.0);
45 float c2 = (2413.0 / 4096.0) * 32.0;
46 float c3 = (2392.0 / 4096.0) * 32.0;
47
48 float3 tmp = pow(clamp(color, 0.0, 1.0), 1.0 / float3(m2));
49 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
50 return pow(tmp, 1.0 / float3(m1));
51 }
52 )");
53 break;
54 case HAL_DATASPACE_TRANSFER_HLG:
55 shader.append(R"(
56 float EOTF_channel(float channel) {
57 const float a = 0.17883277;
58 const float b = 0.28466892;
59 const float c = 0.55991073;
60 return channel <= 0.5 ? channel * channel / 3.0 :
61 (exp((channel - c) / a) + b) / 12.0;
62 }
63
64 float3 EOTF(float3 color) {
65 return float3(EOTF_channel(color.r), EOTF_channel(color.g),
66 EOTF_channel(color.b));
67 }
68 )");
69 break;
70 case HAL_DATASPACE_TRANSFER_LINEAR:
71 shader.append(R"(
72 float3 EOTF(float3 color) {
73 return color;
74 }
75 )");
76 break;
Sally Qi2019fd22021-11-22 10:19:04 -080077 case HAL_DATASPACE_TRANSFER_SMPTE_170M:
78 shader.append(R"(
79
80 float EOTF_sRGB(float srgb) {
81 return srgb <= 0.08125 ? srgb / 4.50 : pow((srgb + 0.099) / 1.099, 0.45);
82 }
83
84 float3 EOTF_sRGB(float3 srgb) {
85 return float3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
86 }
87
88 float3 EOTF(float3 srgb) {
89 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
90 }
91 )");
92 break;
93 case HAL_DATASPACE_TRANSFER_GAMMA2_2:
94 shader.append(R"(
95
96 float EOTF_sRGB(float srgb) {
97 return pow(srgb, 2.2);
98 }
99
100 float3 EOTF_sRGB(float3 srgb) {
101 return float3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
102 }
103
104 float3 EOTF(float3 srgb) {
105 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
106 }
107 )");
108 break;
109 case HAL_DATASPACE_TRANSFER_GAMMA2_6:
110 shader.append(R"(
111
112 float EOTF_sRGB(float srgb) {
113 return pow(srgb, 2.6);
114 }
115
116 float3 EOTF_sRGB(float3 srgb) {
117 return float3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
118 }
119
120 float3 EOTF(float3 srgb) {
121 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
122 }
123 )");
124 break;
125 case HAL_DATASPACE_TRANSFER_GAMMA2_8:
126 shader.append(R"(
127
128 float EOTF_sRGB(float srgb) {
129 return pow(srgb, 2.8);
130 }
131
132 float3 EOTF_sRGB(float3 srgb) {
133 return float3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
134 }
135
136 float3 EOTF(float3 srgb) {
137 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
138 }
139 )");
140 break;
Alec Mouri492c85c2021-11-19 15:58:10 -0800141 case HAL_DATASPACE_TRANSFER_SRGB:
142 default:
143 shader.append(R"(
144
145 float EOTF_sRGB(float srgb) {
146 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
147 }
148
149 float3 EOTF_sRGB(float3 srgb) {
150 return float3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
151 }
152
153 float3 EOTF(float3 srgb) {
154 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
155 }
156 )");
157 break;
158 }
159}
160
Alec Mouri5a493722022-01-26 16:43:02 -0800161void generateXYZTransforms(std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800162 shader.append(R"(
163 uniform float4x4 in_rgbToXyz;
164 uniform float4x4 in_xyzToRgb;
165 float3 ToXYZ(float3 rgb) {
166 return (in_rgbToXyz * float4(rgb, 1.0)).rgb;
167 }
168
169 float3 ToRGB(float3 xyz) {
170 return clamp((in_xyzToRgb * float4(xyz, 1.0)).rgb, 0.0, 1.0);
171 }
172 )");
173}
174
175// Conversion from relative light to absolute light (maps from [0, 1] to [0, maxNits])
Alec Mouri5a493722022-01-26 16:43:02 -0800176void generateLuminanceScalesForOOTF(ui::Dataspace inputDataspace, ui::Dataspace outputDataspace,
177 std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800178 switch (inputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
179 case HAL_DATASPACE_TRANSFER_ST2084:
180 shader.append(R"(
181 float3 ScaleLuminance(float3 xyz) {
182 return xyz * 10000.0;
183 }
184 )");
185 break;
186 case HAL_DATASPACE_TRANSFER_HLG:
187 shader.append(R"(
Alec Mouri5a493722022-01-26 16:43:02 -0800188 uniform float in_hlgGamma;
Alec Mouri492c85c2021-11-19 15:58:10 -0800189 float3 ScaleLuminance(float3 xyz) {
Alec Mouri5a493722022-01-26 16:43:02 -0800190 return xyz * 1000.0 * pow(xyz.y, in_hlgGamma - 1);
Alec Mouri492c85c2021-11-19 15:58:10 -0800191 }
192 )");
193 break;
194 default:
195 switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
196 case HAL_DATASPACE_TRANSFER_ST2084:
197 case HAL_DATASPACE_TRANSFER_HLG:
198 // SDR -> HDR tonemap
199 shader.append(R"(
200 float3 ScaleLuminance(float3 xyz) {
201 return xyz * in_libtonemap_inputMaxLuminance;
202 }
203 )");
204 break;
205 default:
206 // Input and output are both SDR, so no tone-mapping is expected so
207 // no-op the luminance normalization.
208 shader.append(R"(
209 float3 ScaleLuminance(float3 xyz) {
210 return xyz * in_libtonemap_displayMaxLuminance;
211 }
212 )");
213 break;
214 }
215 }
216}
217
218// Normalizes from absolute light back to relative light (maps from [0, maxNits] back to [0, 1])
219static void generateLuminanceNormalizationForOOTF(ui::Dataspace outputDataspace,
220 std::string& shader) {
221 switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) {
222 case HAL_DATASPACE_TRANSFER_ST2084:
223 shader.append(R"(
224 float3 NormalizeLuminance(float3 xyz) {
225 return xyz / 10000.0;
226 }
227 )");
228 break;
229 case HAL_DATASPACE_TRANSFER_HLG:
230 shader.append(R"(
Alec Mouri5a493722022-01-26 16:43:02 -0800231 uniform float in_hlgGamma;
Alec Mouri492c85c2021-11-19 15:58:10 -0800232 float3 NormalizeLuminance(float3 xyz) {
Alec Mouri5a493722022-01-26 16:43:02 -0800233 return xyz / 1000.0 *
234 pow(xyz.y / 1000.0, (1 - in_hlgGamma) / (in_hlgGamma));
Alec Mouri492c85c2021-11-19 15:58:10 -0800235 }
236 )");
237 break;
238 default:
239 shader.append(R"(
240 float3 NormalizeLuminance(float3 xyz) {
241 return xyz / in_libtonemap_displayMaxLuminance;
242 }
243 )");
244 break;
245 }
246}
247
Alec Mouri5a493722022-01-26 16:43:02 -0800248void generateOOTF(ui::Dataspace inputDataspace, ui::Dataspace outputDataspace,
249 std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800250 shader.append(tonemap::getToneMapper()
251 ->generateTonemapGainShaderSkSL(toAidlDataspace(inputDataspace),
252 toAidlDataspace(outputDataspace))
253 .c_str());
254
255 generateLuminanceScalesForOOTF(inputDataspace, outputDataspace, shader);
256 generateLuminanceNormalizationForOOTF(outputDataspace, shader);
257
258 shader.append(R"(
259 float3 OOTF(float3 linearRGB, float3 xyz) {
260 float3 scaledLinearRGB = ScaleLuminance(linearRGB);
261 float3 scaledXYZ = ScaleLuminance(xyz);
262
263 float gain = libtonemap_LookupTonemapGain(scaledLinearRGB, scaledXYZ);
264
265 return NormalizeLuminance(scaledXYZ * gain);
266 }
267 )");
268}
269
Alec Mouri5a493722022-01-26 16:43:02 -0800270void generateOETF(ui::Dataspace dataspace, std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800271 switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) {
272 case HAL_DATASPACE_TRANSFER_ST2084:
273 shader.append(R"(
274
275 float3 OETF(float3 xyz) {
276 float m1 = (2610.0 / 4096.0) / 4.0;
277 float m2 = (2523.0 / 4096.0) * 128.0;
278 float c1 = (3424.0 / 4096.0);
279 float c2 = (2413.0 / 4096.0) * 32.0;
280 float c3 = (2392.0 / 4096.0) * 32.0;
281
282 float3 tmp = pow(xyz, float3(m1));
283 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
284 return pow(tmp, float3(m2));
285 }
286 )");
287 break;
288 case HAL_DATASPACE_TRANSFER_HLG:
289 shader.append(R"(
290 float OETF_channel(float channel) {
291 const float a = 0.17883277;
292 const float b = 0.28466892;
293 const float c = 0.55991073;
294 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
295 a * log(12.0 * channel - b) + c;
296 }
297
298 float3 OETF(float3 linear) {
299 return float3(OETF_channel(linear.r), OETF_channel(linear.g),
300 OETF_channel(linear.b));
301 }
302 )");
303 break;
304 case HAL_DATASPACE_TRANSFER_LINEAR:
305 shader.append(R"(
306 float3 OETF(float3 linear) {
307 return linear;
308 }
309 )");
310 break;
Sally Qi2019fd22021-11-22 10:19:04 -0800311 case HAL_DATASPACE_TRANSFER_SMPTE_170M:
312 shader.append(R"(
313 float OETF_sRGB(float linear) {
314 return linear <= 0.018 ?
315 linear * 4.50 : (pow(linear, 0.45) * 1.099) - 0.099;
316 }
317
318 float3 OETF_sRGB(float3 linear) {
319 return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
320 }
321
322 float3 OETF(float3 linear) {
323 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
324 }
325 )");
326 break;
327 case HAL_DATASPACE_TRANSFER_GAMMA2_2:
328 shader.append(R"(
329 float OETF_sRGB(float linear) {
330 return pow(linear, (1.0 / 2.2));
331 }
332
333 float3 OETF_sRGB(float3 linear) {
334 return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
335 }
336
337 float3 OETF(float3 linear) {
338 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
339 }
340 )");
341 break;
342 case HAL_DATASPACE_TRANSFER_GAMMA2_6:
343 shader.append(R"(
344 float OETF_sRGB(float linear) {
345 return pow(linear, (1.0 / 2.6));
346 }
347
348 float3 OETF_sRGB(float3 linear) {
349 return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
350 }
351
352 float3 OETF(float3 linear) {
353 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
354 }
355 )");
356 break;
357 case HAL_DATASPACE_TRANSFER_GAMMA2_8:
358 shader.append(R"(
359 float OETF_sRGB(float linear) {
360 return pow(linear, (1.0 / 2.8));
361 }
362
363 float3 OETF_sRGB(float3 linear) {
364 return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
365 }
366
367 float3 OETF(float3 linear) {
368 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
369 }
370 )");
371 break;
Alec Mouri492c85c2021-11-19 15:58:10 -0800372 case HAL_DATASPACE_TRANSFER_SRGB:
373 default:
374 shader.append(R"(
375 float OETF_sRGB(float linear) {
376 return linear <= 0.0031308 ?
377 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
378 }
379
380 float3 OETF_sRGB(float3 linear) {
381 return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
382 }
383
384 float3 OETF(float3 linear) {
385 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
386 }
387 )");
388 break;
389 }
390}
391
Alec Mouri5a493722022-01-26 16:43:02 -0800392void generateEffectiveOOTF(bool undoPremultipliedAlpha, std::string& shader) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800393 shader.append(R"(
394 uniform shader child;
395 half4 main(float2 xy) {
396 float4 c = float4(child.eval(xy));
397 )");
398 if (undoPremultipliedAlpha) {
399 shader.append(R"(
400 c.rgb = c.rgb / (c.a + 0.0019);
401 )");
402 }
403 shader.append(R"(
404 float3 linearRGB = EOTF(c.rgb);
405 float3 xyz = ToXYZ(linearRGB);
406 c.rgb = OETF(ToRGB(OOTF(linearRGB, xyz)));
407 )");
408 if (undoPremultipliedAlpha) {
409 shader.append(R"(
410 c.rgb = c.rgb * (c.a + 0.0019);
411 )");
412 }
413 shader.append(R"(
414 return c;
415 }
416 )");
417}
Sally Qi2019fd22021-11-22 10:19:04 -0800418
419// please keep in sync with toSkColorSpace function in renderengine/skia/ColorSpaces.cpp
Alec Mouri5a493722022-01-26 16:43:02 -0800420ColorSpace toColorSpace(ui::Dataspace dataspace) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800421 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
422 case HAL_DATASPACE_STANDARD_BT709:
423 return ColorSpace::sRGB();
Alec Mouri492c85c2021-11-19 15:58:10 -0800424 case HAL_DATASPACE_STANDARD_DCI_P3:
425 return ColorSpace::DisplayP3();
Alec Mouri492c85c2021-11-19 15:58:10 -0800426 case HAL_DATASPACE_STANDARD_BT2020:
Sally Qi2019fd22021-11-22 10:19:04 -0800427 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
Alec Mouri492c85c2021-11-19 15:58:10 -0800428 return ColorSpace::BT2020();
Sally Qi2019fd22021-11-22 10:19:04 -0800429 case HAL_DATASPACE_STANDARD_ADOBE_RGB:
430 return ColorSpace::AdobeRGB();
431 // TODO(b/208290320): BT601 format and variants return different primaries
432 case HAL_DATASPACE_STANDARD_BT601_625:
433 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
434 case HAL_DATASPACE_STANDARD_BT601_525:
435 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
436 // TODO(b/208290329): BT407M format returns different primaries
437 case HAL_DATASPACE_STANDARD_BT470M:
438 // TODO(b/208290904): FILM format returns different primaries
439 case HAL_DATASPACE_STANDARD_FILM:
440 case HAL_DATASPACE_STANDARD_UNSPECIFIED:
Alec Mouri492c85c2021-11-19 15:58:10 -0800441 default:
442 return ColorSpace::sRGB();
Alec Mouri492c85c2021-11-19 15:58:10 -0800443 }
444}
445
Alec Mouri5a493722022-01-26 16:43:02 -0800446template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true>
447std::vector<uint8_t> buildUniformValue(T value) {
448 std::vector<uint8_t> result;
449 result.resize(sizeof(value));
450 std::memcpy(result.data(), &value, sizeof(value));
451 return result;
452}
453
454// Refer to BT2100-2
455float computeHlgGamma(float currentDisplayBrightnessNits) {
456 return 1.2 + 0.42 * std::log10(currentDisplayBrightnessNits / 1000);
457}
458
459} // namespace
460
Alec Mouri492c85c2021-11-19 15:58:10 -0800461std::string buildLinearEffectSkSL(const LinearEffect& linearEffect) {
462 std::string shaderString;
463 generateEOTF(linearEffect.fakeInputDataspace == ui::Dataspace::UNKNOWN
464 ? linearEffect.inputDataspace
465 : linearEffect.fakeInputDataspace,
466 shaderString);
467 generateXYZTransforms(shaderString);
468 generateOOTF(linearEffect.inputDataspace, linearEffect.outputDataspace, shaderString);
469 generateOETF(linearEffect.outputDataspace, shaderString);
470 generateEffectiveOOTF(linearEffect.undoPremultipliedAlpha, shaderString);
471 return shaderString;
472}
473
Alec Mouri492c85c2021-11-19 15:58:10 -0800474// Generates a list of uniforms to set on the LinearEffect shader above.
475std::vector<tonemap::ShaderUniform> buildLinearEffectUniforms(const LinearEffect& linearEffect,
476 const mat4& colorTransform,
477 float maxDisplayLuminance,
Alec Mourib21d94e2022-01-13 17:44:10 -0800478 float currentDisplayLuminanceNits,
Alec Mouri1a3c5452022-03-04 22:44:51 +0000479 float maxLuminance,
480 AHardwareBuffer* buffer) {
Alec Mouri492c85c2021-11-19 15:58:10 -0800481 std::vector<tonemap::ShaderUniform> uniforms;
482 if (linearEffect.inputDataspace == linearEffect.outputDataspace) {
483 uniforms.push_back({.name = "in_rgbToXyz", .value = buildUniformValue<mat4>(mat4())});
484 uniforms.push_back(
485 {.name = "in_xyzToRgb", .value = buildUniformValue<mat4>(colorTransform)});
486 } else {
487 ColorSpace inputColorSpace = toColorSpace(linearEffect.inputDataspace);
488 ColorSpace outputColorSpace = toColorSpace(linearEffect.outputDataspace);
489 uniforms.push_back({.name = "in_rgbToXyz",
490 .value = buildUniformValue<mat4>(mat4(inputColorSpace.getRGBtoXYZ()))});
491 uniforms.push_back({.name = "in_xyzToRgb",
492 .value = buildUniformValue<mat4>(
493 colorTransform * mat4(outputColorSpace.getXYZtoRGB()))});
494 }
495
Alec Mouri5a493722022-01-26 16:43:02 -0800496 if ((linearEffect.inputDataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_HLG) {
497 uniforms.push_back(
498 {.name = "in_hlgGamma",
499 .value = buildUniformValue<float>(computeHlgGamma(currentDisplayLuminanceNits))});
500 }
501
Alec Mouri492c85c2021-11-19 15:58:10 -0800502 tonemap::Metadata metadata{.displayMaxLuminance = maxDisplayLuminance,
Alec Mouri492c85c2021-11-19 15:58:10 -0800503 // If the input luminance is unknown, use display luminance (aka,
504 // no-op any luminance changes)
505 // This will be the case for eg screenshots in addition to
506 // uncalibrated displays
507 .contentMaxLuminance =
Alec Mouri1a3c5452022-03-04 22:44:51 +0000508 maxLuminance > 0 ? maxLuminance : maxDisplayLuminance,
509 .buffer = buffer};
Alec Mouri492c85c2021-11-19 15:58:10 -0800510
511 for (const auto uniform : tonemap::getToneMapper()->generateShaderSkSLUniforms(metadata)) {
512 uniforms.push_back(uniform);
513 }
514
515 return uniforms;
516}
517
Alec Mouri1a3c5452022-03-04 22:44:51 +0000518} // namespace android::shaders