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 "hdi_screen.h"
17 #include <chrono>
18 #include "hdi_log.h"
19 #include "vsync_sampler.h"
20
21 namespace OHOS {
22 namespace Rosen {
23
CreateHdiScreen(uint32_t screenId)24 std::unique_ptr<HdiScreen> HdiScreen::CreateHdiScreen(uint32_t screenId)
25 {
26 return std::make_unique<HdiScreen>(screenId);
27 }
28
HdiScreen(uint32_t screenId)29 HdiScreen::HdiScreen(uint32_t screenId) : screenId_(screenId)
30 {
31 HLOGI("Create screen, screenId is %{public}d", screenId);
32 }
33
~HdiScreen()34 HdiScreen::~HdiScreen()
35 {
36 HLOGI("Destroy screen, screenId is %{public}d", screenId_);
37
38 Destroy();
39 }
40
OnVsync(uint32_t sequence,uint64_t ns,void * data)41 void HdiScreen::OnVsync(uint32_t sequence, uint64_t ns, void *data)
42 {
43 if (ns == 0) {
44 HLOGW("Vsync ns is 0, drop this callback");
45 return;
46 }
47
48 // trigger vsync
49 // if the sampler->GetHardwareVSyncStatus() is false, this OnVsync callback will be disable
50 // we need to add this process
51 auto sampler = CreateVSyncSampler();
52 if (sampler->GetHardwareVSyncStatus()) {
53 bool enable = sampler->AddSample(ns);
54 sampler->SetHardwareVSyncStatus(enable);
55 }
56 }
57
Init()58 bool HdiScreen::Init()
59 {
60 if (device_ != nullptr) {
61 HLOGI("HdiScreen has been initialized");
62 return true;
63 }
64
65 device_ = HdiDevice::GetInstance();
66 if (device_ == nullptr) {
67 return false;
68 }
69
70 int32_t ret = device_->RegScreenVBlankCallback(screenId_, HdiScreen::OnVsync, this);
71 if (ret != DISPLAY_SUCCESS) {
72 Destroy();
73 HLOGE("RegScreenVBlankCallback failed, ret is %{public}d", ret);
74 return false;
75 }
76
77 ret = device_->SetScreenVsyncEnabled(screenId_, true);
78 if (ret != DISPLAY_SUCCESS) {
79 Destroy();
80 HLOGE("SetScreenVsyncEnabled failed, ret is %{public}d", ret);
81 return false;
82 }
83
84 HLOGI("Init hdiScreen succeed");
85
86 return true;
87 }
88
SetHdiDevice(Base::HdiDevice * device)89 void HdiScreen::SetHdiDevice(Base::HdiDevice* device)
90 {
91 if (device == nullptr) {
92 HLOGE("Input HdiDevice is null");
93 return;
94 }
95
96 if (device_ != nullptr) {
97 HLOGW("HdiDevice has been changed");
98 return;
99 }
100 device_ = device;
101 }
102
GetScreenCapability(DisplayCapability & dcap) const103 int32_t HdiScreen::GetScreenCapability(DisplayCapability &dcap) const
104 {
105 if (device_ == nullptr) {
106 return DISPLAY_NULL_PTR;
107 }
108
109 return device_->GetScreenCapability(screenId_, dcap);
110 }
111
GetScreenSupportedModes(std::vector<DisplayModeInfo> & modes) const112 int32_t HdiScreen::GetScreenSupportedModes(std::vector<DisplayModeInfo> &modes) const
113 {
114 if (device_ == nullptr) {
115 return DISPLAY_NULL_PTR;
116 }
117
118 return device_->GetScreenSupportedModes(screenId_, modes);
119 }
120
GetScreenMode(uint32_t & modeId) const121 int32_t HdiScreen::GetScreenMode(uint32_t &modeId) const
122 {
123 if (device_ == nullptr) {
124 return DISPLAY_NULL_PTR;
125 }
126
127 return device_->GetScreenMode(screenId_, modeId);
128 }
129
SetScreenMode(uint32_t modeId) const130 int32_t HdiScreen::SetScreenMode(uint32_t modeId) const
131 {
132 if (device_ == nullptr) {
133 return DISPLAY_NULL_PTR;
134 }
135
136 return device_->SetScreenMode(screenId_, modeId);
137 }
138
GetScreenPowerStatus(DispPowerStatus & status) const139 int32_t HdiScreen::GetScreenPowerStatus(DispPowerStatus &status) const
140 {
141 if (device_ == nullptr) {
142 return DISPLAY_NULL_PTR;
143 }
144
145 return device_->GetScreenPowerStatus(screenId_, status);
146 }
147
SetScreenPowerStatus(DispPowerStatus status) const148 int32_t HdiScreen::SetScreenPowerStatus(DispPowerStatus status) const
149 {
150 if (device_ == nullptr) {
151 return DISPLAY_NULL_PTR;
152 }
153
154 return device_->SetScreenPowerStatus(screenId_, status);
155 }
156
GetScreenBacklight(uint32_t & level) const157 int32_t HdiScreen::GetScreenBacklight(uint32_t &level) const
158 {
159 if (device_ == nullptr) {
160 return DISPLAY_NULL_PTR;
161 }
162
163 return device_->GetScreenBacklight(screenId_, level);
164 }
165
SetScreenBacklight(uint32_t level) const166 int32_t HdiScreen::SetScreenBacklight(uint32_t level) const
167 {
168 if (device_ == nullptr) {
169 return DISPLAY_NULL_PTR;
170 }
171
172 return device_->SetScreenBacklight(screenId_, level);
173 }
174
SetScreenVsyncEnabled(bool enabled) const175 int32_t HdiScreen::SetScreenVsyncEnabled(bool enabled) const
176 {
177 if (device_ == nullptr) {
178 return DISPLAY_NULL_PTR;
179 }
180
181 return device_->SetScreenVsyncEnabled(screenId_, enabled);
182 }
183
GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> & gamuts) const184 int32_t HdiScreen::GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> &gamuts) const
185 {
186 if (device_ == nullptr) {
187 return DISPLAY_NULL_PTR;
188 }
189
190 return device_->GetScreenSupportedColorGamuts(screenId_, gamuts);
191 }
192
SetScreenColorGamut(GraphicColorGamut gamut) const193 int32_t HdiScreen::SetScreenColorGamut(GraphicColorGamut gamut) const
194 {
195 if (device_ == nullptr) {
196 return DISPLAY_NULL_PTR;
197 }
198
199 return device_->SetScreenColorGamut(screenId_, gamut);
200 }
201
GetScreenColorGamut(GraphicColorGamut & gamut) const202 int32_t HdiScreen::GetScreenColorGamut(GraphicColorGamut &gamut) const
203 {
204 if (device_ == nullptr) {
205 return DISPLAY_NULL_PTR;
206 }
207
208 return device_->GetScreenColorGamut(screenId_, gamut);
209 }
210
SetScreenGamutMap(GamutMap gamutMap) const211 int32_t HdiScreen::SetScreenGamutMap(GamutMap gamutMap) const
212 {
213 if (device_ == nullptr) {
214 return DISPLAY_NULL_PTR;
215 }
216
217 return device_->SetScreenGamutMap(screenId_, gamutMap);
218 }
219
GetScreenGamutMap(GamutMap & gamutMap) const220 int32_t HdiScreen::GetScreenGamutMap(GamutMap &gamutMap) const
221 {
222 if (device_ == nullptr) {
223 return DISPLAY_NULL_PTR;
224 }
225
226 return device_->GetScreenGamutMap(screenId_, gamutMap);
227 }
228
SetScreenColorTransform(const float * matrix) const229 int32_t HdiScreen::SetScreenColorTransform(const float *matrix) const
230 {
231 if (device_ == nullptr) {
232 return DISPLAY_NULL_PTR;
233 }
234
235 return device_->SetScreenColorTransform(screenId_, matrix);
236 }
237
GetHDRCapabilityInfos(HDRCapability & info) const238 int32_t HdiScreen::GetHDRCapabilityInfos(HDRCapability &info) const
239 {
240 if (device_ == nullptr) {
241 return DISPLAY_NULL_PTR;
242 }
243
244 return device_->GetHDRCapabilityInfos(screenId_, info);
245 }
246
GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> & keys) const247 int32_t HdiScreen::GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> &keys) const
248 {
249 if (device_ == nullptr) {
250 return DISPLAY_NULL_PTR;
251 }
252
253 return device_->GetSupportedMetaDataKey(screenId_, keys);
254 }
255
Destroy()256 void HdiScreen::Destroy()
257 {
258 }
259
260 } // namespace Rosen
261 } // namespace OHOS
262