blob: dcab05e02f4e3b70f659b2db36bf01ecddeca82d [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
17#include "drmproperty.h"
Sean Paulf72cccd2018-08-27 13:59:08 -040018#include "drmdevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040019
20#include <errno.h>
21#include <stdint.h>
22#include <string>
23
24#include <xf86drmMode.h>
25
26namespace android {
27
28DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
29 : value_(e->value), name_(e->name) {
30}
31
32DrmProperty::DrmPropertyEnum::~DrmPropertyEnum() {
33}
34
35DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value)
Sean Paul1eb60062015-05-19 08:10:50 -070036 : id_(0), type_(DRM_PROPERTY_TYPE_INVALID), flags_(0), name_("") {
Sean Paul6a55e9f2015-04-30 15:31:06 -040037 Init(p, value);
38}
39
Sean Paul6a55e9f2015-04-30 15:31:06 -040040void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
41 id_ = p->prop_id;
42 flags_ = p->flags;
43 name_ = p->name;
44 value_ = value;
45
46 for (int i = 0; i < p->count_values; ++i)
47 values_.push_back(p->values[i]);
48
49 for (int i = 0; i < p->count_enums; ++i)
50 enums_.push_back(DrmPropertyEnum(&p->enums[i]));
51
52 for (int i = 0; i < p->count_blobs; ++i)
53 blob_ids_.push_back(p->blob_ids[i]);
54
55 if (flags_ & DRM_MODE_PROP_RANGE)
56 type_ = DRM_PROPERTY_TYPE_INT;
57 else if (flags_ & DRM_MODE_PROP_ENUM)
58 type_ = DRM_PROPERTY_TYPE_ENUM;
Sean Paul877be972015-06-03 14:08:27 -040059 else if (flags_ & DRM_MODE_PROP_OBJECT)
60 type_ = DRM_PROPERTY_TYPE_OBJECT;
Sean Paul85c58c62015-07-08 10:43:54 -040061 else if (flags_ & DRM_MODE_PROP_BLOB)
62 type_ = DRM_PROPERTY_TYPE_BLOB;
Sean Paul6a55e9f2015-04-30 15:31:06 -040063}
64
65uint32_t DrmProperty::id() const {
66 return id_;
67}
68
69std::string DrmProperty::name() const {
70 return name_;
71}
72
73int DrmProperty::value(uint64_t *value) const {
Sean Paul85c58c62015-07-08 10:43:54 -040074 if (type_ == DRM_PROPERTY_TYPE_BLOB) {
75 *value = value_;
76 return 0;
77 }
78
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 if (values_.size() == 0)
80 return -ENOENT;
81
82 switch (type_) {
83 case DRM_PROPERTY_TYPE_INT:
84 *value = value_;
85 return 0;
86
87 case DRM_PROPERTY_TYPE_ENUM:
88 if (value_ >= enums_.size())
89 return -ENOENT;
90
91 *value = enums_[value_].value_;
92 return 0;
93
Sean Paul877be972015-06-03 14:08:27 -040094 case DRM_PROPERTY_TYPE_OBJECT:
95 *value = value_;
96 return 0;
97
Sean Paul6a55e9f2015-04-30 15:31:06 -040098 default:
99 return -EINVAL;
100 }
101}
Lowry Li9b6cafd2018-08-28 17:58:21 +0800102
103std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
104 std::string name) const {
105 for (auto it : enums_) {
106 if (it.name_.compare(name) == 0) {
107 return std::make_tuple(it.value_, 0);
108 }
109 }
110
111 return std::make_tuple(UINT64_MAX, -EINVAL);
112}
Sean Paulf72cccd2018-08-27 13:59:08 -0400113} // namespace android