Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 1 | /* |
| 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 Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 21 | #include <cmath> |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 22 | #include <optional> |
| 23 | |
| 24 | #include <math/mat4.h> |
| 25 | #include <system/graphics-base-v1.0.h> |
| 26 | #include <ui/ColorSpace.h> |
| 27 | |
| 28 | namespace android::shaders { |
| 29 | |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | aidl::android::hardware::graphics::common::Dataspace toAidlDataspace(ui::Dataspace dataspace) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 33 | return static_cast<aidl::android::hardware::graphics::common::Dataspace>(dataspace); |
| 34 | } |
| 35 | |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 36 | void generateEOTF(ui::Dataspace dataspace, std::string& shader) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 37 | 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 Qi | 2019fd2 | 2021-11-22 10:19:04 -0800 | [diff] [blame] | 77 | case HAL_DATASPACE_TRANSFER_SMPTE_170M: |
| 78 | shader.append(R"( |
| 79 | |
| 80 | float EOTF_sRGB(float srgb) { |
Sally Qi | 575fb07 | 2022-05-09 12:34:52 -0700 | [diff] [blame] | 81 | return srgb <= 0.08125 ? srgb / 4.50 : pow((srgb + 0.099) / 1.099, 1 / 0.45); |
Sally Qi | 2019fd2 | 2021-11-22 10:19:04 -0800 | [diff] [blame] | 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 Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 141 | 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 Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 161 | void generateXYZTransforms(std::string& shader) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 162 | 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 Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 176 | void generateLuminanceScalesForOOTF(ui::Dataspace inputDataspace, ui::Dataspace outputDataspace, |
| 177 | std::string& shader) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 178 | 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"( |
| 188 | float3 ScaleLuminance(float3 xyz) { |
Alec Mouri | 7a57745 | 2022-03-04 23:41:38 +0000 | [diff] [blame] | 189 | return xyz * 1000.0; |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 190 | } |
| 191 | )"); |
| 192 | break; |
| 193 | default: |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 194 | // Input is SDR so map to its white point luminance |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 195 | switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 196 | // Max HLG output is nominally 1000 nits, but BT. 2100-2 allows |
| 197 | // for gamma correcting the HLG OOTF for displays with a different |
| 198 | // dynamic range. Scale to 1000 nits to apply an inverse OOTF against |
| 199 | // a reference display correctly. |
| 200 | // TODO: Use knowledge of the dimming ratio here to prevent |
| 201 | // unintended gamma shaft. |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 202 | case HAL_DATASPACE_TRANSFER_HLG: |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 203 | shader.append(R"( |
| 204 | float3 ScaleLuminance(float3 xyz) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 205 | return xyz * 1000.0; |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 206 | } |
| 207 | )"); |
| 208 | break; |
| 209 | default: |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 210 | shader.append(R"( |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 211 | float3 ScaleLuminance(float3 xyz) { |
| 212 | return xyz * in_libtonemap_displayMaxLuminance; |
| 213 | } |
| 214 | )"); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // Normalizes from absolute light back to relative light (maps from [0, maxNits] back to [0, 1]) |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 221 | static void generateLuminanceNormalizationForOOTF(ui::Dataspace inputDataspace, |
| 222 | ui::Dataspace outputDataspace, |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 223 | std::string& shader) { |
| 224 | switch (outputDataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 225 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 226 | shader.append(R"( |
| 227 | float3 NormalizeLuminance(float3 xyz) { |
| 228 | return xyz / 10000.0; |
| 229 | } |
| 230 | )"); |
| 231 | break; |
| 232 | case HAL_DATASPACE_TRANSFER_HLG: |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 233 | switch (inputDataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 234 | case HAL_DATASPACE_TRANSFER_HLG: |
| 235 | shader.append(R"( |
| 236 | float3 NormalizeLuminance(float3 xyz) { |
| 237 | return xyz / 1000.0; |
| 238 | } |
| 239 | )"); |
| 240 | break; |
| 241 | default: |
| 242 | // Transcoding to HLG requires applying the inverse OOTF |
| 243 | // with the expectation that the OOTF is then applied during |
| 244 | // tonemapping downstream. |
| 245 | shader.append(R"( |
| 246 | float3 NormalizeLuminance(float3 xyz) { |
| 247 | // BT. 2100-2 operates on normalized luminances, |
| 248 | // so renormalize to the input |
| 249 | float ootfGain = pow(xyz.y / 1000.0, -0.2 / 1.2) / 1000.0; |
| 250 | return xyz * ootfGain; |
| 251 | } |
| 252 | )"); |
| 253 | break; |
| 254 | } |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 255 | break; |
| 256 | default: |
| 257 | shader.append(R"( |
| 258 | float3 NormalizeLuminance(float3 xyz) { |
| 259 | return xyz / in_libtonemap_displayMaxLuminance; |
| 260 | } |
| 261 | )"); |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 266 | void generateOOTF(ui::Dataspace inputDataspace, ui::Dataspace outputDataspace, |
| 267 | std::string& shader) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 268 | shader.append(tonemap::getToneMapper() |
| 269 | ->generateTonemapGainShaderSkSL(toAidlDataspace(inputDataspace), |
| 270 | toAidlDataspace(outputDataspace)) |
| 271 | .c_str()); |
| 272 | |
| 273 | generateLuminanceScalesForOOTF(inputDataspace, outputDataspace, shader); |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 274 | generateLuminanceNormalizationForOOTF(inputDataspace, outputDataspace, shader); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 275 | |
| 276 | shader.append(R"( |
| 277 | float3 OOTF(float3 linearRGB, float3 xyz) { |
| 278 | float3 scaledLinearRGB = ScaleLuminance(linearRGB); |
| 279 | float3 scaledXYZ = ScaleLuminance(xyz); |
| 280 | |
| 281 | float gain = libtonemap_LookupTonemapGain(scaledLinearRGB, scaledXYZ); |
| 282 | |
| 283 | return NormalizeLuminance(scaledXYZ * gain); |
| 284 | } |
| 285 | )"); |
| 286 | } |
| 287 | |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 288 | void generateOETF(ui::Dataspace dataspace, std::string& shader) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 289 | switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) { |
| 290 | case HAL_DATASPACE_TRANSFER_ST2084: |
| 291 | shader.append(R"( |
| 292 | |
| 293 | float3 OETF(float3 xyz) { |
| 294 | float m1 = (2610.0 / 4096.0) / 4.0; |
| 295 | float m2 = (2523.0 / 4096.0) * 128.0; |
| 296 | float c1 = (3424.0 / 4096.0); |
| 297 | float c2 = (2413.0 / 4096.0) * 32.0; |
| 298 | float c3 = (2392.0 / 4096.0) * 32.0; |
| 299 | |
| 300 | float3 tmp = pow(xyz, float3(m1)); |
| 301 | tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp); |
| 302 | return pow(tmp, float3(m2)); |
| 303 | } |
| 304 | )"); |
| 305 | break; |
| 306 | case HAL_DATASPACE_TRANSFER_HLG: |
| 307 | shader.append(R"( |
| 308 | float OETF_channel(float channel) { |
| 309 | const float a = 0.17883277; |
| 310 | const float b = 0.28466892; |
| 311 | const float c = 0.55991073; |
| 312 | return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) : |
| 313 | a * log(12.0 * channel - b) + c; |
| 314 | } |
| 315 | |
| 316 | float3 OETF(float3 linear) { |
| 317 | return float3(OETF_channel(linear.r), OETF_channel(linear.g), |
| 318 | OETF_channel(linear.b)); |
| 319 | } |
| 320 | )"); |
| 321 | break; |
| 322 | case HAL_DATASPACE_TRANSFER_LINEAR: |
| 323 | shader.append(R"( |
| 324 | float3 OETF(float3 linear) { |
| 325 | return linear; |
| 326 | } |
| 327 | )"); |
| 328 | break; |
Sally Qi | 2019fd2 | 2021-11-22 10:19:04 -0800 | [diff] [blame] | 329 | case HAL_DATASPACE_TRANSFER_SMPTE_170M: |
| 330 | shader.append(R"( |
| 331 | float OETF_sRGB(float linear) { |
| 332 | return linear <= 0.018 ? |
| 333 | linear * 4.50 : (pow(linear, 0.45) * 1.099) - 0.099; |
| 334 | } |
| 335 | |
| 336 | float3 OETF_sRGB(float3 linear) { |
| 337 | return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 338 | } |
| 339 | |
| 340 | float3 OETF(float3 linear) { |
| 341 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 342 | } |
| 343 | )"); |
| 344 | break; |
| 345 | case HAL_DATASPACE_TRANSFER_GAMMA2_2: |
| 346 | shader.append(R"( |
| 347 | float OETF_sRGB(float linear) { |
| 348 | return pow(linear, (1.0 / 2.2)); |
| 349 | } |
| 350 | |
| 351 | float3 OETF_sRGB(float3 linear) { |
| 352 | return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 353 | } |
| 354 | |
| 355 | float3 OETF(float3 linear) { |
| 356 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 357 | } |
| 358 | )"); |
| 359 | break; |
| 360 | case HAL_DATASPACE_TRANSFER_GAMMA2_6: |
| 361 | shader.append(R"( |
| 362 | float OETF_sRGB(float linear) { |
| 363 | return pow(linear, (1.0 / 2.6)); |
| 364 | } |
| 365 | |
| 366 | float3 OETF_sRGB(float3 linear) { |
| 367 | return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 368 | } |
| 369 | |
| 370 | float3 OETF(float3 linear) { |
| 371 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 372 | } |
| 373 | )"); |
| 374 | break; |
| 375 | case HAL_DATASPACE_TRANSFER_GAMMA2_8: |
| 376 | shader.append(R"( |
| 377 | float OETF_sRGB(float linear) { |
| 378 | return pow(linear, (1.0 / 2.8)); |
| 379 | } |
| 380 | |
| 381 | float3 OETF_sRGB(float3 linear) { |
| 382 | return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 383 | } |
| 384 | |
| 385 | float3 OETF(float3 linear) { |
| 386 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 387 | } |
| 388 | )"); |
| 389 | break; |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 390 | case HAL_DATASPACE_TRANSFER_SRGB: |
| 391 | default: |
| 392 | shader.append(R"( |
| 393 | float OETF_sRGB(float linear) { |
| 394 | return linear <= 0.0031308 ? |
| 395 | linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; |
| 396 | } |
| 397 | |
| 398 | float3 OETF_sRGB(float3 linear) { |
| 399 | return float3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 400 | } |
| 401 | |
| 402 | float3 OETF(float3 linear) { |
| 403 | return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)); |
| 404 | } |
| 405 | )"); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
Alec Mouri | 3c51bc5 | 2022-11-04 01:28:55 +0000 | [diff] [blame] | 410 | void generateEffectiveOOTF(bool undoPremultipliedAlpha, LinearEffect::SkSLType type, |
| 411 | std::string& shader) { |
| 412 | switch (type) { |
| 413 | case LinearEffect::SkSLType::ColorFilter: |
| 414 | shader.append(R"( |
| 415 | half4 main(half4 inputColor) { |
| 416 | float4 c = float4(inputColor); |
| 417 | )"); |
| 418 | break; |
| 419 | case LinearEffect::SkSLType::Shader: |
| 420 | shader.append(R"( |
| 421 | uniform shader child; |
| 422 | half4 main(float2 xy) { |
| 423 | float4 c = float4(child.eval(xy)); |
| 424 | )"); |
| 425 | break; |
| 426 | } |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 427 | if (undoPremultipliedAlpha) { |
| 428 | shader.append(R"( |
| 429 | c.rgb = c.rgb / (c.a + 0.0019); |
| 430 | )"); |
| 431 | } |
| 432 | shader.append(R"( |
| 433 | float3 linearRGB = EOTF(c.rgb); |
| 434 | float3 xyz = ToXYZ(linearRGB); |
| 435 | c.rgb = OETF(ToRGB(OOTF(linearRGB, xyz))); |
| 436 | )"); |
| 437 | if (undoPremultipliedAlpha) { |
| 438 | shader.append(R"( |
| 439 | c.rgb = c.rgb * (c.a + 0.0019); |
| 440 | )"); |
| 441 | } |
| 442 | shader.append(R"( |
| 443 | return c; |
| 444 | } |
| 445 | )"); |
| 446 | } |
Sally Qi | 2019fd2 | 2021-11-22 10:19:04 -0800 | [diff] [blame] | 447 | |
| 448 | // please keep in sync with toSkColorSpace function in renderengine/skia/ColorSpaces.cpp |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 449 | ColorSpace toColorSpace(ui::Dataspace dataspace) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 450 | switch (dataspace & HAL_DATASPACE_STANDARD_MASK) { |
| 451 | case HAL_DATASPACE_STANDARD_BT709: |
| 452 | return ColorSpace::sRGB(); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 453 | case HAL_DATASPACE_STANDARD_DCI_P3: |
| 454 | return ColorSpace::DisplayP3(); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 455 | case HAL_DATASPACE_STANDARD_BT2020: |
Sally Qi | 2019fd2 | 2021-11-22 10:19:04 -0800 | [diff] [blame] | 456 | case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE: |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 457 | return ColorSpace::BT2020(); |
Sally Qi | 2019fd2 | 2021-11-22 10:19:04 -0800 | [diff] [blame] | 458 | case HAL_DATASPACE_STANDARD_ADOBE_RGB: |
| 459 | return ColorSpace::AdobeRGB(); |
| 460 | // TODO(b/208290320): BT601 format and variants return different primaries |
| 461 | case HAL_DATASPACE_STANDARD_BT601_625: |
| 462 | case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED: |
| 463 | case HAL_DATASPACE_STANDARD_BT601_525: |
| 464 | case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED: |
| 465 | // TODO(b/208290329): BT407M format returns different primaries |
| 466 | case HAL_DATASPACE_STANDARD_BT470M: |
| 467 | // TODO(b/208290904): FILM format returns different primaries |
| 468 | case HAL_DATASPACE_STANDARD_FILM: |
| 469 | case HAL_DATASPACE_STANDARD_UNSPECIFIED: |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 470 | default: |
| 471 | return ColorSpace::sRGB(); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 475 | template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true> |
| 476 | std::vector<uint8_t> buildUniformValue(T value) { |
| 477 | std::vector<uint8_t> result; |
| 478 | result.resize(sizeof(value)); |
| 479 | std::memcpy(result.data(), &value, sizeof(value)); |
| 480 | return result; |
| 481 | } |
| 482 | |
Alec Mouri | 5a49372 | 2022-01-26 16:43:02 -0800 | [diff] [blame] | 483 | } // namespace |
| 484 | |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 485 | std::string buildLinearEffectSkSL(const LinearEffect& linearEffect) { |
| 486 | std::string shaderString; |
| 487 | generateEOTF(linearEffect.fakeInputDataspace == ui::Dataspace::UNKNOWN |
| 488 | ? linearEffect.inputDataspace |
| 489 | : linearEffect.fakeInputDataspace, |
| 490 | shaderString); |
| 491 | generateXYZTransforms(shaderString); |
| 492 | generateOOTF(linearEffect.inputDataspace, linearEffect.outputDataspace, shaderString); |
| 493 | generateOETF(linearEffect.outputDataspace, shaderString); |
Alec Mouri | 3c51bc5 | 2022-11-04 01:28:55 +0000 | [diff] [blame] | 494 | generateEffectiveOOTF(linearEffect.undoPremultipliedAlpha, linearEffect.type, shaderString); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 495 | return shaderString; |
| 496 | } |
| 497 | |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 498 | // Generates a list of uniforms to set on the LinearEffect shader above. |
Alec Mouri | fcedb9c | 2022-04-11 20:02:17 +0000 | [diff] [blame] | 499 | std::vector<tonemap::ShaderUniform> buildLinearEffectUniforms( |
| 500 | const LinearEffect& linearEffect, const mat4& colorTransform, float maxDisplayLuminance, |
| 501 | float currentDisplayLuminanceNits, float maxLuminance, AHardwareBuffer* buffer, |
| 502 | aidl::android::hardware::graphics::composer3::RenderIntent renderIntent) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 503 | std::vector<tonemap::ShaderUniform> uniforms; |
Alec Mouri | 6ba7f2b | 2022-06-02 22:55:05 +0000 | [diff] [blame] | 504 | |
| 505 | const ui::Dataspace inputDataspace = linearEffect.fakeInputDataspace == ui::Dataspace::UNKNOWN |
| 506 | ? linearEffect.inputDataspace |
| 507 | : linearEffect.fakeInputDataspace; |
| 508 | |
| 509 | if (inputDataspace == linearEffect.outputDataspace) { |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 510 | uniforms.push_back({.name = "in_rgbToXyz", .value = buildUniformValue<mat4>(mat4())}); |
| 511 | uniforms.push_back( |
| 512 | {.name = "in_xyzToRgb", .value = buildUniformValue<mat4>(colorTransform)}); |
| 513 | } else { |
Alec Mouri | 6ba7f2b | 2022-06-02 22:55:05 +0000 | [diff] [blame] | 514 | ColorSpace inputColorSpace = toColorSpace(inputDataspace); |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 515 | ColorSpace outputColorSpace = toColorSpace(linearEffect.outputDataspace); |
| 516 | uniforms.push_back({.name = "in_rgbToXyz", |
| 517 | .value = buildUniformValue<mat4>(mat4(inputColorSpace.getRGBtoXYZ()))}); |
| 518 | uniforms.push_back({.name = "in_xyzToRgb", |
| 519 | .value = buildUniformValue<mat4>( |
| 520 | colorTransform * mat4(outputColorSpace.getXYZtoRGB()))}); |
| 521 | } |
| 522 | |
| 523 | tonemap::Metadata metadata{.displayMaxLuminance = maxDisplayLuminance, |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 524 | // If the input luminance is unknown, use display luminance (aka, |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame^] | 525 | // no-op any luminance changes). |
| 526 | // This is expected to only be meaningful for PQ content |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 527 | .contentMaxLuminance = |
Alec Mouri | 1a3c545 | 2022-03-04 22:44:51 +0000 | [diff] [blame] | 528 | maxLuminance > 0 ? maxLuminance : maxDisplayLuminance, |
Alec Mouri | 7a57745 | 2022-03-04 23:41:38 +0000 | [diff] [blame] | 529 | .currentDisplayLuminance = currentDisplayLuminanceNits > 0 |
| 530 | ? currentDisplayLuminanceNits |
| 531 | : maxDisplayLuminance, |
Alec Mouri | fcedb9c | 2022-04-11 20:02:17 +0000 | [diff] [blame] | 532 | .buffer = buffer, |
| 533 | .renderIntent = renderIntent}; |
Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 534 | |
| 535 | for (const auto uniform : tonemap::getToneMapper()->generateShaderSkSLUniforms(metadata)) { |
| 536 | uniforms.push_back(uniform); |
| 537 | } |
| 538 | |
| 539 | return uniforms; |
| 540 | } |
| 541 | |
Alec Mouri | 1a3c545 | 2022-03-04 22:44:51 +0000 | [diff] [blame] | 542 | } // namespace android::shaders |