blob: dbd307e82d049d8ab411877a7185bfd74f938db2 [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
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030038DrmProperty::DrmProperty(uint32_t obj_id, drmModePropertyPtr p,
39 uint64_t value) {
40 Init(obj_id, p, value);
Sean Paul6a55e9f2015-04-30 15:31:06 -040041}
42
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030043void DrmProperty::Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) {
44 obj_id_ = obj_id;
Sean Paul6a55e9f2015-04-30 15:31:06 -040045 id_ = p->prop_id;
46 flags_ = p->flags;
47 name_ = p->name;
48 value_ = value;
49
50 for (int i = 0; i < p->count_values; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020051 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020052 values_.emplace_back(p->values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040053
54 for (int i = 0; i < p->count_enums; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020055 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030056 enums_.emplace_back(&p->enums[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040057
58 for (int i = 0; i < p->count_blobs; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020059 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020060 blob_ids_.emplace_back(p->blob_ids[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040061}
62
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020063std::optional<uint64_t> DrmProperty::GetValue() const {
64 if ((flags_ & DRM_MODE_PROP_BLOB) != 0)
65 return value_;
Sean Paul85c58c62015-07-08 10:43:54 -040066
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020067 if (values_.empty())
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020068 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040069
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020070 if ((flags_ & DRM_MODE_PROP_RANGE) != 0)
71 return value_;
Sean Paul6a55e9f2015-04-30 15:31:06 -040072
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020073 if ((flags_ & DRM_MODE_PROP_ENUM) != 0) {
74 if (value_ >= enums_.size())
75 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040076
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020077 return enums_[value_].value;
Sean Paul6a55e9f2015-04-30 15:31:06 -040078 }
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020079
80 if ((flags_ & DRM_MODE_PROP_OBJECT) != 0)
81 return value_;
82
83 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040084}
Lowry Li9b6cafd2018-08-28 17:58:21 +080085
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020086std::tuple<int, uint64_t> DrmProperty::RangeMin() const {
87 if (!IsRange())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020088 return std::make_tuple(-EINVAL, 0);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020089 if (values_.empty())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020090 return std::make_tuple(-ENOENT, 0);
91
92 return std::make_tuple(0, values_[0]);
93}
94
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020095std::tuple<int, uint64_t> DrmProperty::RangeMax() const {
96 if (!IsRange())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020097 return std::make_tuple(-EINVAL, 0);
98 if (values_.size() < 2)
99 return std::make_tuple(-ENOENT, 0);
100
101 return std::make_tuple(0, values_[1]);
102}
103
Lowry Li9b6cafd2018-08-28 17:58:21 +0800104std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200105 const std::string &name) const {
106 for (const auto &it : enums_) {
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200107 if (it.name == name) {
108 return std::make_tuple(it.value, 0);
Lowry Li9b6cafd2018-08-28 17:58:21 +0800109 }
110 }
111
112 return std::make_tuple(UINT64_MAX, -EINVAL);
113}
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300114
115auto DrmProperty::AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
116 -> bool {
117 if (id_ == 0) {
118 ALOGE("AtomicSet() is called on non-initialized property!");
119 return false;
120 }
121 if (drmModeAtomicAddProperty(&pset, obj_id_, id_, value) < 0) {
122 ALOGE("Failed to add obj_id: %u, prop_id: %u (%s) to pset", obj_id_, id_,
123 name_.c_str());
124 return false;
125 }
126 return true;
127}
128
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600129std::optional<std::string> DrmProperty::GetEnumNameFromValue(
130 uint64_t value) const {
131 if (enums_.empty()) {
132 ALOGE("No enum values for property: %s", name_.c_str());
133 return {};
134 }
135
136 for (const auto &it : enums_) {
137 if (it.value == value) {
138 return it.name;
139 }
140 }
141
Tim Van Patten2c366512024-10-28 13:49:24 -0600142 ALOGE("Property '%s' has no matching enum for value: %" PRIu64, name_.c_str(),
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600143 value);
144 return {};
145}
146
Sean Paulf72cccd2018-08-27 13:59:08 -0400147} // namespace android