blob: e0bf25e2cd4d0531cbe4e950820892e876958aa0 [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)
18#define LOG_TAG "hwc-drm-property"
19
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)
34 : value_(e->value), name_(e->name) {
35}
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 Stratiienkoe2f2c922021-02-13 10:57:47 +020050 values_.emplace_back(p->values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040051
52 for (int i = 0; i < p->count_enums; ++i)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030053 enums_.emplace_back(&p->enums[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040054
55 for (int i = 0; i < p->count_blobs; ++i)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020056 blob_ids_.emplace_back(p->blob_ids[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040057
58 if (flags_ & DRM_MODE_PROP_RANGE)
59 type_ = DRM_PROPERTY_TYPE_INT;
60 else if (flags_ & DRM_MODE_PROP_ENUM)
61 type_ = DRM_PROPERTY_TYPE_ENUM;
Sean Paul877be972015-06-03 14:08:27 -040062 else if (flags_ & DRM_MODE_PROP_OBJECT)
63 type_ = DRM_PROPERTY_TYPE_OBJECT;
Sean Paul85c58c62015-07-08 10:43:54 -040064 else if (flags_ & DRM_MODE_PROP_BLOB)
65 type_ = DRM_PROPERTY_TYPE_BLOB;
Benjamin Li9127dc82021-05-05 09:23:15 -070066 else if (flags_ & DRM_MODE_PROP_BITMASK)
67 type_ = DRM_PROPERTY_TYPE_BITMASK;
Sean Paul6a55e9f2015-04-30 15:31:06 -040068}
69
70uint32_t DrmProperty::id() const {
71 return id_;
72}
73
74std::string DrmProperty::name() const {
75 return name_;
76}
77
Sean Paulfc0b1da2019-03-06 09:48:42 -050078std::tuple<int, uint64_t> DrmProperty::value() const {
79 if (type_ == DRM_PROPERTY_TYPE_BLOB)
80 return std::make_tuple(0, value_);
Sean Paul85c58c62015-07-08 10:43:54 -040081
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020082 if (values_.empty())
Sean Paulfc0b1da2019-03-06 09:48:42 -050083 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040084
85 switch (type_) {
86 case DRM_PROPERTY_TYPE_INT:
Sean Paulfc0b1da2019-03-06 09:48:42 -050087 return std::make_tuple(0, value_);
Sean Paul6a55e9f2015-04-30 15:31:06 -040088
89 case DRM_PROPERTY_TYPE_ENUM:
90 if (value_ >= enums_.size())
Sean Paulfc0b1da2019-03-06 09:48:42 -050091 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040092
Sean Paulfc0b1da2019-03-06 09:48:42 -050093 return std::make_tuple(0, enums_[value_].value_);
Sean Paul6a55e9f2015-04-30 15:31:06 -040094
Sean Paul877be972015-06-03 14:08:27 -040095 case DRM_PROPERTY_TYPE_OBJECT:
Sean Paulfc0b1da2019-03-06 09:48:42 -050096 return std::make_tuple(0, value_);
Sean Paul877be972015-06-03 14:08:27 -040097
Benjamin Li9127dc82021-05-05 09:23:15 -070098 case DRM_PROPERTY_TYPE_BITMASK:
Sean Paul6a55e9f2015-04-30 15:31:06 -040099 default:
Sean Paulfc0b1da2019-03-06 09:48:42 -0500100 return std::make_tuple(-EINVAL, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400101 }
102}
Lowry Li9b6cafd2018-08-28 17:58:21 +0800103
Sean Paul2689aeb2019-03-13 14:36:52 -0400104bool DrmProperty::is_immutable() const {
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100105 return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
106}
107
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200108bool DrmProperty::is_range() const {
109 return id_ && (flags_ & DRM_MODE_PROP_RANGE);
110}
111
112std::tuple<int, uint64_t> DrmProperty::range_min() const {
113 if (!is_range())
114 return std::make_tuple(-EINVAL, 0);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200115 if (values_.empty())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200116 return std::make_tuple(-ENOENT, 0);
117
118 return std::make_tuple(0, values_[0]);
119}
120
121std::tuple<int, uint64_t> DrmProperty::range_max() const {
122 if (!is_range())
123 return std::make_tuple(-EINVAL, 0);
124 if (values_.size() < 2)
125 return std::make_tuple(-ENOENT, 0);
126
127 return std::make_tuple(0, values_[1]);
128}
129
Lowry Li9b6cafd2018-08-28 17:58:21 +0800130std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200131 const std::string &name) const {
132 for (const auto &it : enums_) {
133 if (it.name_ == name) {
Lowry Li9b6cafd2018-08-28 17:58:21 +0800134 return std::make_tuple(it.value_, 0);
135 }
136 }
137
138 return std::make_tuple(UINT64_MAX, -EINVAL);
139}
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300140
141auto DrmProperty::AtomicSet(drmModeAtomicReq &pset, uint64_t value) const
142 -> bool {
143 if (id_ == 0) {
144 ALOGE("AtomicSet() is called on non-initialized property!");
145 return false;
146 }
147 if (drmModeAtomicAddProperty(&pset, obj_id_, id_, value) < 0) {
148 ALOGE("Failed to add obj_id: %u, prop_id: %u (%s) to pset", obj_id_, id_,
149 name_.c_str());
150 return false;
151 }
152 return true;
153}
154
Sean Paulf72cccd2018-08-27 13:59:08 -0400155} // namespace android