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