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 <scoped_bytrace.h>
17 #include "hdi_screen.h"
18 #include <chrono>
19 #include "hdi_log.h"
20 #include "vsync_sampler.h"
21
22 #define CHECK_DEVICE_NULL(sptrDevice) \
23 do { \
24 if ((sptrDevice) == nullptr) { \
25 HLOGE("[%{public}s]: HdiDevice is nullptr.", __func__); \
26 return GRAPHIC_DISPLAY_NULL_PTR; \
27 } \
28 } while (0)
29
30 namespace OHOS {
31 namespace Rosen {
32
CreateHdiScreen(uint32_t screenId)33 std::unique_ptr<HdiScreen> HdiScreen::CreateHdiScreen(uint32_t screenId)
34 {
35 return std::make_unique<HdiScreen>(screenId);
36 }
37
HdiScreen(uint32_t screenId)38 HdiScreen::HdiScreen(uint32_t screenId) : screenId_(screenId)
39 {
40 HLOGI("Create screen, screenId is %{public}d", screenId);
41 }
42
~HdiScreen()43 HdiScreen::~HdiScreen()
44 {
45 HLOGI("Destroy screen, screenId is %{public}d", screenId_);
46
47 Destroy();
48 }
49
OnVsync(uint32_t sequence,uint64_t ns,void * data)50 void HdiScreen::OnVsync(uint32_t sequence, uint64_t ns, void *data)
51 {
52 ScopedBytrace onVsyncTrace(__func__ + std::to_string((ns)));
53 if (ns == 0) {
54 HLOGW("Vsync ns is 0, drop this callback");
55 return;
56 }
57
58 // trigger vsync
59 // if the sampler->GetHardwareVSyncStatus() is false, this OnVsync callback will be disable
60 // we need to add this process
61 auto sampler = CreateVSyncSampler();
62 if (sampler->GetHardwareVSyncStatus()) {
63 bool enable = sampler->AddSample(ns);
64 sampler->SetHardwareVSyncStatus(enable);
65 }
66 }
67
Init()68 bool HdiScreen::Init()
69 {
70 if (device_ != nullptr) {
71 HLOGI("HdiScreen has been initialized");
72 return true;
73 }
74
75 device_ = HdiDevice::GetInstance();
76 if (device_ == nullptr) {
77 HLOGE("[%{public}s]: HdiDevice is nullptr.", __func__);
78 return false;
79 }
80
81 int32_t ret = device_->RegScreenVBlankCallback(screenId_, HdiScreen::OnVsync, this);
82 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
83 Destroy();
84 HLOGE("RegScreenVBlankCallback failed, ret is %{public}d", ret);
85 return false;
86 }
87
88 ret = device_->SetScreenVsyncEnabled(screenId_, true);
89 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
90 Destroy();
91 HLOGE("SetScreenVsyncEnabled failed, ret is %{public}d", ret);
92 return false;
93 }
94
95 HLOGI("Init hdiScreen succeed");
96
97 return true;
98 }
99
SetHdiDevice(HdiDevice * device)100 bool HdiScreen::SetHdiDevice(HdiDevice* device)
101 {
102 if (device_ != nullptr) {
103 return true;
104 }
105
106 if (device == nullptr) {
107 HLOGE("Input HdiDevice is null");
108 return false;
109 }
110
111 device_ = device;
112 return true;
113 }
114
GetScreenCapability(GraphicDisplayCapability & dcap) const115 int32_t HdiScreen::GetScreenCapability(GraphicDisplayCapability &dcap) const
116 {
117 CHECK_DEVICE_NULL(device_);
118 return device_->GetScreenCapability(screenId_, dcap);
119 }
120
GetScreenSupportedModes(std::vector<GraphicDisplayModeInfo> & modes) const121 int32_t HdiScreen::GetScreenSupportedModes(std::vector<GraphicDisplayModeInfo> &modes) const
122 {
123 CHECK_DEVICE_NULL(device_);
124 return device_->GetScreenSupportedModes(screenId_, modes);
125 }
126
GetScreenMode(uint32_t & modeId) const127 int32_t HdiScreen::GetScreenMode(uint32_t &modeId) const
128 {
129 CHECK_DEVICE_NULL(device_);
130 return device_->GetScreenMode(screenId_, modeId);
131 }
132
SetScreenMode(uint32_t modeId) const133 int32_t HdiScreen::SetScreenMode(uint32_t modeId) const
134 {
135 CHECK_DEVICE_NULL(device_);
136 return device_->SetScreenMode(screenId_, modeId);
137 }
138
GetScreenPowerStatus(GraphicDispPowerStatus & status) const139 int32_t HdiScreen::GetScreenPowerStatus(GraphicDispPowerStatus &status) const
140 {
141 CHECK_DEVICE_NULL(device_);
142 return device_->GetScreenPowerStatus(screenId_, status);
143 }
144
SetScreenPowerStatus(GraphicDispPowerStatus status) const145 int32_t HdiScreen::SetScreenPowerStatus(GraphicDispPowerStatus status) const
146 {
147 CHECK_DEVICE_NULL(device_);
148 return device_->SetScreenPowerStatus(screenId_, status);
149 }
150
GetScreenBacklight(uint32_t & level) const151 int32_t HdiScreen::GetScreenBacklight(uint32_t &level) const
152 {
153 CHECK_DEVICE_NULL(device_);
154 return device_->GetScreenBacklight(screenId_, level);
155 }
156
SetScreenBacklight(uint32_t level) const157 int32_t HdiScreen::SetScreenBacklight(uint32_t level) const
158 {
159 CHECK_DEVICE_NULL(device_);
160 return device_->SetScreenBacklight(screenId_, level);
161 }
162
SetScreenVsyncEnabled(bool enabled) const163 int32_t HdiScreen::SetScreenVsyncEnabled(bool enabled) const
164 {
165 CHECK_DEVICE_NULL(device_);
166 return device_->SetScreenVsyncEnabled(screenId_, enabled);
167 }
168
GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> & gamuts) const169 int32_t HdiScreen::GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> &gamuts) const
170 {
171 CHECK_DEVICE_NULL(device_);
172 return device_->GetScreenSupportedColorGamuts(screenId_, gamuts);
173 }
174
SetScreenColorGamut(GraphicColorGamut gamut) const175 int32_t HdiScreen::SetScreenColorGamut(GraphicColorGamut gamut) const
176 {
177 CHECK_DEVICE_NULL(device_);
178 return device_->SetScreenColorGamut(screenId_, gamut);
179 }
180
GetScreenColorGamut(GraphicColorGamut & gamut) const181 int32_t HdiScreen::GetScreenColorGamut(GraphicColorGamut &gamut) const
182 {
183 CHECK_DEVICE_NULL(device_);
184 return device_->GetScreenColorGamut(screenId_, gamut);
185 }
186
SetScreenGamutMap(GraphicGamutMap gamutMap) const187 int32_t HdiScreen::SetScreenGamutMap(GraphicGamutMap gamutMap) const
188 {
189 CHECK_DEVICE_NULL(device_);
190 return device_->SetScreenGamutMap(screenId_, gamutMap);
191 }
192
GetScreenGamutMap(GraphicGamutMap & gamutMap) const193 int32_t HdiScreen::GetScreenGamutMap(GraphicGamutMap &gamutMap) const
194 {
195 CHECK_DEVICE_NULL(device_);
196 return device_->GetScreenGamutMap(screenId_, gamutMap);
197 }
198
SetScreenColorTransform(const std::vector<float> & matrix) const199 int32_t HdiScreen::SetScreenColorTransform(const std::vector<float>& matrix) const
200 {
201 CHECK_DEVICE_NULL(device_);
202 return device_->SetScreenColorTransform(screenId_, matrix);
203 }
204
GetHDRCapabilityInfos(GraphicHDRCapability & info) const205 int32_t HdiScreen::GetHDRCapabilityInfos(GraphicHDRCapability &info) const
206 {
207 CHECK_DEVICE_NULL(device_);
208 return device_->GetHDRCapabilityInfos(screenId_, info);
209 }
210
GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> & keys) const211 int32_t HdiScreen::GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> &keys) const
212 {
213 CHECK_DEVICE_NULL(device_);
214 return device_->GetSupportedMetaDataKey(screenId_, keys);
215 }
216
Destroy()217 void HdiScreen::Destroy()
218 {
219 }
220
221 } // namespace Rosen
222 } // namespace OHOS
223