• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         return;
37     }
38     if (!isRequestVsync_ && platformWindow_ != nullptr) {
39         platformWindow_->RequestFrame();
40         isRequestVsync_ = true;
41     }
42 }
43 
SetRootRenderNode(const RefPtr<RenderNode> & root)44 void Window::SetRootRenderNode(const RefPtr<RenderNode>& root)
45 {
46     CHECK_NULL_VOID(platformWindow_);
47     platformWindow_->SetRootRenderNode(root);
48 }
49 
OnVsync(uint64_t nanoTimestamp,uint32_t frameCount)50 void Window::OnVsync(uint64_t nanoTimestamp, uint32_t frameCount)
51 {
52     isRequestVsync_ = false;
53 
54     for (auto& callback : callbacks_) {
55         if (callback.callback_ == nullptr) {
56             continue;
57         }
58         callback.callback_(nanoTimestamp, frameCount);
59     }
60 }
61 
SetVsyncCallback(AceVsyncCallback && callback)62 void Window::SetVsyncCallback(AceVsyncCallback&& callback)
63 {
64     callbacks_.push_back({
65         .callback_ = std::move(callback),
66         .containerId_ = Container::CurrentId(),
67     });
68 }
69 } // namespace OHOS::Ace
70