• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "core/common/window.h"
17 
18 #include "core/common/container.h"
19 
20 namespace OHOS::Ace {
Window(std::unique_ptr<PlatformWindow> platformWindow)21 Window::Window(std::unique_ptr<PlatformWindow> platformWindow) : platformWindow_(std::move(platformWindow))
22 {
23     CHECK_NULL_VOID(platformWindow_);
24     auto&& callback = [this](uint64_t nanoTimestamp, uint64_t frameCount) { OnVsync(nanoTimestamp, frameCount); };
25     platformWindow_->RegisterVsyncCallback(callback);
26     LOGI("Window Created success.");
27 }
28 
RequestFrame()29 void Window::RequestFrame()
30 {
31     if (!onShow_) {
32         return;
33     }
34     if (!isRequestVsync_ && platformWindow_ != nullptr) {
35         platformWindow_->RequestFrame();
36         isRequestVsync_ = true;
37     }
38 }
39 
SetRootRenderNode(const RefPtr<RenderNode> & root)40 void Window::SetRootRenderNode(const RefPtr<RenderNode>& root)
41 {
42     CHECK_NULL_VOID(platformWindow_);
43     platformWindow_->SetRootRenderNode(root);
44 }
45 
OnVsync(uint64_t nanoTimestamp,uint64_t frameCount)46 void Window::OnVsync(uint64_t nanoTimestamp, uint64_t frameCount)
47 {
48     isRequestVsync_ = false;
49 
50     for (auto& callback : callbacks_) {
51         if (callback.callback_ == nullptr) {
52             continue;
53         }
54         callback.callback_(nanoTimestamp, frameCount);
55     }
56 }
57 
SetVsyncCallback(AceVsyncCallback && callback)58 void Window::SetVsyncCallback(AceVsyncCallback&& callback)
59 {
60     callbacks_.push_back({
61         .callback_ = std::move(callback),
62         .containerId_ = Container::CurrentId(),
63     });
64 }
65 
SetForceVsyncRequests(bool forceVsyncRequests)66 void Window::SetForceVsyncRequests(bool forceVsyncRequests)
67 {
68     forceVsync_ = forceVsyncRequests;
69 }
70 
SetUiDvsyncSwitch(bool dvsyncSwitch)71 void Window::SetUiDvsyncSwitch(bool dvsyncSwitch)
72 {
73     if (!onShow_) {
74         return;
75     }
76     if (platformWindow_ != nullptr) {
77         platformWindow_->SetUiDvsyncSwitch(dvsyncSwitch);
78     }
79 }
80 
GetDeadlineByFrameCount(int64_t deadline,int64_t ts,int64_t frameBufferCount)81 int64_t Window::GetDeadlineByFrameCount(int64_t deadline, int64_t ts, int64_t frameBufferCount)
82 {
83     if (!dvsyncOn_) {
84         return deadline;
85     }
86 
87     constexpr int64_t MAX_INBIHIT_PREDICT_DUR = 100 * 1000000;
88     int64_t lastInbihitPredictTs = lastDVsyncInbihitPredictTs_;
89     if (frameBufferCount < 1) {
90         if (lastInbihitPredictTs == 0 || (ts - lastInbihitPredictTs < MAX_INBIHIT_PREDICT_DUR)) {
91             // 100ms, inbihit
92             deadline = 0;
93         }
94         if (lastInbihitPredictTs == 0) {
95             lastDVsyncInbihitPredictTs_ = ts;
96         }
97     } else {
98         if (lastInbihitPredictTs != 0) {
99             lastDVsyncInbihitPredictTs_ = 0;
100         }
101     }
102     return deadline;
103 }
104 
GetCalcWidthBreakpoint(const OHOS::Ace::WidthLayoutBreakPoint & finalBreakpoints,double density,double width)105 WidthBreakpoint GetCalcWidthBreakpoint(
106     const OHOS::Ace::WidthLayoutBreakPoint &finalBreakpoints, double density, double width)
107 {
108     WidthBreakpoint breakpoint;
109     if (finalBreakpoints.widthVPXS_ < 0 || GreatNotEqual(finalBreakpoints.widthVPXS_ * density, width)) {
110         breakpoint = WidthBreakpoint::WIDTH_XS;
111     } else if (finalBreakpoints.widthVPSM_ < 0 || GreatNotEqual(finalBreakpoints.widthVPSM_ * density, width)) {
112         breakpoint = WidthBreakpoint::WIDTH_SM;
113     } else if (finalBreakpoints.widthVPMD_ < 0 || GreatNotEqual(finalBreakpoints.widthVPMD_ * density, width)) {
114         breakpoint = WidthBreakpoint::WIDTH_MD;
115     } else if (finalBreakpoints.widthVPLG_ < 0 || GreatNotEqual(finalBreakpoints.widthVPLG_ * density, width)) {
116         breakpoint = WidthBreakpoint::WIDTH_LG;
117     } else if (finalBreakpoints.widthVPXL_ < 0 || GreatNotEqual(finalBreakpoints.widthVPXL_ * density, width)) {
118         breakpoint = WidthBreakpoint::WIDTH_XL;
119     } else {
120         breakpoint = WidthBreakpoint::WIDTH_XXL;
121     }
122     return breakpoint;
123 }
124 
GetWidthBreakpoint(const WidthLayoutBreakPoint & layoutBreakpoints) const125 WidthBreakpoint Window::GetWidthBreakpoint(const WidthLayoutBreakPoint &layoutBreakpoints) const
126 {
127     auto width = GetCurrentWindowRect().Width();
128     auto pipeline = PipelineBase::GetCurrentContextSafelyWithCheck();
129     if (pipeline) {
130         width = pipeline->CalcPageWidth(width);
131     }
132     double density = PipelineBase::GetCurrentDensity();
133     return GetCalcWidthBreakpoint(layoutBreakpoints, density, width);
134 }
135 
GetHeightBreakpoint(const HeightLayoutBreakPoint & layoutBreakpoints) const136 HeightBreakpoint Window::GetHeightBreakpoint(const HeightLayoutBreakPoint& layoutBreakpoints) const
137 {
138     auto width = GetCurrentWindowRect().Width();
139     auto pipeline = PipelineBase::GetCurrentContextSafelyWithCheck();
140     if (pipeline) {
141         width = pipeline->CalcPageWidth(width);
142     }
143     auto height = GetCurrentWindowRect().Height();
144     auto aspectRatio = 0.0;
145     if (NearZero(width)) {
146         aspectRatio = 0.0;
147     } else {
148         aspectRatio = height / width;
149     }
150 
151     HeightBreakpoint breakpoint;
152     if (aspectRatio < layoutBreakpoints.heightVPRATIOSM_) {
153         breakpoint = HeightBreakpoint::HEIGHT_SM;
154     } else if (aspectRatio < layoutBreakpoints.heightVPRATIOMD_) {
155         breakpoint = HeightBreakpoint::HEIGHT_MD;
156     } else {
157         breakpoint = HeightBreakpoint::HEIGHT_LG;
158     }
159     return breakpoint;
160 }
161 } // namespace OHOS::Ace
162