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