1 /*
2  * Copyright (c) 2022 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_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_WINDOW_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_WINDOW_H
18 
19 #include "render_service_client/core/ui/rs_ui_director.h"
20 
21 #ifdef OHOS_PLATFORM
22 #include "vsync_receiver.h"
23 #include "wm/window.h"
24 #elif VIRTUAL_RS_WINDOW
25 // use real rs window later
26 #ifdef IOS_PLATFORM
27 #include "adapter/ios/entrance/virtual_rs_window.h"
28 #else
29 #include "adapter/android/entrance/java/jni/virtual_rs_window.h"
30 #endif
31 #else
32 #include "previewer/include/window.h"
33 #endif
34 
35 #include "base/thread/task_executor.h"
36 #include "base/utils/noncopyable.h"
37 #include "core/common/window.h"
38 
39 namespace OHOS::Ace::NG {
40 
41 class RosenWindow : public Window {
42 public:
43     RosenWindow(const OHOS::sptr<OHOS::Rosen::Window>& window, RefPtr<TaskExecutor> taskExecutor, int32_t id);
44     ~RosenWindow() override = default;
45 
46     void RequestFrame() override;
47 
48     void Init() override;
49 
50     void Destroy() override;
51 
SetRootRenderNode(const RefPtr<RenderNode> & root)52     void SetRootRenderNode(const RefPtr<RenderNode>& root) override {}
53 
54     void SetRootFrameNode(const RefPtr<NG::FrameNode>& root) override;
55 
56     void FlushFrameRate(int32_t rate, int32_t animatorExpectedFrameRate, int32_t rateType) override;
57 
GetRSUIDirector()58     std::shared_ptr<Rosen::RSUIDirector> GetRSUIDirector() const override
59     {
60         return rsUIDirector_;
61     }
62 
GetRSWindow()63     sptr<Rosen::Window> GetRSWindow() const
64     {
65         return rsWindow_;
66     }
67 
68     void RecordFrameTime(uint64_t timeStamp, const std::string& name) override;
69 
70     void FlushTasks() override;
71 
72     void SetTaskRunner(RefPtr<TaskExecutor> taskExecutor, int32_t id);
73 
74     void FlushLayoutSize(int32_t width, int32_t height) override;
75 
FlushAnimation(uint64_t timeStamp)76     bool FlushAnimation(uint64_t timeStamp) override
77     {
78         int64_t vsyncPeriod = GetVSyncPeriod();
79         return rsUIDirector_->FlushAnimation(timeStamp, vsyncPeriod);
80     }
81 
FlushAnimationStartTime(uint64_t timeStamp)82     void FlushAnimationStartTime(uint64_t timeStamp) override
83     {
84         rsUIDirector_->FlushAnimationStartTime(timeStamp);
85     }
86 
FlushModifier()87     void FlushModifier() override
88     {
89         rsUIDirector_->FlushModifier();
90     }
91 
HasUIRunningAnimation()92     bool HasUIRunningAnimation() override
93     {
94         return rsUIDirector_->HasUIRunningAnimation();
95     }
96 
97     void OnShow() override;
98     void OnHide() override;
99 
100     void SetDrawTextAsBitmap(bool useBitmap) override;
101 
102     float GetRefreshRate() const override;
103 
104     void SetKeepScreenOn(bool keepScreenOn) override;
105 
106     int64_t GetVSyncPeriod() const override;
107 
108     std::string GetWindowName() const override;
109 
GetCurrentRefreshRateMode()110     int32_t GetCurrentRefreshRateMode() const override
111     {
112         return rsUIDirector_->GetCurrentRefreshRateMode();
113     }
114 
GetAnimateExpectedRate()115     int32_t GetAnimateExpectedRate() const override
116     {
117         return rsUIDirector_->GetAnimateExpectedRate();
118     }
119     void SetUiDvsyncSwitch(bool vsyncSwitch) override;
120 
121     void OnVsync(uint64_t nanoTimestamp, uint32_t frameCount) override;
122 
123 private:
124     OHOS::sptr<OHOS::Rosen::Window> rsWindow_;
125     WeakPtr<TaskExecutor> taskExecutor_;
126     int32_t id_ = 0;
127     std::shared_ptr<OHOS::Rosen::RSUIDirector> rsUIDirector_;
128     std::shared_ptr<OHOS::Rosen::VsyncCallback> vsyncCallback_;
129     bool isFirstRequestVsync_ = true;
130 
131     ACE_DISALLOW_COPY_AND_MOVE(RosenWindow);
132 };
133 
134 } // namespace OHOS::Ace::NG
135 
136 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_WINDOW_H
137