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 | |
| 27 | namespace android { |
| 28 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 29 | class DrmProperty { |
| 30 | public: |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 31 | DrmProperty() = default; |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 32 | DrmProperty(uint32_t obj_id, drmModePropertyPtr p, uint64_t value); |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 33 | DrmProperty(const DrmProperty &) = delete; |
| 34 | DrmProperty &operator=(const DrmProperty &) = delete; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 35 | |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 36 | auto Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) -> void; |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 37 | std::tuple<uint64_t, int> GetEnumValueWithName(const std::string &name) const; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 38 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 39 | auto GetId() const { |
| 40 | return id_; |
| 41 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 42 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 43 | auto GetName() const { |
| 44 | return name_; |
| 45 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 46 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 47 | auto GetValue() const -> std::optional<uint64_t>; |
| 48 | |
| 49 | bool IsImmutable() const { |
| 50 | return id_ != 0 && (flags_ & DRM_MODE_PROP_IMMUTABLE) != 0; |
| 51 | } |
| 52 | |
| 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>; |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 59 | |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 60 | [[nodiscard]] auto AtomicSet(drmModeAtomicReq &pset, uint64_t value) const |
| 61 | -> bool; |
| 62 | |
Roman Stratiienko | 0f679aa | 2021-09-29 12:59:48 +0300 | [diff] [blame] | 63 | template <class E> |
| 64 | auto AddEnumToMap(const std::string &name, E key, std::map<E, uint64_t> &map) |
| 65 | -> bool; |
| 66 | |
Roman Stratiienko | e78235c | 2021-12-23 17:36:12 +0200 | [diff] [blame] | 67 | explicit operator bool() const { |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 68 | return id_ != 0; |
| 69 | } |
| 70 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 71 | private: |
| 72 | class DrmPropertyEnum { |
| 73 | public: |
Roman Stratiienko | e78235c | 2021-12-23 17:36:12 +0200 | [diff] [blame] | 74 | explicit DrmPropertyEnum(drm_mode_property_enum *e); |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 75 | ~DrmPropertyEnum() = default; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 76 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 77 | uint64_t value; |
| 78 | std::string name; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 81 | uint32_t obj_id_ = 0; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 82 | uint32_t id_ = 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 83 | |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 84 | uint32_t flags_ = 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 85 | std::string name_; |
Zach Reizner | ff30b52 | 2015-10-28 19:08:45 -0700 | [diff] [blame] | 86 | uint64_t value_ = 0; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 87 | |
| 88 | std::vector<uint64_t> values_; |
| 89 | std::vector<DrmPropertyEnum> enums_; |
| 90 | std::vector<uint32_t> blob_ids_; |
| 91 | }; |
Roman Stratiienko | 0f679aa | 2021-09-29 12:59:48 +0300 | [diff] [blame] | 92 | |
| 93 | template <class E> |
| 94 | auto DrmProperty::AddEnumToMap(const std::string &name, E key, |
| 95 | std::map<E, uint64_t> &map) -> bool { |
| 96 | uint64_t enum_value = UINT64_MAX; |
| 97 | int err = 0; |
| 98 | std::tie(enum_value, err) = GetEnumValueWithName(name); |
| 99 | if (err == 0) { |
| 100 | map[key] = enum_value; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 107 | } // namespace android |