summaryrefslogtreecommitdiff
path: root/libs/tonemap/tonemap.cpp
blob: c2372fe82885a97b0204fc00ad5c0d5ae43530f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
 * Copyright 2021 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 <tonemap/tonemap.h>

#include <algorithm>
#include <cstdint>
#include <mutex>
#include <type_traits>

namespace android::tonemap {

namespace {

// Flag containing the variant of tone map algorithm to use.
enum class ToneMapAlgorithm {
    AndroidO,  // Default algorithm in place since Android O,
    Android13, // Algorithm used in Android 13.
};

static const constexpr auto kToneMapAlgorithm = ToneMapAlgorithm::Android13;

static const constexpr auto kTransferMask =
        static_cast<int32_t>(aidl::android::hardware::graphics::common::Dataspace::TRANSFER_MASK);
static const constexpr auto kTransferST2084 =
        static_cast<int32_t>(aidl::android::hardware::graphics::common::Dataspace::TRANSFER_ST2084);
static const constexpr auto kTransferHLG =
        static_cast<int32_t>(aidl::android::hardware::graphics::common::Dataspace::TRANSFER_HLG);

template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, bool> = true>
std::vector<uint8_t> buildUniformValue(T value) {
    std::vector<uint8_t> result;
    result.resize(sizeof(value));
    std::memcpy(result.data(), &value, sizeof(value));
    return result;
}

class ToneMapperO : public ToneMapper {
public:
    std::string generateTonemapGainShaderSkSL(
            aidl::android::hardware::graphics::common::Dataspace sourceDataspace,
            aidl::android::hardware::graphics::common::Dataspace destinationDataspace) override {
        const int32_t sourceDataspaceInt = static_cast<int32_t>(sourceDataspace);
        const int32_t destinationDataspaceInt = static_cast<int32_t>(destinationDataspace);

        std::string program;
        // Define required uniforms
        program.append(R"(
                uniform float in_libtonemap_displayMaxLuminance;
                uniform float in_libtonemap_inputMaxLuminance;
            )");
        switch (sourceDataspaceInt & kTransferMask) {
            case kTransferST2084:
            case kTransferHLG:
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(vec3 xyz) {
                                        return xyz.y;
                                    }
                                )");
                        break;
                    case kTransferHLG:
                        // PQ has a wider luminance range (10,000 nits vs. 1,000 nits) than HLG, so
                        // we'll clamp the luminance range in case we're mapping from PQ input to
                        // HLG output.
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(vec3 xyz) {
                                        return clamp(xyz.y, 0.0, 1000.0);
                                    }
                                )");
                        break;
                    default:
                        // Here we're mapping from HDR to SDR content, so interpolate using a
                        // Hermitian polynomial onto the smaller luminance range.
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(vec3 xyz) {
                                        float maxInLumi = in_libtonemap_inputMaxLuminance;
                                        float maxOutLumi = in_libtonemap_displayMaxLuminance;

                                        float nits = xyz.y;

                                        // if the max input luminance is less than what we can
                                        // output then no tone mapping is needed as all color
                                        // values will be in range.
                                        if (maxInLumi <= maxOutLumi) {
                                            return xyz.y;
                                        } else {

                                            // three control points
                                            const float x0 = 10.0;
                                            const float y0 = 17.0;
                                            float x1 = maxOutLumi * 0.75;
                                            float y1 = x1;
                                            float x2 = x1 + (maxInLumi - x1) / 2.0;
                                            float y2 = y1 + (maxOutLumi - y1) * 0.75;

                                            // horizontal distances between the last three
                                            // control points
                                            float h12 = x2 - x1;
                                            float h23 = maxInLumi - x2;
                                            // tangents at the last three control points
                                            float m1 = (y2 - y1) / h12;
                                            float m3 = (maxOutLumi - y2) / h23;
                                            float m2 = (m1 + m3) / 2.0;

                                            if (nits < x0) {
                                                // scale [0.0, x0] to [0.0, y0] linearly
                                                float slope = y0 / x0;
                                                return nits * slope;
                                            } else if (nits < x1) {
                                                // scale [x0, x1] to [y0, y1] linearly
                                                float slope = (y1 - y0) / (x1 - x0);
                                                nits = y0 + (nits - x0) * slope;
                                            } else if (nits < x2) {
                                                // scale [x1, x2] to [y1, y2] using Hermite interp
                                                float t = (nits - x1) / h12;
                                                nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) *
                                                        (1.0 - t) * (1.0 - t) +
                                                        (y2 * (3.0 - 2.0 * t) +
                                                        h12 * m2 * (t - 1.0)) * t * t;
                                            } else {
                                                // scale [x2, maxInLumi] to [y2, maxOutLumi] using
                                                // Hermite interp
                                                float t = (nits - x2) / h23;
                                                nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) *
                                                        (1.0 - t) * (1.0 - t) + (maxOutLumi *
                                                        (3.0 - 2.0 * t) + h23 * m3 *
                                                        (t - 1.0)) * t * t;
                                            }
                                        }

                                        return nits;
                                    }
                                )");
                        break;
                }
                break;
            default:
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                    case kTransferHLG:
                        // Map from SDR onto an HDR output buffer
                        // Here we use a polynomial curve to map from [0, displayMaxLuminance] onto
                        // [0, maxOutLumi] which is hard-coded to be 3000 nits.
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(vec3 xyz) {
                                        const float maxOutLumi = 3000.0;

                                        const float x0 = 5.0;
                                        const float y0 = 2.5;
                                        float x1 = in_libtonemap_displayMaxLuminance * 0.7;
                                        float y1 = maxOutLumi * 0.15;
                                        float x2 = in_libtonemap_displayMaxLuminance * 0.9;
                                        float y2 = maxOutLumi * 0.45;
                                        float x3 = in_libtonemap_displayMaxLuminance;
                                        float y3 = maxOutLumi;

                                        float c1 = y1 / 3.0;
                                        float c2 = y2 / 2.0;
                                        float c3 = y3 / 1.5;

                                        float nits = xyz.y;

                                        if (nits <= x0) {
                                            // scale [0.0, x0] to [0.0, y0] linearly
                                            float slope = y0 / x0;
                                            return nits * slope;
                                        } else if (nits <= x1) {
                                            // scale [x0, x1] to [y0, y1] using a curve
                                            float t = (nits - x0) / (x1 - x0);
                                            nits = (1.0 - t) * (1.0 - t) * y0 +
                                                    2.0 * (1.0 - t) * t * c1 + t * t * y1;
                                        } else if (nits <= x2) {
                                            // scale [x1, x2] to [y1, y2] using a curve
                                            float t = (nits - x1) / (x2 - x1);
                                            nits = (1.0 - t) * (1.0 - t) * y1 +
                                                    2.0 * (1.0 - t) * t * c2 + t * t * y2;
                                        } else {
                                            // scale [x2, x3] to [y2, y3] using a curve
                                            float t = (nits - x2) / (x3 - x2);
                                            nits = (1.0 - t) * (1.0 - t) * y2 +
                                                    2.0 * (1.0 - t) * t * c3 + t * t * y3;
                                        }

                                        return nits;
                                    }
                                )");
                        break;
                    default:
                        // For completeness, this is tone-mapping from SDR to SDR, where this is
                        // just a no-op.
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(vec3 xyz) {
                                        return xyz.y;
                                    }
                                )");
                        break;
                }
                break;
        }

        program.append(R"(
            float libtonemap_LookupTonemapGain(vec3 linearRGB, vec3 xyz) {
                if (xyz.y <= 0.0) {
                    return 1.0;
                }
                return libtonemap_ToneMapTargetNits(xyz) / xyz.y;
            }
        )");
        return program;
    }

    std::vector<ShaderUniform> generateShaderSkSLUniforms(const Metadata& metadata) override {
        std::vector<ShaderUniform> uniforms;

        uniforms.reserve(2);

        uniforms.push_back({.name = "in_libtonemap_displayMaxLuminance",
                            .value = buildUniformValue<float>(metadata.displayMaxLuminance)});
        uniforms.push_back({.name = "in_libtonemap_inputMaxLuminance",
                            .value = buildUniformValue<float>(metadata.contentMaxLuminance)});
        return uniforms;
    }

    double lookupTonemapGain(
            aidl::android::hardware::graphics::common::Dataspace sourceDataspace,
            aidl::android::hardware::graphics::common::Dataspace destinationDataspace,
            vec3 /* linearRGB */, vec3 xyz, const Metadata& metadata) override {
        if (xyz.y <= 0.0) {
            return 1.0;
        }
        const int32_t sourceDataspaceInt = static_cast<int32_t>(sourceDataspace);
        const int32_t destinationDataspaceInt = static_cast<int32_t>(destinationDataspace);

        double targetNits = 0.0;
        switch (sourceDataspaceInt & kTransferMask) {
            case kTransferST2084:
            case kTransferHLG:
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                        targetNits = xyz.y;
                        break;
                    case kTransferHLG:
                        // PQ has a wider luminance range (10,000 nits vs. 1,000 nits) than HLG, so
                        // we'll clamp the luminance range in case we're mapping from PQ input to
                        // HLG output.
                        targetNits = std::clamp(xyz.y, 0.0f, 1000.0f);
                        break;
                    default:
                        // Here we're mapping from HDR to SDR content, so interpolate using a
                        // Hermitian polynomial onto the smaller luminance range.

                        targetNits = xyz.y;
                        // if the max input luminance is less than what we can output then
                        // no tone mapping is needed as all color values will be in range.
                        if (metadata.contentMaxLuminance > metadata.displayMaxLuminance) {
                            // three control points
                            const double x0 = 10.0;
                            const double y0 = 17.0;
                            double x1 = metadata.displayMaxLuminance * 0.75;
                            double y1 = x1;
                            double x2 = x1 + (metadata.contentMaxLuminance - x1) / 2.0;
                            double y2 = y1 + (metadata.displayMaxLuminance - y1) * 0.75;

                            // horizontal distances between the last three control points
                            double h12 = x2 - x1;
                            double h23 = metadata.contentMaxLuminance - x2;
                            // tangents at the last three control points
                            double m1 = (y2 - y1) / h12;
                            double m3 = (metadata.displayMaxLuminance - y2) / h23;
                            double m2 = (m1 + m3) / 2.0;

                            if (targetNits < x0) {
                                // scale [0.0, x0] to [0.0, y0] linearly
                                double slope = y0 / x0;
                                targetNits *= slope;
                            } else if (targetNits < x1) {
                                // scale [x0, x1] to [y0, y1] linearly
                                double slope = (y1 - y0) / (x1 - x0);
                                targetNits = y0 + (targetNits - x0) * slope;
                            } else if (targetNits < x2) {
                                // scale [x1, x2] to [y1, y2] using Hermite interp
                                double t = (targetNits - x1) / h12;
                                targetNits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) *
                                                (1.0 - t) +
                                        (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
                            } else {
                                // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
                                double t = (targetNits - x2) / h23;
                                targetNits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) *
                                                (1.0 - t) +
                                        (metadata.displayMaxLuminance * (3.0 - 2.0 * t) +
                                         h23 * m3 * (t - 1.0)) *
                                                t * t;
                            }
                        }
                        break;
                }
                break;
            default:
                // source is SDR
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                    case kTransferHLG: {
                        // Map from SDR onto an HDR output buffer
                        // Here we use a polynomial curve to map from [0, displayMaxLuminance] onto
                        // [0, maxOutLumi] which is hard-coded to be 3000 nits.
                        const double maxOutLumi = 3000.0;

                        double x0 = 5.0;
                        double y0 = 2.5;
                        double x1 = metadata.displayMaxLuminance * 0.7;
                        double y1 = maxOutLumi * 0.15;
                        double x2 = metadata.displayMaxLuminance * 0.9;
                        double y2 = maxOutLumi * 0.45;
                        double x3 = metadata.displayMaxLuminance;
                        double y3 = maxOutLumi;

                        double c1 = y1 / 3.0;
                        double c2 = y2 / 2.0;
                        double c3 = y3 / 1.5;

                        targetNits = xyz.y;

                        if (targetNits <= x0) {
                            // scale [0.0, x0] to [0.0, y0] linearly
                            double slope = y0 / x0;
                            targetNits *= slope;
                        } else if (targetNits <= x1) {
                            // scale [x0, x1] to [y0, y1] using a curve
                            double t = (targetNits - x0) / (x1 - x0);
                            targetNits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 +
                                    t * t * y1;
                        } else if (targetNits <= x2) {
                            // scale [x1, x2] to [y1, y2] using a curve
                            double t = (targetNits - x1) / (x2 - x1);
                            targetNits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 +
                                    t * t * y2;
                        } else {
                            // scale [x2, x3] to [y2, y3] using a curve
                            double t = (targetNits - x2) / (x3 - x2);
                            targetNits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 +
                                    t * t * y3;
                        }
                    } break;
                    default:
                        // For completeness, this is tone-mapping from SDR to SDR, where this is
                        // just a no-op.
                        targetNits = xyz.y;
                        break;
                }
        }

        return targetNits / xyz.y;
    }
};

class ToneMapper13 : public ToneMapper {
private:
    double OETF_ST2084(double nits) {
        nits = nits / 10000.0;
        double m1 = (2610.0 / 4096.0) / 4.0;
        double m2 = (2523.0 / 4096.0) * 128.0;
        double c1 = (3424.0 / 4096.0);
        double c2 = (2413.0 / 4096.0) * 32.0;
        double c3 = (2392.0 / 4096.0) * 32.0;

        double tmp = std::pow(nits, m1);
        tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
        return std::pow(tmp, m2);
    }

    double OETF_HLG(double nits) {
        nits = nits / 1000.0;
        const double a = 0.17883277;
        const double b = 0.28466892;
        const double c = 0.55991073;
        return nits <= 1.0 / 12.0 ? std::sqrt(3.0 * nits) : a * std::log(12.0 * nits - b) + c;
    }

public:
    std::string generateTonemapGainShaderSkSL(
            aidl::android::hardware::graphics::common::Dataspace sourceDataspace,
            aidl::android::hardware::graphics::common::Dataspace destinationDataspace) override {
        const int32_t sourceDataspaceInt = static_cast<int32_t>(sourceDataspace);
        const int32_t destinationDataspaceInt = static_cast<int32_t>(destinationDataspace);

        std::string program;
        // Input uniforms
        program.append(R"(
                uniform float in_libtonemap_displayMaxLuminance;
                uniform float in_libtonemap_inputMaxLuminance;
            )");
        switch (sourceDataspaceInt & kTransferMask) {
            case kTransferST2084:
            case kTransferHLG:
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(float maxRGB) {
                                        return maxRGB;
                                    }
                                )");
                        break;
                    case kTransferHLG:
                        // PQ has a wider luminance range (10,000 nits vs. 1,000 nits) than HLG, so
                        // we'll clamp the luminance range in case we're mapping from PQ input to
                        // HLG output.
                        program.append(R"(
                                    float libtonemap_ToneMapTargetNits(float maxRGB) {
                                        return clamp(maxRGB, 0.0, 1000.0);
                                    }
                                )");
                        break;

                    default:
                        switch (sourceDataspaceInt & kTransferMask) {
                            case kTransferST2084:
                                program.append(R"(
                                        float libtonemap_OETFTone(float channel) {
                                            channel = channel / 10000.0;
                                            float m1 = (2610.0 / 4096.0) / 4.0;
                                            float m2 = (2523.0 / 4096.0) * 128.0;
                                            float c1 = (3424.0 / 4096.0);
                                            float c2 = (2413.0 / 4096.0) * 32.0;
                                            float c3 = (2392.0 / 4096.0) * 32.0;

                                            float tmp = pow(channel, float(m1));
                                            tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
                                            return pow(tmp, float(m2));
                                        }
                                    )");
                                break;
                            case kTransferHLG:
                                program.append(R"(
                                        float libtonemap_OETFTone(float channel) {
                                            channel = channel / 1000.0;
                                            const float a = 0.17883277;
                                            const float b = 0.28466892;
                                            const float c = 0.55991073;
                                            return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
                                                    a * log(12.0 * channel - b) + c;
                                        }
                                    )");
                                break;
                        }
                        // Here we're mapping from HDR to SDR content, so interpolate using a
                        // Hermitian polynomial onto the smaller luminance range.
                        program.append(R"(
                                float libtonemap_ToneMapTargetNits(float maxRGB) {
                                    float maxInLumi = in_libtonemap_inputMaxLuminance;
                                    float maxOutLumi = in_libtonemap_displayMaxLuminance;

                                    float nits = maxRGB;

                                    float x1 = maxOutLumi * 0.65;
                                    float y1 = x1;

                                    float x3 = maxInLumi;
                                    float y3 = maxOutLumi;

                                    float x2 = x1 + (x3 - x1) * 4.0 / 17.0;
                                    float y2 = maxOutLumi * 0.9;

                                    float greyNorm1 = libtonemap_OETFTone(x1);
                                    float greyNorm2 = libtonemap_OETFTone(x2);
                                    float greyNorm3 = libtonemap_OETFTone(x3);

                                    float slope1 = 0;
                                    float slope2 = (y2 - y1) / (greyNorm2 - greyNorm1);
                                    float slope3 = (y3 - y2 ) / (greyNorm3 - greyNorm2);

                                    if (nits < x1) {
                                        return nits;
                                    }

                                    if (nits > maxInLumi) {
                                        return maxOutLumi;
                                    }

                                    float greyNits = libtonemap_OETFTone(nits);

                                    if (greyNits <= greyNorm2) {
                                        nits = (greyNits - greyNorm2) * slope2 + y2;
                                    } else if (greyNits <= greyNorm3) {
                                        nits = (greyNits - greyNorm3) * slope3 + y3;
                                    } else {
                                        nits = maxOutLumi;
                                    }

                                    return nits;
                                }
                                )");
                        break;
                }
                break;
            default:
                // Inverse tone-mapping and SDR-SDR mapping is not supported.
                program.append(R"(
                            float libtonemap_ToneMapTargetNits(float maxRGB) {
                                return maxRGB;
                            }
                        )");
                break;
        }

        program.append(R"(
            float libtonemap_LookupTonemapGain(vec3 linearRGB, vec3 xyz) {
                float maxRGB = max(linearRGB.r, max(linearRGB.g, linearRGB.b));
                if (maxRGB <= 0.0) {
                    return 1.0;
                }
                return libtonemap_ToneMapTargetNits(maxRGB) / maxRGB;
            }
        )");
        return program;
    }

    std::vector<ShaderUniform> generateShaderSkSLUniforms(const Metadata& metadata) override {
        // Hardcode the max content luminance to a "reasonable" level
        static const constexpr float kContentMaxLuminance = 4000.f;
        std::vector<ShaderUniform> uniforms;
        uniforms.reserve(2);
        uniforms.push_back({.name = "in_libtonemap_displayMaxLuminance",
                            .value = buildUniformValue<float>(metadata.displayMaxLuminance)});
        uniforms.push_back({.name = "in_libtonemap_inputMaxLuminance",
                            .value = buildUniformValue<float>(kContentMaxLuminance)});
        return uniforms;
    }

    double lookupTonemapGain(
            aidl::android::hardware::graphics::common::Dataspace sourceDataspace,
            aidl::android::hardware::graphics::common::Dataspace destinationDataspace,
            vec3 linearRGB, vec3 /* xyz */, const Metadata& metadata) override {
        double maxRGB = std::max({linearRGB.r, linearRGB.g, linearRGB.b});

        if (maxRGB <= 0.0) {
            return 1.0;
        }

        const int32_t sourceDataspaceInt = static_cast<int32_t>(sourceDataspace);
        const int32_t destinationDataspaceInt = static_cast<int32_t>(destinationDataspace);

        double targetNits = 0.0;
        switch (sourceDataspaceInt & kTransferMask) {
            case kTransferST2084:
            case kTransferHLG:
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                        targetNits = maxRGB;
                        break;
                    case kTransferHLG:
                        // PQ has a wider luminance range (10,000 nits vs. 1,000 nits) than HLG, so
                        // we'll clamp the luminance range in case we're mapping from PQ input to
                        // HLG output.
                        targetNits = std::clamp(maxRGB, 0.0, 1000.0);
                        break;
                    default:
                        // Here we're mapping from HDR to SDR content, so interpolate using a
                        // Hermitian polynomial onto the smaller luminance range.

                        double maxInLumi = 4000;
                        double maxOutLumi = metadata.displayMaxLuminance;

                        targetNits = maxRGB;

                        double x1 = maxOutLumi * 0.65;
                        double y1 = x1;

                        double x3 = maxInLumi;
                        double y3 = maxOutLumi;

                        double x2 = x1 + (x3 - x1) * 4.0 / 17.0;
                        double y2 = maxOutLumi * 0.9;

                        double greyNorm1 = 0.0;
                        double greyNorm2 = 0.0;
                        double greyNorm3 = 0.0;

                        if ((sourceDataspaceInt & kTransferMask) == kTransferST2084) {
                            greyNorm1 = OETF_ST2084(x1);
                            greyNorm2 = OETF_ST2084(x2);
                            greyNorm3 = OETF_ST2084(x3);
                        } else if ((sourceDataspaceInt & kTransferMask) == kTransferHLG) {
                            greyNorm1 = OETF_HLG(x1);
                            greyNorm2 = OETF_HLG(x2);
                            greyNorm3 = OETF_HLG(x3);
                        }

                        double slope2 = (y2 - y1) / (greyNorm2 - greyNorm1);
                        double slope3 = (y3 - y2) / (greyNorm3 - greyNorm2);

                        if (targetNits < x1) {
                            break;
                        }

                        if (targetNits > maxInLumi) {
                            targetNits = maxOutLumi;
                            break;
                        }

                        double greyNits = 0.0;
                        if ((sourceDataspaceInt & kTransferMask) == kTransferST2084) {
                            greyNits = OETF_ST2084(targetNits);
                        } else if ((sourceDataspaceInt & kTransferMask) == kTransferHLG) {
                            greyNits = OETF_HLG(targetNits);
                        }

                        if (greyNits <= greyNorm2) {
                            targetNits = (greyNits - greyNorm2) * slope2 + y2;
                        } else if (greyNits <= greyNorm3) {
                            targetNits = (greyNits - greyNorm3) * slope3 + y3;
                        } else {
                            targetNits = maxOutLumi;
                        }
                        break;
                }
                break;
            default:
                switch (destinationDataspaceInt & kTransferMask) {
                    case kTransferST2084:
                    case kTransferHLG:
                    default:
                        targetNits = maxRGB;
                        break;
                }
                break;
        }

        return targetNits / maxRGB;
    }
};

} // namespace

ToneMapper* getToneMapper() {
    static std::once_flag sOnce;
    static std::unique_ptr<ToneMapper> sToneMapper;

    std::call_once(sOnce, [&] {
        switch (kToneMapAlgorithm) {
            case ToneMapAlgorithm::AndroidO:
                sToneMapper = std::unique_ptr<ToneMapper>(new ToneMapperO());
                break;
            case ToneMapAlgorithm::Android13:
                sToneMapper = std::unique_ptr<ToneMapper>(new ToneMapper13());
        }
    });

    return sToneMapper.get();
}
} // namespace android::tonemap