• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "drm_connector.h"
20 #include <cinttypes>
21 #include <securec.h>
22 #include "drm_device.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_LOGD("encoder_id %{public}d", mEncoderId);
44     DISPLAY_LOGD("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_LOGD("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_LOGD("name %{public}s", mName.c_str());
55 }
56 
InitModes(drmModeConnector c)57 void DrmConnector::InitModes(drmModeConnector c)
58 {
59     DISPLAY_LOGD("id %{public}d", mId);
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_LOGD("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_LOGD("set it to prefernce id %{public}d", i);
68             mPreferenceId = i;
69         }
70         mModes.emplace(i, DrmMode { *mode, i });
71     }
72     DISPLAY_LOGD("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_LOGD();
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_LOGD("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_LOGD("encoder_id %{public}d", mEncoderId);
93     DISPLAY_LOGD("mPropCrtcId %{public}d", mPropCrtcId);
94 
95     return DISPLAY_SUCCESS;
96 }
97 
GetBrightness(uint32_t & level)98 int32_t DrmConnector::GetBrightness(uint32_t &level)
99 {
100     level = mBrightnessLevel;
101 
102     return DISPLAY_SUCCESS;
103 }
104 
SetBrightness(uint32_t level)105 int32_t DrmConnector::SetBrightness(uint32_t level)
106 {
107     DISPLAY_LOGD("DrmConnector::SetBrightness set %{public}d", level);
108     static int32_t blFd = 0;
109     int32_t ret = 0;
110     int bytes = 0;
111     char buffer[10];
112 
113     if (blFd <= 0) {
114         blFd = open("/sys/class/backlight/lvds_backlight@0/brightness", O_RDWR);
115         if (blFd < 0) {
116             DISPLAY_LOGE("DrmConnector::SetBrightness open file error %{public}s", strerror(errno));
117             return DISPLAY_FAILURE;
118         }
119     }
120 
121     ret = memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
122     if (ret < 0) {
123         DISPLAY_LOGE("DrmConnector::SetBrightness memset_s error");
124         return DISPLAY_FAILURE;
125     }
126     bytes = sprintf_s(buffer, sizeof(buffer), "%d\n", level);
127     if (bytes < 0) {
128         DISPLAY_LOGE("DrmConnector::SetBrightness sprintf_s error");
129         return DISPLAY_FAILURE;
130     }
131     ret = write(blFd, buffer, bytes);
132     if (ret < 0) {
133         DISPLAY_LOGE("DrmConnector::SetBrightness write error");
134         return DISPLAY_FAILURE;
135     }
136 
137     mBrightnessLevel = level;
138     DISPLAY_LOGD("DrmConnector::SetBrightness exit !!!!");
139 
140     return DISPLAY_SUCCESS;
141 }
142 
GetDisplayCap(DisplayCapability & cap)143 void DrmConnector::GetDisplayCap(DisplayCapability &cap)
144 {
145     cap.phyHeight = mPhyHeight;
146     cap.phyWidth = mPhyWidth;
147     cap.type = mType;
148     memcpy_s(cap.name, sizeof(cap.name), mName.c_str(), mName.size());
149     if (mName.size() >= sizeof(cap.name)) {
150         cap.name[sizeof(cap.name) - 1] = 0;
151     } else {
152         cap.name[mName.size()] = 0;
153     }
154     cap.supportLayers = mSupportLayers;
155     cap.virtualDispCount = mVirtualDispCount;
156     cap.supportWriteBack = mSupportWriteBack;
157     cap.propertyCount = mPropertyCount;
158 }
159 
ConvertTypeToName(uint32_t type,std::string & name)160 void DrmConnector::ConvertTypeToName(uint32_t type, std::string &name)
161 {
162     DISPLAY_LOGD("type %{public}d", type);
163     switch (type) {
164         case DISP_INTF_VGA:
165             name = "VGA";
166             break;
167         case DISP_INTF_HDMI:
168             name = "HDMI";
169             break;
170         case DISP_INTF_MIPI:
171             name = "MIPI";
172             break;
173         default:
174             name = "Unknown";
175             break;
176     }
177 }
178 
ConvertToHdiType(uint32_t type,InterfaceType & hdiType)179 void DrmConnector::ConvertToHdiType(uint32_t type, InterfaceType &hdiType)
180 {
181     switch (type) {
182         case DRM_MODE_CONNECTOR_VGA:
183             hdiType = DISP_INTF_VGA;
184             break;
185         case DRM_MODE_CONNECTOR_DSI:
186             hdiType = DISP_INTF_MIPI;
187             break;
188         case DRM_MODE_CONNECTOR_HDMIA:
189         case DRM_MODE_CONNECTOR_HDMIB:
190             hdiType = DISP_INTF_HDMI;
191             break;
192         default:
193             hdiType = DISP_INTF_BUTT;
194             break;
195     }
196 }
TryPickEncoder(IdMapPtr<DrmEncoder> & encoders,uint32_t encoderId,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)197 int32_t DrmConnector::TryPickEncoder(IdMapPtr<DrmEncoder> &encoders, uint32_t encoderId, IdMapPtr<DrmCrtc> &crtcs,
198     uint32_t &crtcId)
199 {
200     int ret;
201     auto encoderIter = encoders.find(encoderId);
202     if (encoderIter == encoders.end()) {
203         DISPLAY_LOGW("can not find encoder for id : %{public}d", encoderId);
204         return DISPLAY_FAILURE;
205     }
206 
207     auto &encoder = encoderIter->second;
208     DISPLAY_LOGD("connector : %{public}d encoder : %{public}d", mId, encoder->GetId());
209     ret = encoder->PickIdleCrtcId(crtcs, crtcId);
210     DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
211         DISPLAY_LOGD("connector : %{public}d pick encoder : %{public}d", mId, encoder->GetId()));
212     return DISPLAY_FAILURE;
213 }
214 
PickIdleCrtcId(IdMapPtr<DrmEncoder> & encoders,IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)215 int32_t DrmConnector::PickIdleCrtcId(IdMapPtr<DrmEncoder> &encoders, IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
216 {
217     DISPLAY_LOGD();
218     DISPLAY_LOGD("encoder_id %{public}d", mEncoderId);
219     int ret = TryPickEncoder(encoders, mEncoderId, crtcs, crtcId);
220     DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
221         DISPLAY_LOGD("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d",
222         mId, mEncoderId, crtcId));
223 
224     for (auto encoder : mPossibleEncoders) {
225         ret = TryPickEncoder(encoders, encoder, crtcs, crtcId);
226         DISPLAY_CHK_RETURN((ret == DISPLAY_SUCCESS), DISPLAY_SUCCESS,
227             DISPLAY_LOGD("connector : %{public}d pick endcoder : %{public}d crtcId : %{public}d", mId, mEncoderId,
228             crtcId));
229     }
230 
231     DISPLAY_LOGW("can not pick a crtc for connector");
232     return DISPLAY_FAILURE;
233 }
234 
UpdateModes()235 int32_t DrmConnector::UpdateModes()
236 {
237     int drmFd = mDrmFdPtr->GetFd();
238     drmModeConnectorPtr c = drmModeGetConnector(drmFd, mId);
239     DISPLAY_CHK_RETURN((c == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get connector"));
240     mConnectState = c->connection;
241     // init the modes
242     InitModes(*c);
243     drmModeFreeConnector(c);
244     return DISPLAY_SUCCESS;
245 }
246 
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)247 int32_t DrmConnector::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
248 {
249     DISPLAY_CHK_RETURN((num == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("num is nullptr"));
250     *num = static_cast<int32_t>(mModes.size());
251     int i = 0;
252     if (modes != nullptr) {
253         for (const auto &modeMap : mModes) {
254             DrmMode mode = modeMap.second;
255             mode.ConvertToHdiMode(*(modes + i));
256             i++;
257         }
258     }
259     return DISPLAY_SUCCESS;
260 }
261 
SetDpmsState(uint64_t dmps)262 int32_t DrmConnector::SetDpmsState(uint64_t dmps)
263 {
264     DISPLAY_LOGD("dmps %{public}" PRIu64 "", dmps);
265     int ret = drmModeConnectorSetProperty(mDrmFdPtr->GetFd(), mId, mPropDpmsId, dmps);
266     DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("can not set dpms"));
267     mDpmsState = dmps;
268     return DISPLAY_SUCCESS;
269 }
270 
IsConnected()271 bool DrmConnector::IsConnected()
272 {
273     return (mConnectState == DRM_MODE_CONNECTED);
274 }
275 
GetModeFromId(int32_t id,DrmMode & mode)276 int32_t DrmConnector::GetModeFromId(int32_t id, DrmMode &mode)
277 {
278     DISPLAY_LOGD();
279     auto iter = mModes.find(id);
280     if (iter == mModes.end()) {
281         return DISPLAY_FAILURE;
282     }
283     mode = mModes[id];
284     return DISPLAY_SUCCESS;
285 }
286 
GetModeBlockFromId(int32_t id)287 std::unique_ptr<DrmModeBlock> DrmConnector::GetModeBlockFromId(int32_t id)
288 {
289     DISPLAY_LOGD("id %{public}d", id);
290     auto iter = mModes.find(id);
291     DISPLAY_CHK_RETURN((iter == mModes.end()), nullptr, DISPLAY_LOGE("can not the mode %{public}d", id));
292     return std::make_unique<DrmModeBlock>(mModes[id]);
293 }
294 
DrmModeBlock(DrmMode & mode)295 DrmModeBlock::DrmModeBlock(DrmMode &mode)
296 {
297     DISPLAY_LOGD();
298     Init(mode);
299 }
300 
Init(DrmMode & mode)301 int32_t DrmModeBlock::Init(DrmMode &mode)
302 {
303     int ret;
304     int drmFd = DrmDevice::GetDrmFd();
305     DISPLAY_CHK_RETURN((drmFd < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the drm fd is invalid"));
306     drmModeModeInfo modeInfo = *(mode.GetModeInfoPtr());
307     ret = drmModeCreatePropertyBlob(drmFd, static_cast<void *>(&modeInfo), sizeof(modeInfo), &mBlockId);
308     DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("create property blob failed"));
309     DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
310     return DISPLAY_SUCCESS;
311 }
312 
~DrmModeBlock()313 DrmModeBlock::~DrmModeBlock()
314 {
315     DISPLAY_LOGD("mBlockId %{public}d", mBlockId);
316     int drmFd;
317     int ret;
318     drmFd = DrmDevice::GetDrmFd();
319     if ((mBlockId != DRM_INVALID_ID) && (drmFd >= 0)) {
320         ret = drmModeDestroyPropertyBlob(drmFd, mBlockId);
321         if (ret != 0) {
322             DISPLAY_LOGE("destroy property blob failed errno %{public}d", errno);
323         }
324     } else {
325         DISPLAY_LOGE("can not destruct the block id %{public}d drmFd %{public}d", mBlockId, drmFd);
326     }
327 }
328 } // namespace OHOS
329 } // namespace HDI
330 } // namespace DISPLAY
331