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