1 /*
2  * Copyright (c) 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 OHOS_ROSEN_ROOT_SCENE_H
17 #define OHOS_ROSEN_ROOT_SCENE_H
18 
19 #include <mutex>
20 
21 #include "vsync_station.h"
22 #include "window.h"
23 #include "ws_common.h"
24 
25 typedef struct napi_env__* napi_env;
26 typedef struct napi_value__* napi_value;
27 namespace OHOS::AppExecFwk {
28 class EventHandler;
29 class LauncherService;
30 } // namespace OHOS::AppExecFwk
31 
32 namespace OHOS::Ace {
33 class UIContent;
34 } // namespace OHOS::Ace
35 
36 namespace OHOS {
37 namespace Rosen {
38 using GetSessionRectCallback = std::function<WSRect(AvoidAreaType)>;
39 
40 class RootScene : public Window {
41 public:
42     RootScene();
43     virtual ~RootScene();
44 
45     void LoadContent(const std::string& contentUrl, napi_env env, napi_value storage,
46         AbilityRuntime::Context* context);
47     void UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason);
48     static void UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration);
49     void UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration) override;
50 
51     void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback) override;
52     int64_t GetVSyncPeriod() override;
53     void FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType = 0) override;
54 
55     /**
56      * Window Immersive
57      */
58     bool IsLastFrameLayoutFinished();
59     void OnFlushUIParams();
60 
61     void OnBundleUpdated(const std::string& bundleName);
62     static void SetOnConfigurationUpdatedCallback(
63         const std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)>& callback);
64     void SetFrameLayoutFinishCallback(std::function<void()>&& callback);
65 
SetGetSessionRectCallback(GetSessionRectCallback && callback)66     void SetGetSessionRectCallback(GetSessionRectCallback&& callback)
67     {
68         getSessionRectCallback_ = std::move(callback);
69     }
70 
SetDisplayDensity(float density)71     void SetDisplayDensity(float density)
72     {
73         density_ = density;
74     }
75 
76     void SetDisplayOrientation(int32_t orientation);
77 
GetDisplayDensity()78     float GetDisplayDensity()
79     {
80         return density_;
81     }
82 
GetWindowState()83     WindowState GetWindowState() const override
84     {
85         return WindowState::STATE_SHOWN;
86     }
87 
GetType()88     WindowType GetType() const override
89     {
90         return type_;
91     }
92 
GetWindowName()93     const std::string& GetWindowName() const override
94     {
95         return name_;
96     }
97 
GetWindowId()98     uint32_t GetWindowId() const override
99     {
100         return 1; // 1 for root
101     }
102 
GetUIContent()103     Ace::UIContent* GetUIContent() const override
104     {
105         return uiContent_.get();
106     }
107 
108     void SetUiDvsyncSwitch(bool dvsyncSwitch) override;
109 
110     WMError GetSessionRectByType(AvoidAreaType type, WSRect& rect);
111 
112 	/*
113      * Window Property
114      */
115     static void UpdateConfigurationSyncForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration);
116     void UpdateConfigurationSync(const std::shared_ptr<AppExecFwk::Configuration>& configuration) override;
117 
118     static sptr<RootScene> staticRootScene_;
119 
GetClassType()120     std::string GetClassType() const override { return "RootScene"; }
121 
122 private:
123     void RegisterInputEventListener();
124 
125     std::unique_ptr<Ace::UIContent> uiContent_;
126     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
127     sptr<AppExecFwk::LauncherService> launcherService_;
128     float density_ = 1.0f;
129     int32_t orientation_ = 0;
130     WindowType type_ = WindowType::WINDOW_TYPE_SCENE_BOARD;
131     std::string name_ = "EntryView";
132 
133     static std::function<void(const std::shared_ptr<AppExecFwk::Configuration>&)> configurationUpdatedCallback_;
134     std::function<void()> frameLayoutFinishCb_ = nullptr;
135     std::shared_ptr<VsyncStation> vsyncStation_ = nullptr;
136 
137     GetSessionRectCallback getSessionRectCallback_ = nullptr;
138 };
139 } // namespace Rosen
140 } // namespace OHOS
141 
142 #endif // OHOS_ROSEN_ROOT_SCENE_H
143