• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) const;
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()58     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, const FdPtr &fd);
~DrmConnector()70     virtual ~DrmConnector() {}
GetId()71     uint32_t GetId() const
72     {
73         return mId;
74     }
GetEncoderId()75     uint32_t GetEncoderId() const
76     {
77         return mEncoderId;
78     }
79     void GetDisplayCap(DisplayCapability &cap);
80     int32_t Init(DrmDevice &drmDevice);
81     int32_t PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId);
82     int32_t GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes);
GetPreferenceId()83     int32_t GetPreferenceId() const
84     {
85         return mPreferenceId;
86     }
GetPropCrtcId()87     uint32_t GetPropCrtcId() const
88     {
89         return mPropCrtcId;
90     }
91     int32_t TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
92         uint32_t &crtcId);
93     // update modes will reset the preference mode id and active mode id
94     int32_t UpdateModes();
95     std::unique_ptr<DrmModeBlock> GetModeBlockFromId(int32_t id);
96     int32_t GetModeFromId(int32_t id, DrmMode &mode);
GetDpmsState()97     uint64_t GetDpmsState() const
98     {
99         return mDpmsState;
100     }
101     int32_t SetDpmsState(uint64_t dmps);
102     bool IsConnected() const;
103 
104     int32_t GetBrightness(uint32_t& level) const;
105     int32_t SetBrightness(uint32_t level);
106 
107 private:
108     static void ConvertTypeToName(uint32_t type, std::string &name);
109     static void ConvertToHdiType(uint32_t type, InterfaceType &hdiType);
110 
111     void InitModes(drmModeConnector c);
112     uint32_t mId;
113     InterfaceType mType;
114     uint32_t mPhyWidth;
115     uint32_t mPhyHeight;
116     uint32_t mSupportLayers = 0;
117     uint32_t mVirtualDispCount = 0;
118     bool mSupportWriteBack = false;
119     uint32_t mPropertyCount = 0;
120     uint32_t mEncoderId;
121     std::vector<uint32_t> mPossibleEncoders;
122     std::string mName;
123     drmModeConnection mConnectState;
124     uint32_t mPropDpmsId = DRM_INVALID_ID;
125     uint64_t mDpmsState = 0;
126     uint32_t mPropCrtcId = DRM_INVALID_ID;
127     uint32_t mPropBrightnessId = DRM_INVALID_ID;
128     uint32_t mBrightnessLevel = 0;
129     std::unordered_map<int32_t, DrmMode> mModes;
130     int32_t mPreferenceId = INVALID_MODE_ID;
131 
132     FdPtr mDrmFdPtr;
133 };
134 } // namespace OHOS
135 } // namespace HDI
136 } // namespace DISPLAY
137 #endif // DRM_CONNECTOR_H
138