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 #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", mId);
61 mModes.clear();
62 mPreferenceId = -1;
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 == -1) && (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 nullptr"));
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 ((mType == DISP_INTF_HDMI) && (mPropBrightnessId == 0xFFFFFFFF)) {
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
SetBrightness(uint32_t level)117 int32_t DrmConnector::SetBrightness(uint32_t level)
118 {
119 DISPLAY_LOGD("set %{public}d", level);
120 int32_t brFd = 0;
121 const int32_t buffer_size = 10;
122 char buffer[buffer_size];
123
124 if ((mType == DISP_INTF_HDMI) && (mPropBrightnessId == 0xFFFFFFFF)) {
125 DISPLAY_LOGE("the prop id of brightness is invalid");
126 return DISPLAY_NOT_SUPPORT;
127 }
128
129 brFd = open("/sys/class/brightness/brightness", O_RDWR);
130 if (brFd < 0) {
131 DISPLAY_LOGE("oepn Brightness file failed\n");
132 return DISPLAY_NOT_SUPPORT;
133 }
134
135 errno_t ret = memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
136 if (ret != EOK) {
137 DISPLAY_LOGE("memset_s failed\n");
138 close(brFd);
139 return DISPLAY_FAILURE;
140 }
141
142 int bytes = sprintf_s(buffer, sizeof(buffer), "%d\n", level);
143 if (bytes < 0) {
144 DISPLAY_LOGE("change failed\n");
145 close(brFd);
146 return DISPLAY_FAILURE;
147 }
148
149 write(brFd, buffer, bytes);
150 close(brFd);
151
152 mBrightnessLevel = level;
153 return DISPLAY_SUCCESS;
154 }
155
GetDisplayCap(DisplayCapability & cap)156 void DrmConnector::GetDisplayCap(DisplayCapability &cap)
157 {
158 cap.phyHeight = mPhyHeight;
159 cap.phyWidth = mPhyWidth;
160 cap.type = mType;
161 memcpy_s(const_cast<char*>(cap.name.c_str()), cap.name.size(), mName.c_str(), mName.size());
162 if (mName.size() >= sizeof(cap.name)) {
163 cap.name[sizeof(cap.name) - 1] = 0;
164 } else {
165 cap.name[mName.size()] = 0;
166 }
167 cap.supportLayers = mSupportLayers;
168 cap.virtualDispCount = mVirtualDispCount;
169 cap.supportWriteBack = mSupportWriteBack;
170 cap.propertyCount = mPropertyCount;
171 }
172
ConvertTypeToName(uint32_t type,std::string & name)173 void DrmConnector::ConvertTypeToName(uint32_t type, std::string &name)
174 {
175 DISPLAY_LOGD("type %{public}d", type);
176 switch (type) {
177 case DISP_INTF_VGA:
178 name = "VGA";
179 break;
180 case DISP_INTF_HDMI:
181 name = "HDMI";
182 break;
183 case DISP_INTF_MIPI:
184 name = "MIPI";
185 break;
186 default:
187 name = "Unknown";
188 break;
189 }
190 }
191
ConvertToHdiType(uint32_t type,InterfaceType & hdiType)192 void DrmConnector::ConvertToHdiType(uint32_t type, InterfaceType &hdiType)
193 {
194 switch (type) {
195 case DRM_MODE_CONNECTOR_VGA:
196 hdiType = DISP_INTF_VGA;
197 break;
198 case DRM_MODE_CONNECTOR_DSI:
199 hdiType = DISP_INTF_MIPI;
200 break;
201 case DRM_MODE_CONNECTOR_HDMIA:
202 case DRM_MODE_CONNECTOR_HDMIB:
203 hdiType = DISP_INTF_HDMI;
204 break;
205 default:
206 hdiType = DISP_INTF_BUTT;
207 break;
208 }
209 }
TryPickEncoder(IdMapPtr<DrmEncoder> & encoders,uint32_t encoderId,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)210 int32_t DrmConnector::TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
211 uint32_t &crtcId)
212 {
213 int ret;
214 auto encoderIter = encoders.find(encoderId);
215 if (encoderIter == encoders.end()) {
216 DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderId);
217 return DISPLAY_FAILURE;
218 }
219
220 auto &encoder = encoderIter->second;
221 DISPLAY_LOGD("connector : %{public}d encoder : %{public}d", mId, encoder->GetId());
222 ret = encoder->PickIdleCrtcId(crtcs, crtcId);
223 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
224 DISPLAY_LOGD("connector : %{public}d pick encoder : %{public}d", mId, encoder->GetId()));
225 return DISPLAY_FAILURE;
226 }
227
PickIdleCrtcId(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)228 int32_t DrmConnector::PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
229 {
230 DISPLAY_LOGD();
231 DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
232 int ret = TryPickEncoder(encoders, mEncoderId, crtcs, crtcId);
233 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
234 DISPLAY_LOGD("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d",
235 mId, mEncoderId, crtcId));
236
237 for (auto encoder : mPossibleEncoders) {
238 ret = TryPickEncoder(encoders, encoder, crtcs, crtcId);
239 DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
240 DISPLAY_LOGD("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d", mId, mEncoderId,
241 crtcId));
242 }
243
244 DISPLAY_LOGW("can not pick a crtc for connector");
245 return DISPLAY_FAILURE;
246 }
247
UpdateModes()248 int32_t DrmConnector::UpdateModes()
249 {
250 int drmFd = mDrmFdPtr->GetFd();
251 drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
252 DISPLAY_CHK_RETURN((c == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get connector"));
253 mConnectState = c->connection;
254 // init the modes
255 InitModes(*c);
256 drmModeFreeConnector(c);
257 return DISPLAY_SUCCESS;
258 }
259
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)260 int32_t DrmConnector::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
261 {
262 DISPLAY_CHK_RETURN((num == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("num is nullptr"));
263 *num = static_cast<int32_t>(mModes.size());
264 int i = 0;
265 if (modes != nullptr) {
266 for (const auto &modeMap : mModes) {
267 DrmMode mode = modeMap.second;
268 mode.ConvertToHdiMode(*(modes + (*num - (i + 1))));
269 i++;
270 }
271 }
272 return DISPLAY_SUCCESS;
273 }
274
SetDpmsState(uint64_t dmps)275 int32_t DrmConnector::SetDpmsState(uint64_t dmps)
276 {
277 DISPLAY_LOGD("dmps %{public}" PRIu64 "", dmps);
278 mDpmsState = dmps;
279 return DISPLAY_SUCCESS;
280 }
281
IsConnected()282 bool DrmConnector::IsConnected()
283 {
284 return (mConnectState == DRM_MODE_CONNECTED);
285 }
286
GetModeFromId(int32_t id,DrmMode & mode)287 int32_t DrmConnector::GetModeFromId(int32_t id, DrmMode &mode)
288 {
289 DISPLAY_LOGD();
290 auto iter = mModes.find(id);
291 if (iter == mModes.end()) {
292 return DISPLAY_FAILURE;
293 }
294 mode = mModes[id];
295 return DISPLAY_SUCCESS;
296 }
297
GetModeBlockFromId(int32_t id)298 std::unique_ptr<DrmModeBlock> DrmConnector::GetModeBlockFromId(int32_t id)
299 {
300 DISPLAY_LOGD("id %{public}d", id);
301 auto iter = mModes.find(id);
302 DISPLAY_CHK_RETURN((iter == mModes.end()), nullptr, DISPLAY_LOGE("can not the mode %{public}d", id));
303 return std::make_unique<DrmModeBlock>(mModes[id]);
304 }
305
DrmModeBlock(DrmMode & mode)306 DrmModeBlock::DrmModeBlock(DrmMode &mode)
307 {
308 DISPLAY_LOGD();
309 Init(mode);
310 }
311
Init(DrmMode & mode)312 int32_t DrmModeBlock::Init(DrmMode &mode)
313 {
314 int ret;
315 int drmFd = DrmDevice::GetDrmFd();
316 DISPLAY_CHK_RETURN((drmFd < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is invalid"));
317 drmModeModeInfo modeInfo = *(mode.GetModeInfoPtr());
318 ret = drmModeCreatePropertyBlob(drmFd, static_cast<void *>(&modeInfo), sizeof(modeInfo), &mBlockId);
319 DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("create property blob failed"));
320 DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
321 return DISPLAY_SUCCESS;
322 }
323
~DrmModeBlock()324 DrmModeBlock::~DrmModeBlock()
325 {
326 DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
327 int drmFd;
328 int ret;
329 drmFd = DrmDevice::GetDrmFd();
330 if ((mBlockId != 0xFFFFFFFF) && (drmFd >= 0)) {
331 ret = drmModeDestroyPropertyBlob(drmFd, mBlockId);
332 if (ret != 0) {
333 DISPLAY_LOGE("destroy property blob failed errno %{public}d", errno);
334 }
335 } else {
336 DISPLAY_LOGE("can not destruct the block id %{public}d drmFd %{public}d", mBlockId, drmFd);
337 }
338 }
339 } // namespace OHOS
340 } // namespace HDI
341 } // namespace DISPLAY
342