Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 1 | /* |
| 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 Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 17 | #include "DrmProperty.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 18 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 19 | #include <xf86drmMode.h> |
| 20 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 21 | #include <cerrno> |
Roman Stratiienko | d518a05 | 2021-02-25 19:15:14 +0200 | [diff] [blame] | 22 | #include <cstdint> |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 25 | #include "DrmDevice.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e) |
| 30 | : value_(e->value), name_(e->name) { |
| 31 | } |
| 32 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 33 | DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 34 | Init(p, value); |
| 35 | } |
| 36 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 37 | void 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 Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 44 | values_.emplace_back(p->values[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 45 | |
| 46 | for (int i = 0; i < p->count_enums; ++i) |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 47 | enums_.emplace_back(DrmPropertyEnum(&p->enums[i])); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 48 | |
| 49 | for (int i = 0; i < p->count_blobs; ++i) |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 50 | blob_ids_.emplace_back(p->blob_ids[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 51 | |
| 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 Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 56 | else if (flags_ & DRM_MODE_PROP_OBJECT) |
| 57 | type_ = DRM_PROPERTY_TYPE_OBJECT; |
Sean Paul | 85c58c6 | 2015-07-08 10:43:54 -0400 | [diff] [blame] | 58 | else if (flags_ & DRM_MODE_PROP_BLOB) |
| 59 | type_ = DRM_PROPERTY_TYPE_BLOB; |
Benjamin Li | 9127dc8 | 2021-05-05 09:23:15 -0700 | [diff] [blame] | 60 | else if (flags_ & DRM_MODE_PROP_BITMASK) |
| 61 | type_ = DRM_PROPERTY_TYPE_BITMASK; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | uint32_t DrmProperty::id() const { |
| 65 | return id_; |
| 66 | } |
| 67 | |
| 68 | std::string DrmProperty::name() const { |
| 69 | return name_; |
| 70 | } |
| 71 | |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 72 | std::tuple<int, uint64_t> DrmProperty::value() const { |
| 73 | if (type_ == DRM_PROPERTY_TYPE_BLOB) |
| 74 | return std::make_tuple(0, value_); |
Sean Paul | 85c58c6 | 2015-07-08 10:43:54 -0400 | [diff] [blame] | 75 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 76 | if (values_.empty()) |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 77 | return std::make_tuple(-ENOENT, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 78 | |
| 79 | switch (type_) { |
| 80 | case DRM_PROPERTY_TYPE_INT: |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 81 | return std::make_tuple(0, value_); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 82 | |
| 83 | case DRM_PROPERTY_TYPE_ENUM: |
| 84 | if (value_ >= enums_.size()) |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 85 | return std::make_tuple(-ENOENT, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 86 | |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 87 | return std::make_tuple(0, enums_[value_].value_); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 88 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 89 | case DRM_PROPERTY_TYPE_OBJECT: |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 90 | return std::make_tuple(0, value_); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 91 | |
Benjamin Li | 9127dc8 | 2021-05-05 09:23:15 -0700 | [diff] [blame] | 92 | case DRM_PROPERTY_TYPE_BITMASK: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 93 | default: |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame] | 94 | return std::make_tuple(-EINVAL, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 95 | } |
| 96 | } |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 97 | |
Sean Paul | 2689aeb | 2019-03-13 14:36:52 -0400 | [diff] [blame] | 98 | bool DrmProperty::is_immutable() const { |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 99 | return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE); |
| 100 | } |
| 101 | |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 102 | bool DrmProperty::is_range() const { |
| 103 | return id_ && (flags_ & DRM_MODE_PROP_RANGE); |
| 104 | } |
| 105 | |
| 106 | std::tuple<int, uint64_t> DrmProperty::range_min() const { |
| 107 | if (!is_range()) |
| 108 | return std::make_tuple(-EINVAL, 0); |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 109 | if (values_.empty()) |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 110 | return std::make_tuple(-ENOENT, 0); |
| 111 | |
| 112 | return std::make_tuple(0, values_[0]); |
| 113 | } |
| 114 | |
| 115 | std::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 Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 124 | std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName( |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 125 | const std::string &name) const { |
| 126 | for (const auto &it : enums_) { |
| 127 | if (it.name_ == name) { |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 128 | return std::make_tuple(it.value_, 0); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return std::make_tuple(UINT64_MAX, -EINVAL); |
| 133 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 134 | } // namespace android |