xref: /aosp_15_r20/external/drm_hwcomposer/drm/DrmProperty.h (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 2015 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 <xf86drmMode.h>
20 
21 #include <cstdint>
22 #include <map>
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
27 namespace android {
28 
29 class DrmProperty {
30  public:
31   DrmProperty() = default;
32   DrmProperty(uint32_t obj_id, drmModePropertyPtr p, uint64_t value);
33   DrmProperty(const DrmProperty &) = delete;
34   DrmProperty &operator=(const DrmProperty &) = delete;
35 
36   auto Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) -> void;
37   std::tuple<uint64_t, int> GetEnumValueWithName(const std::string &name) const;
38 
GetId()39   auto GetId() const {
40     return id_;
41   }
42 
GetName()43   auto GetName() const {
44     return name_;
45   }
46 
47   auto GetValue() const -> std::optional<uint64_t>;
48 
IsImmutable()49   bool IsImmutable() const {
50     return id_ != 0 && (flags_ & DRM_MODE_PROP_IMMUTABLE) != 0;
51   }
52 
IsRange()53   bool IsRange() const {
54     return id_ != 0 && (flags_ & DRM_MODE_PROP_RANGE) != 0;
55   }
56 
57   auto RangeMin() const -> std::tuple<int, uint64_t>;
58   auto RangeMax() const -> std::tuple<int, uint64_t>;
59 
60   [[nodiscard]] auto AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
61       -> bool;
62 
63   template <class E>
64   auto AddEnumToMap(const std::string &name, E key, std::map<E, uint64_t> &map)
65       -> bool;
66 
67   template <class E>
68   auto AddEnumToMapReverse(const std::string &name, E value,
69                            std::map<uint64_t, E> &map) -> bool;
70 
71   explicit operator bool() const {
72     return id_ != 0;
73   }
74 
75   auto GetEnumNameFromValue(uint64_t value) const -> std::optional<std::string>;
76 
77  private:
78   class DrmPropertyEnum {
79    public:
80     explicit DrmPropertyEnum(drm_mode_property_enum *e);
81     ~DrmPropertyEnum() = default;
82 
83     uint64_t value;
84     std::string name;
85   };
86 
87   uint32_t obj_id_ = 0;
88   uint32_t id_ = 0;
89 
90   uint32_t flags_ = 0;
91   std::string name_;
92   uint64_t value_ = 0;
93 
94   std::vector<uint64_t> values_;
95   std::vector<DrmPropertyEnum> enums_;
96   std::vector<uint32_t> blob_ids_;
97 };
98 
99 template <class E>
100 auto DrmProperty::AddEnumToMap(const std::string &name, E key,
101                                std::map<E, uint64_t> &map) -> bool {
102   uint64_t enum_value = UINT64_MAX;
103   int err = 0;
104   std::tie(enum_value, err) = GetEnumValueWithName(name);
105   if (err == 0) {
106     map[key] = enum_value;
107     return true;
108   }
109 
110   return false;
111 }
112 
113 template <class E>
114 auto DrmProperty::AddEnumToMapReverse(const std::string &name, E value,
115                                       std::map<uint64_t, E> &map) -> bool {
116   uint64_t enum_value = UINT64_MAX;
117   int err = 0;
118   std::tie(enum_value, err) = GetEnumValueWithName(name);
119   if (err == 0) {
120     map[enum_value] = value;
121     return true;
122   }
123 
124   return false;
125 }
126 
127 }  // namespace android
128