• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetScreenActiveRect(const GraphicIRect & activeRect)155 int32_t HdiScreen::SetScreenActiveRect(const GraphicIRect& activeRect)
156 {
157     std::unique_lock<std::mutex> locker(mutex_);
158     CHECK_DEVICE_NULL(device_);
159     return device_->SetScreenActiveRect(screenId_, activeRect);
160 }
161 
SetScreenOverlayResolution(uint32_t width,uint32_t height) const162 int32_t HdiScreen::SetScreenOverlayResolution(uint32_t width, uint32_t height) const
163 {
164     CHECK_DEVICE_NULL(device_);
165     return device_->SetScreenOverlayResolution(screenId_, width, height);
166 }
167 
GetScreenPowerStatus(GraphicDispPowerStatus & status) const168 int32_t HdiScreen::GetScreenPowerStatus(GraphicDispPowerStatus &status) const
169 {
170     CHECK_DEVICE_NULL(device_);
171     return device_->GetScreenPowerStatus(screenId_, status);
172 }
173 
SetScreenPowerStatus(GraphicDispPowerStatus status) const174 int32_t HdiScreen::SetScreenPowerStatus(GraphicDispPowerStatus status) const
175 {
176     CHECK_DEVICE_NULL(device_);
177     return device_->SetScreenPowerStatus(screenId_, status);
178 }
179 
GetScreenBacklight(uint32_t & level) const180 int32_t HdiScreen::GetScreenBacklight(uint32_t &level) const
181 {
182     CHECK_DEVICE_NULL(device_);
183     return device_->GetScreenBacklight(screenId_, level);
184 }
185 
SetScreenBacklight(uint32_t level) const186 int32_t HdiScreen::SetScreenBacklight(uint32_t level) const
187 {
188     CHECK_DEVICE_NULL(device_);
189     return device_->SetScreenBacklight(screenId_, level);
190 }
191 
SetScreenVsyncEnabled(bool enabled) const192 int32_t HdiScreen::SetScreenVsyncEnabled(bool enabled) const
193 {
194     CHECK_DEVICE_NULL(device_);
195     int32_t ret = device_->SetScreenVsyncEnabled(screenId_, enabled);
196     if (ret != HDF_SUCCESS) {
197         HLOGE("SetScreenVsyncEnabled Failed, screenId:%{public}u, enabled:%{public}d, ret:%{public}d",
198             screenId_, enabled, ret);
199         RS_TRACE_NAME_FMT("SetScreenVsyncEnabled Failed, screenId:%u, enabled:%d, ret:%d", screenId_, enabled, ret);
200     }
201     return ret;
202 }
203 
GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> & gamuts) const204 int32_t HdiScreen::GetScreenSupportedColorGamuts(std::vector<GraphicColorGamut> &gamuts) const
205 {
206     CHECK_DEVICE_NULL(device_);
207     return device_->GetScreenSupportedColorGamuts(screenId_, gamuts);
208 }
209 
SetScreenColorGamut(GraphicColorGamut gamut) const210 int32_t HdiScreen::SetScreenColorGamut(GraphicColorGamut gamut) const
211 {
212     CHECK_DEVICE_NULL(device_);
213     return device_->SetScreenColorGamut(screenId_, gamut);
214 }
215 
GetScreenColorGamut(GraphicColorGamut & gamut) const216 int32_t HdiScreen::GetScreenColorGamut(GraphicColorGamut &gamut) const
217 {
218     CHECK_DEVICE_NULL(device_);
219     return device_->GetScreenColorGamut(screenId_, gamut);
220 }
221 
SetScreenGamutMap(GraphicGamutMap gamutMap) const222 int32_t HdiScreen::SetScreenGamutMap(GraphicGamutMap gamutMap) const
223 {
224     CHECK_DEVICE_NULL(device_);
225     return device_->SetScreenGamutMap(screenId_, gamutMap);
226 }
227 
GetScreenGamutMap(GraphicGamutMap & gamutMap) const228 int32_t HdiScreen::GetScreenGamutMap(GraphicGamutMap &gamutMap) const
229 {
230     CHECK_DEVICE_NULL(device_);
231     return device_->GetScreenGamutMap(screenId_, gamutMap);
232 }
233 
SetScreenColorTransform(const std::vector<float> & matrix) const234 int32_t HdiScreen::SetScreenColorTransform(const std::vector<float>& matrix) const
235 {
236     CHECK_DEVICE_NULL(device_);
237     return device_->SetScreenColorTransform(screenId_, matrix);
238 }
239 
GetHDRCapabilityInfos(GraphicHDRCapability & info) const240 int32_t HdiScreen::GetHDRCapabilityInfos(GraphicHDRCapability &info) const
241 {
242     CHECK_DEVICE_NULL(device_);
243     return device_->GetHDRCapabilityInfos(screenId_, info);
244 }
245 
GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> & keys) const246 int32_t HdiScreen::GetSupportedMetaDataKey(std::vector<GraphicHDRMetadataKey> &keys) const
247 {
248     CHECK_DEVICE_NULL(device_);
249     return device_->GetSupportedMetaDataKey(screenId_, keys);
250 }
251 
SetScreenConstraint(uint64_t frameId,uint64_t timestamp,uint32_t type)252 int32_t HdiScreen::SetScreenConstraint(uint64_t frameId, uint64_t timestamp, uint32_t type)
253 {
254     CHECK_DEVICE_NULL(device_);
255     return device_->SetScreenConstraint(screenId_, frameId, timestamp, type);
256 }
257 
258 } // namespace Rosen
259 } // namespace OHOS
260