• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_context.h"
25 #include "ui/rs_ui_director.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
RegisterTypefaceCallback()30 bool RegisterTypefaceCallback()
31 {
32     static std::once_flag flag;
33     std::call_once(flag, []() {
34         std::function<bool (std::shared_ptr<Drawing::Typeface>)> registerTypefaceFunc =
35             [] (std::shared_ptr<Drawing::Typeface> typeface) -> bool {
36                 static Rosen::RSInterfaces& rsInterface = Rosen::RSInterfaces::GetInstance();
37                 return rsInterface.RegisterTypeface(typeface);
38             };
39         Drawing::Typeface::RegisterCallBackFunc(registerTypefaceFunc);
40 
41         std::function<bool (std::shared_ptr<Drawing::Typeface>)> unregisterTypefaceFunc =
42             [] (std::shared_ptr<Drawing::Typeface> typeface) -> bool {
43                 static Rosen::RSInterfaces& rsInterface = Rosen::RSInterfaces::GetInstance();
44                 return rsInterface.UnRegisterTypeface(typeface);
45             };
46         Drawing::Typeface::UnRegisterCallBackFunc(unregisterTypefaceFunc);
47     });
48     return true;
49 }
50 
51 class TypefaceAutoRegister {
52 public:
TypefaceAutoRegister()53     TypefaceAutoRegister()
54     {
55         RegisterTypefaceCallback();
56     }
57 
~TypefaceAutoRegister()58     ~TypefaceAutoRegister()
59     {
60         Drawing::Typeface::RegisterCallBackFunc(nullptr);
61         Drawing::Typeface::UnRegisterCallBackFunc(nullptr);
62     }
63 };
64 
65 #ifndef ARKUI_X_ENABLE
66 // Prohibiting resigter the callback function in advance when arkui-x use custom's font
67 TypefaceAutoRegister g_typefaceAutoRegister;
68 #endif
69 }
70 
Create(bool isRenderServiceNode,bool isTextureExportNode,std::shared_ptr<RSUIContext> rsUIContext)71 std::shared_ptr<RSNode> RSRootNode::Create(
72     bool isRenderServiceNode, bool isTextureExportNode, std::shared_ptr<RSUIContext> rsUIContext)
73 {
74     RegisterTypefaceCallback();
75 
76     std::shared_ptr<RSRootNode> node(new RSRootNode(isRenderServiceNode, isTextureExportNode, rsUIContext));
77     if (rsUIContext != nullptr) {
78         rsUIContext->GetMutableNodeMap().RegisterNode(node);
79         auto transaction = rsUIContext->GetRSTransaction();
80         if (transaction == nullptr) {
81             return node;
82         }
83         std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeCreate>(node->GetId(), isTextureExportNode);
84         transaction->AddCommand(command, node->IsRenderServiceNode());
85     } else {
86         RSNodeMap::MutableInstance().RegisterNode(node);
87         auto transactionProxy = RSTransactionProxy::GetInstance();
88         if (transactionProxy == nullptr) {
89             return node;
90         }
91         std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeCreate>(node->GetId(), isTextureExportNode);
92         transactionProxy->AddCommand(command, node->IsRenderServiceNode());
93     }
94     node->SetUIContextToken();
95     return node;
96 }
97 
RegisterNodeMap()98 void RSRootNode::RegisterNodeMap()
99 {
100     auto rsContext = GetRSUIContext();
101     if (rsContext == nullptr) {
102         return;
103     }
104     auto& nodeMap = rsContext->GetMutableNodeMap();
105     nodeMap.RegisterNode(shared_from_this());
106 }
107 
RSRootNode(bool isRenderServiceNode,bool isSamelayerRender,std::shared_ptr<RSUIContext> rsUIContext)108 RSRootNode::RSRootNode(bool isRenderServiceNode, bool isSamelayerRender, std::shared_ptr<RSUIContext> rsUIContext)
109     : RSCanvasNode(isRenderServiceNode, isSamelayerRender, rsUIContext) {}
110 
AttachRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode)111 void RSRootNode::AttachRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode)
112 {
113     if (!IsUniRenderEnabled() || isTextureExportNode_) {
114         std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeAttachRSSurfaceNode>(GetId(),
115             surfaceNode->GetId(), surfaceNode->GetRSUIContext() ? surfaceNode->GetRSUIContext()->GetToken() : 0);
116         AddCommand(command, false);
117     } else {
118         std::unique_ptr<RSCommand> command =
119             std::make_unique<RSRootNodeAttachToUniSurfaceNode>(GetId(), surfaceNode->GetId());
120         AddCommand(command, true);
121     }
122     SetIsOnTheTree(surfaceNode->GetIsOnTheTree());
123 }
124 
SetEnableRender(bool flag) const125 void RSRootNode::SetEnableRender(bool flag) const
126 {
127     std::unique_ptr<RSCommand> command = std::make_unique<RSRootNodeSetEnableRender>(GetId(), flag);
128     auto transaction = GetRSTransaction();
129     if (transaction != nullptr) {
130         transaction->AddCommand(command, IsRenderServiceNode());
131         if (!isTextureExportNode_) {
132             transaction->FlushImplicitTransaction();
133         }
134     } else {
135         auto transactionProxy = RSTransactionProxy::GetInstance();
136         if (transactionProxy != nullptr) {
137             transactionProxy->AddCommand(command, IsRenderServiceNode());
138             if (!isTextureExportNode_) {
139                 transactionProxy->FlushImplicitTransaction();
140             }
141         }
142     }
143 }
144 
OnBoundsSizeChanged() const145 void RSRootNode::OnBoundsSizeChanged() const
146 {
147     if (IsUniRenderEnabled() && !isTextureExportNode_) {
148         return;
149     }
150     // Planning: we should use frame size instead of bounds size to calculate the surface size.
151     auto bounds = GetStagingProperties().GetBounds();
152     // Set RootNode Surface Size with animation final value. NOTE: this logic is only used in RenderThreadVisitor
153     std::unique_ptr<RSCommand> command =
154         std::make_unique<RSRootNodeUpdateSuggestedBufferSize>(GetId(), bounds.z_, bounds.w_);
155     AddCommand(command, false);
156 }
157 } // namespace Rosen
158 } // namespace OHOS
159