Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 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 | |
Roman Stratiienko | bde9566 | 2022-12-10 20:27:58 +0200 | [diff] [blame] | 17 | #pragma once |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 18 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 19 | #include <xf86drmMode.h> |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 20 | |
Roman Stratiienko | e78235c | 2021-12-23 17:36:12 +0200 | [diff] [blame] | 21 | #include <cstdint> |
Roman Stratiienko | 0f679aa | 2021-09-29 12:59:48 +0300 | [diff] [blame] | 22 | #include <map> |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 23 | #include <optional> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 24 | #include <string> |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Andrew Wolfers | e778d03 | 2024-12-12 17:32:33 +0000 | [diff] [blame^] | 27 | #include "drm/DrmUnique.h" |
| 28 | #include "utils/fd.h" |
| 29 | #include "utils/log.h" |
| 30 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 33 | class DrmProperty { |
| 34 | public: |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 35 | DrmProperty() = default; |
Andrew Wolfers | e778d03 | 2024-12-12 17:32:33 +0000 | [diff] [blame^] | 36 | DrmProperty(const SharedFd &fd, uint32_t obj_id, drmModePropertyPtr p, |
| 37 | uint64_t value); |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 38 | DrmProperty(const DrmProperty &) = delete; |
| 39 | DrmProperty &operator=(const DrmProperty &) = delete; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 40 | |
Andrew Wolfers | e778d03 | 2024-12-12 17:32:33 +0000 | [diff] [blame^] | 41 | auto Init(const SharedFd &fd, uint32_t obj_id, drmModePropertyPtr p, |
| 42 | uint64_t value) -> void; |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 43 | std::tuple<uint64_t, int> GetEnumValueWithName(const std::string &name) const; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 44 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 45 | auto GetId() const { |
| 46 | return id_; |
| 47 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 48 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 49 | auto GetName() const { |
| 50 | return name_; |
| 51 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 52 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 53 | auto GetValue() const -> std::optional<uint64_t>; |
| 54 | |
| 55 | bool IsImmutable() const { |
| 56 | return id_ != 0 && (flags_ & DRM_MODE_PROP_IMMUTABLE) != 0; |
| 57 | } |
| 58 | |
| 59 | bool IsRange() const { |
| 60 | return id_ != 0 && (flags_ & DRM_MODE_PROP_RANGE) != 0; |
| 61 | } |
| 62 | |
Drew Davenport | 5b58356 | 2025-01-29 09:04:22 -0700 | [diff] [blame] | 63 | bool IsBitmask() const { |
| 64 | return id_ != 0 && (flags_ & DRM_MODE_PROP_BITMASK) != 0; |
| 65 | } |
| 66 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 67 | auto RangeMin() const -> std::tuple<int, uint64_t>; |
| 68 | auto RangeMax() const -> std::tuple<int, uint64_t>; |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 69 | |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 70 | [[nodiscard]] auto AtomicSet(drmModeAtomicReq &pset, uint64_t value) const |
| 71 | -> bool; |
| 72 | |
Roman Stratiienko | 0f679aa | 2021-09-29 12:59:48 +0300 | [diff] [blame] | 73 | template <class E> |
| 74 | auto AddEnumToMap(const std::string &name, E key, std::map<E, uint64_t> &map) |
| 75 | -> bool; |
| 76 | |
Tim Van Patten | a2f3efa | 2024-10-15 17:44:54 -0600 | [diff] [blame] | 77 | template <class E> |
| 78 | auto AddEnumToMapReverse(const std::string &name, E value, |
| 79 | std::map<uint64_t, E> &map) -> bool; |
| 80 | |
Roman Stratiienko | da2fcf6 | 2025-01-23 17:47:03 +0200 | [diff] [blame] | 81 | auto GetEnumMask(uint64_t &mask) -> bool; |
| 82 | |
Roman Stratiienko | e78235c | 2021-12-23 17:36:12 +0200 | [diff] [blame] | 83 | explicit operator bool() const { |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 84 | return id_ != 0; |
| 85 | } |
| 86 | |
Tim Van Patten | a2f3efa | 2024-10-15 17:44:54 -0600 | [diff] [blame] | 87 | auto GetEnumNameFromValue(uint64_t value) const -> std::optional<std::string>; |
| 88 | |
Andrew Wolfers | e778d03 | 2024-12-12 17:32:33 +0000 | [diff] [blame^] | 89 | bool IsBlob() const { |
| 90 | return id_ != 0 && (flags_ & DRM_MODE_PROP_BLOB) != 0; |
| 91 | } |
| 92 | template <typename T> |
| 93 | bool GetBlobData(std::vector<T> &data_out) const; |
| 94 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 95 | private: |
| 96 | class DrmPropertyEnum { |
| 97 | public: |
Roman Stratiienko | e78235c | 2021-12-23 17:36:12 +0200 | [diff] [blame] | 98 | explicit DrmPropertyEnum(drm_mode_property_enum *e); |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 99 | ~DrmPropertyEnum() = default; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 100 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 101 | uint64_t value; |
| 102 | std::string name; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 103 | }; |
| 104 | |
Andrew Wolfers | e778d03 | 2024-12-12 17:32:33 +0000 | [diff] [blame^] | 105 | SharedFd fd_ = nullptr; |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 106 | uint32_t obj_id_ = 0; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 107 | uint32_t id_ = 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 108 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 109 | uint32_t flags_ = 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 110 | std::string name_; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 111 | uint64_t value_ = 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 112 | |
| 113 | std::vector<uint64_t> values_; |
| 114 | std::vector<DrmPropertyEnum> enums_; |
| 115 | std::vector<uint32_t> blob_ids_; |
| 116 | }; |
Roman Stratiienko | 0f679aa | 2021-09-29 12:59:48 +0300 | [diff] [blame] | 117 | |
| 118 | template <class E> |
| 119 | auto DrmProperty::AddEnumToMap(const std::string &name, E key, |
| 120 | std::map<E, uint64_t> &map) -> bool { |
| 121 | uint64_t enum_value = UINT64_MAX; |
| 122 | int err = 0; |
| 123 | std::tie(enum_value, err) = GetEnumValueWithName(name); |
| 124 | if (err == 0) { |
| 125 | map[key] = enum_value; |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
Tim Van Patten | a2f3efa | 2024-10-15 17:44:54 -0600 | [diff] [blame] | 132 | template <class E> |
| 133 | auto DrmProperty::AddEnumToMapReverse(const std::string &name, E value, |
| 134 | std::map<uint64_t, E> &map) -> bool { |
| 135 | uint64_t enum_value = UINT64_MAX; |
| 136 | int err = 0; |
| 137 | std::tie(enum_value, err) = GetEnumValueWithName(name); |
| 138 | if (err == 0) { |
| 139 | map[enum_value] = value; |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
Andrew Wolfers | e778d03 | 2024-12-12 17:32:33 +0000 | [diff] [blame^] | 146 | template <typename T> |
| 147 | bool DrmProperty::GetBlobData(std::vector<T> &data_out) const { |
| 148 | auto value = GetValue(); |
| 149 | if (!fd_) { |
| 150 | ALOGE("Could not read blob data from property %s: No fd", name_.c_str()); |
| 151 | return false; |
| 152 | } |
| 153 | if (!IsBlob()) { |
| 154 | ALOGE("Property %s is not blob type", name_.c_str()); |
| 155 | return false; |
| 156 | } |
| 157 | if (!value.has_value()) { |
| 158 | ALOGE("Could not read blob data from property %s: No blob id", |
| 159 | name_.c_str()); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | auto blob = MakeDrmModePropertyBlobUnique(*fd_, value.value()); |
| 164 | if (blob == nullptr) { |
| 165 | ALOGE("Failed to read blob with id=%d from property %s", value.value(), |
| 166 | name_.c_str()); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | if (blob->length % sizeof(T) != 0) { |
| 171 | ALOGE( |
| 172 | "Property %s blob size of %zu bytes is not divisible by type argument " |
| 173 | "size of %zu bytes", |
| 174 | name_.c_str(), blob->length, sizeof(T)); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | auto cast_data = static_cast<T *>(blob->data); |
| 179 | size_t cast_data_length = blob->length / sizeof(T); |
| 180 | data_out.assign(cast_data, cast_data + cast_data_length); |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 185 | } // namespace android |