• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
51     values_.emplace_back(p->values[i]);
52 
53   for (int i = 0; i < p->count_enums; ++i)
54     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
55     enums_.emplace_back(&p->enums[i]);
56 
57   for (int i = 0; i < p->count_blobs; ++i)
58     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic):
59     blob_ids_.emplace_back(p->blob_ids[i]);
60 }
61 
GetValue() const62 std::optional<uint64_t> DrmProperty::GetValue() const {
63   if ((flags_ & DRM_MODE_PROP_BLOB) != 0)
64     return value_;
65 
66   if (values_.empty())
67     return {};
68 
69   if ((flags_ & DRM_MODE_PROP_RANGE) != 0)
70     return value_;
71 
72   if ((flags_ & DRM_MODE_PROP_ENUM) != 0) {
73     if (value_ >= enums_.size())
74       return {};
75 
76     return enums_[value_].value;
77   }
78 
79   if ((flags_ & DRM_MODE_PROP_OBJECT) != 0)
80     return value_;
81 
82   return {};
83 }
84 
RangeMin() const85 std::tuple<int, uint64_t> DrmProperty::RangeMin() const {
86   if (!IsRange())
87     return std::make_tuple(-EINVAL, 0);
88   if (values_.empty())
89     return std::make_tuple(-ENOENT, 0);
90 
91   return std::make_tuple(0, values_[0]);
92 }
93 
RangeMax() const94 std::tuple<int, uint64_t> DrmProperty::RangeMax() const {
95   if (!IsRange())
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 
GetEnumValueWithName(const std::string & name) const103 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
104     const std::string &name) const {
105   for (const auto &it : enums_) {
106     if (it.name == name) {
107       return std::make_tuple(it.value, 0);
108     }
109   }
110 
111   return std::make_tuple(UINT64_MAX, -EINVAL);
112 }
113 
AtomicSet(drmModeAtomicReq & pset,uint64_t value) const114 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 
128 }  // namespace android
129