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