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
17 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
18 #define LOG_TAG "hwc-drm-property"
19
20 #include "DrmProperty.h"
21
22 #include <xf86drmMode.h>
23
24 #include <cerrno>
25 #include <cstdint>
26 #include <string>
27
28 #include "DrmDevice.h"
29 #include "utils/log.h"
30
31 namespace android {
32
DrmPropertyEnum(drm_mode_property_enum * e)33 DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
34 : value_(e->value), name_(e->name) {
35 }
36
DrmProperty(uint32_t obj_id,drmModePropertyPtr p,uint64_t value)37 DrmProperty::DrmProperty(uint32_t obj_id, drmModePropertyPtr p,
38 uint64_t value) {
39 Init(obj_id, p, value);
40 }
41
Init(uint32_t obj_id,drmModePropertyPtr p,uint64_t value)42 void DrmProperty::Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) {
43 obj_id_ = obj_id;
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)
50 values_.emplace_back(p->values[i]);
51
52 for (int i = 0; i < p->count_enums; ++i)
53 enums_.emplace_back(DrmPropertyEnum(&p->enums[i]));
54
55 for (int i = 0; i < p->count_blobs; ++i)
56 blob_ids_.emplace_back(p->blob_ids[i]);
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;
62 else if (flags_ & DRM_MODE_PROP_OBJECT)
63 type_ = DRM_PROPERTY_TYPE_OBJECT;
64 else if (flags_ & DRM_MODE_PROP_BLOB)
65 type_ = DRM_PROPERTY_TYPE_BLOB;
66 else if (flags_ & DRM_MODE_PROP_BITMASK)
67 type_ = DRM_PROPERTY_TYPE_BITMASK;
68 }
69
id() const70 uint32_t DrmProperty::id() const {
71 return id_;
72 }
73
name() const74 std::string DrmProperty::name() const {
75 return name_;
76 }
77
value() const78 std::tuple<int, uint64_t> DrmProperty::value() const {
79 if (type_ == DRM_PROPERTY_TYPE_BLOB)
80 return std::make_tuple(0, value_);
81
82 if (values_.empty())
83 return std::make_tuple(-ENOENT, 0);
84
85 switch (type_) {
86 case DRM_PROPERTY_TYPE_INT:
87 return std::make_tuple(0, value_);
88
89 case DRM_PROPERTY_TYPE_ENUM:
90 if (value_ >= enums_.size())
91 return std::make_tuple(-ENOENT, 0);
92
93 return std::make_tuple(0, enums_[value_].value_);
94
95 case DRM_PROPERTY_TYPE_OBJECT:
96 return std::make_tuple(0, value_);
97
98 case DRM_PROPERTY_TYPE_BITMASK:
99 default:
100 return std::make_tuple(-EINVAL, 0);
101 }
102 }
103
is_immutable() const104 bool DrmProperty::is_immutable() const {
105 return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
106 }
107
is_range() const108 bool DrmProperty::is_range() const {
109 return id_ && (flags_ & DRM_MODE_PROP_RANGE);
110 }
111
range_min() const112 std::tuple<int, uint64_t> DrmProperty::range_min() const {
113 if (!is_range())
114 return std::make_tuple(-EINVAL, 0);
115 if (values_.empty())
116 return std::make_tuple(-ENOENT, 0);
117
118 return std::make_tuple(0, values_[0]);
119 }
120
range_max() const121 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
GetEnumValueWithName(const std::string & name) const130 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
131 const std::string &name) const {
132 for (const auto &it : enums_) {
133 if (it.name_ == name) {
134 return std::make_tuple(it.value_, 0);
135 }
136 }
137
138 return std::make_tuple(UINT64_MAX, -EINVAL);
139 }
140
AtomicSet(drmModeAtomicReq & pset,uint64_t value) const141 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
155 } // namespace android
156