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