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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_WINDOW_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_WINDOW_H
18 
19 #include <memory>
20 
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/mousestyle/mouse_style.h"
23 #include "base/thread/task_executor.h"
24 #include "base/utils/macros.h"
25 #include "base/utils/noncopyable.h"
26 #include "core/common/ace_page.h"
27 #include "core/common/platform_window.h"
28 
29 namespace OHOS::Rosen {
30 class RSUIDirector;
31 }
32 
33 namespace OHOS::Ace {
34 
35 namespace NG {
36 class FrameNode;
37 } // namespace NG
38 class ACE_EXPORT Window : public std::enable_shared_from_this<Window> {
39 public:
40     Window() = default;
41     explicit Window(std::unique_ptr<PlatformWindow> platformWindow);
42     virtual ~Window() = default;
43 
GetWindowId()44     uint32_t GetWindowId() const
45     {
46         return windowId_;
47     }
48 
49     virtual void RequestFrame();
50 
FlushFrameRate(int32_t rate,int32_t animatorExpectedFrameRate,int32_t rateTyte)51     virtual void FlushFrameRate(int32_t rate, int32_t animatorExpectedFrameRate, int32_t rateTyte) {}
52 
SetTaskExecutor(const RefPtr<TaskExecutor> & taskExecutor)53     virtual void SetTaskExecutor(const RefPtr<TaskExecutor>& taskExecutor) {}
54 
SetInstanceId(int32_t instanceId)55     virtual void SetInstanceId(int32_t instanceId) {}
56 
Init()57     virtual void Init() {}
58 
Destroy()59     virtual void Destroy()
60     {
61         platformWindow_->Destroy();
62         platformWindow_.reset();
63     }
64 
65     virtual void SetRootRenderNode(const RefPtr<RenderNode>& root);
66 
SetRootFrameNode(const RefPtr<NG::FrameNode> & root)67     virtual void SetRootFrameNode(const RefPtr<NG::FrameNode>& root) {}
68 
RecordFrameTime(uint64_t timeStamp,const std::string & name)69     virtual void RecordFrameTime(uint64_t timeStamp, const std::string& name) {}
70 
FlushTasks()71     virtual void FlushTasks() {}
72 
GetRSUIDirector()73     virtual std::shared_ptr<Rosen::RSUIDirector> GetRSUIDirector() const
74     {
75         return nullptr;
76     }
77 
FlushLayoutSize(int32_t width,int32_t height)78     virtual void FlushLayoutSize(int32_t width, int32_t height) {}
79 
FlushAnimation(uint64_t timeStamp)80     virtual bool FlushAnimation(uint64_t timeStamp)
81     {
82         return false;
83     }
84 
FlushAnimationStartTime(uint64_t timeStamp)85     virtual void FlushAnimationStartTime(uint64_t timeStamp) {}
86 
FlushModifier()87     virtual void FlushModifier() {}
88 
HasUIRunningAnimation()89     virtual bool HasUIRunningAnimation()
90     {
91         return false;
92     }
93 
94     virtual void OnVsync(uint64_t nanoTimestamp, uint32_t frameCount);
95 
96     virtual void SetVsyncCallback(AceVsyncCallback&& callback);
97 
OnShow()98     virtual void OnShow()
99     {
100         onShow_ = true;
101     }
102 
OnHide()103     virtual void OnHide()
104     {
105         onShow_ = false;
106     }
107 
IsHide()108     bool IsHide() const
109     {
110         return !onShow_;
111     }
112 
SetDensity(double density)113     void SetDensity(double density)
114     {
115         density_ = density;
116     }
117 
SetGetWindowRectImpl(std::function<Rect ()> && callback)118     void SetGetWindowRectImpl(std::function<Rect()>&& callback)
119     {
120         windowRectImpl_ = std::move(callback);
121     }
122 
GetCurrentWindowRect()123     virtual Rect GetCurrentWindowRect() const
124     {
125         Rect rect;
126         if (windowRectImpl_) {
127             rect = windowRectImpl_();
128         }
129         return rect;
130     }
131 
SetDrawTextAsBitmap(bool useBitmap)132     virtual void SetDrawTextAsBitmap(bool useBitmap) {}
133 
GetRefreshRate()134     virtual float GetRefreshRate() const
135     {
136         return 0.0f;
137     }
138 
GetLastRequestVsyncTime()139     uint64_t GetLastRequestVsyncTime() const
140     {
141         return lastRequestVsyncTime_;
142     }
143 
GetLastVsyncEndTimestamp()144     int64_t GetLastVsyncEndTimestamp() const
145     {
146         return lastVsyncEndTimestamp_;
147     }
148 
SetLastVsyncEndTimestamp(int64_t lastVsyncEndTimestamp)149     void SetLastVsyncEndTimestamp(int64_t lastVsyncEndTimestamp)
150     {
151         lastVsyncEndTimestamp_ = lastVsyncEndTimestamp;
152     }
153 
SetKeepScreenOn(bool keepScreenOn)154     virtual void SetKeepScreenOn(bool keepScreenOn) {};
155 
GetVSyncPeriod()156     virtual int64_t GetVSyncPeriod() const
157     {
158         return 0;
159     };
160 
GetWindowName()161     virtual std::string GetWindowName() const
162     {
163         static std::string name;
164         return name;
165     };
166 
SetCursor(MouseFormat cursor)167     void SetCursor(MouseFormat cursor)
168     {
169         cursor_ = cursor;
170     }
171 
GetCursor()172     MouseFormat GetCursor() const
173     {
174         return cursor_;
175     }
176 
SetUserSetCursor(bool isUserSetCursor)177     void SetUserSetCursor(bool isUserSetCursor)
178     {
179         isUserSetCursor_ = isUserSetCursor;
180     }
181 
IsUserSetCursor()182     bool IsUserSetCursor() const
183     {
184         return isUserSetCursor_;
185     }
186 
GetCurrentRefreshRateMode()187     virtual int32_t GetCurrentRefreshRateMode() const
188     {
189         return -1;
190     }
191 
GetAnimateExpectedRate()192     virtual int32_t GetAnimateExpectedRate() const
193     {
194         return 0;
195     }
196 
Lock()197     virtual void Lock() {}
198 
Unlock()199     virtual void Unlock() {}
200 
201     virtual void SetUiDvsyncSwitch(bool dvsyncSwitch);
202 
203 protected:
204     bool isRequestVsync_ = false;
205     bool onShow_ = true;
206     double density_ = 1.0;
207     MouseFormat cursor_ = MouseFormat::DEFAULT;
208     bool isUserSetCursor_ = false;
209 
210     struct VsyncCallback {
211         AceVsyncCallback callback_ = nullptr;
212         int32_t containerId_ = -1;
213     };
214     std::list<struct VsyncCallback> callbacks_;
215 
216     uint64_t lastRequestVsyncTime_ = 0;
217     int64_t lastVsyncEndTimestamp_ = 0;
218     uint32_t windowId_ = 0;
219 
220 private:
221     std::function<Rect()> windowRectImpl_;
222     std::unique_ptr<PlatformWindow> platformWindow_;
223 
224     ACE_DISALLOW_COPY_AND_MOVE(Window);
225 };
226 
227 } // namespace OHOS::Ace
228 
229 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_WINDOW_H
230