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_display_node.h"
17
18 #include "rs_trace.h"
19 #ifdef ROSEN_OHOS
20 #include "hisysevent.h"
21 #include "sandbox_utils.h"
22 #endif
23 #include "command/rs_display_node_command.h"
24 #include "pipeline/rs_node_map.h"
25 #include "platform/common/rs_log.h"
26 #include "transaction/rs_render_service_client.h"
27 #include "transaction/rs_transaction_proxy.h"
28 #include "ui/rs_ui_context.h"
29 namespace OHOS {
30 namespace Rosen {
31
Create(const RSDisplayNodeConfig & displayNodeConfig,std::shared_ptr<RSUIContext> rsUIContext)32 RSDisplayNode::SharedPtr RSDisplayNode::Create(
33 const RSDisplayNodeConfig& displayNodeConfig, std::shared_ptr<RSUIContext> rsUIContext)
34 {
35 SharedPtr node(new RSDisplayNode(displayNodeConfig, rsUIContext));
36 if (rsUIContext != nullptr) {
37 rsUIContext->GetMutableNodeMap().RegisterNode(node);
38 } else {
39 RSNodeMap::MutableInstance().RegisterNode(node);
40 }
41
42 if (LIKELY(!displayNodeConfig.isSync)) {
43 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeCreate>(node->GetId(), displayNodeConfig);
44 if (node->GetRSUIContext() != nullptr) {
45 auto transaction = node->GetRSUIContext()->GetRSTransaction();
46 if (transaction != nullptr) {
47 transaction->AddCommand(command, true);
48 }
49 } else {
50 auto transactionProxy = RSTransactionProxy::GetInstance();
51 if (transactionProxy != nullptr) {
52 transactionProxy->AddCommand(command, true);
53 }
54 }
55 } else {
56 if (!node->CreateNode(displayNodeConfig, node->GetId())) {
57 ROSEN_LOGE("RSDisplayNode::Create: CreateNode Failed.");
58 return nullptr;
59 }
60 }
61 ROSEN_LOGI("RSDisplayNode::Create, id:%{public}" PRIu64 " config[screenId=%{public}" PRIu64
62 ", isMirror=%{public}d, mirroredNodeId=%{public}" PRIu64 ", isSync=%{public}d]",
63 node->GetId(), displayNodeConfig.screenId, displayNodeConfig.isMirrored,
64 displayNodeConfig.mirrorNodeId, displayNodeConfig.isSync);
65 node->SetUIContextToken();
66 return node;
67 }
68
CreateNode(const RSDisplayNodeConfig & displayNodeConfig,NodeId nodeId)69 bool RSDisplayNode::CreateNode(const RSDisplayNodeConfig& displayNodeConfig, NodeId nodeId)
70 {
71 return std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
72 CreateNode(displayNodeConfig, nodeId);
73 }
74
RegisterNodeMap()75 void RSDisplayNode::RegisterNodeMap()
76 {
77 auto rsContext = GetRSUIContext();
78 if (rsContext == nullptr) {
79 return;
80 }
81 auto& nodeMap = rsContext->GetMutableNodeMap();
82 nodeMap.RegisterNode(shared_from_this());
83 }
84
AddDisplayNodeToTree()85 void RSDisplayNode::AddDisplayNodeToTree()
86 {
87 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeAddToTree>(GetId());
88 AddCommand(command, true);
89 SetIsOnTheTree(true);
90 ROSEN_LOGI("RSDisplayNode::AddDisplayNodeToTree, id:%{public}" PRIu64, GetId());
91 }
92
RemoveDisplayNodeFromTree()93 void RSDisplayNode::RemoveDisplayNodeFromTree()
94 {
95 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeRemoveFromTree>(GetId());
96 AddCommand(command, true);
97 SetIsOnTheTree(false);
98 ROSEN_LOGI("RSDisplayNode::RemoveDisplayNodeFromTree, id:%{public}" PRIu64, GetId());
99 }
100
Marshalling(Parcel & parcel) const101 bool RSDisplayNode::Marshalling(Parcel& parcel) const
102 {
103 bool success = parcel.WriteUint64(GetId()) && parcel.WriteUint64(screenId_) && parcel.WriteBool(isMirrorDisplay_);
104 if (!success) {
105 ROSEN_LOGE("RSDisplayNode::Marshalling failed");
106 }
107 return success;
108 }
109
Unmarshalling(Parcel & parcel)110 RSDisplayNode::SharedPtr RSDisplayNode::Unmarshalling(Parcel& parcel)
111 {
112 uint64_t id = UINT64_MAX;
113 uint64_t screenId = UINT64_MAX;
114 bool isMirror = false;
115 if (!(parcel.ReadUint64(id) && parcel.ReadUint64(screenId) && parcel.ReadBool(isMirror))) {
116 ROSEN_LOGE("RSDisplayNode::Unmarshalling, read param failed");
117 return nullptr;
118 }
119
120 if (auto prevNode = RSNodeMap::Instance().GetNode(id)) { // delete
121 // if the node id is already in the map, we should not create a new node
122 return prevNode->ReinterpretCastTo<RSDisplayNode>();
123 }
124
125 RSDisplayNodeConfig config { .screenId = screenId, .isMirrored = isMirror };
126
127 SharedPtr displayNode(new RSDisplayNode(config, id));
128 RSNodeMap::MutableInstance().RegisterNode(displayNode);
129
130 // for nodes constructed by unmarshalling, we should not destroy the corresponding render node on destruction
131 displayNode->skipDestroyCommandInDestructor_ = true;
132
133 return displayNode;
134 }
135
SetSecurityDisplay(bool isSecurityDisplay)136 void RSDisplayNode::SetSecurityDisplay(bool isSecurityDisplay)
137 {
138 isSecurityDisplay_ = isSecurityDisplay;
139 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetSecurityDisplay>(GetId(), isSecurityDisplay);
140 AddCommand(command, true);
141 ROSEN_LOGD("RSDisplayNode::SetSecurityDisplay, displayNodeId:[%{public}" PRIu64 "]"
142 " isSecurityDisplay:[%{public}s]", GetId(), isSecurityDisplay ? "true" : "false");
143 }
144
GetSecurityDisplay() const145 bool RSDisplayNode::GetSecurityDisplay() const
146 {
147 return isSecurityDisplay_;
148 }
149
ClearChildren()150 void RSDisplayNode::ClearChildren()
151 {
152 auto children = GetChildren();
153 for (auto child : children) {
154 if (auto childPtr = child.lock()) {
155 RemoveChild(childPtr);
156 }
157 }
158 }
159
SetScreenId(uint64_t screenId)160 void RSDisplayNode::SetScreenId(uint64_t screenId)
161 {
162 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetScreenId>(GetId(), screenId);
163 AddCommand(command, true);
164 #ifdef ROSEN_OHOS
165 RS_TRACE_NAME_FMT("RSDisplayNode::SetScreenId HiSysEventWrite, DisplayNode: %" PRIu64 ", ScreenId: %" PRIu64,
166 GetId(), screenId);
167 int32_t ret = HiSysEventWrite(
168 OHOS::HiviewDFX::HiSysEvent::Domain::GRAPHIC,
169 "SET_SCREENID",
170 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
171 "CURRENT_SCREENID", screenId);
172 if (ret != 0) {
173 ROSEN_LOGE("SET_SCREENID Write HiSysEvent error, ret: %{public}d" PRIu64, ret);
174 }
175 #endif
176 ROSEN_LOGI(
177 "RSDisplayNode::SetScreenId, DisplayNode: %{public}" PRIu64 ", ScreenId: %{public}" PRIu64, GetId(), screenId);
178 RS_TRACE_NAME_FMT("RSDisplayNode::SetScreenId, DisplayNode: %" PRIu64 ", ScreenId: %" PRIu64, GetId(), screenId);
179 }
180
SetForceCloseHdr(bool isForceCloseHdr)181 void RSDisplayNode::SetForceCloseHdr(bool isForceCloseHdr)
182 {
183 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeForceCloseHdr>(GetId(), isForceCloseHdr);
184 if (AddCommand(command, true)) {
185 ROSEN_LOGD("RSDisplayNode::SetForceCloseHdr: [%{public}s], displayNodeId:[%{public}" PRIu64 "]",
186 isForceCloseHdr ? "true" : "false", GetId());
187 }
188 }
189
SetDisplayNodeMirrorConfig(const RSDisplayNodeConfig & displayNodeConfig)190 void RSDisplayNode::SetDisplayNodeMirrorConfig(const RSDisplayNodeConfig& displayNodeConfig)
191 {
192 isMirrorDisplay_ = displayNodeConfig.isMirrored;
193 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetDisplayMode>(GetId(), displayNodeConfig);
194 AddCommand(command, true);
195 ROSEN_LOGD("RSDisplayNode::SetDisplayNodeMirrorConfig, displayNodeId:[%{public}" PRIu64 "]"
196 " isMirror:[%{public}d]", GetId(), displayNodeConfig.isMirrored);
197 }
198
IsMirrorDisplay() const199 bool RSDisplayNode::IsMirrorDisplay() const
200 {
201 return isMirrorDisplay_;
202 }
203
SetScreenRotation(const uint32_t & rotation)204 void RSDisplayNode::SetScreenRotation(const uint32_t& rotation)
205 {
206 ScreenRotation screenRotation = ScreenRotation::ROTATION_0;
207 switch (rotation) {
208 case 0: // Rotation::ROTATION_0
209 screenRotation = ScreenRotation::ROTATION_0;
210 break;
211 case 1: // Rotation::ROTATION_90
212 screenRotation = ScreenRotation::ROTATION_90;
213 break;
214 case 2: // Rotation::ROTATION_180
215 screenRotation = ScreenRotation::ROTATION_180;
216 break;
217 case 3: // Rotation::ROTATION_270
218 screenRotation = ScreenRotation::ROTATION_270;
219 break;
220 default:
221 screenRotation = ScreenRotation::INVALID_SCREEN_ROTATION;
222 break;
223 }
224 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetScreenRotation>(GetId(), screenRotation);
225 AddCommand(command, true);
226 ROSEN_LOGI("RSDisplayNode::SetScreenRotation, displayNodeId:[%{public}" PRIu64 "]"
227 " screenRotation:[%{public}d]", GetId(), rotation);
228 }
229
RSDisplayNode(const RSDisplayNodeConfig & config,std::shared_ptr<RSUIContext> rsUIContext)230 RSDisplayNode::RSDisplayNode(const RSDisplayNodeConfig& config, std::shared_ptr<RSUIContext> rsUIContext)
231 : RSNode(true, false, rsUIContext, true), screenId_(config.screenId), isMirrorDisplay_(config.isMirrored)
232 {}
233
RSDisplayNode(const RSDisplayNodeConfig & config,NodeId id,std::shared_ptr<RSUIContext> rsUIContext)234 RSDisplayNode::RSDisplayNode(const RSDisplayNodeConfig& config, NodeId id, std::shared_ptr<RSUIContext> rsUIContext)
235 : RSNode(true, id, false, rsUIContext, true), screenId_(config.screenId), isMirrorDisplay_(config.isMirrored)
236 {}
237
SetBootAnimation(bool isBootAnimation)238 void RSDisplayNode::SetBootAnimation(bool isBootAnimation)
239 {
240 isBootAnimation_ = isBootAnimation;
241 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetBootAnimation>(GetId(), isBootAnimation);
242 AddCommand(command, true);
243 }
244
GetBootAnimation() const245 bool RSDisplayNode::GetBootAnimation() const
246 {
247 return isBootAnimation_;
248 }
249
SetScbNodePid(const std::vector<int32_t> & oldScbPids,int32_t currentScbPid)250 void RSDisplayNode::SetScbNodePid(const std::vector<int32_t>& oldScbPids, int32_t currentScbPid)
251 {
252 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetNodePid>(GetId(), oldScbPids, currentScbPid);
253 AddCommand(command, true);
254 std::ostringstream oldPidsStr;
255 oldPidsStr << " NodeId: " << GetId();
256 oldPidsStr << " currentScbPid: " << currentScbPid;
257 oldPidsStr << " oldScbPids:";
258 for (auto iter = oldScbPids.begin(); iter != oldScbPids.end(); ++iter) {
259 oldPidsStr << *iter << ",";
260 }
261 DoFlushModifier();
262 ROSEN_LOGI("SetScbNodePid %{public}s", oldPidsStr.str().c_str());
263 }
264
SetVirtualScreenMuteStatus(bool virtualScreenMuteStatus)265 void RSDisplayNode::SetVirtualScreenMuteStatus(bool virtualScreenMuteStatus)
266 {
267 std::unique_ptr<RSCommand> command =
268 std::make_unique<RSDisplayNodeSetVirtualScreenMuteStatus>(GetId(), virtualScreenMuteStatus);
269 AddCommand(command, true);
270 ROSEN_LOGI("RSDisplayNode::SetVirtualScreenMuteStatus, displayNodeId:[%{public}" PRIu64 "] "
271 "virtualScreenMuteStatus: %{public}d", GetId(), virtualScreenMuteStatus);
272 }
273
~RSDisplayNode()274 RSDisplayNode::~RSDisplayNode()
275 {
276 RS_LOGI("%{public}s, NodeId:[%{public}" PRIu64 "]", __func__, GetId());
277 }
278 } // namespace Rosen
279 } // namespace OHOS
280