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 
116 const int BACKLIGHT_MIN = 10;
SetBrightness(uint32_t level)117 int32_t DrmConnector::SetBrightness(uint32_t level)
118 {
119     static int32_t brFd = 0;
120     const int32_t buffer_size = 10; /* buffer size */
121     char buffer[buffer_size];
122 
123     DISPLAY_DEBUGLOG("set %{public}d", level);
124     if (mPropBrightnessId == DRM_INVALID_ID) {
125         DISPLAY_LOGE("the prop id of brightness is invalid");
126         return DISPLAY_NOT_SUPPORT;
127     }
128     if (brFd <= 0) {
129         brFd = open("/sys/class/backlight/backlight/brightness", O_RDWR);
130         if (brFd < 0) {
131             DISPLAY_LOGE("open 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         return DISPLAY_FAILURE;
139     }
140     if (level < BACKLIGHT_MIN) {
141         level = BACKLIGHT_MIN;
142     }
143     int bytes = sprintf_s(buffer, sizeof(buffer), "%d\n", level);
144     if (bytes < 0) {
145         DISPLAY_LOGE("change failed\n");
146         return DISPLAY_FAILURE;
147     }
148     write(brFd, buffer, bytes);
149     mBrightnessLevel = level;
150     return DISPLAY_SUCCESS;
151 }
152 
GetDisplayCap(DisplayCapability &cap)153 void DrmConnector::GetDisplayCap(DisplayCapability &cap)
154 {
155     cap.phyHeight = mPhyHeight;
156     cap.phyWidth = mPhyWidth;
157     cap.type = mType;
158     memcpy_s(cap.name, sizeof(cap.name), mName.c_str(), mName.size());
159     if (mName.size() >= sizeof(cap.name)) {
160         cap.name[sizeof(cap.name) - 1] = 0;
161     } else {
162         cap.name[mName.size()] = 0;
163     }
164     cap.supportLayers = mSupportLayers;
165     cap.virtualDispCount = mVirtualDispCount;
166     cap.supportWriteBack = mSupportWriteBack;
167     cap.propertyCount = mPropertyCount;
168 }
169 
ConvertTypeToName(uint32_t type, std::string &name)170 void DrmConnector::ConvertTypeToName(uint32_t type, std::string &name)
171 {
172     DISPLAY_DEBUGLOG("type %{public}d", type);
173     switch (type) {
174         case DISP_INTF_VGA:
175             name = "VGA";
176             break;
177         case DISP_INTF_HDMI:
178             name = "HDMI";
179             break;
180         case DISP_INTF_MIPI:
181             name = "MIPI";
182             break;
183         default:
184             name = "Unknown";
185             break;
186     }
187 }
188 
ConvertToHdiType(uint32_t type, InterfaceType &hdiType)189 void DrmConnector::ConvertToHdiType(uint32_t type, InterfaceType &hdiType)
190 {
191     switch (type) {
192         case DRM_MODE_CONNECTOR_VGA:
193             hdiType = DISP_INTF_VGA;
194             break;
195         case DRM_MODE_CONNECTOR_DSI:
196             hdiType = DISP_INTF_MIPI;
197             break;
198         case DRM_MODE_CONNECTOR_HDMIA:
199         case DRM_MODE_CONNECTOR_HDMIB:
200             hdiType = DISP_INTF_HDMI;
201             break;
202         default:
203             hdiType = DISP_INTF_BUTT;
204             break;
205     }
206 }
TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)207 int32_t DrmConnector::TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
208     uint32_t &crtcId)
209 {
210     int ret;
211     auto encoderIter = encoders.find(encoderId);
212     if (encoderIter == encoders.end()) {
213         DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderId);
214         return DISPLAY_FAILURE;
215     }
216 
217     auto &encoder = encoderIter->second;
218     DISPLAY_DEBUGLOG("connector : %{public}d encoder : %{public}d", mId, encoder->GetId());
219     ret = encoder->PickIdleCrtcId(crtcs, crtcId);
220     DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
221         DISPLAY_DEBUGLOG("connector : %{public}d pick encoder : %{public}d", mId, encoder->GetId()));
222     return DISPLAY_FAILURE;
223 }
224 
PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)225 int32_t DrmConnector::PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
226 {
227     DISPLAY_DEBUGLOG();
228     DISPLAY_DEBUGLOG("encoder_id %{public}d", mEncoderId);
229     int ret = TryPickEncoder(encoders, mEncoderId, crtcs, crtcId);
230     DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
231         DISPLAY_DEBUGLOG("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d",
232         mId, mEncoderId, crtcId));
233 
234     for (auto encoder : mPossibleEncoders) {
235         ret = TryPickEncoder(encoders, encoder, crtcs, crtcId);
236         DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
237             DISPLAY_DEBUGLOG("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d", mId, mEncoderId,
238             crtcId));
239     }
240 
241     DISPLAY_LOGW("can not pick a crtc for connector");
242     return DISPLAY_FAILURE;
243 }
244 
UpdateModes()245 int32_t DrmConnector::UpdateModes()
246 {
247     int drmFd = mDrmFdPtr->GetFd();
248     drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
249     DISPLAY_CHK_RETURN((c == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get connector"));
250     mConnectState = c->connection;
251     // init the modes
252     InitModes(*c);
253     drmModeFreeConnector(c);
254     return DISPLAY_SUCCESS;
255 }
256 
UpdateCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, bool plugIn, drmModeConnectorPtr c, int *crtc_id)257 std::shared_ptr<DrmCrtc> DrmConnector::UpdateCrtcId(IdMapPtr<DrmEncoder> &encoders,
258     IdMapPtr<DrmCrtc> &crtcs, bool plugIn, drmModeConnectorPtr c, int *crtc_id)
259 {
260     std::shared_ptr<DrmCrtc> crtc = nullptr;
261     int  encoderid = c->encoders[0];
262     auto encoderIter = encoders.find(encoderid);
263     if (encoderIter == encoders.end()) {
264         DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderid);
265         return crtc;
266     }
267 
268     auto &encoder = encoderIter->second;
269     int possibleCrtcs = encoder->GetPossibleCrtcs();
270 
271     for (auto crtcIter = crtcs.begin(); crtcIter != crtcs.end(); ++crtcIter) {
272         auto &posCrts = crtcIter->second;
273         if (possibleCrtcs == (1<<posCrts->GetPipe())) {
274             DISPLAY_DEBUGLOG("find crtc id %{public}d, pipe %{public}d", posCrts->GetId(), posCrts->GetPipe());
275             crtc = posCrts;
276             *crtc_id = posCrts->GetId();
277         }
278     }
279     if (plugIn) {
280         encoder->SetCrtcId(*crtc_id);
281         mEncoderId = c->encoders[0];
282     } else if (!plugIn) {
283         *crtc_id = 0;
284         mEncoderId = 0;
285         encoder->SetCrtcId(0);
286     }
287     return crtc;
288 }
289 
HandleHotplug(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, bool plugIn)290 bool DrmConnector::HandleHotplug(IdMapPtr<DrmEncoder> &encoders,
291     IdMapPtr<DrmCrtc> &crtcs, bool plugIn)
292 {
293     DISPLAY_DEBUGLOG("plug %{public}d", plugIn);
294     int drmFd = mDrmFdPtr->GetFd();
295     int ret;
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_DEBUGLOG("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         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_DEBUGLOG("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_DEBUGLOG();
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_DEBUGLOG("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_DEBUGLOG();
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_DEBUGLOG("mBlockId %{public}d", mBlockId);
399     return DISPLAY_SUCCESS;
400 }
401 
~DrmModeBlock()402 DrmModeBlock::~DrmModeBlock()
403 {
404     DISPLAY_DEBUGLOG("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