OSDN Git Service

8e6f7e5a49c10b1857d177673cf458e92bd59525
[android-x86/external-drm_hwcomposer.git] / drm / DrmProperty.cpp
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
19 #include <xf86drmMode.h>
20
21 #include <cerrno>
22 #include <cstdint>
23 #include <string>
24
25 #include "DrmDevice.h"
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::DrmProperty(drmModePropertyPtr p, uint64_t value) {
34   Init(p, value);
35 }
36
37 void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
38   id_ = p->prop_id;
39   flags_ = p->flags;
40   name_ = p->name;
41   value_ = value;
42
43   for (int i = 0; i < p->count_values; ++i)
44     values_.emplace_back(p->values[i]);
45
46   for (int i = 0; i < p->count_enums; ++i)
47     enums_.emplace_back(DrmPropertyEnum(&p->enums[i]));
48
49   for (int i = 0; i < p->count_blobs; ++i)
50     blob_ids_.emplace_back(p->blob_ids[i]);
51
52   if (flags_ & DRM_MODE_PROP_RANGE)
53     type_ = DRM_PROPERTY_TYPE_INT;
54   else if (flags_ & DRM_MODE_PROP_ENUM)
55     type_ = DRM_PROPERTY_TYPE_ENUM;
56   else if (flags_ & DRM_MODE_PROP_OBJECT)
57     type_ = DRM_PROPERTY_TYPE_OBJECT;
58   else if (flags_ & DRM_MODE_PROP_BLOB)
59     type_ = DRM_PROPERTY_TYPE_BLOB;
60   else if (flags_ & DRM_MODE_PROP_BITMASK)
61     type_ = DRM_PROPERTY_TYPE_BITMASK;
62 }
63
64 uint32_t DrmProperty::id() const {
65   return id_;
66 }
67
68 std::string DrmProperty::name() const {
69   return name_;
70 }
71
72 std::tuple<int, uint64_t> DrmProperty::value() const {
73   if (type_ == DRM_PROPERTY_TYPE_BLOB)
74     return std::make_tuple(0, value_);
75
76   if (values_.empty())
77     return std::make_tuple(-ENOENT, 0);
78
79   switch (type_) {
80     case DRM_PROPERTY_TYPE_INT:
81       return std::make_tuple(0, value_);
82
83     case DRM_PROPERTY_TYPE_ENUM:
84       if (value_ >= enums_.size())
85         return std::make_tuple(-ENOENT, 0);
86
87       return std::make_tuple(0, enums_[value_].value_);
88
89     case DRM_PROPERTY_TYPE_OBJECT:
90       return std::make_tuple(0, value_);
91
92     case DRM_PROPERTY_TYPE_BITMASK:
93     default:
94       return std::make_tuple(-EINVAL, 0);
95   }
96 }
97
98 bool DrmProperty::is_immutable() const {
99   return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
100 }
101
102 bool DrmProperty::is_range() const {
103   return id_ && (flags_ & DRM_MODE_PROP_RANGE);
104 }
105
106 std::tuple<int, uint64_t> DrmProperty::range_min() const {
107   if (!is_range())
108     return std::make_tuple(-EINVAL, 0);
109   if (values_.empty())
110     return std::make_tuple(-ENOENT, 0);
111
112   return std::make_tuple(0, values_[0]);
113 }
114
115 std::tuple<int, uint64_t> DrmProperty::range_max() const {
116   if (!is_range())
117     return std::make_tuple(-EINVAL, 0);
118   if (values_.size() < 2)
119     return std::make_tuple(-ENOENT, 0);
120
121   return std::make_tuple(0, values_[1]);
122 }
123
124 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
125     const std::string &name) const {
126   for (const auto &it : enums_) {
127     if (it.name_ == name) {
128       return std::make_tuple(it.value_, 0);
129     }
130   }
131
132   return std::make_tuple(UINT64_MAX, -EINVAL);
133 }
134 }  // namespace android