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 #pragma once 18 19 #include <xf86drmMode.h> 20 21 #include <cstdint> 22 #include <map> 23 #include <optional> 24 #include <string> 25 #include <vector> 26 27 namespace android { 28 29 class DrmProperty { 30 public: 31 DrmProperty() = default; 32 DrmProperty(uint32_t obj_id, drmModePropertyPtr p, uint64_t value); 33 DrmProperty(const DrmProperty &) = delete; 34 DrmProperty &operator=(const DrmProperty &) = delete; 35 36 auto Init(uint32_t obj_id, drmModePropertyPtr p, uint64_t value) -> void; 37 std::tuple<uint64_t, int> GetEnumValueWithName(const std::string &name) const; 38 GetId()39 auto GetId() const { 40 return id_; 41 } 42 GetName()43 auto GetName() const { 44 return name_; 45 } 46 47 auto GetValue() const -> std::optional<uint64_t>; 48 IsImmutable()49 bool IsImmutable() const { 50 return id_ != 0 && (flags_ & DRM_MODE_PROP_IMMUTABLE) != 0; 51 } 52 IsRange()53 bool IsRange() const { 54 return id_ != 0 && (flags_ & DRM_MODE_PROP_RANGE) != 0; 55 } 56 57 auto RangeMin() const -> std::tuple<int, uint64_t>; 58 auto RangeMax() const -> std::tuple<int, uint64_t>; 59 60 [[nodiscard]] auto AtomicSet(drmModeAtomicReq &pset, uint64_t value) const 61 -> bool; 62 63 template <class E> 64 auto AddEnumToMap(const std::string &name, E key, std::map<E, uint64_t> &map) 65 -> bool; 66 67 explicit operator bool() const { 68 return id_ != 0; 69 } 70 71 private: 72 class DrmPropertyEnum { 73 public: 74 explicit DrmPropertyEnum(drm_mode_property_enum *e); 75 ~DrmPropertyEnum() = default; 76 77 uint64_t value; 78 std::string name; 79 }; 80 81 uint32_t obj_id_ = 0; 82 uint32_t id_ = 0; 83 84 uint32_t flags_ = 0; 85 std::string name_; 86 uint64_t value_ = 0; 87 88 std::vector<uint64_t> values_; 89 std::vector<DrmPropertyEnum> enums_; 90 std::vector<uint32_t> blob_ids_; 91 }; 92 93 template <class E> 94 auto DrmProperty::AddEnumToMap(const std::string &name, E key, 95 std::map<E, uint64_t> &map) -> bool { 96 uint64_t enum_value = UINT64_MAX; 97 int err = 0; 98 std::tie(enum_value, err) = GetEnumValueWithName(name); 99 if (err == 0) { 100 map[key] = enum_value; 101 return true; 102 } 103 104 return false; 105 } 106 107 } // namespace android 108