blob: 031918a2e0f8e573f9d3b9704b0e6499867f249e [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>
Roman Stratiienkod518a052021-02-25 19:15:14 +020025#include <cstdint>
Sean Paul6a55e9f2015-04-30 15:31:06 -040026#include <string>
27
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include "DrmDevice.h"
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030029#include "utils/log.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040030
31namespace android {
32
33DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020034 : value(e->value), name(e->name) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040035}
36
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030037DrmProperty::DrmProperty(uint32_t obj_id, drmModePropertyPtr p,
38 uint64_t value) {
39 Init(obj_id, p, value);
Sean Paul6a55e9f2015-04-30 15:31:06 -040040}
41
Roman Stratiienko7fd8f882021-09-29 12:46:54 +030042void DrmProperty::Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) {
43 obj_id_ = obj_id;
Sean Paul6a55e9f2015-04-30 15:31:06 -040044 id_ = p->prop_id;
45 flags_ = p->flags;
46 name_ = p->name;
47 value_ = value;
48
49 for (int i = 0; i < p->count_values; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020050 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020051 values_.emplace_back(p->values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040052
53 for (int i = 0; i < p->count_enums; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020054 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030055 enums_.emplace_back(&p->enums[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040056
57 for (int i = 0; i < p->count_blobs; ++i)
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020058 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020059 blob_ids_.emplace_back(p->blob_ids[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040060}
61
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020062std::optional<uint64_t> DrmProperty::GetValue() const {
63 if ((flags_ & DRM_MODE_PROP_BLOB) != 0)
64 return value_;
Sean Paul85c58c62015-07-08 10:43:54 -040065
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020066 if (values_.empty())
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020067 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040068
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020069 if ((flags_ & DRM_MODE_PROP_RANGE) != 0)
70 return value_;
Sean Paul6a55e9f2015-04-30 15:31:06 -040071
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020072 if ((flags_ & DRM_MODE_PROP_ENUM) != 0) {
73 if (value_ >= enums_.size())
74 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040075
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020076 return enums_[value_].value;
Sean Paul6a55e9f2015-04-30 15:31:06 -040077 }
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020078
79 if ((flags_ & DRM_MODE_PROP_OBJECT) != 0)
80 return value_;
81
82 return {};
Sean Paul6a55e9f2015-04-30 15:31:06 -040083}
Lowry Li9b6cafd2018-08-28 17:58:21 +080084
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020085std::tuple<int, uint64_t> DrmProperty::RangeMin() const {
86 if (!IsRange())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020087 return std::make_tuple(-EINVAL, 0);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020088 if (values_.empty())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020089 return std::make_tuple(-ENOENT, 0);
90
91 return std::make_tuple(0, values_[0]);
92}
93
Roman Stratiienkoabd8e532022-12-06 23:31:33 +020094std::tuple<int, uint64_t> DrmProperty::RangeMax() const {
95 if (!IsRange())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +020096 return std::make_tuple(-EINVAL, 0);
97 if (values_.size() < 2)
98 return std::make_tuple(-ENOENT, 0);
99
100 return std::make_tuple(0, values_[1]);
101}
102
Lowry Li9b6cafd2018-08-28 17:58:21 +0800103std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200104 const std::string &name) const {
105 for (const auto &it : enums_) {
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200106 if (it.name == name) {
107 return std::make_tuple(it.value, 0);
Lowry Li9b6cafd2018-08-28 17:58:21 +0800108 }
109 }
110
111 return std::make_tuple(UINT64_MAX, -EINVAL);
112}
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300113
114auto DrmProperty::AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
115 -> bool {
116 if (id_ == 0) {
117 ALOGE("AtomicSet() is called on non-initialized property!");
118 return false;
119 }
120 if (drmModeAtomicAddProperty(&pset, obj_id_, id_, value) < 0) {
121 ALOGE("Failed to add obj_id: %u, prop_id: %u (%s) to pset", obj_id_, id_,
122 name_.c_str());
123 return false;
124 }
125 return true;
126}
127
Sean Paulf72cccd2018-08-27 13:59:08 -0400128} // namespace android