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