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