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