1 /*
2  * Copyright (c) 2021-2022 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 "ui/rs_root_node.h"
17 
18 #include "command/rs_root_node_command.h"
19 #include "pipeline/rs_node_map.h"
20 #include "platform/common/rs_log.h"
21 #include "transaction/rs_interfaces.h"
22 #include "transaction/rs_transaction_proxy.h"
23 #include "ui/rs_surface_node.h"
24 #include "ui/rs_ui_director.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
RegisterTypefaceCallback()29 bool RegisterTypefaceCallback()
30 {
31     static std::once_flag flag;
32     std::call_once(flag, []() {
33         std::function<bool (std::shared_ptr<Drawing::Typeface>)> registerTypefaceFunc =
34             [] (std::shared_ptr<Drawing::Typeface> typeface) -> bool {
35                 static Rosen::RSInterfaces& interface = Rosen::RSInterfaces::GetInstance();
36                 return interface.RegisterTypeface(typeface);
37             };
38         Drawing::Typeface::RegisterCallBackFunc(registerTypefaceFunc);
39 
40         std::function<bool (std::shared_ptr<Drawing::Typeface>)> unregisterTypefaceFunc =
41             [] (std::shared_ptr<Drawing::Typeface> typeface) -> bool {
42                 static Rosen::RSInterfaces& interface = Rosen::RSInterfaces::GetInstance();
43                 return interface.UnRegisterTypeface(typeface);
44             };
45         Drawing::Typeface::UnRegisterCallBackFunc(unregisterTypefaceFunc);
46     });
47     return true;
48 }
49 
50 #ifndef ARKUI_X_ENABLE
51 // Prohibiting resigter the callback function in advance when arkui-x use custom's font
52 bool g_typefaceAutoRegister = RegisterTypefaceCallback();
53 #endif
54 }
55 
Create(bool isRenderServiceNode,bool isTextureExportNode)56 std::shared_ptr<RSNode> RSRootNode::Create(bool isRenderServiceNode, bool isTextureExportNode)
57 {
58     RegisterTypefaceCallback();
59 
60     std::shared_ptr<RSRootNode> node(new RSRootNode(isRenderServiceNode, isTextureExportNode));
61     RSNodeMap::MutableInstance().RegisterNode(node);
62 
63     auto transactionProxy = RSTransactionProxy::GetInstance();
64     if (transactionProxy == nullptr) {
65         return node;
66     }
67     std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeCreate>(node->GetId(), isTextureExportNode);
68     transactionProxy->AddCommand(command, node->IsRenderServiceNode());
69     return node;
70 }
71 
RSRootNode(bool isRenderServiceNode,bool isSamelayerRender)72 RSRootNode::RSRootNode(bool isRenderServiceNode, bool isSamelayerRender)
73     : RSCanvasNode(isRenderServiceNode, isSamelayerRender) {}
74 
AttachRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode) const75 void RSRootNode::AttachRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode) const
76 {
77     auto transactionProxy = RSTransactionProxy::GetInstance();
78     if (transactionProxy == nullptr) {
79         return;
80     }
81     if (!IsUniRenderEnabled() || isTextureExportNode_) {
82         std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeAttachRSSurfaceNode>(GetId(),
83             surfaceNode->GetId());
84         transactionProxy->AddCommand(command, false);
85     } else {
86         std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeAttachToUniSurfaceNode>(GetId(),
87             surfaceNode->GetId());
88         transactionProxy->AddCommand(command, true);
89     }
90 }
91 
SetEnableRender(bool flag) const92 void RSRootNode::SetEnableRender(bool flag) const
93 {
94     auto transactionProxy = RSTransactionProxy::GetInstance();
95     if (transactionProxy == nullptr) {
96         return;
97     }
98     std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeSetEnableRender>(GetId(), flag);
99     transactionProxy->AddCommand(command, IsRenderServiceNode());
100     if (!isTextureExportNode_) {
101         transactionProxy->FlushImplicitTransaction();
102     }
103 }
104 
OnBoundsSizeChanged() const105 void RSRootNode::OnBoundsSizeChanged() const
106 {
107     if (IsUniRenderEnabled() && !isTextureExportNode_) {
108         return;
109     }
110     // Planning: we should use frame size instead of bounds size to calculate the surface size.
111     auto bounds = GetStagingProperties().GetBounds();
112     // Set RootNode Surface Size with animation final value. NOTE: this logic is only used in RenderThreadVisitor
113     std::unique_ptr<RSCommand> command =
114         std::make_unique<RSRootNodeUpdateSuggestedBufferSize>(GetId(), bounds.z_, bounds.w_);
115     auto transactionProxy = RSTransactionProxy::GetInstance();
116     if (transactionProxy != nullptr) {
117         transactionProxy->AddCommand(command, false);
118     }
119 }
120 } // namespace Rosen
121 } // namespace OHOS
122