blob: 99bddb18b89365a104295629196b47e7b53c4401 [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
Drew Davenport5b583562025-01-29 09:04:22 -070057 bool IsBitmask() const {
58 return id_ != 0 && (flags_ & DRM_MODE_PROP_BITMASK) != 0;
59 }
60
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020061 auto RangeMin() const -> std::tuple<int, uint64_t>;
62 auto RangeMax() const -> std::tuple<int, uint64_t>;
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020063
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030064 [[nodiscard]] auto AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
65 -> bool;
66
Roman Stratiienko0f679aa2021-09-29 12:59:48 +030067 template <class E>
68 auto AddEnumToMap(const std::string &name, E key, std::map<E, uint64_t> &map)
69 -> bool;
70
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060071 template <class E>
72 auto AddEnumToMapReverse(const std::string &name, E value,
73 std::map<uint64_t, E> &map) -> bool;
74
Roman Stratiienkoda2fcf62025-01-23 17:47:03 +020075 auto GetEnumMask(uint64_t &mask) -> bool;
76
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020077 explicit operator bool() const {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030078 return id_ != 0;
79 }
80
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060081 auto GetEnumNameFromValue(uint64_t value) const -> std::optional<std::string>;
82
Sean Paul6a55e9f2015-04-30 15:31:06 -040083 private:
84 class DrmPropertyEnum {
85 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020086 explicit DrmPropertyEnum(drm_mode_property_enum *e);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020087 ~DrmPropertyEnum() = default;
Sean Paul6a55e9f2015-04-30 15:31:06 -040088
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020089 uint64_t value;
90 std::string name;
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 };
92
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030093 uint32_t obj_id_ = 0;
Zach Reiznerff30b522015-10-28 19:08:45 -070094 uint32_t id_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040095
Zach Reiznerff30b522015-10-28 19:08:45 -070096 uint32_t flags_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040097 std::string name_;
Zach Reiznerff30b522015-10-28 19:08:45 -070098 uint64_t value_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040099
100 std::vector<uint64_t> values_;
101 std::vector<DrmPropertyEnum> enums_;
102 std::vector<uint32_t> blob_ids_;
103};
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300104
105template <class E>
106auto DrmProperty::AddEnumToMap(const std::string &name, E key,
107 std::map<E, uint64_t> &map) -> bool {
108 uint64_t enum_value = UINT64_MAX;
109 int err = 0;
110 std::tie(enum_value, err) = GetEnumValueWithName(name);
111 if (err == 0) {
112 map[key] = enum_value;
113 return true;
114 }
115
116 return false;
117}
118
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600119template <class E>
120auto DrmProperty::AddEnumToMapReverse(const std::string &name, E value,
121 std::map<uint64_t, E> &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[enum_value] = value;
127 return true;
128 }
129
130 return false;
131}
132
Sean Paulf72cccd2018-08-27 13:59:08 -0400133} // namespace android