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 #include <hdf_base.h>
22 #include <rs_trace.h>
23 #include <mutex>
24
25 #define CHECK_DEVICE_NULL(sptrDevice) \
26 do { \
27 if ((sptrDevice) == nullptr) { \
28 HLOGE("[%{public}s]: HdiDevice is nullptr.", __func__); \
29 return GRAPHIC_DISPLAY_NULL_PTR; \
30 } \
31 } while (0)
32
33 namespace OHOS {
34 namespace Rosen {
35
CreateHdiScreen(uint32_t screenId)36 std::unique_ptr<HdiScreen> HdiScreen::CreateHdiScreen(uint32_t screenId)
37 {
38 return std::make_unique<HdiScreen>(screenId);
39 }
40
HdiScreen(uint32_t screenId)41 HdiScreen::HdiScreen(uint32_t screenId) : screenId_(screenId)
42 {
43 HLOGI("Create screen, screenId is %{public}d", screenId);
44 }
45
~HdiScreen()46 HdiScreen::~HdiScreen()
47 {
48 HLOGI("Destroy screen, screenId is %{public}d", screenId_);
49 }
50
OnVsync(uint32_t sequence,uint64_t ns,void * data)51 void HdiScreen::OnVsync(uint32_t sequence, uint64_t ns, void *data)
52 {
53 (void)data;
54 ScopedBytrace onVsyncTrace("HdiScreen::OnVsync_" + std::to_string((ns)));
55 if (ns == 0) {
56 HLOGW("Vsync ns is 0, drop this callback");
57 return;
58 }
59
60 // trigger vsync
61 // if the sampler->GetHardwareVSyncStatus() is false, this OnVsync callback will be disable
62 // we need to add this process
63 auto sampler = CreateVSyncSampler();
64 if (sampler->GetHardwareVSyncStatus()) {
65 bool enable = sampler->AddSample(ns);
66 sampler->SetHardwareVSyncStatus(enable);
67 }
68 }
69
Init()70 bool HdiScreen::Init()
71 {
72 if (device_ != nullptr) {
73 HLOGI("HdiScreen has been initialized");
74 return true;
75 }
76
77 device_ = HdiDevice::GetInstance();
78 if (device_ == nullptr) {
79 HLOGE("[%{public}s]: HdiDevice is nullptr.", __func__);
80 return false;
81 }
82
83 int32_t ret = device_->RegScreenVBlankCallback(screenId_, HdiScreen::OnVsync, this);
84 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
85 HLOGE("RegScreenVBlankCallback failed, ret is %{public}d", ret);
86 return false;
87 }
88
89 ret = device_->SetScreenVsyncEnabled(screenId_, true);
90 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
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)127 int32_t HdiScreen::GetScreenMode(uint32_t &modeId)
128 {
129 std::unique_lock<std::mutex> locker(mutex_);
130 CHECK_DEVICE_NULL(device_);
131 if (modeId_ != UINT32_MAX) {
132 modeId = modeId_;
133 return HDF_SUCCESS;
134 }
135 int32_t ret = device_->GetScreenMode(screenId_, modeId);
136 if (ret == HDF_SUCCESS) {
137 modeId_ = modeId;
138 }
139 return ret;
140 }
141
SetScreenMode(uint32_t modeId)142 int32_t HdiScreen::SetScreenMode(uint32_t modeId)
143 {
144 std::unique_lock<std::mutex> locker(mutex_);
145 CHECK_DEVICE_NULL(device_);
146 int32_t ret = device_->SetScreenMode(screenId_, modeId);
147 if (ret == HDF_SUCCESS) {
148 modeId_ = modeId;
149 } else {
150 modeId_ = UINT32_MAX;
151 }
152 return ret;
153 }
154
SetScreenOverlayResolution(uint32_t width,uint32_t height) const155 int32_t HdiScreen::SetScreenOverlayResolution(uint32_t width, uint32_t height) const
156 {
157 CHECK_DEVICE_NULL(device_);
158 return device_->SetScreenOverlayResolution(screenId_, width, height);
159 }
160
GetScreenPowerStatus(GraphicDispPowerStatus & status) const161 int32_t HdiScreen::GetScreenPowerStatus(GraphicDispPowerStatus &status) const
162 {
163 CHECK_DEVICE_NULL(device_);
164 return device_->GetScreenPowerStatus(screenId_, status);
165 }
166
SetScreenPowerStatus(GraphicDispPowerStatus status) const167 int32_t HdiScreen::SetScreenPowerStatus(GraphicDispPowerStatus status) const
168 {
169 CHECK_DEVICE_NULL(device_);
170 return device_->SetScreenPowerStatus(screenId_, status);
171 }
172
GetScreenBacklight(uint32_t & level) const173 int32_t HdiScreen::GetScreenBacklight(uint32_t &level) const
174 {
175 CHECK_DEVICE_NULL(device_);
176 return device_->GetScreenBacklight(screenId_, level);
177 }
178
SetScreenBacklight(uint32_t level) const179 int32_t HdiScreen::SetScreenBacklight(uint32_t level) const
180 {
181 CHECK_DEVICE_NULL(device_);
182 return device_->SetScreenBacklight(screenId_, level);
183 }
184
SetScreenVsyncEnabled(bool enabled) const185 int32_t HdiScreen::SetScreenVsyncEnabled(bool enabled) const
186 {
187 CHECK_DEVICE_NULL(device_);
188 int32_t ret = device_->SetScreenVsyncEnabled(screenId_, enabled);
189 if (ret != HDF_SUCCESS) {
190 HLOGE("SetScreenVsyncEnabled Failed, screenId:%{public}u, enabled:%{public}d, ret:%{public}d",
191 screenId_, enabled, ret);
192 RS_TRACE_NAME_FMT("SetScreenVsyncEnabled Failed, screenId:%u, enabled:%d, ret:%d", screenId_, enabled, ret);
193 }
194 return ret;
195 }
196
GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> & gamuts) const197 int32_t HdiScreen::GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> &gamuts) const
198 {
199 CHECK_DEVICE_NULL(device_);
200 return device_->GetScreenSupportedColorGamuts(screenId_, gamuts);
201 }
202
SetScreenColorGamut(GraphicColorGamut gamut) const203 int32_t HdiScreen::SetScreenColorGamut(GraphicColorGamut gamut) const
204 {
205 CHECK_DEVICE_NULL(device_);
206 return device_->SetScreenColorGamut(screenId_, gamut);
207 }
208
GetScreenColorGamut(GraphicColorGamut & gamut) const209 int32_t HdiScreen::GetScreenColorGamut(GraphicColorGamut &gamut) const
210 {
211 CHECK_DEVICE_NULL(device_);
212 return device_->GetScreenColorGamut(screenId_, gamut);
213 }
214
SetScreenGamutMap(GraphicGamutMap gamutMap) const215 int32_t HdiScreen::SetScreenGamutMap(GraphicGamutMap gamutMap) const
216 {
217 CHECK_DEVICE_NULL(device_);
218 return device_->SetScreenGamutMap(screenId_, gamutMap);
219 }
220
GetScreenGamutMap(GraphicGamutMap & gamutMap) const221 int32_t HdiScreen::GetScreenGamutMap(GraphicGamutMap &gamutMap) const
222 {
223 CHECK_DEVICE_NULL(device_);
224 return device_->GetScreenGamutMap(screenId_, gamutMap);
225 }
226
SetScreenColorTransform(const std::vector<float> & matrix) const227 int32_t HdiScreen::SetScreenColorTransform(const std::vector<float>& matrix) const
228 {
229 CHECK_DEVICE_NULL(device_);
230 return device_->SetScreenColorTransform(screenId_, matrix);
231 }
232
GetHDRCapabilityInfos(GraphicHDRCapability & info) const233 int32_t HdiScreen::GetHDRCapabilityInfos(GraphicHDRCapability &info) const
234 {
235 CHECK_DEVICE_NULL(device_);
236 return device_->GetHDRCapabilityInfos(screenId_, info);
237 }
238
GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> & keys) const239 int32_t HdiScreen::GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> &keys) const
240 {
241 CHECK_DEVICE_NULL(device_);
242 return device_->GetSupportedMetaDataKey(screenId_, keys);
243 }
244
SetScreenConstraint(uint64_t frameId,uint64_t timestamp,uint32_t type)245 int32_t HdiScreen::SetScreenConstraint(uint64_t frameId, uint64_t timestamp, uint32_t type)
246 {
247 CHECK_DEVICE_NULL(device_);
248 return device_->SetScreenConstraint(screenId_, frameId, timestamp, type);
249 }
250
251 } // namespace Rosen
252 } // namespace OHOS
253