blob: 860f9ca407c77bfee1958a859daa4d4028d90c58 [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 Stratiienkoda2fcf62025-01-23 17:47:03 +020071 auto GetEnumMask(uint64_t &mask) -> bool;
72
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020073 explicit operator bool() const {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030074 return id_ != 0;
75 }
76
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060077 auto GetEnumNameFromValue(uint64_t value) const -> std::optional<std::string>;
78
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 private:
80 class DrmPropertyEnum {
81 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020082 explicit DrmPropertyEnum(drm_mode_property_enum *e);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020083 ~DrmPropertyEnum() = default;
Sean Paul6a55e9f2015-04-30 15:31:06 -040084
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020085 uint64_t value;
86 std::string name;
Sean Paul6a55e9f2015-04-30 15:31:06 -040087 };
88
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030089 uint32_t obj_id_ = 0;
Zach Reiznerff30b522015-10-28 19:08:45 -070090 uint32_t id_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040091
Zach Reiznerff30b522015-10-28 19:08:45 -070092 uint32_t flags_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040093 std::string name_;
Zach Reiznerff30b522015-10-28 19:08:45 -070094 uint64_t value_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040095
96 std::vector<uint64_t> values_;
97 std::vector<DrmPropertyEnum> enums_;
98 std::vector<uint32_t> blob_ids_;
99};
Roman Stratiienko0f679aa2021-09-29 12:59:48 +0300100
101template <class E>
102auto DrmProperty::AddEnumToMap(const std::string &name, E key,
103 std::map<E, uint64_t> &map) -> bool {
104 uint64_t enum_value = UINT64_MAX;
105 int err = 0;
106 std::tie(enum_value, err) = GetEnumValueWithName(name);
107 if (err == 0) {
108 map[key] = enum_value;
109 return true;
110 }
111
112 return false;
113}
114
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600115template <class E>
116auto DrmProperty::AddEnumToMapReverse(const std::string &name, E value,
117 std::map<uint64_t, E> &map) -> bool {
118 uint64_t enum_value = UINT64_MAX;
119 int err = 0;
120 std::tie(enum_value, err) = GetEnumValueWithName(name);
121 if (err == 0) {
122 map[enum_value] = value;
123 return true;
124 }
125
126 return false;
127}
128
Sean Paulf72cccd2018-08-27 13:59:08 -0400129} // namespace android