1 /*
2 * Copyright 2022 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 <android-base/logging.h>
20 #include <android-base/unique_fd.h>
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23
24 #include <cstdint>
25 #include <string>
26 #include <unordered_map>
27
28 #include "Common.h"
29
30 namespace aidl::android::hardware::graphics::composer3::impl {
31
32 class DrmProperty {
33 public:
DrmProperty()34 DrmProperty() {}
DrmProperty(uint32_t id,uint64_t value,std::string name)35 DrmProperty(uint32_t id, uint64_t value, std::string name)
36 : mId(id), mValue(value), mName(name) {}
37
~DrmProperty()38 ~DrmProperty() {}
39
getId()40 uint32_t getId() const { return mId; }
41
getValue()42 uint64_t getValue() const { return mValue; }
43
getName()44 const std::string& getName() const { return mName; }
45
46 private:
47 uint32_t mId = -1;
48 uint64_t mValue = -1;
49 std::string mName;
50 };
51
52 template <typename T>
53 using DrmPropertyMember = DrmProperty T::*;
54
55 template <typename T>
56 using DrmPropertyMemberMap = std::unordered_map<std::string, DrmPropertyMember<T>>;
57
58 // Helper to many DrmProperty members for DrmCrtc, DrmConnector, and DrmPlane.
59 template <typename T>
LoadDrmProperties(::android::base::borrowed_fd drmFd,uint32_t objectId,uint32_t objectType,const DrmPropertyMemberMap<T> & objectPropertyMap,T * object)60 bool LoadDrmProperties(::android::base::borrowed_fd drmFd, uint32_t objectId, uint32_t objectType,
61 const DrmPropertyMemberMap<T>& objectPropertyMap, T* object) {
62 auto drmProperties = drmModeObjectGetProperties(drmFd.get(), objectId, objectType);
63 if (!drmProperties) {
64 ALOGE("%s: Failed to get properties: %s", __FUNCTION__, strerror(errno));
65 return false;
66 }
67
68 for (uint32_t i = 0; i < drmProperties->count_props; ++i) {
69 const auto propertyId = drmProperties->props[i];
70
71 auto drmProperty = drmModeGetProperty(drmFd.get(), propertyId);
72 if (!drmProperty) {
73 ALOGE("%s: Failed to get property: %s", __FUNCTION__, strerror(errno));
74 continue;
75 }
76
77 const auto propertyName = drmProperty->name;
78 const auto propertyValue = drmProperties->prop_values[i];
79
80 auto it = objectPropertyMap.find(propertyName);
81 if (it != objectPropertyMap.end()) {
82 DEBUG_LOG("%s: Loaded property:%" PRIu32 " (%s) val:%" PRIu64, __FUNCTION__, propertyId,
83 propertyName, propertyValue);
84
85 auto& objectPointerToMember = it->second;
86 object->*objectPointerToMember = DrmProperty(propertyId, propertyValue, propertyName);
87 }
88
89 drmModeFreeProperty(drmProperty);
90 }
91
92 drmModeFreeObjectProperties(drmProperties);
93
94 return true;
95 }
96
97 } // namespace aidl::android::hardware::graphics::composer3::impl
98