blob: 24a67bcb784a4cb8cd444f87361bcf56eab2c21c [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 Stratiienko7fd8f882021-09-29 12:46:54 +030017// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Sean Paul468a7542024-07-16 19:50:58 +000018#define LOG_TAG "drmhwc"
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030019
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmProperty.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040021
Roman Stratiienko13cc3662020-08-29 21:35:39 +030022#include <xf86drmMode.h>
23
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020024#include <cerrno>
Tim Van Patten2c366512024-10-28 13:49:24 -060025#include <cinttypes>
Roman Stratiienkod518a052021-02-25 19:15:14 +020026#include <cstdint>
Sean Paul6a55e9f2015-04-30 15:31:06 -040027#include <string>
28
Roman Stratiienko13cc3662020-08-29 21:35:39 +030029#include "DrmDevice.h"
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030030#include "utils/log.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040031
32namespace android {
33
34DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020035 : value(e->value), name(e->name) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040036}
37
Andrew Wolferse778d032024-12-12 17:32:33 +000038DrmProperty::DrmProperty(const SharedFd &fd, uint32_t obj_id,
39 drmModePropertyPtr p, uint64_t value) {
40 Init(fd, obj_id, p, value);
Sean Paul6a55e9f2015-04-30 15:31:06 -040041}
42
Andrew Wolferse778d032024-12-12 17:32:33 +000043void DrmProperty::Init(const SharedFd &fd, uint32_t obj_id,
44 drmModePropertyPtr p, uint64_t value) {
45 fd_ = fd;
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030046 obj_id_ = obj_id;
Sean Paul6a55e9f2015-04-30 15:31:06 -040047 id_ = p->prop_id;
48 flags_ = p->flags;
49 name_ = p->name;
50 value_ = value;
51
52 for (int i = 0; i < p->count_values; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020053 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020054 values_.emplace_back(p->values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040055
56 for (int i = 0; i < p->count_enums; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020057 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030058 enums_.emplace_back(&p->enums[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040059
60 for (int i = 0; i < p->count_blobs; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020061 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020062 blob_ids_.emplace_back(p->blob_ids[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040063}
64
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020065std::optional<uint64_t> DrmProperty::GetValue() const {
66 if ((flags_ & DRM_MODE_PROP_BLOB) != 0)
67 return value_;
Sean Paul85c58c62015-07-08 10:43:54 -040068
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020069 if (values_.empty())
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020070 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040071
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020072 if ((flags_ & DRM_MODE_PROP_RANGE) != 0)
73 return value_;
Sean Paul6a55e9f2015-04-30 15:31:06 -040074
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020075 if ((flags_ & DRM_MODE_PROP_ENUM) != 0) {
76 if (value_ >= enums_.size())
77 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040078
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020079 return enums_[value_].value;
Sean Paul6a55e9f2015-04-30 15:31:06 -040080 }
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020081
82 if ((flags_ & DRM_MODE_PROP_OBJECT) != 0)
83 return value_;
84
85 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040086}
Lowry Li9b6cafd2018-08-28 17:58:21 +080087
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020088std::tuple<int, uint64_t> DrmProperty::RangeMin() const {
89 if (!IsRange())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020090 return std::make_tuple(-EINVAL, 0);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020091 if (values_.empty())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020092 return std::make_tuple(-ENOENT, 0);
93
94 return std::make_tuple(0, values_[0]);
95}
96
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020097std::tuple<int, uint64_t> DrmProperty::RangeMax() const {
98 if (!IsRange())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020099 return std::make_tuple(-EINVAL, 0);
100 if (values_.size() < 2)
101 return std::make_tuple(-ENOENT, 0);
102
103 return std::make_tuple(0, values_[1]);
104}
105
Lowry Li9b6cafd2018-08-28 17:58:21 +0800106std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200107 const std::string &name) const {
108 for (const auto &it : enums_) {
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200109 if (it.name == name) {
110 return std::make_tuple(it.value, 0);
Lowry Li9b6cafd2018-08-28 17:58:21 +0800111 }
112 }
113
114 return std::make_tuple(UINT64_MAX, -EINVAL);
115}
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300116
117auto DrmProperty::AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
118 -> bool {
119 if (id_ == 0) {
120 ALOGE("AtomicSet() is called on non-initialized property!");
121 return false;
122 }
123 if (drmModeAtomicAddProperty(&pset, obj_id_, id_, value) < 0) {
124 ALOGE("Failed to add obj_id: %u, prop_id: %u (%s) to pset", obj_id_, id_,
125 name_.c_str());
126 return false;
127 }
128 return true;
129}
130
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600131std::optional<std::string> DrmProperty::GetEnumNameFromValue(
132 uint64_t value) const {
133 if (enums_.empty()) {
134 ALOGE("No enum values for property: %s", name_.c_str());
135 return {};
136 }
137
138 for (const auto &it : enums_) {
139 if (it.value == value) {
140 return it.name;
141 }
142 }
143
Tim Van Patten2c366512024-10-28 13:49:24 -0600144 ALOGE("Property '%s' has no matching enum for value: %" PRIu64, name_.c_str(),
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600145 value);
146 return {};
147}
148
Roman Stratiienkoda2fcf62025-01-23 17:47:03 +0200149auto DrmProperty::GetEnumMask(uint64_t &mask) -> bool {
150 if (enums_.empty()) {
151 ALOGE("No enum values for property: %s", name_.c_str());
152 return false;
153 }
154
Drew Davenport5b583562025-01-29 09:04:22 -0700155 if (!IsBitmask()) {
156 ALOGE("Property %s is not a bitmask property.", name_.c_str());
157 return false;
158 }
159
Roman Stratiienkoda2fcf62025-01-23 17:47:03 +0200160 mask = 0;
161
162 for (const auto &it : enums_) {
Drew Davenport5b583562025-01-29 09:04:22 -0700163 mask |= (1 << it.value);
Roman Stratiienkoda2fcf62025-01-23 17:47:03 +0200164 }
165
166 return true;
167}
168
Sean Paulf72cccd2018-08-27 13:59:08 -0400169} // namespace android