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