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