blob: 516518b929f5705623bcd67373891f80ce16091e [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
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020067 explicit operator bool() const {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030068 return id_ != 0;
69 }
70
Sean Paul6a55e9f2015-04-30 15:31:06 -040071 private:
72 class DrmPropertyEnum {
73 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020074 explicit DrmPropertyEnum(drm_mode_property_enum *e);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020075 ~DrmPropertyEnum() = default;
Sean Paul6a55e9f2015-04-30 15:31:06 -040076
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020077 uint64_t value;
78 std::string name;
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 };
80
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030081 uint32_t obj_id_ = 0;
Zach Reiznerff30b522015-10-28 19:08:45 -070082 uint32_t id_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040083
Zach Reiznerff30b522015-10-28 19:08:45 -070084 uint32_t flags_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040085 std::string name_;
Zach Reiznerff30b522015-10-28 19:08:45 -070086 uint64_t value_ = 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040087
88 std::vector<uint64_t> values_;
89 std::vector<DrmPropertyEnum> enums_;
90 std::vector<uint32_t> blob_ids_;
91};
Roman Stratiienko0f679aa2021-09-29 12:59:48 +030092
93template <class E>
94auto 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 Paulf72cccd2018-08-27 13:59:08 -0400107} // namespace android