xref: /aosp_15_r20/frameworks/native/libs/ui/include_types/ui/HdrRenderTypeUtils.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 #pragma once
18 
19 #include <ui/GraphicTypes.h>
20 
21 namespace android {
22 
23 enum class HdrRenderType {
24     SDR,         // just render to SDR
25     DISPLAY_HDR, // HDR by extended brightness
26     GENERIC_HDR  // tonemapped HDR
27 };
28 
29 /***
30  * A helper function to classify how we treat the result based on params.
31  *
32  * @param dataspace the dataspace
33  * @param pixelFormat optional, in case there is no source buffer.
34  * @param hdrSdrRatio default is 1.f, render engine side doesn't take care of it.
35  * @return HdrRenderType
36  */
37 inline HdrRenderType getHdrRenderType(ui::Dataspace dataspace,
38                                       std::optional<ui::PixelFormat> pixelFormat,
39                                       float hdrSdrRatio = 1.f) {
40     const auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK;
41     const auto range = dataspace & HAL_DATASPACE_RANGE_MASK;
42 
43     if (transfer == HAL_DATASPACE_TRANSFER_ST2084 || transfer == HAL_DATASPACE_TRANSFER_HLG) {
44         return HdrRenderType::GENERIC_HDR;
45     }
46 
47     static const auto BT2020_LINEAR_EXT = static_cast<ui::Dataspace>(HAL_DATASPACE_STANDARD_BT2020 |
48                                                                      HAL_DATASPACE_TRANSFER_LINEAR |
49                                                                      HAL_DATASPACE_RANGE_EXTENDED);
50 
51     if ((dataspace == BT2020_LINEAR_EXT || dataspace == ui::Dataspace::V0_SCRGB) &&
52         pixelFormat.has_value() && pixelFormat.value() == ui::PixelFormat::RGBA_FP16) {
53         return HdrRenderType::GENERIC_HDR;
54     }
55 
56     // Extended range layer with an hdr/sdr ratio of > 1.01f can "self-promote" to HDR.
57     if (range == HAL_DATASPACE_RANGE_EXTENDED && hdrSdrRatio > 1.01f) {
58         return HdrRenderType::DISPLAY_HDR;
59     }
60 
61     return HdrRenderType::SDR;
62 }
63 
64 /**
65  * Returns the maximum headroom allowed for this content under "idealized"
66  * display conditions (low surround luminance, high-enough display brightness).
67  *
68  * TODO: take into account hdr metadata, but square it with the fact that some
69  * HLG content has CTA.861-3 metadata
70  */
getIdealizedMaxHeadroom(ui::Dataspace dataspace)71 inline float getIdealizedMaxHeadroom(ui::Dataspace dataspace) {
72     const auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK;
73 
74     switch (transfer) {
75         case HAL_DATASPACE_TRANSFER_ST2084:
76             return 10000.0f / 203.0f;
77         case HAL_DATASPACE_TRANSFER_HLG:
78             return 1000.0f / 203.0f;
79         default:
80             return 1.0f;
81     }
82 }
83 
84 } // namespace android
85