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 "base/log/log.h"
19 #include "base/utils/macros.h"
20 #include "base/utils/utils.h"
21 #include "core/common/container.h"
22 #include "core/common/container_scope.h"
23
24 namespace OHOS::Ace {
Window(std::unique_ptr<PlatformWindow> platformWindow)25 Window::Window(std::unique_ptr<PlatformWindow> platformWindow) : platformWindow_(std::move(platformWindow))
26 {
27 CHECK_NULL_VOID(platformWindow_);
28 auto&& callback = [this](uint64_t nanoTimestamp, uint32_t frameCount) { OnVsync(nanoTimestamp, frameCount); };
29 platformWindow_->RegisterVsyncCallback(callback);
30 LOGI("Window Created success.");
31 }
32
RequestFrame()33 void Window::RequestFrame()
34 {
35 if (!onShow_) {
36 LOGD("window is not show, stop request frame");
37 return;
38 }
39 if (!isRequestVsync_ && platformWindow_ != nullptr) {
40 LOGD("request next vsync");
41 platformWindow_->RequestFrame();
42 isRequestVsync_ = true;
43 }
44 }
45
SetRootRenderNode(const RefPtr<RenderNode> & root)46 void Window::SetRootRenderNode(const RefPtr<RenderNode>& root)
47 {
48 CHECK_NULL_VOID(platformWindow_);
49 platformWindow_->SetRootRenderNode(root);
50 }
51
OnVsync(uint64_t nanoTimestamp,uint32_t frameCount)52 void Window::OnVsync(uint64_t nanoTimestamp, uint32_t frameCount)
53 {
54 isRequestVsync_ = false;
55
56 for (auto& callback : callbacks_) {
57 if (callback.callback_ == nullptr) {
58 continue;
59 }
60 callback.callback_(nanoTimestamp, frameCount);
61 }
62 }
63
SetVsyncCallback(AceVsyncCallback && callback)64 void Window::SetVsyncCallback(AceVsyncCallback&& callback)
65 {
66 callbacks_.push_back({
67 .callback_ = std::move(callback),
68 .containerId_ = Container::CurrentId(),
69 });
70 }
71 } // namespace OHOS::Ace
72