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