blob: 2683ad8be11aca11b38b3a409bd1ecaf22140224 [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
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 Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Sean Paul6a55e9f2015-04-30 15:31:06 -040018
Sean Paul6a55e9f2015-04-30 15:31:06 -040019#include <xf86drmMode.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030020
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020021#include <cstdint>
Roman Stratiienko0f679aa2021-09-29 12:59:48 +030022#include <map>
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020023#include <optional>
Sean Paulf72cccd2018-08-27 13:59:08 -040024#include <string>
Sean Paul6a55e9f2015-04-30 15:31:06 -040025#include <vector>
26
27namespace android {
28
Sean Paul6a55e9f2015-04-30 15:31:06 -040029class DrmProperty {
30 public:
Zach Reiznerff30b522015-10-28 19:08:45 -070031 DrmProperty() = default;
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030032 DrmProperty(uint32_t obj_id, drmModePropertyPtr p, uint64_t value);
Zach Reiznerff30b522015-10-28 19:08:45 -070033 DrmProperty(const DrmProperty &) = delete;
34 DrmProperty &operator=(const DrmProperty &) = delete;
Sean Paul6a55e9f2015-04-30 15:31:06 -040035
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030036 auto Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) -> void;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020037 std::tuple<uint64_t, int> GetEnumValueWithName(const std::string &name) const;
Sean Paul6a55e9f2015-04-30 15:31:06 -040038
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020039 auto GetId() const {
40 return id_;
41 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040042
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020043 auto GetName() const {
44 return name_;
45 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040046
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020047 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 Gheorghe2ed463c2019-01-08 19:21:08 +020059
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030060 [[nodiscard]] auto AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
61 -> bool;
62
Roman Stratiienko0f679aa2021-09-29 12:59:48 +030063 template <class E>
64 auto AddEnumToMap(const std::string &name, E key, std::map<E, uint64_t> &map)
65 -> bool;
66
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060067 template <class E>
68 auto AddEnumToMapReverse(const std::string &name, E value,
69 std::map<uint64_t, E> &map) -> bool;
70
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020071 explicit operator bool() const {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030072 return id_ != 0;
73 }
74
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060075 auto GetEnumNameFromValue(uint64_t value) const -> std::optional<std::string>;
76
Sean Paul6a55e9f2015-04-30 15:31:06 -040077 private:
78 class DrmPropertyEnum {
79 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020080 explicit DrmPropertyEnum(drm_mode_property_enum *e);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020081 ~DrmPropertyEnum() = default;
Sean Paul6a55e9f2015-04-30 15:31:06 -040082
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020083 uint64_t value;
84 std::string name;
Sean Paul6a55e9f2015-04-30 15:31:06 -040085 };
86
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030087 uint32_t obj_id_ = 0;
Zach Reiznerff30b522015-10-28 19:08:45 -070088 uint32_t id_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040089
Zach Reiznerff30b522015-10-28 19:08:45 -070090 uint32_t flags_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 std::string name_;
Zach Reiznerff30b522015-10-28 19:08:45 -070092 uint64_t value_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040093
94 std::vector<uint64_t> values_;
95 std::vector<DrmPropertyEnum> enums_;
96 std::vector<uint32_t> blob_ids_;
97};
Roman Stratiienko0f679aa2021-09-29 12:59:48 +030098
99template <class E>
100auto 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
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600113template <class E>
114auto 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
Sean Paulf72cccd2018-08-27 13:59:08 -0400127} // namespace android