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 #include "drmproperty.h"
18 #include "drmdevice.h"
19
20 #include <errno.h>
21 #include <stdint.h>
22 #include <string>
23
24 #include <xf86drmMode.h>
25 #include <log/log.h>
26 #include <inttypes.h>
27 #include <utils/Errors.h>
28
29 namespace android {
30
DrmPropertyEnum(drm_mode_property_enum * e)31 DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
32 : value_(e->value), name_(e->name) {
33 }
34
~DrmPropertyEnum()35 DrmProperty::DrmPropertyEnum::~DrmPropertyEnum() {
36 }
37
DrmProperty(drmModePropertyPtr p,uint64_t value)38 DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value)
39 : id_(0), type_(DRM_PROPERTY_TYPE_INVALID), flags_(0), name_("") {
40 Init(p, value);
41 }
42
Init(drmModePropertyPtr p,uint64_t value)43 void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
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_.push_back(p->values[i]);
51
52 for (int i = 0; i < p->count_enums; ++i)
53 enums_.push_back(DrmPropertyEnum(&p->enums[i]));
54
55 for (int i = 0; i < p->count_blobs; ++i)
56 blob_ids_.push_back(p->blob_ids[i]);
57
58 if ((flags_ & DRM_MODE_PROP_RANGE) ||
59 (flags_ & DRM_MODE_PROP_SIGNED_RANGE))
60 type_ = DRM_PROPERTY_TYPE_INT;
61 else if (flags_ & DRM_MODE_PROP_ENUM)
62 type_ = DRM_PROPERTY_TYPE_ENUM;
63 else if (flags_ & DRM_MODE_PROP_OBJECT)
64 type_ = DRM_PROPERTY_TYPE_OBJECT;
65 else if (flags_ & DRM_MODE_PROP_BLOB)
66 type_ = DRM_PROPERTY_TYPE_BLOB;
67 else if (flags_ & DRM_MODE_PROP_BITMASK)
68 type_ = DRM_PROPERTY_TYPE_BITMASK;
69 }
70
id() const71 uint32_t DrmProperty::id() const {
72 return id_;
73 }
74
name() const75 std::string DrmProperty::name() const {
76 return name_;
77 }
78
value() const79 std::tuple<int, uint64_t> DrmProperty::value() const {
80 if (type_ == DRM_PROPERTY_TYPE_BLOB)
81 return std::make_tuple(0, value_);
82
83 if (values_.size() == 0)
84 return std::make_tuple(-ENOENT, 0);
85
86 switch (type_) {
87 case DRM_PROPERTY_TYPE_INT:
88 return std::make_tuple(0, value_);
89
90 case DRM_PROPERTY_TYPE_BITMASK:
91 return std::make_tuple(0, value_);
92
93 case DRM_PROPERTY_TYPE_ENUM:
94 if (value_ >= enums_.size())
95 return std::make_tuple(-ENOENT, 0);
96
97 return std::make_tuple(0, enums_[value_].value_);
98
99 case DRM_PROPERTY_TYPE_OBJECT:
100 return std::make_tuple(0, value_);
101
102 default:
103 return std::make_tuple(-EINVAL, 0);
104 }
105 }
106
printProperty() const107 void DrmProperty::printProperty() const
108 {
109 ALOGD("=====================================================");
110 ALOGD("name: %s, type(%d), value_(%" PRId64 ")", name_.c_str(), type_, value_);
111 ALOGD("values.size(%zu)", values_.size());
112 for(uint32_t i = 0; i < values_.size(); i++) {
113 ALOGD("[%d] %" PRId64 "", i, values_[i]);
114 }
115 ALOGD("enums.size(%zu)", enums_.size());
116 uint32_t i = 0;
117 for (auto const &it : enums_) {
118 ALOGD("[%d] %s %" PRId64 "", i++, it.name_.c_str(), it.value_);
119 }
120 }
121
is_immutable() const122 bool DrmProperty::is_immutable() const {
123 return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
124 }
125
is_range() const126 bool DrmProperty::is_range() const {
127 return id_ && (flags_ & DRM_MODE_PROP_RANGE);
128 }
129
range_min() const130 std::tuple<int, uint64_t> DrmProperty::range_min() const {
131 if (!is_range())
132 return std::make_tuple(-EINVAL, 0);
133 if (values_.size() < 1)
134 return std::make_tuple(-ENOENT, 0);
135
136 return std::make_tuple(0, values_[0]);
137 }
138
range_max() const139 std::tuple<int, uint64_t> DrmProperty::range_max() const {
140 if (!is_range())
141 return std::make_tuple(-EINVAL, 0);
142 if (values_.size() < 2)
143 return std::make_tuple(-ENOENT, 0);
144
145 return std::make_tuple(0, values_[1]);
146 }
147
GetEnumValueWithName(std::string name) const148 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
149 std::string name) const {
150 for (auto it : enums_) {
151 if (it.name_.compare(name) == 0) {
152 return std::make_tuple(it.value_, 0);
153 }
154 }
155
156 return std::make_tuple(UINT64_MAX, -EINVAL);
157 }
158
UpdateValue(uint64_t value)159 void DrmProperty::UpdateValue(uint64_t value) {
160 value_ = value;
161 }
162
halToDrmEnum(const uint32_t halData,const MapHal2DrmEnum & drmEnums)163 std::tuple<uint64_t, int> DrmEnumParser::halToDrmEnum(const uint32_t halData,
164 const MapHal2DrmEnum& drmEnums) {
165 auto it = drmEnums.find(halData);
166 if (it != drmEnums.end()) {
167 return std::make_tuple(it->second, NO_ERROR);
168 } else {
169 ALOGE("%s::Failed to find standard enum(%d)",
170 __func__, halData);
171 return std::make_tuple(0, -EINVAL);
172 }
173 }
174
parseEnums(const DrmProperty & property,const std::vector<std::pair<uint32_t,const char * >> & enums,MapHal2DrmEnum & out_enums)175 void DrmEnumParser::parseEnums(const DrmProperty &property,
176 const std::vector<std::pair<uint32_t, const char *>> &enums,
177 MapHal2DrmEnum& out_enums) {
178 uint64_t value;
179 int err;
180 for (auto &e : enums) {
181 std::tie(value, err) = property.GetEnumValueWithName(e.second);
182 if (err)
183 ALOGE("Fail to find enum value with name %s", e.second);
184 else
185 out_enums[e.first] = value;
186 }
187 }
188
189 } // namespace android
190