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 <xf86drm.h>
18 #include <xf86drmMode.h>
19 #include <cinttypes>
20 #include <securec.h>
21 #include "display_log.h"
22 #include "drm_device.h"
23 #include "drm_vsync_worker.h"
24
25 namespace OHOS {
26 namespace HDI {
27 namespace DISPLAY {
ConvertToHdiMode(DisplayModeInfo & hdiMode)28 void DrmMode::ConvertToHdiMode(DisplayModeInfo &hdiMode)
29 {
30 hdiMode.height = mModeInfo.vdisplay;
31 hdiMode.width = mModeInfo.hdisplay;
32 hdiMode.freshRate = mModeInfo.vrefresh;
33 hdiMode.id = mId;
34 }
35
DrmConnector(drmModeConnector c,FdPtr & fd)36 DrmConnector::DrmConnector(drmModeConnector c, FdPtr &fd)
37 : mId(c.connector_id),
38 mPhyWidth(c.mmWidth),
39 mPhyHeight(c.mmHeight),
40 mEncoderId(c.encoder_id),
41 mConnectState(c.connection),
42 mDrmFdPtr(fd)
43 {
44 DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
45 DISPLAY_LOGD("the connect state is %{public}d", mConnectState);
46
47 for (int i = 0; i < c.count_encoders; i++) {
48 mPossibleEncoders.push_back(c.encoders[i]);
49 DISPLAY_LOGD("add possible encoder id %{public}d", c.encoders[i]);
50 }
51
52 ConvertToHdiType(c.connector_type, mType);
53 ConvertTypeToName(mType, mName);
54 InitModes(c);
55 DISPLAY_LOGD("name %{public}s", mName.c_str());
56 }
57
InitModes(drmModeConnector c)58 void DrmConnector::InitModes(drmModeConnector c)
59 {
60 DISPLAY_LOGD("id %{public}d mode size %{public}d", mId, c.count_modes);
61 mModes.clear();
62 mPreferenceId = INVALID_MODE_ID;
63 for (int i = 0; i < c.count_modes; i++) {
64 drmModeModeInfoPtr mode = c.modes + i;
65 DISPLAY_LOGD("mode: hdisplay %{public}d, vdisplay %{public}d vrefresh %{public}d type %{public}d",
66 mode->hdisplay, mode->vdisplay, mode->vrefresh, mode->type);
67 if ((mPreferenceId == INVALID_MODE_ID) && (mode->type & DRM_MODE_TYPE_PREFERRED)) {
68 DISPLAY_LOGD("set it to prefernce id %{public}d", i);
69 mPreferenceId = i;
70 }
71 mModes.emplace(i, DrmMode { *mode, i });
72 }
73 DISPLAY_LOGD("mode count %{public}zd", mModes.size());
74 }
75
Init(DrmDevice & drmDevice)76 int32_t DrmConnector::Init(DrmDevice &drmDevice)
77 {
78 int32_t ret;
79 DrmProperty prop;
80 DISPLAY_LOGD();
81 DISPLAY_CHK_RETURN((mDrmFdPtr == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("the mDrmFdPtr is NULL"));
82 DISPLAY_CHK_RETURN((mDrmFdPtr->GetFd() == -1), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is -1"));
83 // find dpms prop
84 ret = drmDevice.GetConnectorProperty(*this, PROP_DPMS, prop);
85 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get mode prop id"));
86 mPropDpmsId = prop.propId;
87 mDpmsState = prop.value;
88 DISPLAY_LOGD("dpms state : %{public}" PRIu64 "", mDpmsState);
89 // find the crtcid
90 ret = drmDevice.GetConnectorProperty(*this, PROP_CRTCID, prop);
91 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get out fence prop id"));
92 mPropCrtcId = prop.propId;
93 DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
94 DISPLAY_LOGD("mPropCrtcId %{public}d", mPropCrtcId);
95 // find the brightness
96 ret = drmDevice.GetConnectorProperty(*this, PROP_BRIGHTNESS, prop);
97 if (ret == DISPLAY_SUCCESS) {
98 mPropBrightnessId = prop.propId;
99 mBrightnessLevel = static_cast<uint32_t>(prop.value);
100 DISPLAY_LOGD("prop brightness is %{public}d, level is %{public}d", mPropBrightnessId, mBrightnessLevel);
101 } else {
102 DISPLAY_LOGW("can not get the brightness prop, can not set the brightness");
103 }
104 return DISPLAY_SUCCESS;
105 }
106
GetBrightness(uint32_t & level)107 int32_t DrmConnector::GetBrightness(uint32_t& level)
108 {
109 if (mPropBrightnessId == DRM_INVALID_ID) {
110 DISPLAY_LOGE("the prop id of brightness is invalid");
111 return DISPLAY_NOT_SUPPORT;
112 }
113 level = mBrightnessLevel;
114 return DISPLAY_SUCCESS;
115 }
116
117 const int BACKLIGHT_MIN = 10;
SetBrightness(uint32_t level)118 int32_t DrmConnector::SetBrightness(uint32_t level)
119 {
120 static int32_t brFd = 0;
121 const int32_t buffer_size = 10; /* buffer size */
122 char buffer[buffer_size];
123
124 DISPLAY_LOGD("set %{public}d", level);
125 if (mPropBrightnessId == DRM_INVALID_ID) {
126 DISPLAY_LOGE("the prop id of brightness is invalid");
127 return DISPLAY_NOT_SUPPORT;
128 }
129 if (brFd <= 0) {
130 brFd = open("/sys/class/backlight/backlight/brightness", O_RDWR);
131 if (brFd < 0) {
132 DISPLAY_LOGE("open brightness file failed\n");
133 return DISPLAY_NOT_SUPPORT;
134 }
135 }
136 errno_t ret = memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
137 if (ret != EOK) {
138 DISPLAY_LOGE("memset_s failed\n");
139 return DISPLAY_FAILURE;
140 }
141 if (level < BACKLIGHT_MIN) {
142 level = BACKLIGHT_MIN;
143 }
144 int bytes = sprintf_s(buffer, sizeof(buffer), "%d\n", level);
145 if (bytes < 0) {
146 DISPLAY_LOGE("change failed\n");
147 return DISPLAY_FAILURE;
148 }
149 write(brFd, buffer, bytes);
150 mBrightnessLevel = level;
151 return DISPLAY_SUCCESS;
152 }
153
GetDisplayCap(DisplayCapability & cap)154 void DrmConnector::GetDisplayCap(DisplayCapability &cap)
155 {
156 cap.phyHeight = mPhyHeight;
157 cap.phyWidth = mPhyWidth;
158 cap.type = mType;
159 memcpy_s(const_cast<char*>(cap.name.c_str()), cap.name.size(), mName.c_str(), mName.size());
160 if (mName.size() >= sizeof(cap.name)) {
161 cap.name[sizeof(cap.name) - 1] = 0;
162 } else {
163 cap.name[mName.size()] = 0;
164 }
165 cap.supportLayers = mSupportLayers;
166 cap.virtualDispCount = mVirtualDispCount;
167 cap.supportWriteBack = mSupportWriteBack;
168 cap.propertyCount = mPropertyCount;
169 }
170
ConvertTypeToName(uint32_t type,std::string & name)171 void DrmConnector::ConvertTypeToName(uint32_t type, std::string &name)
172 {
173 DISPLAY_LOGD("type %{public}d", type);
174 switch (type) {
175 case DISP_INTF_VGA:
176 name = "VGA";
177 break;
178 case DISP_INTF_HDMI:
179 name = "HDMI";
180 break;
181 case DISP_INTF_MIPI:
182 name = "MIPI";
183 break;
184 default:
185 name = "Unknown";
186 break;
187 }
188 }
189
ConvertToHdiType(uint32_t type,InterfaceType & hdiType)190 void DrmConnector::ConvertToHdiType(uint32_t type, InterfaceType &hdiType)
191 {
192 switch (type) {
193 case DRM_MODE_CONNECTOR_VGA:
194 hdiType = DISP_INTF_VGA;
195 break;
196 case DRM_MODE_CONNECTOR_DSI:
197 hdiType = DISP_INTF_MIPI;
198 break;
199 case DRM_MODE_CONNECTOR_HDMIA:
200 case DRM_MODE_CONNECTOR_HDMIB:
201 hdiType = DISP_INTF_HDMI;
202 break;
203 default:
204 hdiType = DISP_INTF_BUTT;
205 break;
206 }
207 }
TryPickEncoder(IdMapPtr<DrmEncoder> & encoders,uint32_t encoderId,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)208 int32_t DrmConnector::TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
209 uint32_t &crtcId)
210 {
211 int ret;
212 auto encoderIter = encoders.find(encoderId);
213 if (encoderIter == encoders.end()) {
214 DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderId);
215 return DISPLAY_FAILURE;
216 }
217
218 auto &encoder = encoderIter->second;
219 DISPLAY_LOGD("connector : %{public}d encoder : %{public}d", mId, encoder->GetId());
220 ret = encoder->PickIdleCrtcId(crtcs, crtcId);
221 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
222 DISPLAY_LOGD("connector : %{public}d pick encoder : %{public}d", mId, encoder->GetId()));
223 return DISPLAY_FAILURE;
224 }
225
PickIdleCrtcId(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)226 int32_t DrmConnector::PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
227 {
228 DISPLAY_LOGD();
229 DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
230 int ret = TryPickEncoder(encoders, mEncoderId, crtcs, crtcId);
231 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
232 DISPLAY_LOGD("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d",
233 mId, mEncoderId, crtcId));
234
235 for (auto encoder : mPossibleEncoders) {
236 ret = TryPickEncoder(encoders, encoder, crtcs, crtcId);
237 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
238 DISPLAY_LOGD("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d", mId, mEncoderId,
239 crtcId));
240 }
241
242 DISPLAY_LOGW("can not pick a crtc for connector");
243 return DISPLAY_FAILURE;
244 }
245
UpdateModes()246 int32_t DrmConnector::UpdateModes()
247 {
248 int drmFd = mDrmFdPtr->GetFd();
249 drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
250 DISPLAY_CHK_RETURN((c == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get connector"));
251 mConnectState = c->connection;
252 // init the modes
253 InitModes(*c);
254 drmModeFreeConnector(c);
255 return DISPLAY_SUCCESS;
256 }
257
UpdateCrtcId(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,bool plugIn,drmModeConnectorPtr c,int * crtc_id)258 std::shared_ptr<DrmCrtc> DrmConnector::UpdateCrtcId(IdMapPtr<DrmEncoder> &encoders,
259 IdMapPtr<DrmCrtc> &crtcs, bool plugIn, drmModeConnectorPtr c, int *crtc_id)
260 {
261 std::shared_ptr<DrmCrtc> crtc = nullptr;
262 int encoderid = c->encoders[0];
263 auto encoderIter = encoders.find(encoderid);
264 if (encoderIter == encoders.end()) {
265 DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderid);
266 return crtc;
267 }
268
269 auto &encoder = encoderIter->second;
270 int possibleCrtcs = encoder->GetPossibleCrtcs();
271
272 for (auto crtcIter = crtcs.begin(); crtcIter != crtcs.end(); ++crtcIter) {
273 auto &posCrts = crtcIter->second;
274 if (possibleCrtcs == (1<<posCrts->GetPipe())) {
275 DISPLAY_LOGD("find crtc id %{public}d, pipe %{public}d", posCrts->GetId(), posCrts->GetPipe());
276 crtc = posCrts;
277 *crtc_id = posCrts->GetId();
278 }
279 }
280 if (plugIn) {
281 encoder->SetCrtcId(*crtc_id);
282 mEncoderId = c->encoders[0];
283 } else if (!plugIn) {
284 *crtc_id = 0;
285 mEncoderId = 0;
286 encoder->SetCrtcId(0);
287 }
288 return crtc;
289 }
290
HandleHotplug(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,bool plugIn)291 bool DrmConnector::HandleHotplug(IdMapPtr<DrmEncoder> &encoders,
292 IdMapPtr<DrmCrtc> &crtcs, bool plugIn)
293 {
294 DISPLAY_LOGD("plug %{public}d", plugIn);
295 int drmFd = mDrmFdPtr->GetFd();
296 int crtc_id = 0;
297 std::shared_ptr<DrmCrtc> crtc;
298 uint32_t blob_id;
299 drmModeAtomicReq *pset = drmModeAtomicAlloc();
300 DISPLAY_CHK_RETURN((pset == nullptr), DISPLAY_NULL_PTR,
301 DISPLAY_LOGE("drm atomic alloc failed errno %{public}d", errno));
302
303 drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
304 DISPLAY_CHK_RETURN((c == nullptr), false, DISPLAY_LOGE("can not get connector"));
305 if (mConnectState == c->connection) {
306 drmModeFreeConnector(c);
307 return false;
308 } else {
309 crtc = UpdateCrtcId(encoders, crtcs, plugIn, c, &crtc_id);
310 if (crtc == nullptr) {
311 return DISPLAY_FAILURE;
312 }
313 DISPLAY_LOGD("get crtc id %{public}d ", crtc_id);
314
315 DrmVsyncWorker::GetInstance().EnableVsync(plugIn);
316 drmModeCreatePropertyBlob(drmFd, &c->modes[0],
317 sizeof(c->modes[0]), &blob_id);
318 int ret = drmModeAtomicAddProperty(pset, crtc->GetId(), crtc->GetActivePropId(), (int)plugIn);
319 ret |= drmModeAtomicAddProperty(pset, crtc->GetId(), crtc->GetModePropId(), blob_id);
320 ret |= drmModeAtomicAddProperty(pset, GetId(), GetPropCrtcId(), crtc_id);
321 DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE,
322 DISPLAY_LOGE("can not add the crtc id prop %{public}d", errno));
323
324 ret = drmModeAtomicCommit(drmFd, pset, DRM_MODE_ATOMIC_ALLOW_MODESET, nullptr);
325 DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE,
326 DISPLAY_LOGE("can not add the crtc id prop %{public}d", errno));
327 drmModeAtomicFree(pset);
328
329 mConnectState = c->connection;
330 InitModes(*c);
331 drmModeFreeConnector(c);
332 return true;
333 }
334 }
335
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)336 int32_t DrmConnector::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
337 {
338 DISPLAY_CHK_RETURN((num == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("num is nullptr"));
339 *num = static_cast<int32_t>(mModes.size());
340 if (modes != nullptr) {
341 int i = 0;
342 for (const auto &modeMap : mModes) {
343 DrmMode mode = modeMap.second;
344 mode.ConvertToHdiMode(*(modes + i));
345 i++;
346 }
347 }
348 return DISPLAY_SUCCESS;
349 }
350
SetDpmsState(uint64_t dmps)351 int32_t DrmConnector::SetDpmsState(uint64_t dmps)
352 {
353 DISPLAY_LOGD("dmps %{public}" PRIu64 "", dmps);
354 int ret = drmModeConnectorSetProperty(mDrmFdPtr->GetFd(), mId, mPropDpmsId, dmps);
355 DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("can not set dpms"));
356 mDpmsState = dmps;
357 return DISPLAY_SUCCESS;
358 }
359
IsConnected()360 bool DrmConnector::IsConnected()
361 {
362 return (mConnectState == DRM_MODE_CONNECTED);
363 }
364
GetModeFromId(int32_t id,DrmMode & mode)365 int32_t DrmConnector::GetModeFromId(int32_t id, DrmMode &mode)
366 {
367 DISPLAY_LOGD();
368 auto iter = mModes.find(id);
369 if (iter == mModes.end()) {
370 return DISPLAY_FAILURE;
371 }
372 mode = mModes[id];
373 return DISPLAY_SUCCESS;
374 }
375
GetModeBlockFromId(int32_t id)376 std::unique_ptr<DrmModeBlock> DrmConnector::GetModeBlockFromId(int32_t id)
377 {
378 DISPLAY_LOGD("id %{public}d", id);
379 auto iter = mModes.find(id);
380 DISPLAY_CHK_RETURN((iter == mModes.end()), nullptr, DISPLAY_LOGE("can not the mode %{public}d", id));
381 return std::make_unique<DrmModeBlock>(mModes[id]);
382 }
383
DrmModeBlock(DrmMode & mode)384 DrmModeBlock::DrmModeBlock(DrmMode &mode)
385 {
386 DISPLAY_LOGD();
387 Init(mode);
388 }
389
Init(DrmMode & mode)390 int32_t DrmModeBlock::Init(DrmMode &mode)
391 {
392 int ret;
393 int drmFd = DrmDevice::GetDrmFd();
394 DISPLAY_CHK_RETURN((drmFd < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is invalid"));
395 drmModeModeInfo modeInfo = *(mode.GetModeInfoPtr());
396 ret = drmModeCreatePropertyBlob(drmFd, static_cast<void *>(&modeInfo), sizeof(modeInfo), &mBlockId);
397 DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("create property blob failed"));
398 DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
399 return DISPLAY_SUCCESS;
400 }
401
~DrmModeBlock()402 DrmModeBlock::~DrmModeBlock()
403 {
404 DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
405 int drmFd = DrmDevice::GetDrmFd();
406 if ((mBlockId != DRM_INVALID_ID) && (drmFd >= 0)) {
407 int ret = drmModeDestroyPropertyBlob(drmFd, mBlockId);
408 if (ret != 0) {
409 DISPLAY_LOGE("destroy property blob failed errno %{public}d", errno);
410 }
411 } else {
412 DISPLAY_LOGE("can not destruct the block id %{public}d drmFd %{public}d", mBlockId, drmFd);
413 }
414 }
415 } // namespace OHOS
416 } // namespace HDI
417 } // namespace DISPLAY
418