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 <memory>
26 #include <optional>
27 #include <string>
28 #include <unordered_map>
29 
30 #include "Common.h"
31 #include "DrmMode.h"
32 #include "DrmProperty.h"
33 #include "EdidInfo.h"
34 
35 namespace aidl::android::hardware::graphics::composer3::impl {
36 
37 // A "cable" to the display (HDMI, DisplayPort, etc).
38 class DrmConnector {
39    public:
40     static std::unique_ptr<DrmConnector> create(::android::base::borrowed_fd drmFd,
41                                                 uint32_t connectorId);
~DrmConnector()42     ~DrmConnector(){};
43 
getId()44     uint32_t getId() const { return mId; }
45 
46     uint32_t getWidth() const;
47     uint32_t getHeight() const;
48 
49     uint32_t getDpiX() const;
50     uint32_t getDpiY() const;
51 
52     float getRefreshRate() const;
getRefreshRateUint()53     uint32_t getRefreshRateUint() const { return (uint32_t)(getRefreshRate() + 0.5f); }
54 
isConnected()55     bool isConnected() const { return mStatus == DRM_MODE_CONNECTED; }
56 
getEdid()57     std::optional<std::vector<uint8_t>> getEdid() const { return mEdid; }
58 
getCrtcProperty()59     const DrmProperty& getCrtcProperty() const { return mCrtc; }
getDefaultMode()60     const DrmMode* getDefaultMode() const { return mModes[0].get(); }
61 
62     bool update(::android::base::borrowed_fd drmFd);
63 
64    private:
DrmConnector(uint32_t id)65     DrmConnector(uint32_t id) : mId(id) {}
66 
67     std::optional<EdidInfo> loadEdid(::android::base::borrowed_fd drmFd);
68 
69     const uint32_t mId;
70 
71     drmModeConnection mStatus = DRM_MODE_UNKNOWNCONNECTION;
72     std::optional<uint32_t> mWidthMillimeters;
73     std::optional<uint32_t> mHeightMillimeters;
74     std::vector<std::unique_ptr<DrmMode>> mModes;
75 
76     DrmProperty mCrtc;
77     DrmProperty mEdidProp;
78     std::optional<std::vector<uint8_t>> mEdid;
79 
GetPropertiesMap()80     static const auto& GetPropertiesMap() {
81         static const auto* sMap = []() {
82             return new DrmPropertyMemberMap<DrmConnector>{
83                 {"CRTC_ID", &DrmConnector::mCrtc},
84                 {"EDID", &DrmConnector::mEdidProp},
85             };
86         }();
87         return *sMap;
88     }
89 };
90 
91 }  // namespace aidl::android::hardware::graphics::composer3::impl
92