blob: 8e6f7e5a49c10b1857d177673cf458e92bd59525 [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 Stratiienko13cc3662020-08-29 21:35:39 +030017#include "DrmProperty.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040018
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include <xf86drmMode.h>
20
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020021#include <cerrno>
Roman Stratiienkod518a052021-02-25 19:15:14 +020022#include <cstdint>
Sean Paul6a55e9f2015-04-30 15:31:06 -040023#include <string>
24
Roman Stratiienko13cc3662020-08-29 21:35:39 +030025#include "DrmDevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040026
27namespace android {
28
29DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
30 : value_(e->value), name_(e->name) {
31}
32
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020033DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040034 Init(p, value);
35}
36
Sean Paul6a55e9f2015-04-30 15:31:06 -040037void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
38 id_ = p->prop_id;
39 flags_ = p->flags;
40 name_ = p->name;
41 value_ = value;
42
43 for (int i = 0; i < p->count_values; ++i)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020044 values_.emplace_back(p->values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040045
46 for (int i = 0; i < p->count_enums; ++i)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020047 enums_.emplace_back(DrmPropertyEnum(&p->enums[i]));
Sean Paul6a55e9f2015-04-30 15:31:06 -040048
49 for (int i = 0; i < p->count_blobs; ++i)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020050 blob_ids_.emplace_back(p->blob_ids[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040051
52 if (flags_ & DRM_MODE_PROP_RANGE)
53 type_ = DRM_PROPERTY_TYPE_INT;
54 else if (flags_ & DRM_MODE_PROP_ENUM)
55 type_ = DRM_PROPERTY_TYPE_ENUM;
Sean Paul877be972015-06-03 14:08:27 -040056 else if (flags_ & DRM_MODE_PROP_OBJECT)
57 type_ = DRM_PROPERTY_TYPE_OBJECT;
Sean Paul85c58c62015-07-08 10:43:54 -040058 else if (flags_ & DRM_MODE_PROP_BLOB)
59 type_ = DRM_PROPERTY_TYPE_BLOB;
Benjamin Li9127dc82021-05-05 09:23:15 -070060 else if (flags_ & DRM_MODE_PROP_BITMASK)
61 type_ = DRM_PROPERTY_TYPE_BITMASK;
Sean Paul6a55e9f2015-04-30 15:31:06 -040062}
63
64uint32_t DrmProperty::id() const {
65 return id_;
66}
67
68std::string DrmProperty::name() const {
69 return name_;
70}
71
Sean Paulfc0b1da2019-03-06 09:48:42 -050072std::tuple<int, uint64_t> DrmProperty::value() const {
73 if (type_ == DRM_PROPERTY_TYPE_BLOB)
74 return std::make_tuple(0, value_);
Sean Paul85c58c62015-07-08 10:43:54 -040075
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020076 if (values_.empty())
Sean Paulfc0b1da2019-03-06 09:48:42 -050077 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040078
79 switch (type_) {
80 case DRM_PROPERTY_TYPE_INT:
Sean Paulfc0b1da2019-03-06 09:48:42 -050081 return std::make_tuple(0, value_);
Sean Paul6a55e9f2015-04-30 15:31:06 -040082
83 case DRM_PROPERTY_TYPE_ENUM:
84 if (value_ >= enums_.size())
Sean Paulfc0b1da2019-03-06 09:48:42 -050085 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040086
Sean Paulfc0b1da2019-03-06 09:48:42 -050087 return std::make_tuple(0, enums_[value_].value_);
Sean Paul6a55e9f2015-04-30 15:31:06 -040088
Sean Paul877be972015-06-03 14:08:27 -040089 case DRM_PROPERTY_TYPE_OBJECT:
Sean Paulfc0b1da2019-03-06 09:48:42 -050090 return std::make_tuple(0, value_);
Sean Paul877be972015-06-03 14:08:27 -040091
Benjamin Li9127dc82021-05-05 09:23:15 -070092 case DRM_PROPERTY_TYPE_BITMASK:
Sean Paul6a55e9f2015-04-30 15:31:06 -040093 default:
Sean Paulfc0b1da2019-03-06 09:48:42 -050094 return std::make_tuple(-EINVAL, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040095 }
96}
Lowry Li9b6cafd2018-08-28 17:58:21 +080097
Sean Paul2689aeb2019-03-13 14:36:52 -040098bool DrmProperty::is_immutable() const {
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +010099 return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
100}
101
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200102bool DrmProperty::is_range() const {
103 return id_ && (flags_ & DRM_MODE_PROP_RANGE);
104}
105
106std::tuple<int, uint64_t> DrmProperty::range_min() const {
107 if (!is_range())
108 return std::make_tuple(-EINVAL, 0);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200109 if (values_.empty())
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200110 return std::make_tuple(-ENOENT, 0);
111
112 return std::make_tuple(0, values_[0]);
113}
114
115std::tuple<int, uint64_t> DrmProperty::range_max() const {
116 if (!is_range())
117 return std::make_tuple(-EINVAL, 0);
118 if (values_.size() < 2)
119 return std::make_tuple(-ENOENT, 0);
120
121 return std::make_tuple(0, values_[1]);
122}
123
Lowry Li9b6cafd2018-08-28 17:58:21 +0800124std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200125 const std::string &name) const {
126 for (const auto &it : enums_) {
127 if (it.name_ == name) {
Lowry Li9b6cafd2018-08-28 17:58:21 +0800128 return std::make_tuple(it.value_, 0);
129 }
130 }
131
132 return std::make_tuple(UINT64_MAX, -EINVAL);
133}
Sean Paulf72cccd2018-08-27 13:59:08 -0400134} // namespace android