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