blob: b8ce6806417c43b73f376c7a9b716a1d606ade7b [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
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 Stratiienko13cc3662020-08-29 21:35:39 +030017#include "DrmProperty.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040018
19#include <errno.h>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include <xf86drmMode.h>
21
Roman Stratiienkod518a052021-02-25 19:15:14 +020022#include <cstdint>
23#include <cstring>
Sean Paul6a55e9f2015-04-30 15:31:06 -040024#include <string>
25
Roman Stratiienko13cc3662020-08-29 21:35:39 +030026#include "DrmDevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040027
28namespace android {
29
30DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
31 : value_(e->value), name_(e->name) {
32}
33
34DrmProperty::DrmPropertyEnum::~DrmPropertyEnum() {
35}
36
37DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value)
Sean Paul1eb60062015-05-19 08:10:50 -070038 : id_(0), type_(DRM_PROPERTY_TYPE_INVALID), flags_(0), name_("") {
Sean Paul6a55e9f2015-04-30 15:31:06 -040039 Init(p, value);
40}
41
Sean Paul6a55e9f2015-04-30 15:31:06 -040042void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
43 id_ = p->prop_id;
44 flags_ = p->flags;
45 name_ = p->name;
46 value_ = value;
47
48 for (int i = 0; i < p->count_values; ++i)
49 values_.push_back(p->values[i]);
50
51 for (int i = 0; i < p->count_enums; ++i)
52 enums_.push_back(DrmPropertyEnum(&p->enums[i]));
53
54 for (int i = 0; i < p->count_blobs; ++i)
55 blob_ids_.push_back(p->blob_ids[i]);
56
57 if (flags_ & DRM_MODE_PROP_RANGE)
58 type_ = DRM_PROPERTY_TYPE_INT;
59 else if (flags_ & DRM_MODE_PROP_ENUM)
60 type_ = DRM_PROPERTY_TYPE_ENUM;
Sean Paul877be972015-06-03 14:08:27 -040061 else if (flags_ & DRM_MODE_PROP_OBJECT)
62 type_ = DRM_PROPERTY_TYPE_OBJECT;
Sean Paul85c58c62015-07-08 10:43:54 -040063 else if (flags_ & DRM_MODE_PROP_BLOB)
64 type_ = DRM_PROPERTY_TYPE_BLOB;
Sean Paul6a55e9f2015-04-30 15:31:06 -040065}
66
67uint32_t DrmProperty::id() const {
68 return id_;
69}
70
71std::string DrmProperty::name() const {
72 return name_;
73}
74
Sean Paulfc0b1da2019-03-06 09:48:42 -050075std::tuple<int, uint64_t> DrmProperty::value() const {
76 if (type_ == DRM_PROPERTY_TYPE_BLOB)
77 return std::make_tuple(0, value_);
Sean Paul85c58c62015-07-08 10:43:54 -040078
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 if (values_.size() == 0)
Sean Paulfc0b1da2019-03-06 09:48:42 -050080 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040081
82 switch (type_) {
83 case DRM_PROPERTY_TYPE_INT:
Sean Paulfc0b1da2019-03-06 09:48:42 -050084 return std::make_tuple(0, value_);
Sean Paul6a55e9f2015-04-30 15:31:06 -040085
86 case DRM_PROPERTY_TYPE_ENUM:
87 if (value_ >= enums_.size())
Sean Paulfc0b1da2019-03-06 09:48:42 -050088 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040089
Sean Paulfc0b1da2019-03-06 09:48:42 -050090 return std::make_tuple(0, enums_[value_].value_);
Sean Paul6a55e9f2015-04-30 15:31:06 -040091
Sean Paul877be972015-06-03 14:08:27 -040092 case DRM_PROPERTY_TYPE_OBJECT:
Sean Paulfc0b1da2019-03-06 09:48:42 -050093 return std::make_tuple(0, value_);
Sean Paul877be972015-06-03 14:08:27 -040094
Sean Paul6a55e9f2015-04-30 15:31:06 -040095 default:
Sean Paulfc0b1da2019-03-06 09:48:42 -050096 return std::make_tuple(-EINVAL, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040097 }
98}
Lowry Li9b6cafd2018-08-28 17:58:21 +080099
Sean Paul2689aeb2019-03-13 14:36:52 -0400100bool DrmProperty::is_immutable() const {
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100101 return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
102}
103
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200104bool DrmProperty::is_range() const {
105 return id_ && (flags_ & DRM_MODE_PROP_RANGE);
106}
107
108std::tuple<int, uint64_t> DrmProperty::range_min() const {
109 if (!is_range())
110 return std::make_tuple(-EINVAL, 0);
111 if (values_.size() < 1)
112 return std::make_tuple(-ENOENT, 0);
113
114 return std::make_tuple(0, values_[0]);
115}
116
117std::tuple<int, uint64_t> DrmProperty::range_max() const {
118 if (!is_range())
119 return std::make_tuple(-EINVAL, 0);
120 if (values_.size() < 2)
121 return std::make_tuple(-ENOENT, 0);
122
123 return std::make_tuple(0, values_[1]);
124}
125
Lowry Li9b6cafd2018-08-28 17:58:21 +0800126std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
127 std::string name) const {
128 for (auto it : enums_) {
129 if (it.name_.compare(name) == 0) {
130 return std::make_tuple(it.value_, 0);
131 }
132 }
133
134 return std::make_tuple(UINT64_MAX, -EINVAL);
135}
Sean Paulf72cccd2018-08-27 13:59:08 -0400136} // namespace android