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, uint32_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,uint32_t frameCount)46 void Window::OnVsync(uint64_t nanoTimestamp, uint32_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 
SetUiDvsyncSwitch(bool dvsyncSwitch)66 void Window::SetUiDvsyncSwitch(bool dvsyncSwitch)
67 {
68     if (!onShow_) {
69         return;
70     }
71     if (platformWindow_ != nullptr) {
72         platformWindow_->SetUiDvsyncSwitch(dvsyncSwitch);
73     }
74 }
75 } // namespace OHOS::Ace
76