1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DRM_CONNECTOR_H
17 #define DRM_CONNECTOR_H
18 #include <string>
19 #include <unordered_map>
20 #include <vector>
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23 #include "display_type.h"
24 #include "drm_encoder.h"
25 #include "hdi_device_common.h"
26 #include "hdi_shared_fd.h"
27 
28 namespace OHOS {
29     namespace HDI {
30         namespace DISPLAY {
31             const std::string PROP_DPMS = "DPMS";
32             const std::string PROP_CRTCID = "CRTC_ID";
33             const std::string PROP_BRIGHTNESS = "brightness";
34             class DrmDevice;
35             class DrmModeBlock;
36 
37             class DrmMode {
38             public:
DrmMode()39                 DrmMode() {};
DrmMode(drmModeModeInfo &modeInfo, uint32_t id)40                 DrmMode(drmModeModeInfo &modeInfo, uint32_t id) : mModeInfo(modeInfo), mId(id)
41                 {
42                 }
~DrmMode()43                 virtual ~DrmMode() {};
GetModeInfoPtr()44                 drmModeModeInfoPtr GetModeInfoPtr()
45                 {
46                     return &mModeInfo;
47                 }
48                 void ConvertToHdiMode(DisplayModeInfo &hdiMode);
49 
50             private:
51                 drmModeModeInfo mModeInfo = {0};
52                 int32_t mId = -1;
53             };
54 
55             class DrmModeBlock {
56             public:
57                 explicit DrmModeBlock(DrmMode &mode);
58                 virtual ~DrmModeBlock();
59                 int32_t Init(DrmMode &mode);
GetBlockId() const60                 uint32_t GetBlockId() const
61                 {
62                     return mBlockId;
63                 }
64 
65             private:
66                 uint32_t mBlockId = DRM_INVALID_ID;
67             };
68 
69             class DrmConnector {
70             public:
71                 DrmConnector(drmModeConnector c, FdPtr &fd);
~DrmConnector()72                 virtual ~DrmConnector() {};
GetId() const73                 uint32_t GetId() const
74                 {
75                     return mId;
76                 }
GetEncoderId() const77                 uint32_t GetEncoderId() const
78                 {
79                     return mEncoderId;
80                 }
SetEncoderId(uint32_t id)81                 void SetEncoderId(uint32_t id)
82                 {
83                     mEncoderId = id;
84                 }
85                 void GetDisplayCap(DisplayCapability &cap);
86                 int32_t Init(DrmDevice &drmDevice);
87                 int32_t PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId);
88                 int32_t GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes);
GetPreferenceId() const89                 int32_t GetPreferenceId() const
90                 {
91                     return mPreferenceId;
92                 }
GetPropCrtcId() const93                 uint32_t GetPropCrtcId() const
94                 {
95                     return mPropCrtcId;
96                 }
97                 int32_t TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
98                                        uint32_t &crtcId);
99                 // update modes will reset the preference mode id and active mode id
100                 int32_t UpdateModes();
101                 std::unique_ptr<DrmModeBlock> GetModeBlockFromId(int32_t id);
102                 int32_t GetModeFromId(int32_t id, DrmMode &mode);
GetDpmsState() const103                 uint64_t GetDpmsState() const
104                 {
105                     return mDpmsState;
106                 }
107                 int32_t SetDpmsState(uint64_t dmps);
108                 bool IsConnected();
109                 bool HandleHotplug(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, bool plugIn);
110                 std::shared_ptr<DrmCrtc> UpdateCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs,
111                                                       bool plugIn, drmModeConnectorPtr c, int *crtc_id);
112                 int32_t GetBrightness(uint32_t &level);
113                 int32_t SetBrightness(uint32_t level);
114 
115             private:
116                 static void ConvertTypeToName(uint32_t type, std::string &name);
117                 static void ConvertToHdiType(uint32_t type, InterfaceType &hdiType);
118 
119                 void InitModes(drmModeConnector c);
120                 uint32_t mId;
121                 InterfaceType mType;
122                 uint32_t mPhyWidth;
123                 uint32_t mPhyHeight;
124                 uint32_t mSupportLayers = 0;
125                 uint32_t mVirtualDispCount = 0;
126                 bool mSupportWriteBack = false;
127                 uint32_t mPropertyCount = 0;
128                 uint32_t mEncoderId;
129                 std::vector<uint32_t> mPossibleEncoders;
130                 std::string mName;
131                 drmModeConnection mConnectState;
132                 uint32_t mPropDpmsId = DRM_INVALID_ID;
133                 uint64_t mDpmsState = 0;
134                 uint32_t mPropCrtcId = DRM_INVALID_ID;
135                 uint32_t mPropBrightnessId = DRM_INVALID_ID;
136                 uint32_t mBrightnessLevel = 0;
137                 std::unordered_map<int32_t, DrmMode> mModes;
138                 int32_t mPreferenceId = INVALID_MODE_ID;
139 
140                 FdPtr mDrmFdPtr;
141             };
142         } // namespace OHOS
143     }     // namespace HDI
144 } // namespace DISPLAY
145 #endif // DRM_CONNECTOR_H
146