1 /*
2 * Copyright (c) 2021 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_ui_director.h"
17
18 #include "command/rs_animation_command.h"
19 #include "command/rs_message_processor.h"
20 #include "pipeline/rs_frame_report.h"
21 #include "pipeline/rs_node_map.h"
22 #include "pipeline/rs_render_thread.h"
23 #include "platform/common/rs_log.h"
24 #include "rs_trace.h"
25 #include "transaction/rs_interfaces.h"
26 #include "transaction/rs_transaction_proxy.h"
27 #include "ui/rs_root_node.h"
28 #include "ui/rs_surface_extractor.h"
29 #include "ui/rs_surface_node.h"
30
31 namespace OHOS {
32 namespace Rosen {
33 static TaskRunner g_uiTaskRunner;
34
Create()35 std::shared_ptr<RSUIDirector> RSUIDirector::Create()
36 {
37 return std::shared_ptr<RSUIDirector>(new RSUIDirector());
38 }
39
~RSUIDirector()40 RSUIDirector::~RSUIDirector()
41 {
42 Destory();
43 }
44
Init()45 void RSUIDirector::Init()
46 {
47 auto renderThreadClient = RSIRenderClient::CreateRenderThreadClient();
48 auto transactionProxy = RSTransactionProxy::GetInstance();
49 if (transactionProxy != nullptr) {
50 transactionProxy->SetRenderThreadClient(renderThreadClient);
51 }
52
53 AnimationCommandHelper::SetFinisCallbackProcessor(AnimationCallbackProcessor);
54
55 RsFrameReport::GetInstance().Init();
56 RSRenderThread::Instance().Start();
57 GoForeground();
58 }
59
GoForeground()60 void RSUIDirector::GoForeground()
61 {
62 ROSEN_LOGI("RSUIDirector::GoForeground");
63 if (!isActive_) {
64 RSRenderThread::Instance().UpdateWindowStatus(true);
65 isActive_ = true;
66 if (auto node = RSNodeMap::Instance().GetNode<RSRootNode>(root_)) {
67 node->SetVisible(true);
68 }
69 }
70 }
71
GoBackground()72 void RSUIDirector::GoBackground()
73 {
74 ROSEN_LOGI("RSUIDirector::GoBackground");
75 if (isActive_) {
76 RSRenderThread::Instance().UpdateWindowStatus(false);
77 isActive_ = false;
78 if (auto node = RSNodeMap::Instance().GetNode<RSRootNode>(root_)) {
79 node->SetVisible(false);
80 }
81 }
82 }
83
Destory()84 void RSUIDirector::Destory()
85 {
86 if (root_ != 0) {
87 RSRenderThread::Instance().Detach(root_);
88 root_ = 0;
89 }
90 GoBackground();
91 }
92
SetRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode)93 void RSUIDirector::SetRSSurfaceNode(std::shared_ptr<RSSurfaceNode> surfaceNode)
94 {
95 surfaceNode_ = surfaceNode;
96 AttachSurface();
97 }
98
SetRoot(NodeId root)99 void RSUIDirector::SetRoot(NodeId root)
100 {
101 if (root_ == root) {
102 ROSEN_LOGW("RSUIDirector::SetRoot, root_ is not change");
103 return;
104 }
105 root_ = root;
106 AttachSurface();
107 }
108
AttachSurface()109 void RSUIDirector::AttachSurface()
110 {
111 auto node = RSNodeMap::Instance().GetNode<RSRootNode>(root_);
112 if (node != nullptr && surfaceNode_ != nullptr) {
113 node->AttachRSSurfaceNode(surfaceNode_);
114 ROSEN_LOGD("RSUIDirector::AttachSurface [%llu]", surfaceNode_->GetId());
115 } else {
116 ROSEN_LOGD("RSUIDirector::AttachSurface not ready");
117 }
118 }
119
SetTimeStamp(uint64_t timeStamp)120 void RSUIDirector::SetTimeStamp(uint64_t timeStamp)
121 {
122 timeStamp_ = timeStamp;
123 }
124
SetUITaskRunner(const TaskRunner & uiTaskRunner)125 void RSUIDirector::SetUITaskRunner(const TaskRunner& uiTaskRunner)
126 {
127 g_uiTaskRunner = uiTaskRunner;
128 }
129
SendMessages()130 void RSUIDirector::SendMessages()
131 {
132 ROSEN_TRACE_BEGIN(BYTRACE_TAG_GRAPHIC_AGP, "SendCommands");
133 auto transactionProxy = RSTransactionProxy::GetInstance();
134 if (transactionProxy != nullptr) {
135 transactionProxy->FlushImplicitTransaction();
136 }
137 ROSEN_TRACE_END(BYTRACE_TAG_GRAPHIC_AGP);
138 }
139
RecvMessages()140 void RSUIDirector::RecvMessages()
141 {
142 if (getpid() == -1) {
143 return;
144 }
145 static const uint32_t pid = static_cast<uint32_t>(getpid());
146 if (!RSMessageProcessor::Instance().HasTransaction(pid)) {
147 return;
148 }
149 auto transactionDataPtr = std::make_shared<RSTransactionData>(RSMessageProcessor::Instance().GetTransaction(pid));
150 RecvMessages(transactionDataPtr);
151 }
152
RecvMessages(std::shared_ptr<RSTransactionData> cmds)153 void RSUIDirector::RecvMessages(std::shared_ptr<RSTransactionData> cmds)
154 {
155 if (g_uiTaskRunner == nullptr) {
156 ROSEN_LOGE("RSUIDirector::RecvMessages, Notify ui message failed, uiTaskRunner is null");
157 return;
158 }
159 if (cmds == nullptr || cmds->IsEmpty()) {
160 return;
161 }
162
163 g_uiTaskRunner([cmds]() { RSUIDirector::ProcessMessages(cmds); });
164 }
165
ProcessMessages(std::shared_ptr<RSTransactionData> cmds)166 void RSUIDirector::ProcessMessages(std::shared_ptr<RSTransactionData> cmds)
167 {
168 static RSContext context; // RSCommand->process() needs it
169 cmds->Process(context);
170 }
171
AnimationCallbackProcessor(NodeId nodeId,AnimationId animId)172 void RSUIDirector::AnimationCallbackProcessor(NodeId nodeId, AnimationId animId)
173 {
174 if (auto nodePtr = RSNodeMap::Instance().GetNode<RSNode>(nodeId)) {
175 nodePtr->AnimationFinish(animId);
176 }
177 }
178 } // namespace Rosen
179 } // namespace OHOS
180