• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, bool hasHdrMetadata = false) {
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         hasHdrMetadata) {
54         return HdrRenderType::GENERIC_HDR;
55     }
56 
57     // Extended range layer with an hdr/sdr ratio of > 1.01f can "self-promote" to HDR.
58     if (range == HAL_DATASPACE_RANGE_EXTENDED && hdrSdrRatio > 1.01f) {
59         return HdrRenderType::DISPLAY_HDR;
60     }
61 
62     return HdrRenderType::SDR;
63 }
64 
65 /**
66  * Returns the maximum headroom allowed for this content under "idealized"
67  * display conditions (low surround luminance, high-enough display brightness).
68  *
69  * TODO: take into account hdr metadata, but square it with the fact that some
70  * HLG content has CTA.861-3 metadata
71  */
getIdealizedMaxHeadroom(ui::Dataspace dataspace)72 inline float getIdealizedMaxHeadroom(ui::Dataspace dataspace) {
73     const auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK;
74 
75     switch (transfer) {
76         case HAL_DATASPACE_TRANSFER_ST2084:
77             return 10000.0f / 203.0f;
78         case HAL_DATASPACE_TRANSFER_HLG:
79             return 1000.0f / 203.0f;
80         default:
81             return 1.0f;
82     }
83 }
84 
85 } // namespace android
86