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