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_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) {}
~DrmMode()41     virtual ~DrmMode() {};
GetModeInfoPtr()42     drmModeModeInfoPtr GetModeInfoPtr()
43     {
44         return &mModeInfo;
45     }
46     void ConvertToHdiMode(DisplayModeInfo &hdiMode);
47 
48 private:
49     drmModeModeInfo mModeInfo = { 0 };
50     int32_t mId = -1;
51 };
52 
53 class DrmModeBlock {
54 public:
55     explicit DrmModeBlock(DrmMode &mode);
56     virtual ~DrmModeBlock();
57     int32_t Init(DrmMode &mode);
GetBlockId() const58     uint32_t GetBlockId() const
59     {
60         return mBlockId;
61     }
62 
63 private:
64     uint32_t mBlockId = DRM_INVALID_ID;
65 };
66 
67 class DrmConnector {
68 public:
69     DrmConnector(drmModeConnector c, FdPtr &fd);
~DrmConnector()70     virtual ~DrmConnector() {};
GetId() const71     uint32_t GetId() const
72     {
73         return mId;
74     }
GetEncoderId() const75     uint32_t GetEncoderId() const
76     {
77         return mEncoderId;
78     }
SetEncoderId(uint32_t id)79     void SetEncoderId(uint32_t id)
80     {
81         mEncoderId = id;
82     }
83     void GetDisplayCap(DisplayCapability &cap);
84     int32_t Init(DrmDevice &drmDevice);
85     int32_t PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId);
86     int32_t GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes);
GetPreferenceId() const87     int32_t GetPreferenceId() const
88     {
89         return mPreferenceId;
90     }
GetPropCrtcId() const91     uint32_t GetPropCrtcId() const
92     {
93         return mPropCrtcId;
94     }
95     int32_t TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
96         uint32_t &crtcId);
97     // update modes will reset the preference mode id and active mode id
98     int32_t UpdateModes();
99     std::unique_ptr<DrmModeBlock> GetModeBlockFromId(int32_t id);
100     int32_t GetModeFromId(int32_t id, DrmMode &mode);
GetDpmsState() const101     uint64_t GetDpmsState() const
102     {
103         return mDpmsState;
104     }
105     int32_t SetDpmsState(uint64_t dmps);
106     bool IsConnected();
107     bool HandleHotplug(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, bool plugIn);
108     std::shared_ptr<DrmCrtc> UpdateCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs,
109         bool plugIn, drmModeConnectorPtr c, int *crtc_id);
110     int32_t GetBrightness(uint32_t& level);
111     int32_t SetBrightness(uint32_t level);
112 
113 private:
114     static void ConvertTypeToName(uint32_t type, std::string &name);
115     static void ConvertToHdiType(uint32_t type, InterfaceType &hdiType);
116 
117     void InitModes(drmModeConnector c);
118     uint32_t mId;
119     InterfaceType mType;
120     uint32_t mPhyWidth;
121     uint32_t mPhyHeight;
122     uint32_t mSupportLayers = 0;
123     uint32_t mVirtualDispCount = 0;
124     bool mSupportWriteBack = false;
125     uint32_t mPropertyCount = 0;
126     uint32_t mEncoderId;
127     std::vector<uint32_t> mPossibleEncoders;
128     std::string mName;
129     drmModeConnection mConnectState;
130     uint32_t mPropDpmsId = DRM_INVALID_ID;
131     uint64_t mDpmsState = 0;
132     uint32_t mPropCrtcId = DRM_INVALID_ID;
133     uint32_t mPropBrightnessId = DRM_INVALID_ID;
134     uint32_t mBrightnessLevel = 0;
135     std::unordered_map<int32_t, DrmMode> mModes;
136     int32_t mPreferenceId = INVALID_MODE_ID;
137 
138     FdPtr mDrmFdPtr;
139 };
140 } // namespace OHOS
141 } // namespace HDI
142 } // namespace DISPLAY
143 #endif // DRM_CONNECTOR_H
144