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) |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 34 | : value(e->value), name(e->name) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 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 | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 50 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic): |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 51 | values_.emplace_back(p->values[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 52 | |
| 53 | for (int i = 0; i < p->count_enums; ++i) |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 54 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic): |
Roman Stratiienko | a7913de | 2022-10-20 13:18:57 +0300 | [diff] [blame] | 55 | enums_.emplace_back(&p->enums[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 56 | |
| 57 | for (int i = 0; i < p->count_blobs; ++i) |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 58 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic): |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 59 | blob_ids_.emplace_back(p->blob_ids[i]); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 62 | std::optional<uint64_t> DrmProperty::GetValue() const { |
| 63 | if ((flags_ & DRM_MODE_PROP_BLOB) != 0) |
| 64 | return value_; |
Sean Paul | 85c58c6 | 2015-07-08 10:43:54 -0400 | [diff] [blame] | 65 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 66 | if (values_.empty()) |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 67 | return {}; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 68 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 69 | if ((flags_ & DRM_MODE_PROP_RANGE) != 0) |
| 70 | return value_; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 71 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 72 | if ((flags_ & DRM_MODE_PROP_ENUM) != 0) { |
| 73 | if (value_ >= enums_.size()) |
| 74 | return {}; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 75 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 76 | return enums_[value_].value; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 77 | } |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 78 | |
| 79 | if ((flags_ & DRM_MODE_PROP_OBJECT) != 0) |
| 80 | return value_; |
| 81 | |
| 82 | return {}; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 83 | } |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 84 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 85 | std::tuple<int, uint64_t> DrmProperty::RangeMin() const { |
| 86 | if (!IsRange()) |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 87 | return std::make_tuple(-EINVAL, 0); |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 88 | if (values_.empty()) |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 89 | return std::make_tuple(-ENOENT, 0); |
| 90 | |
| 91 | return std::make_tuple(0, values_[0]); |
| 92 | } |
| 93 | |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 94 | std::tuple<int, uint64_t> DrmProperty::RangeMax() const { |
| 95 | if (!IsRange()) |
Alexandru Gheorghe | 2ed463c | 2019-01-08 19:21:08 +0200 | [diff] [blame] | 96 | return std::make_tuple(-EINVAL, 0); |
| 97 | if (values_.size() < 2) |
| 98 | return std::make_tuple(-ENOENT, 0); |
| 99 | |
| 100 | return std::make_tuple(0, values_[1]); |
| 101 | } |
| 102 | |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 103 | std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName( |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 104 | const std::string &name) const { |
| 105 | for (const auto &it : enums_) { |
Roman Stratiienko | abd8e53 | 2022-12-06 23:31:33 +0200 | [diff] [blame] | 106 | if (it.name == name) { |
| 107 | return std::make_tuple(it.value, 0); |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | return std::make_tuple(UINT64_MAX, -EINVAL); |
| 112 | } |
Roman Stratiienko | 7fd8f88 | 2021-09-29 12:46:54 +0300 | [diff] [blame] | 113 | |
| 114 | auto DrmProperty::AtomicSet(drmModeAtomicReq &pset, uint64_t value) const |
| 115 | -> bool { |
| 116 | if (id_ == 0) { |
| 117 | ALOGE("AtomicSet() is called on non-initialized property!"); |
| 118 | return false; |
| 119 | } |
| 120 | if (drmModeAtomicAddProperty(&pset, obj_id_, id_, value) < 0) { |
| 121 | ALOGE("Failed to add obj_id: %u, prop_id: %u (%s) to pset", obj_id_, id_, |
| 122 | name_.c_str()); |
| 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 128 | } // namespace android |