1 /*
2 * Copyright (c) 2021-2022 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)24 void DrmMode::ConvertToHdiMode(DisplayModeInfo &hdiMode)
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,FdPtr & fd)32 DrmConnector::DrmConnector(drmModeConnector c, 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_DEBUGLOG("encoder_id %{public}d", mEncoderId);
41 DISPLAY_DEBUGLOG("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_DEBUGLOG("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_DEBUGLOG("name %{public}s", mName.c_str());
52 }
53
InitModes(drmModeConnector c)54 void DrmConnector::InitModes(drmModeConnector c)
55 {
56 DISPLAY_DEBUGLOG("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_DEBUGLOG("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_DEBUGLOG("set it to prefernce id %{public}d", i);
65 mPreferenceId = i;
66 }
67 mModes.emplace(i, DrmMode { *mode, i });
68 }
69 DISPLAY_DEBUGLOG("mode count %{public}zd", mModes.size());
70 }
71
Init(DrmDevice & drmDevice)72 int32_t DrmConnector::Init(DrmDevice &drmDevice)
73 {
74 int32_t ret;
75 DrmProperty prop;
76 DISPLAY_DEBUGLOG();
77 DISPLAY_CHK_RETURN((mDrmFdPtr == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("the mDrmFdPtr is NULL"));
78 DISPLAY_CHK_RETURN((mDrmFdPtr->GetFd() == -1), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is -1"));
79 // find dpms prop
80 ret = drmDevice.GetConnectorProperty(*this, PROP_DPMS, prop);
81 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get mode prop id"));
82 mPropDpmsId = prop.propId;
83 mDpmsState = prop.value;
84 DISPLAY_DEBUGLOG("dpms state : %{public}" PRIu64 "", mDpmsState);
85 // find the crtcid
86 ret = drmDevice.GetConnectorProperty(*this, PROP_CRTCID, prop);
87 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get out fence prop id"));
88 mPropCrtcId = prop.propId;
89 DISPLAY_DEBUGLOG("encoder_id %{public}d", mEncoderId);
90 DISPLAY_DEBUGLOG("mPropCrtcId %{public}d", mPropCrtcId);
91 // find the brightness
92 ret = drmDevice.GetConnectorProperty(*this, PROP_BRIGHTNESS, prop);
93 if (ret == DISPLAY_SUCCESS) {
94 mPropBrightnessId = prop.propId;
95 mBrightnessLevel = static_cast<uint32_t>(prop.value);
96 DISPLAY_DEBUGLOG("prop brightness is %{public}d, level is %{public}d", mPropBrightnessId, mBrightnessLevel);
97 } else {
98 DISPLAY_LOGW("can not get the brightness prop, can not set the brightness");
99 }
100 return DISPLAY_SUCCESS;
101 }
102
GetBrightness(uint32_t & level)103 int32_t DrmConnector::GetBrightness(uint32_t& level)
104 {
105 if (mPropBrightnessId == DRM_INVALID_ID) {
106 DISPLAY_LOGE("the prop id of brightness is invalid");
107 return DISPLAY_NOT_SUPPORT;
108 }
109 level = mBrightnessLevel;
110 return DISPLAY_SUCCESS;
111 }
112
113 const int BACKLIGHT_MIN = 10;
SetBrightness(uint32_t level)114 int32_t DrmConnector::SetBrightness(uint32_t level)
115 {
116 static int32_t brFd = 0;
117 const int32_t buffer_size = 10; /* buffer size */
118 char buffer[buffer_size];
119
120 DISPLAY_DEBUGLOG("set %{public}d", level);
121 if (mPropBrightnessId == DRM_INVALID_ID) {
122 DISPLAY_LOGE("the prop id of brightness is invalid");
123 return DISPLAY_NOT_SUPPORT;
124 }
125 if (brFd <= 0) {
126 brFd = open("/sys/class/backlight/backlight/brightness", O_RDWR);
127 if (brFd < 0) {
128 DISPLAY_LOGE("open brightness file failed\n");
129 return DISPLAY_NOT_SUPPORT;
130 }
131 }
132 errno_t ret = memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
133 if (ret != EOK) {
134 DISPLAY_LOGE("memset_s failed\n");
135 return DISPLAY_FAILURE;
136 }
137 if (level < BACKLIGHT_MIN) {
138 level = BACKLIGHT_MIN;
139 }
140 int bytes = sprintf_s(buffer, sizeof(buffer), "%d\n", level);
141 if (bytes < 0) {
142 DISPLAY_LOGE("change failed\n");
143 return DISPLAY_FAILURE;
144 }
145 write(brFd, buffer, bytes);
146 mBrightnessLevel = level;
147 return DISPLAY_SUCCESS;
148 }
149
GetDisplayCap(DisplayCapability & cap)150 void DrmConnector::GetDisplayCap(DisplayCapability &cap)
151 {
152 cap.phyHeight = mPhyHeight;
153 cap.phyWidth = mPhyWidth;
154 cap.type = mType;
155 memcpy_s(cap.name, sizeof(cap.name), mName.c_str(), mName.size());
156 if (mName.size() >= sizeof(cap.name)) {
157 cap.name[sizeof(cap.name) - 1] = 0;
158 } else {
159 cap.name[mName.size()] = 0;
160 }
161 cap.supportLayers = mSupportLayers;
162 cap.virtualDispCount = mVirtualDispCount;
163 cap.supportWriteBack = mSupportWriteBack;
164 cap.propertyCount = mPropertyCount;
165 }
166
ConvertTypeToName(uint32_t type,std::string & name)167 void DrmConnector::ConvertTypeToName(uint32_t type, std::string &name)
168 {
169 DISPLAY_DEBUGLOG("type %{public}d", type);
170 switch (type) {
171 case DISP_INTF_VGA:
172 name = "VGA";
173 break;
174 case DISP_INTF_HDMI:
175 name = "HDMI";
176 break;
177 case DISP_INTF_MIPI:
178 name = "MIPI";
179 break;
180 default:
181 name = "Unknown";
182 break;
183 }
184 }
185
ConvertToHdiType(uint32_t type,InterfaceType & hdiType)186 void DrmConnector::ConvertToHdiType(uint32_t type, InterfaceType &hdiType)
187 {
188 switch (type) {
189 case DRM_MODE_CONNECTOR_VGA:
190 hdiType = DISP_INTF_VGA;
191 break;
192 case DRM_MODE_CONNECTOR_DSI:
193 hdiType = DISP_INTF_MIPI;
194 break;
195 case DRM_MODE_CONNECTOR_HDMIA:
196 case DRM_MODE_CONNECTOR_HDMIB:
197 hdiType = DISP_INTF_HDMI;
198 break;
199 default:
200 hdiType = DISP_INTF_BUTT;
201 break;
202 }
203 }
TryPickEncoder(IdMapPtr<DrmEncoder> & encoders,uint32_t encoderId,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)204 int32_t DrmConnector::TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
205 uint32_t &crtcId)
206 {
207 int ret;
208 auto encoderIter = encoders.find(encoderId);
209 if (encoderIter == encoders.end()) {
210 DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderId);
211 return DISPLAY_FAILURE;
212 }
213
214 auto &encoder = encoderIter->second;
215 DISPLAY_DEBUGLOG("connector : %{public}d encoder : %{public}d", mId, encoder->GetId());
216 ret = encoder->PickIdleCrtcId(crtcs, crtcId);
217 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
218 DISPLAY_DEBUGLOG("connector : %{public}d pick encoder : %{public}d", mId, encoder->GetId()));
219 return DISPLAY_FAILURE;
220 }
221
PickIdleCrtcId(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)222 int32_t DrmConnector::PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
223 {
224 DISPLAY_DEBUGLOG();
225 DISPLAY_DEBUGLOG("encoder_id %{public}d", mEncoderId);
226 int ret = TryPickEncoder(encoders, mEncoderId, crtcs, crtcId);
227 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
228 DISPLAY_DEBUGLOG("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d",
229 mId, mEncoderId, crtcId));
230
231 for (auto encoder : mPossibleEncoders) {
232 ret = TryPickEncoder(encoders, encoder, crtcs, crtcId);
233 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
234 DISPLAY_DEBUGLOG("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d", mId, mEncoderId,
235 crtcId));
236 }
237
238 DISPLAY_LOGW("can not pick a crtc for connector");
239 return DISPLAY_FAILURE;
240 }
241
UpdateModes()242 int32_t DrmConnector::UpdateModes()
243 {
244 int drmFd = mDrmFdPtr->GetFd();
245 drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
246 DISPLAY_CHK_RETURN((c == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get connector"));
247 mConnectState = c->connection;
248 // init the modes
249 InitModes(*c);
250 drmModeFreeConnector(c);
251 return DISPLAY_SUCCESS;
252 }
253
HandleHotplug()254 bool DrmConnector::HandleHotplug()
255 {
256 int drmFd = mDrmFdPtr->GetFd();
257 drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
258 DISPLAY_CHK_RETURN((c == nullptr), false, DISPLAY_LOGE("can not get connector"));
259 if (mConnectState == c->connection) {
260 drmModeFreeConnector(c);
261 return false;
262 } else {
263 mConnectState = c->connection;
264 InitModes(*c);
265 drmModeFreeConnector(c);
266 return true;
267 }
268 }
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)269 int32_t DrmConnector::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
270 {
271 DISPLAY_CHK_RETURN((num == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("num is nullptr"));
272 *num = static_cast<int32_t>(mModes.size());
273 if (modes != nullptr) {
274 int i = 0;
275 for (const auto &modeMap : mModes) {
276 DrmMode mode = modeMap.second;
277 mode.ConvertToHdiMode(*(modes + i));
278 i++;
279 }
280 }
281 return DISPLAY_SUCCESS;
282 }
283
SetDpmsState(uint64_t dmps)284 int32_t DrmConnector::SetDpmsState(uint64_t dmps)
285 {
286 DISPLAY_DEBUGLOG("dmps %{public}" PRIu64 "", dmps);
287 int ret = drmModeConnectorSetProperty(mDrmFdPtr->GetFd(), mId, mPropDpmsId, dmps);
288 DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("can not set dpms"));
289 mDpmsState = dmps;
290 return DISPLAY_SUCCESS;
291 }
292
IsConnected()293 bool DrmConnector::IsConnected()
294 {
295 return (mConnectState == DRM_MODE_CONNECTED);
296 }
297
GetModeFromId(int32_t id,DrmMode & mode)298 int32_t DrmConnector::GetModeFromId(int32_t id, DrmMode &mode)
299 {
300 DISPLAY_DEBUGLOG();
301 auto iter = mModes.find(id);
302 if (iter == mModes.end()) {
303 return DISPLAY_FAILURE;
304 }
305 mode = mModes[id];
306 return DISPLAY_SUCCESS;
307 }
308
GetModeBlockFromId(int32_t id)309 std::unique_ptr<DrmModeBlock> DrmConnector::GetModeBlockFromId(int32_t id)
310 {
311 DISPLAY_DEBUGLOG("id %{public}d", id);
312 auto iter = mModes.find(id);
313 DISPLAY_CHK_RETURN((iter == mModes.end()), nullptr, DISPLAY_LOGE("can not the mode %{public}d", id));
314 return std::make_unique<DrmModeBlock>(mModes[id]);
315 }
316
DrmModeBlock(DrmMode & mode)317 DrmModeBlock::DrmModeBlock(DrmMode &mode)
318 {
319 DISPLAY_DEBUGLOG();
320 Init(mode);
321 }
322
Init(DrmMode & mode)323 int32_t DrmModeBlock::Init(DrmMode &mode)
324 {
325 int ret;
326 int drmFd;
327 drmFd = DrmDevice::GetDrmFd();
328 DISPLAY_CHK_RETURN((drmFd < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is invalid"));
329 drmModeModeInfo modeInfo = *(mode.GetModeInfoPtr());
330 ret = drmModeCreatePropertyBlob(drmFd, static_cast<void *>(&modeInfo), sizeof(modeInfo), &mBlockId);
331 DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("create property blob failed"));
332 DISPLAY_DEBUGLOG("mBlockId %{public}d", mBlockId);
333 return DISPLAY_SUCCESS;
334 }
335
~DrmModeBlock()336 DrmModeBlock::~DrmModeBlock()
337 {
338 DISPLAY_DEBUGLOG("mBlockId %{public}d", mBlockId);
339 int drmFd;
340 drmFd = DrmDevice::GetDrmFd();
341 if ((mBlockId != DRM_INVALID_ID) && (drmFd >= 0)) {
342 int ret = drmModeDestroyPropertyBlob(drmFd, mBlockId);
343 if (ret != 0) {
344 DISPLAY_LOGE("destroy property blob failed errno %{public}d", errno);
345 }
346 } else {
347 DISPLAY_LOGE("can not destruct the block id %{public}d drmFd %{public}d", mBlockId, drmFd);
348 }
349 }
350 } // namespace OHOS
351 } // namespace HDI
352 } // namespace DISPLAY
353