• 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 #include "drm_connector.h"
17 #include <cinttypes>
18 #include <securec.h>
19 #include "drm_device.h"
20 
21 namespace OHOS {
22 namespace HDI {
23 namespace DISPLAY {
ConvertToHdiMode(DisplayModeInfo & hdiMode) const24 void DrmMode::ConvertToHdiMode(DisplayModeInfo &hdiMode) const
25 {
26     hdiMode.height = mModeInfo.vdisplay;
27     hdiMode.width = mModeInfo.hdisplay;
28     hdiMode.freshRate = mModeInfo.vrefresh;
29     hdiMode.id = mId;
30 }
31 
DrmConnector(drmModeConnector c,const FdPtr & fd)32 DrmConnector::DrmConnector(drmModeConnector c, const FdPtr &fd)
33     : mId(c.connector_id),
34       mPhyWidth(c.mmWidth),
35       mPhyHeight(c.mmHeight),
36       mEncoderId(c.encoder_id),
37       mConnectState(c.connection),
38       mDrmFdPtr(fd)
39 {
40     DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
41     DISPLAY_LOGD("the connect state is %{public}d", mConnectState);
42 
43     for (int i = 0; i < c.count_encoders; i++) {
44         mPossibleEncoders.push_back(c.encoders[i]);
45         DISPLAY_LOGD("add possible encoder id %{public}d", c.encoders[i]);
46     }
47 
48     ConvertToHdiType(c.connector_type, mType);
49     ConvertTypeToName(mType, mName);
50     InitModes(c);
51     DISPLAY_LOGD("name %{public}s", mName.c_str());
52 }
53 
InitModes(drmModeConnector c)54 void DrmConnector::InitModes(drmModeConnector c)
55 {
56     DISPLAY_LOGD("id %{public}d", mId);
57     mModes.clear();
58     mPreferenceId = INVALID_MODE_ID;
59     for (int i = 0; i < c.count_modes; i++) {
60         drmModeModeInfoPtr mode = c.modes + i;
61         DISPLAY_LOGD("mode: hdisplay %{public}d, vdisplay %{public}d vrefresh %{public}d type %{public}d",
62             mode->hdisplay, mode->vdisplay, mode->vrefresh, mode->type);
63         if ((mPreferenceId == INVALID_MODE_ID) && (mode->type & DRM_MODE_TYPE_PREFERRED)) {
64             DISPLAY_LOGD("set it to preference id %{public}d", i);
65             mPreferenceId = i;
66         }
67         mModes.emplace(i, DrmMode { *mode, i });
68     }
69     DISPLAY_LOGD("mode count %{public}zd", mModes.size());
70 }
71 
Init(DrmDevice & drmDevice)72 int32_t DrmConnector::Init(DrmDevice &drmDevice)
73 {
74     DrmProperty prop;
75     DISPLAY_LOGD();
76     DISPLAY_CHK_RETURN((mDrmFdPtr == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("the mDrmFdPtr is NULL"));
77     DISPLAY_CHK_RETURN((mDrmFdPtr->GetFd() == -1), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is -1"));
78     // find dpms prop
79     int32_t ret = drmDevice.GetConnectorProperty(*this, PROP_DPMS, prop);
80     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get mode prop id"));
81     mPropDpmsId = prop.propId;
82     mDpmsState = prop.value;
83     DISPLAY_LOGD("dpms state : %{public}" PRIu64 "", mDpmsState);
84     // find the crtcid
85     ret = drmDevice.GetConnectorProperty(*this, PROP_CRTCID, prop);
86     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get out fence prop id"));
87     mPropCrtcId = prop.propId;
88     DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
89     DISPLAY_LOGD("mPropCrtcId %{public}d", mPropCrtcId);
90     // find the brightness
91     ret = drmDevice.GetConnectorProperty(*this, PROP_BRIGHTNESS, prop);
92     if (ret == DISPLAY_SUCCESS) {
93         mPropBrightnessId =  prop.propId;
94         mBrightnessLevel = static_cast<uint32_t>(prop.value);
95         DISPLAY_LOGD("prop brightness is %{public}d, level is %{public}d", mPropBrightnessId, mBrightnessLevel);
96     } else {
97         DISPLAY_LOGW("can not get the brightness prop, can not set the brightness");
98     }
99     return DISPLAY_SUCCESS;
100 }
101 
GetBrightness(uint32_t & level) const102 int32_t DrmConnector::GetBrightness(uint32_t &level) const
103 {
104     if (mPropBrightnessId == DRM_INVALID_ID) {
105         DISPLAY_LOGE("the prop id of brightness is invalid");
106         return DISPLAY_NOT_SUPPORT;
107     }
108     level = mBrightnessLevel;
109     return DISPLAY_SUCCESS;
110 }
111 
SetBrightness(uint32_t level)112 int32_t DrmConnector::SetBrightness(uint32_t level)
113 {
114     DISPLAY_LOGD("set %{public}d", level);
115     if (mPropBrightnessId == DRM_INVALID_ID) {
116         DISPLAY_LOGE("the prop id of brightness is invalid");
117         return DISPLAY_NOT_SUPPORT;
118     }
119     int ret = drmModeConnectorSetProperty(mDrmFdPtr->GetFd(), mId, mPropBrightnessId, level);
120     DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("can not set dpms"));
121     mBrightnessLevel = level;
122     return DISPLAY_SUCCESS;
123 }
124 
GetDisplayCap(DisplayCapability & cap)125 void DrmConnector::GetDisplayCap(DisplayCapability &cap)
126 {
127     cap.phyHeight = mPhyHeight;
128     cap.phyWidth = mPhyWidth;
129     cap.type = mType;
130     memcpy_s(cap.name, sizeof(cap.name), mName.c_str(), mName.size());
131     if (mName.size() >= sizeof(cap.name)) {
132         cap.name[sizeof(cap.name) - 1] = 0;
133     } else {
134         cap.name[mName.size()] = 0;
135     }
136     cap.supportLayers = mSupportLayers;
137     cap.virtualDispCount = mVirtualDispCount;
138     cap.supportWriteBack = mSupportWriteBack;
139     cap.propertyCount = mPropertyCount;
140 }
141 
ConvertTypeToName(uint32_t type,std::string & name)142 void DrmConnector::ConvertTypeToName(uint32_t type, std::string &name)
143 {
144     DISPLAY_LOGD("type %{public}d", type);
145     switch (type) {
146         case DISP_INTF_VGA:
147             name = "VGA";
148             break;
149         case DISP_INTF_HDMI:
150             name = "HDMI";
151             break;
152         case DISP_INTF_MIPI:
153             name = "MIPI";
154             break;
155         default:
156             name = "Unknown";
157             break;
158     }
159 }
160 
ConvertToHdiType(uint32_t type,InterfaceType & hdiType)161 void DrmConnector::ConvertToHdiType(uint32_t type, InterfaceType &hdiType)
162 {
163     switch (type) {
164         case DRM_MODE_CONNECTOR_VGA:
165             hdiType = DISP_INTF_VGA;
166             break;
167         case DRM_MODE_CONNECTOR_DSI:
168             hdiType = DISP_INTF_MIPI;
169             break;
170         case DRM_MODE_CONNECTOR_HDMIA:
171         case DRM_MODE_CONNECTOR_HDMIB:
172             hdiType = DISP_INTF_HDMI;
173             break;
174         default:
175             hdiType = DISP_INTF_BUTT;
176             break;
177     }
178 }
TryPickEncoder(IdMapPtr<DrmEncoder> & encoders,uint32_t encoderId,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)179 int32_t DrmConnector::TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
180     uint32_t &crtcId)
181 {
182     auto encoderIter = encoders.find(encoderId);
183     if (encoderIter == encoders.end()) {
184         DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderId);
185         return DISPLAY_FAILURE;
186     }
187 
188     auto &encoder = encoderIter->second;
189     DISPLAY_LOGD("connector : %{public}d encoder : %{public}d", mId, encoder->GetId());
190     int32_t ret = encoder->PickIdleCrtcId(crtcs, crtcId);
191     DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
192         DISPLAY_LOGD("connector : %{public}d pick encoder : %{public}d", mId, encoder->GetId()));
193     return DISPLAY_FAILURE;
194 }
195 
PickIdleCrtcId(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)196 int32_t DrmConnector::PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
197 {
198     DISPLAY_LOGD();
199     DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
200     int ret = TryPickEncoder(encoders, mEncoderId, crtcs, crtcId);
201     DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
202         DISPLAY_LOGD("connector : %{public}d pick encoder : %{public}d crtcId : %{public}d",
203         mId, mEncoderId, crtcId));
204 
205     for (auto encoder : mPossibleEncoders) {
206         ret = TryPickEncoder(encoders, encoder, crtcs, crtcId);
207         DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
208             DISPLAY_LOGD("connector : %{public}d pick encoder : %{public}d crtcId : %{public}d", mId, mEncoderId,
209             crtcId));
210     }
211 
212     DISPLAY_LOGW("can not pick a crtc for connector");
213     return DISPLAY_FAILURE;
214 }
215 
UpdateModes()216 int32_t DrmConnector::UpdateModes()
217 {
218     int drmFd = mDrmFdPtr->GetFd();
219     drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
220     DISPLAY_CHK_RETURN((c == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get connector"));
221     mConnectState = c->connection;
222     // init the modes
223     InitModes(*c);
224     drmModeFreeConnector(c);
225     return DISPLAY_SUCCESS;
226 }
227 
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)228 int32_t DrmConnector::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
229 {
230     DISPLAY_CHK_RETURN((num == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("num is nullptr"));
231     *num = static_cast<int32_t>(mModes.size());
232     if (modes != nullptr) {
233         int i = 0;
234         for (const auto &modeMap : mModes) {
235             DrmMode mode = modeMap.second;
236             mode.ConvertToHdiMode(*(modes + i));
237             i++;
238         }
239     }
240     return DISPLAY_SUCCESS;
241 }
242 
SetDpmsState(uint64_t dmps)243 int32_t DrmConnector::SetDpmsState(uint64_t dmps)
244 {
245     DISPLAY_LOGD("dmps %{public}" PRIu64 "", dmps);
246     int ret = drmModeConnectorSetProperty(mDrmFdPtr->GetFd(), mId, mPropDpmsId, dmps);
247     DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("can not set dpms"));
248     mDpmsState = dmps;
249     return DISPLAY_SUCCESS;
250 }
251 
IsConnected() const252 bool DrmConnector::IsConnected() const
253 {
254     return (mConnectState == DRM_MODE_CONNECTED);
255 }
256 
GetModeFromId(int32_t id,DrmMode & mode)257 int32_t DrmConnector::GetModeFromId(int32_t id, DrmMode &mode)
258 {
259     DISPLAY_LOGD();
260     auto iter = mModes.find(id);
261     if (iter == mModes.end()) {
262         return DISPLAY_FAILURE;
263     }
264     mode = mModes[id];
265     return DISPLAY_SUCCESS;
266 }
267 
GetModeBlockFromId(int32_t id)268 std::unique_ptr<DrmModeBlock> DrmConnector::GetModeBlockFromId(int32_t id)
269 {
270     DISPLAY_LOGD("id %{public}d", id);
271     auto iter = mModes.find(id);
272     DISPLAY_CHK_RETURN((iter == mModes.end()), nullptr, DISPLAY_LOGE("can not the mode %{public}d", id));
273     return std::make_unique<DrmModeBlock>(mModes[id]);
274 }
275 
DrmModeBlock(DrmMode & mode)276 DrmModeBlock::DrmModeBlock(DrmMode &mode)
277 {
278     DISPLAY_LOGD();
279     Init(mode);
280 }
281 
Init(DrmMode & mode)282 int32_t DrmModeBlock::Init(DrmMode &mode)
283 {
284     int drmFd = DrmDevice::GetDrmFd();
285     DISPLAY_CHK_RETURN((drmFd < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is invalid"));
286     drmModeModeInfo modeInfo = *(mode.GetModeInfoPtr());
287     int ret = drmModeCreatePropertyBlob(drmFd, static_cast<void *>(&modeInfo), sizeof(modeInfo), &mBlockId);
288     DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("create property blob failed"));
289     DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
290     return DISPLAY_SUCCESS;
291 }
292 
~DrmModeBlock()293 DrmModeBlock::~DrmModeBlock()
294 {
295     DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
296     int drmFd = DrmDevice::GetDrmFd();
297     if ((mBlockId != DRM_INVALID_ID) && (drmFd >= 0)) {
298         int ret = drmModeDestroyPropertyBlob(drmFd, mBlockId);
299         if (ret != 0) {
300             DISPLAY_LOGE("destroy property blob failed errno %{public}d", errno);
301         }
302     } else {
303         DISPLAY_LOGE("can not destruct the block id %{public}d drmFd %{public}d", mBlockId, drmFd);
304     }
305 }
306 } // namespace OHOS
307 } // namespace HDI
308 } // namespace DISPLAY
309