• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(std::vector<int> & fds)41 FbDisplay::FbDisplay(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     DISPLAY_CHK_RETURN((*num < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the num is invalid"));
103     if (modes == NULL) {
104         *num = modes_.size();
105         return DISPLAY_SUCCESS;
106     }
107     if (*num > modes_.size()) {
108         *num = modes_.size();
109     }
110     memcpy_s(modes, sizeof(DisplayModeInfo) * (*num), modes_.data(), sizeof(DisplayModeInfo) * (*num));
111     return DISPLAY_SUCCESS;
112 }
113 
GetDisplayMode(uint32_t * modeId)114 int32_t FbDisplay::GetDisplayMode(uint32_t *modeId)
115 {
116     DISPLAY_LOGD();
117     *modeId = mActiveModeId;
118     if (mActiveModeId == static_cast<uint32_t>(INVALID_MODE_ID)) {
119         DISPLAY_LOGE("now has not active mode");
120         return DISPLAY_FAILURE;
121     }
122     return DISPLAY_SUCCESS;
123 }
124 
SetDisplayMode(uint32_t modeId)125 int32_t FbDisplay::SetDisplayMode(uint32_t modeId)
126 {
127     DISPLAY_LOGD();
128     if (modeId < modes_.size()) {
129         mActiveModeId = modeId;
130     } else {
131         DISPLAY_LOGE("the modeId is invalid");
132     }
133     return DISPLAY_SUCCESS;
134 }
135 
GetDisplayPowerStatus(DispPowerStatus * status)136 int32_t FbDisplay::GetDisplayPowerStatus(DispPowerStatus *status)
137 {
138     DISPLAY_LOGD();
139     DISPLAY_CHK_RETURN((status == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("the status is nullptr"));
140     *status = mPowerstatus;
141     return DISPLAY_SUCCESS;
142 }
143 
SetDisplayPowerStatus(DispPowerStatus status)144 int32_t FbDisplay::SetDisplayPowerStatus(DispPowerStatus status)
145 {
146     DISPLAY_LOGD();
147     mPowerstatus = status;
148     return DISPLAY_SUCCESS;
149 }
150 
GetDisplayBacklight(uint32_t * value)151 int32_t FbDisplay::GetDisplayBacklight(uint32_t *value)
152 {
153     DISPLAY_LOGD();
154     return DISPLAY_NOT_SUPPORT;
155 }
156 
SetDisplayBacklight(uint32_t value)157 int32_t FbDisplay::SetDisplayBacklight(uint32_t value)
158 {
159     DISPLAY_LOGD();
160     return DISPLAY_NOT_SUPPORT;
161 }
162 
RegDisplayVBlankCallback(VBlankCallback cb,void * data)163 int32_t FbDisplay::RegDisplayVBlankCallback(VBlankCallback cb, void *data)
164 {
165     DISPLAY_LOGD("the VBlankCallback %{public}p ", cb);
166     std::shared_ptr<VsyncCallBack> vsyncCb = std::make_shared<VsyncCallBack>(cb, nullptr);
167     SorftVsync::GetInstance().ReqesterVBlankCb(vsyncCb);
168     return DISPLAY_SUCCESS;
169 }
170 
IsConnected()171 bool FbDisplay::IsConnected()
172 {
173     DISPLAY_LOGD();
174     return true;
175 }
176 
SetDisplayVsyncEnabled(bool enabled)177 int32_t FbDisplay::SetDisplayVsyncEnabled(bool enabled)
178 {
179     DISPLAY_LOGD("enable %{public}d", enabled);
180     SorftVsync::GetInstance().EnableVsync(enabled);
181     return DISPLAY_SUCCESS;
182 }
183 } // namespace OHOS
184 } // namespace HDI
185 } // namespace DISPLAY
186