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