1 /*
2  * Copyright (C) 2020 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 #ifndef DISPLAYCOLOR_H_
18 #define DISPLAYCOLOR_H_
19 
20 #include <android/hardware/graphics/common/1.1/types.h>
21 #include <android/hardware/graphics/common/1.2/types.h>
22 
23 #include <string>
24 
25 namespace displaycolor {
26 
27 namespace hwc {
28 using android::hardware::graphics::common::V1_1::RenderIntent;
29 using android::hardware::graphics::common::V1_2::ColorMode;
30 using android::hardware::graphics::common::V1_2::Dataspace;
31 }  // namespace hwc
32 
33 /**
34  * hwc/displaycolor interface history
35  *
36  * 3.0.0.2021-11-18 calibration info intf
37  * 2.0.0.2021-08-27 pass brightness table for hdr10+
38  * 1.0.0.2021-08-25 Initial release
39  */
40 
41 constexpr struct DisplayColorIntfVer {
42     uint16_t major; // increase it for new functionalities
43     uint16_t minor; // for bug fix and cause binary incompatible
44     uint16_t patch; // for bug fix and binary compatible
45 
46     bool operator==(const DisplayColorIntfVer &rhs) const {
47         return major == rhs.major &&
48             minor == rhs.minor &&
49             patch == rhs.patch;
50     }
51 
CompatibleDisplayColorIntfVer52     bool Compatible(const DisplayColorIntfVer &rhs) const {
53         return major == rhs.major &&
54             minor == rhs.minor;
55     }
56 
57 } kInterfaceVersion {
58     3,
59     0,
60     0,
61 };
62 
63 /// A map associating supported RenderIntents for each supported ColorMode
64 using ColorModesMap = std::map<hwc::ColorMode, std::vector<hwc::RenderIntent>>;
65 
66 /// Image data bit depths.
67 enum class BitDepth { kEight, kTen };
68 
69 /// Display type used to get pipeline or update display scene.
70 enum DisplayType {
71     /// builtin primary display
72     DISPLAY_PRIMARY = 0,
73     /// builtin secondary display
74     DISPLAY_SECONDARY = 1,
75     /// number of display
76     DISPLAY_MAX = 2,
77 };
78 
79 enum BrightnessMode {
80     BM_NOMINAL = 0,
81     BM_HBM = 1,
82     BM_MAX = 2,
83 };
84 
85 struct DisplayBrightnessTable {
86     float nbm_nits_min;
87     float nbm_nits_max;
88     float hbm_nits_min;
89     float hbm_nits_max;
90 
91     uint32_t nbm_dbv_min;
92     uint32_t nbm_dbv_max;
93     uint32_t hbm_dbv_min;
94     uint32_t hbm_dbv_max;
95 };
96 
97 struct DisplayInfo {
98     std::string panel_name;
99     std::string panel_serial;
100 
101     DisplayBrightnessTable brightness_table;
102 };
103 
104 struct LayerColorData {
105     bool operator==(const LayerColorData &rhs) const {
106         return dataspace == rhs.dataspace && matrix == rhs.matrix &&
107                static_metadata == rhs.static_metadata &&
108                dynamic_metadata == rhs.dynamic_metadata;
109     }
110 
111     /**
112      * @brief HDR static metadata.
113      *
114      * See HWC v2.2 (IComposerClient::PerFrameMetadataKey)
115      * for more information.
116      */
117     struct HdrStaticMetadata {
118        private:
119         std::array<int32_t, 13> data;
120 
121        public:
122         HdrStaticMetadata() = default;
HdrStaticMetadataLayerColorData::HdrStaticMetadata123         HdrStaticMetadata(const HdrStaticMetadata &other)
124             : data(other.data), is_valid(other.is_valid) {}
125         HdrStaticMetadata(const HdrStaticMetadata &&other) = delete;
126         HdrStaticMetadata &operator=(const HdrStaticMetadata &other) {
127             data = other.data;
128             is_valid = other.is_valid;
129             return *this;
130         }
131         HdrStaticMetadata &operator=(HdrStaticMetadata &&other) = delete;
132         ~HdrStaticMetadata() = default;
133 
134         bool operator==(const HdrStaticMetadata &rhs) const {
135             return data == rhs.data && is_valid == rhs.is_valid;
136         }
137 
138         /// Indicator for whether the data in this struct should be used.
139         bool is_valid = false;
140         /// This device's display's peak luminance, in nits.
141         int32_t &device_max_luminance = data[0];
142 
143         // Mastering display properties
144         int32_t &display_red_primary_x = data[1];
145         int32_t &display_red_primary_y = data[2];
146         int32_t &display_green_primary_x = data[3];
147         int32_t &display_green_primary_y = data[4];
148         int32_t &display_blue_primary_x = data[5];
149         int32_t &display_blue_primary_y = data[6];
150         int32_t &white_point_x = data[7];
151         int32_t &white_point_y = data[8];
152         int32_t &max_luminance = data[9];
153         int32_t &min_luminance = data[10];
154 
155         // Content properties
156         int32_t &max_content_light_level = data[11];
157         int32_t &max_frame_average_light_level = data[12];
158     };
159 
160     /**
161      * @brief HDR dynamic metadata.
162      *
163      * The members defined here are a subset of metadata define in
164      * SMPTE ST 2094-40:2016.
165      * Also see module videoapi information.
166      */
167     struct HdrDynamicMetadata {
168         bool operator==(const HdrDynamicMetadata &rhs) const {
169             return is_valid == rhs.is_valid &&
170                    display_maximum_luminance == rhs.display_maximum_luminance &&
171                    maxscl == rhs.maxscl &&
172                    maxrgb_percentages == rhs.maxrgb_percentages &&
173                    maxrgb_percentiles == rhs.maxrgb_percentiles &&
174                    tm_flag == rhs.tm_flag && tm_knee_x == rhs.tm_knee_x &&
175                    tm_knee_y == rhs.tm_knee_y &&
176                    bezier_curve_anchors == rhs.bezier_curve_anchors;
177         }
178 
179         /// Indicator for whether the data in this struct should be used.
180         bool is_valid = false;
181 
182         uint32_t display_maximum_luminance;
183         std::array<uint32_t, 3> maxscl;
184         std::vector<uint8_t> maxrgb_percentages;
185         std::vector<uint32_t> maxrgb_percentiles;
186         uint16_t tm_flag;
187         uint16_t tm_knee_x;
188         uint16_t tm_knee_y;
189         std::vector<uint16_t> bezier_curve_anchors;
190     };
191 
192     /// This layer's dataspace (color gamut, transfer function, and range).
193     hwc::Dataspace dataspace = hwc::Dataspace::UNKNOWN;
194     /// Color transform for this layer. See SET_LAYER_COLOR_TRANSFORM HWC v2.3.
195     // clang-format off
196     std::array<float, 16> matrix {
197         1.0, 0.0, 0.0, 0.0,
198         0.0, 1.0, 0.0, 0.0,
199         0.0, 0.0, 1.0, 0.0,
200         0.0, 0.0, 0.0, 1.0
201     };
202     // clang-format on
203     /**
204      * @brief This layer's HDR static metadata. Only applicable when dataspace
205      * indicates this is an HDR layer.
206      */
207     HdrStaticMetadata static_metadata;
208     /**
209      * @brief This layer's HDR dynamic metadata. Only applicable when dataspace
210      * indicates this is an HDR layer.
211      */
212     HdrDynamicMetadata dynamic_metadata;
213 };
214 
215 /**
216  * @brief DisplayScene holds all the information required for libdisplaycolor to
217  * return correct data.
218  */
219 struct DisplayScene {
220     bool operator==(const DisplayScene &rhs) const {
221         return layer_data == rhs.layer_data &&
222                dpu_bit_depth == rhs.dpu_bit_depth &&
223                color_mode == rhs.color_mode &&
224                render_intent == rhs.render_intent &&
225                matrix == rhs.matrix &&
226                force_hdr == rhs.force_hdr &&
227                bm == rhs.bm &&
228                lhbm_on == rhs.lhbm_on &&
229                (lhbm_on && dbv == rhs.dbv) &&
230                refresh_rate == rhs.refresh_rate &&
231                hdr_full_screen == rhs.hdr_full_screen;
232     }
233 
234     /// A vector of layer color data.
235     std::vector<LayerColorData> layer_data;
236     /// The bit depth the DPU is currently outputting
237     BitDepth dpu_bit_depth = BitDepth::kTen;
238     /// The current ColorMode (typically set by SurfaceFlinger)
239     hwc::ColorMode color_mode = hwc::ColorMode::NATIVE;
240     /// The current RenderIntent (typically set by SurfaceFlinger)
241     hwc::RenderIntent render_intent = hwc::RenderIntent::COLORIMETRIC;
242     /// Color transform for this layer. See SET_COLOR_TRANSFORM HWC v2.1.
243     // clang-format off
244     std::array<float, 16> matrix {
245         1.0, 0.0, 0.0, 0.0,
246         0.0, 1.0, 0.0, 0.0,
247         0.0, 0.0, 1.0, 0.0,
248         0.0, 0.0, 0.0, 1.0
249     };
250     // clang-format on
251     /// When this bit is set, process hdr layers and the layer matrix even if
252     //it's in native color mode.
253     bool force_hdr;
254 
255     /// display brightness mode
256     BrightnessMode bm;
257 
258     /// dbv level
259     uint32_t dbv;
260 
261     /// lhbm status
262     bool lhbm_on;
263 
264     /// refresh rate
265     float refresh_rate;
266 
267     /// hdr full screen mode
268     bool hdr_full_screen;
269 };
270 
271 struct CalibrationInfo {
272     bool factory_cal_loaded;
273     bool golden_cal_loaded;
274     bool common_cal_loaded;
275     bool dev_cal_loaded;
276 };
277 
278 /// An interface specifying functions that are HW-agnostic.
279 class IDisplayColorGeneric {
280    public:
281     /// A generic stage in the display pipeline.
282     template <typename T>
283     struct DisplayStage {
284         using ConfigType = T;
285 
286         std::function<void(void)> data_applied_notifier = nullptr;
NotifyDataAppliedDisplayStage287         void NotifyDataApplied() const {
288             if (data_applied_notifier) {
289                 data_applied_notifier();
290             }
291         }
292 
293         bool enable = false;
294         /// A flag indicating if the data has been changed in last Update call.
295         // It should be set when enable is changed from false to true.
296         bool dirty = false;
297 
298         const ConfigType *config = nullptr;
299     };
300 
301     /// Interface for accessing data for panel
302     class IPanel {
303       public:
304         /// Get the adjusted dbv for panel.
305         virtual uint32_t GetAdjustedBrightnessLevel() const = 0;
306 
~IPanel()307         virtual ~IPanel() {}
308     };
309 
~IDisplayColorGeneric()310     virtual ~IDisplayColorGeneric() {}
311 
312     /**
313      * @brief Update display color data. This function is expected to be called
314      * in the context of HWC::validateDisplay, if the display scene has changed.
315      *
316      * @param display The display relating to the scene.
317      * @param scene Display scene data to use during the update.
318      * @return OK if successful, error otherwise.
319      */
320     virtual int Update(DisplayType display, const DisplayScene &scene) = 0;
321 
322     /**
323      * @brief Update display color data. This function is expected to be called
324      * in the context of HWC::presentDisplay, if the display scene has changed
325      * since the Update call for HWC::validateDisplay.
326      *
327      * @param display The display relating to the scene.
328      * @param scene Display scene data to use during the update.
329      * @return OK if successful, error otherwise.
330      */
331     virtual int UpdatePresent(DisplayType display, const DisplayScene &scene) = 0;
332 
333     /**
334      * @brief Check if refresh rate regamma compensation is enabled.
335      *
336      * @return true for yes.
337      */
338     virtual bool IsRrCompensationEnabled(DisplayType display) = 0;
339 
340     /**
341      * @brief Get calibration information for each profiles.
342      * @param display The display to get the calibration information.
343      */
344     virtual const CalibrationInfo &GetCalibrationInfo(
345         DisplayType display) const = 0;
346 
347     /**
348      * @brief Get a map of supported ColorModes, and supported RenderIntents for
349      * each ColorMode.
350      * @param display The display to get the color modes and render intents.
351      */
352     virtual const ColorModesMap &ColorModesAndRenderIntents(
353         DisplayType display) const = 0;
354 };
355 
356 extern "C" {
357     const DisplayColorIntfVer *GetInterfaceVersion();
358 }
359 
360 }  // namespace displaycolor
361 
362 #endif  // DISPLAYCOLOR_H_
363