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 } // namespace android