1 /*
2  * Copyright (c) 2022 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 "fb_display.h"
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <sys/ioctl.h>
20 #include <cerrno>
21 #include <securec.h>
22 #include <linux/fb.h>
23 #include "display_common.h"
24 #include "hdi_gfx_composition.h"
25 #include "fb_composition.h"
26 #include "vsync/sorft_vsync.h"
27 #include "hdi_gfx_composition.h"
28 #include "display_adapter.h"
29 
30 namespace OHOS {
31 namespace HDI {
32 namespace DISPLAY {
CreateHdiLayer(LayerType type)33 std::unique_ptr<HdiLayer> FbDisplay::CreateHdiLayer(LayerType type)
34 {
35     DISPLAY_LOGD();
36     auto layer = HdiDisplay::CreateHdiLayer(type);
37     layer->SetReleaseFence(-1); // the fd display will return the current buffer fence.
38     return layer;
39 }
40 
FbDisplay(const std::vector<int> & fds)41 FbDisplay::FbDisplay(const std::vector<int> &fds)
42 {
43     DISPLAY_LOGD();
44     deviceFds_ = fds;
45 }
46 
~FbDisplay()47 FbDisplay::~FbDisplay()
48 {
49     DISPLAY_LOGD();
50 }
51 
Init()52 int32_t FbDisplay::Init()
53 {
54     int ret;
55     int deviceFd = deviceFds_[0];
56     struct fb_var_screeninfo varInfo;
57     const uint32_t defaultFreshRate = 60 * 1000;
58     ret = HdiDisplay::Init();
59     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("init failed"));
60     ret = DisplayAdapter::GetInstance()->Ioctl(deviceFd, FBIOGET_VSCREENINFO, &varInfo);
61     DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE, DISPLAY_LOGE("failed to get screen information"));
62     DisplayModeInfo mode;
63     mode.freshRate = defaultFreshRate;
64     mode.width = varInfo.xres;
65     mode.height = varInfo.yres;
66     mode.id = 0;
67     modes_.push_back(mode);
68     DISPLAY_LOGD("xres : %{public}d yres : %{public}d fps : %{public}d", varInfo.xres, varInfo.yres, mode.freshRate);
69 
70     displayCapability_.phyWidth = varInfo.xres;
71     displayCapability_.phyHeight = varInfo.yres;
72     displayCapability_.type = DISP_INTF_PANEL;
73     mActiveModeId = 0;
74     auto preComp = std::make_unique<HdiGfxComposition>();
75     DISPLAY_CHK_RETURN((preComp == nullptr), DISPLAY_FAILURE,
76         DISPLAY_LOGE("can not new HdiGfxComposition errno %{public}d", errno));
77     ret = preComp->Init();
78     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not init HdiGfxComposition"));
79 
80     auto postComp = std::make_unique<FbComposition>(deviceFds_);
81     DISPLAY_CHK_RETURN((postComp == nullptr), DISPLAY_FAILURE,
82         DISPLAY_LOGE("can not new HdiDrmComposition errno %{public}d", errno));
83     ret = postComp->Init();
84     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not init HdiDrmComposition"));
85     mComposer = std::make_unique<HdiComposer>(std::move(preComp), std::move(postComp));
86     mClientLayer->SetReleaseFence(-1);
87     return DISPLAY_SUCCESS;
88 }
89 
GetDisplayCapability(DisplayCapability * info)90 int32_t FbDisplay::GetDisplayCapability(DisplayCapability *info)
91 {
92     DISPLAY_LOGD();
93     DISPLAY_CHK_RETURN((info == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("inof is nullptr"));
94     *info = displayCapability_;
95     return DISPLAY_SUCCESS;
96 }
97 
GetDisplaySupportedModes(uint32_t * num,DisplayModeInfo * modes)98 int32_t FbDisplay::GetDisplaySupportedModes(uint32_t *num, DisplayModeInfo *modes)
99 {
100     DISPLAY_LOGD();
101     DISPLAY_CHK_RETURN((num == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("num and modes is nullptr"));
102     if (modes == NULL) {
103         *num = modes_.size();
104         return DISPLAY_SUCCESS;
105     }
106     if (*num > modes_.size()) {
107         *num = modes_.size();
108     }
109     memcpy_s(modes, sizeof(DisplayModeInfo) * (*num), modes_.data(), sizeof(DisplayModeInfo) * (*num));
110     return DISPLAY_SUCCESS;
111 }
112 
GetDisplayMode(uint32_t * modeId)113 int32_t FbDisplay::GetDisplayMode(uint32_t *modeId)
114 {
115     DISPLAY_LOGD();
116     *modeId = mActiveModeId;
117     if (mActiveModeId == static_cast<uint32_t>(INVALID_MODE_ID)) {
118         DISPLAY_LOGE("now has not active mode");
119         return DISPLAY_FAILURE;
120     }
121     return DISPLAY_SUCCESS;
122 }
123 
SetDisplayMode(uint32_t modeId)124 int32_t FbDisplay::SetDisplayMode(uint32_t modeId)
125 {
126     DISPLAY_LOGD();
127     if (modeId < modes_.size()) {
128         mActiveModeId = modeId;
129     } else {
130         DISPLAY_LOGE("the modeId is invalid");
131     }
132     return DISPLAY_SUCCESS;
133 }
134 
GetDisplayPowerStatus(DispPowerStatus * status)135 int32_t FbDisplay::GetDisplayPowerStatus(DispPowerStatus *status)
136 {
137     DISPLAY_LOGD();
138     DISPLAY_CHK_RETURN((status == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("the status is nullptr"));
139     *status = mPowerstatus;
140     return DISPLAY_SUCCESS;
141 }
142 
SetDisplayPowerStatus(DispPowerStatus status)143 int32_t FbDisplay::SetDisplayPowerStatus(DispPowerStatus status)
144 {
145     DISPLAY_LOGD();
146     mPowerstatus = status;
147     return DISPLAY_SUCCESS;
148 }
149 
GetDisplayBacklight(uint32_t * value)150 int32_t FbDisplay::GetDisplayBacklight(uint32_t *value)
151 {
152     DISPLAY_LOGD();
153     return DISPLAY_NOT_SUPPORT;
154 }
155 
SetDisplayBacklight(uint32_t value)156 int32_t FbDisplay::SetDisplayBacklight(uint32_t value)
157 {
158     DISPLAY_LOGD();
159     return DISPLAY_NOT_SUPPORT;
160 }
161 
RegDisplayVBlankCallback(VBlankCallback cb,void * data)162 int32_t FbDisplay::RegDisplayVBlankCallback(VBlankCallback cb, void *data)
163 {
164     std::shared_ptr<VsyncCallBack> vsyncCb = std::make_shared<VsyncCallBack>(cb, nullptr);
165     SorftVsync::GetInstance().ReqesterVBlankCb(vsyncCb);
166     return DISPLAY_SUCCESS;
167 }
168 
IsConnected()169 bool FbDisplay::IsConnected()
170 {
171     DISPLAY_LOGD();
172     return true;
173 }
174 
SetDisplayVsyncEnabled(bool enabled)175 int32_t FbDisplay::SetDisplayVsyncEnabled(bool enabled)
176 {
177     DISPLAY_LOGD("enable %{public}d", enabled);
178     SorftVsync::GetInstance().EnableVsync(enabled);
179     return DISPLAY_SUCCESS;
180 }
181 } // namespace OHOS
182 } // namespace HDI
183 } // namespace DISPLAY
184