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