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_display.h"
17 #include <string>
18 #include <cerrno>
19 #include <memory>
20 #include <xf86drm.h>
21 #include <xf86drmMode.h>
22 #include "display_gralloc.h"
23 #include "display_common.h"
24 #include "drm_device.h"
25 #include "drm_vsync_worker.h"
26 #include "hdi_drm_composition.h"
27 #include "hdi_gfx_composition.h"
28
29 namespace OHOS {
30 namespace HDI {
31 namespace DISPLAY {
DrmDisplay(std::shared_ptr<DrmConnector> & connector,const std::shared_ptr<DrmCrtc> & crtc,std::shared_ptr<DrmDevice> & drmDevice)32 DrmDisplay::DrmDisplay(std::shared_ptr<DrmConnector> &connector, const std::shared_ptr<DrmCrtc> &crtc,
33 std::shared_ptr<DrmDevice> &drmDevice)
34 : mDrmDevice(drmDevice), mConnector(connector), mCrtc(crtc)
35 {}
36
~DrmDisplay()37 DrmDisplay::~DrmDisplay()
38 {
39 if (mCrtc != nullptr) {
40 mCrtc->UnBindDisplay(GetId());
41 }
42 }
43
Init()44 int32_t DrmDisplay::Init()
45 {
46 int ret;
47 DISPLAY_CHK_RETURN((mCrtc == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("crtc is null"));
48 DISPLAY_CHK_RETURN((mConnector == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("connector is null"));
49 DISPLAY_CHK_RETURN((mDrmDevice == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("drmDevice is null"));
50
51 ret = HdiDisplay::Init();
52 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("init failed"));
53 auto preComp = std::make_unique<HdiGfxComposition>();
54 DISPLAY_CHK_RETURN((preComp == nullptr), DISPLAY_FAILURE,
55 DISPLAY_LOGE("can not new HdiGfxComposition errno %{public}d", errno));
56 ret = preComp->Init();
57 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not init HdiGfxComposition"));
58
59 auto postComp = std::make_unique<HdiDrmComposition>(mConnector, mCrtc, mDrmDevice);
60 DISPLAY_CHK_RETURN((postComp == nullptr), DISPLAY_FAILURE,
61 DISPLAY_LOGE("can not new HdiDrmComposition errno %{public}d", errno));
62 ret = postComp->Init();
63 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not init HdiDrmComposition"));
64 mComposer = std::make_unique<HdiComposer>(std::move(preComp), std::move(postComp));
65 ret = mCrtc->BindToDisplay(GetId());
66 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("bind crtc failed"));
67
68 ret = ChosePreferenceMode();
69 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("choose preference mode fialed"));
70 return DISPLAY_SUCCESS;
71 }
72
GetDisplayCapability(DisplayCapability * info)73 int32_t DrmDisplay::GetDisplayCapability(DisplayCapability *info)
74 {
75 mConnector->GetDisplayCap(*info);
76 return DISPLAY_SUCCESS;
77 }
78
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)79 int32_t DrmDisplay::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
80 {
81 mConnector->GetDisplaySupportedModes(num, modes);
82 return DISPLAY_SUCCESS;
83 }
84
GetDisplayMode(uint32_t * modeId)85 int32_t DrmDisplay::GetDisplayMode(uint32_t *modeId)
86 {
87 DISPLAY_CHK_RETURN((modeId == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("the in modeId is nullptr"));
88 *modeId = mCrtc->GetActiveModeId();
89 return DISPLAY_SUCCESS;
90 }
91
SetDisplayMode(uint32_t modeId)92 int32_t DrmDisplay::SetDisplayMode(uint32_t modeId)
93 {
94 return mCrtc->SetActivieMode(modeId);
95 }
96
GetDisplayPowerStatus(DispPowerStatus * status)97 int32_t DrmDisplay::GetDisplayPowerStatus(DispPowerStatus *status)
98 {
99 DISPLAY_CHK_RETURN((status == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("status is nullptr"));
100 return ConvertToHdiPowerState(mConnector->GetDpmsState(), *status);
101 }
102
SetDisplayPowerStatus(DispPowerStatus status)103 int32_t DrmDisplay::SetDisplayPowerStatus(DispPowerStatus status)
104 {
105 DISPLAY_LOGD("the status %{public}d ", status);
106 uint32_t drmPowerState = 0;
107 int ret = ConvertToDrmPowerState(status, drmPowerState);
108 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_PARAM_ERR,
109 DISPLAY_LOGE("unknown power status %{public}d", status));
110 mConnector->SetDpmsState(drmPowerState);
111 return DISPLAY_SUCCESS;
112 }
113
ConvertToHdiPowerState(uint32_t drmPowerState,DispPowerStatus & hdiPowerState)114 int32_t DrmDisplay::ConvertToHdiPowerState(uint32_t drmPowerState, DispPowerStatus &hdiPowerState)
115 {
116 int32_t ret = DISPLAY_SUCCESS;
117 switch (drmPowerState) {
118 case DRM_MODE_DPMS_OFF:
119 hdiPowerState = POWER_STATUS_OFF;
120 break;
121 case DRM_MODE_DPMS_ON:
122 hdiPowerState = POWER_STATUS_ON;
123 break;
124 case DRM_MODE_DPMS_STANDBY:
125 hdiPowerState = POWER_STATUS_STANDBY;
126 break;
127 case DRM_MODE_DPMS_SUSPEND:
128 hdiPowerState = POWER_STATUS_SUSPEND;
129 break;
130 default:
131 hdiPowerState = POWER_STATUS_BUTT;
132 ret = DISPLAY_FAILURE;
133 break;
134 }
135 DISPLAY_LOGD("hdi power state %{public}u", hdiPowerState);
136 return ret;
137 }
138
ConvertToDrmPowerState(DispPowerStatus hdiPowerState,uint32_t & drmPowerState)139 int32_t DrmDisplay::ConvertToDrmPowerState(DispPowerStatus hdiPowerState, uint32_t &drmPowerState)
140 {
141 int32_t ret = DISPLAY_SUCCESS;
142 switch (hdiPowerState) {
143 case POWER_STATUS_OFF:
144 drmPowerState = DRM_MODE_DPMS_OFF;
145 break;
146 case POWER_STATUS_ON:
147 drmPowerState = DRM_MODE_DPMS_ON;
148 break;
149 case POWER_STATUS_STANDBY:
150 drmPowerState = DRM_MODE_DPMS_STANDBY;
151 break;
152 case POWER_STATUS_SUSPEND:
153 drmPowerState = DRM_MODE_DPMS_SUSPEND;
154 break;
155 default:
156 ret = DISPLAY_FAILURE;
157 break;
158 }
159 return ret;
160 }
161
CreateHdiLayer(LayerType type)162 std::unique_ptr<HdiLayer> DrmDisplay::CreateHdiLayer(LayerType type)
163 {
164 DISPLAY_LOGD();
165 return std::make_unique<HdiDrmLayer>(type);
166 }
167
WaitForVBlank(uint64_t * ns)168 int32_t DrmDisplay::WaitForVBlank(uint64_t *ns)
169 {
170 int ret;
171 constexpr uint64_t nPerS = 1000000000;
172 constexpr uint64_t nPerUS = 1000;
173 drmVBlank vbl = {
174 .request.type = DRM_VBLANK_RELATIVE,
175 .request.sequence = 0,
176 .request.signal = 0,
177 };
178 DISPLAY_CHK_RETURN((ns == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("in ns is nullptr"));
179 ret = drmWaitVBlank(mDrmDevice->GetDrmFd(), &vbl);
180 DISPLAY_CHK_RETURN((ret != 0), DISPLAY_FAILURE, DISPLAY_LOGE("wait vblank failed errno %{public}d", errno));
181 *ns = static_cast<uint64_t>(vbl.reply.tval_sec * nPerS + vbl.reply.tval_usec * nPerUS);
182 return DISPLAY_SUCCESS;
183 }
184
IsConnected()185 bool DrmDisplay::IsConnected()
186 {
187 DISPLAY_LOGD("conneted %{public}d", mConnector->IsConnected());
188 return mConnector->IsConnected();
189 }
190
PushFirstFrame()191 int32_t DrmDisplay::PushFirstFrame()
192 {
193 GrallocFuncs *grallocFucs = nullptr;
194 int ret = GrallocInitialize(&grallocFucs);
195 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("Gralloc init failed"));
196 DrmMode mode;
197 ret = mConnector->GetModeFromId(mCrtc->GetActiveModeId(), mode);
198 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
199 DISPLAY_LOGE("can not get the mode from id %{public}d", mCrtc->GetActiveModeId()));
200 AllocInfo info = {
201 .width = mode.GetModeInfoPtr()->hdisplay,
202 .height = mode.GetModeInfoPtr()->vdisplay,
203 .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
204 .format = PIXEL_FMT_BGRA_8888
205 };
206
207 BufferHandle *buffer = nullptr;
208 ret = grallocFucs->AllocMem(&info, &buffer);
209 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not alloc memory"));
210 mClientLayer->SetLayerBuffer(buffer, -1);
211
212 std::vector<HdiLayer *> layers;
213 HdiDrmComposition *drmComp = static_cast<HdiDrmComposition *>(mComposer->GetPostCompostion(0));
214 drmComp->SetLayers(layers, *mClientLayer);
215 drmComp->Apply(true);
216 return DISPLAY_SUCCESS;
217 }
218
ChosePreferenceMode()219 int32_t DrmDisplay::ChosePreferenceMode()
220 {
221 int32_t ret;
222 int32_t modeId = mConnector->GetPreferenceId();
223 if (modeId == INVALID_MODE_ID) {
224 uint32_t num = 0;
225 ret = GetDisplaySupportedModes(&num, nullptr);
226 DISPLAY_CHK_RETURN((num == 0) && (ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get modes"));
227 modeId = 0;
228 }
229 ret = SetDisplayMode(modeId);
230 // Push first frame to the drm, for that the vblank must init all the components.
231 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("set display mode failed"));
232 return PushFirstFrame();
233 }
234
RegDisplayVBlankCallback(VBlankCallback cb,void * data)235 int32_t DrmDisplay::RegDisplayVBlankCallback(VBlankCallback cb, void *data)
236 {
237 std::shared_ptr<VsyncCallBack> vsyncCb = std::make_shared<VsyncCallBack>(cb, data);
238 DrmVsyncWorker::GetInstance().ReqesterVBlankCb(vsyncCb);
239 return DISPLAY_SUCCESS;
240 }
241
SetDisplayVsyncEnabled(bool enabled)242 int32_t DrmDisplay::SetDisplayVsyncEnabled(bool enabled)
243 {
244 DISPLAY_LOGD("enable %{public}d", enabled);
245 DrmVsyncWorker::GetInstance().EnableVsync(enabled);
246 return DISPLAY_SUCCESS;
247 }
248
GetDisplayBacklight(uint32_t * value)249 int32_t DrmDisplay::GetDisplayBacklight(uint32_t *value)
250 {
251 DISPLAY_CHK_RETURN((value == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("value is nullptr"));
252 return mConnector->GetBrightness(*value);
253 }
254
SetDisplayBacklight(uint32_t value)255 int32_t DrmDisplay::SetDisplayBacklight(uint32_t value)
256 {
257 return mConnector->SetBrightness(value);
258 }
259 } // namespace OHOS
260 } // namespace HDI
261 } // namespace DISPLAY
262