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 "base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/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 ", isMirrored=%{public}d, mirrorNodeId=%{public}" PRIu64 ", isSync=%{public}d]",
63 node->GetId(), displayNodeConfig.screenId, displayNodeConfig.isMirrored,
64 displayNodeConfig.mirrorNodeId, displayNodeConfig.isSync);
65 return node;
66 }
67
CreateNode(const RSDisplayNodeConfig & displayNodeConfig,NodeId nodeId)68 bool RSDisplayNode::CreateNode(const RSDisplayNodeConfig& displayNodeConfig, NodeId nodeId)
69 {
70 return std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
71 CreateNode(displayNodeConfig, nodeId);
72 }
73
RegisterNodeMap()74 void RSDisplayNode::RegisterNodeMap()
75 {
76 auto rsContext = GetRSUIContext();
77 if (rsContext == nullptr) {
78 return;
79 }
80 auto& nodeMap = rsContext->GetMutableNodeMap();
81 nodeMap.RegisterNode(shared_from_this());
82 }
83
AddDisplayNodeToTree()84 void RSDisplayNode::AddDisplayNodeToTree()
85 {
86 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeAddToTree>(GetId());
87 AddCommand(command, true);
88 ROSEN_LOGI("RSDisplayNode::AddDisplayNodeToTree, id:%{public}" PRIu64, GetId());
89 }
90
RemoveDisplayNodeFromTree()91 void RSDisplayNode::RemoveDisplayNodeFromTree()
92 {
93 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeRemoveFromTree>(GetId());
94 AddCommand(command, true);
95 ROSEN_LOGI("RSDisplayNode::RemoveDisplayNodeFromTree, id:%{public}" PRIu64, GetId());
96 }
97
Marshalling(Parcel & parcel) const98 bool RSDisplayNode::Marshalling(Parcel& parcel) const
99 {
100 bool success = parcel.WriteUint64(GetId()) && parcel.WriteUint64(screenId_) && parcel.WriteBool(isMirroredDisplay_);
101 if (!success) {
102 ROSEN_LOGE("RSDisplayNode::Marshalling failed");
103 }
104 return success;
105 }
106
Unmarshalling(Parcel & parcel)107 RSDisplayNode::SharedPtr RSDisplayNode::Unmarshalling(Parcel& parcel)
108 {
109 uint64_t id = UINT64_MAX;
110 uint64_t screenId = UINT64_MAX;
111 bool isMirrored = false;
112 if (!(parcel.ReadUint64(id) && parcel.ReadUint64(screenId) && parcel.ReadBool(isMirrored))) {
113 ROSEN_LOGE("RSDisplayNode::Unmarshalling, read param failed");
114 return nullptr;
115 }
116
117 if (auto prevNode = RSNodeMap::Instance().GetNode(id)) { // delete
118 // if the node id is already in the map, we should not create a new node
119 return prevNode->ReinterpretCastTo<RSDisplayNode>();
120 }
121
122 RSDisplayNodeConfig config { .screenId = screenId, .isMirrored = isMirrored };
123
124 SharedPtr displayNode(new RSDisplayNode(config, id));
125 RSNodeMap::MutableInstance().RegisterNode(displayNode); // ToDo
126
127 // for nodes constructed by unmarshalling, we should not destroy the corresponding render node on destruction
128 displayNode->skipDestroyCommandInDestructor_ = true;
129
130 return displayNode;
131 }
132
ClearChildren()133 void RSDisplayNode::ClearChildren()
134 {
135 auto children = GetChildren();
136 for (auto child : children) {
137 if (auto childPtr = (GetRSUIContext() ? GetRSUIContext()->GetNodeMap().GetNode(child)
138 : RSNodeMap::Instance().GetNode(child))) {
139 RemoveChild(childPtr);
140 }
141 }
142 }
143
SetScreenId(uint64_t screenId)144 void RSDisplayNode::SetScreenId(uint64_t screenId)
145 {
146 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetScreenId>(GetId(), screenId);
147 AddCommand(command, true);
148 #ifdef ROSEN_OHOS
149 RS_TRACE_NAME_FMT("RSDisplayNode::SetScreenId HiSysEventWrite, DisplayNode: %" PRIu64 ", ScreenId: %" PRIu64,
150 GetId(), screenId);
151 int32_t ret = HiSysEventWrite(
152 OHOS::HiviewDFX::HiSysEvent::Domain::GRAPHIC,
153 "SET_SCREENID",
154 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
155 "CURRENT_SCREENID", screenId);
156 if (ret != 0) {
157 ROSEN_LOGE("SET_SCREENID Write HiSysEvent error, ret: %{public}d" PRIu64, ret);
158 }
159 #endif
160 ROSEN_LOGI(
161 "RSDisplayNode::SetScreenId, DisplayNode: %{public}" PRIu64 ", ScreenId: %{public}" PRIu64, GetId(), screenId);
162 RS_TRACE_NAME_FMT("RSDisplayNode::SetScreenId, DisplayNode: %" PRIu64 ", ScreenId: %" PRIu64, GetId(), screenId);
163 }
164
OnBoundsSizeChanged() const165 void RSDisplayNode::OnBoundsSizeChanged() const
166 {
167 auto bounds = GetStagingProperties().GetBounds();
168 ROSEN_LOGD("RSDisplayNode::OnBoundsSizeChanged, w: %{public}d, h: %{public}d.",
169 (uint32_t)bounds.z_, (uint32_t)bounds.w_);
170 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetRogSize>(GetId(), bounds.z_, bounds.w_);
171 AddCommand(command, true);
172 }
173
SetDisplayOffset(int32_t offsetX,int32_t offsetY)174 void RSDisplayNode::SetDisplayOffset(int32_t offsetX, int32_t offsetY)
175 {
176 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetDisplayOffset>(GetId(), offsetX, offsetY);
177 AddCommand(command, true);
178 ROSEN_LOGD("RSDisplayNode::SetDisplayOffset, offsetX:%{public}d, offsetY:%{public}d", offsetX, offsetY);
179 }
180
SetSecurityDisplay(bool isSecurityDisplay)181 void RSDisplayNode::SetSecurityDisplay(bool isSecurityDisplay)
182 {
183 isSecurityDisplay_ = isSecurityDisplay;
184 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetSecurityDisplay>(GetId(), isSecurityDisplay);
185 AddCommand(command, true);
186 ROSEN_LOGD("RSDisplayNode::SetSecurityDisplay, displayNodeId:[%{public}" PRIu64 "]"
187 " isSecurityDisplay:[%{public}s]", GetId(), isSecurityDisplay ? "true" : "false");
188 }
189
GetSecurityDisplay() const190 bool RSDisplayNode::GetSecurityDisplay() const
191 {
192 return isSecurityDisplay_;
193 }
194
SetDisplayNodeMirrorConfig(const RSDisplayNodeConfig & displayNodeConfig)195 void RSDisplayNode::SetDisplayNodeMirrorConfig(const RSDisplayNodeConfig& displayNodeConfig)
196 {
197 isMirroredDisplay_ = displayNodeConfig.isMirrored;
198 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetDisplayMode>(GetId(), displayNodeConfig);
199 AddCommand(command, true);
200 ROSEN_LOGD("RSDisplayNode::SetDisplayNodeMirrorConfig, displayNodeId:[%{public}" PRIu64 "]"
201 " isMirrored:[%{public}s]", GetId(), displayNodeConfig.isMirrored ? "true" : "false");
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
IsMirrorDisplay() const230 bool RSDisplayNode::IsMirrorDisplay() const
231 {
232 return isMirroredDisplay_;
233 }
234
RSDisplayNode(const RSDisplayNodeConfig & config,std::shared_ptr<RSUIContext> rsUIContext)235 RSDisplayNode::RSDisplayNode(const RSDisplayNodeConfig& config, std::shared_ptr<RSUIContext> rsUIContext)
236 : RSNode(true, false, rsUIContext), screenId_(config.screenId), offsetX_(0), offsetY_(0),
237 isMirroredDisplay_(config.isMirrored)
238 {
239 (void)screenId_;
240 (void)offsetX_;
241 (void)offsetY_;
242 }
243
RSDisplayNode(const RSDisplayNodeConfig & config,NodeId id,std::shared_ptr<RSUIContext> rsUIContext)244 RSDisplayNode::RSDisplayNode(const RSDisplayNodeConfig& config, NodeId id, std::shared_ptr<RSUIContext> rsUIContext)
245 : RSNode(true, id, false, rsUIContext), screenId_(config.screenId), offsetX_(0), offsetY_(0),
246 isMirroredDisplay_(config.isMirrored)
247 {}
248
SetBootAnimation(bool isBootAnimation)249 void RSDisplayNode::SetBootAnimation(bool isBootAnimation)
250 {
251 isBootAnimation_ = isBootAnimation;
252 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetBootAnimation>(GetId(), isBootAnimation);
253 AddCommand(command, true);
254 }
255
GetBootAnimation() const256 bool RSDisplayNode::GetBootAnimation() const
257 {
258 return isBootAnimation_;
259 }
260
SetScbNodePid(const std::vector<int32_t> & oldScbPids,int32_t currentScbPid)261 void RSDisplayNode::SetScbNodePid(const std::vector<int32_t>& oldScbPids, int32_t currentScbPid)
262 {
263 std::unique_ptr<RSCommand> command = std::make_unique<RSDisplayNodeSetNodePid>(GetId(), oldScbPids, currentScbPid);
264 AddCommand(command, true);
265 std::ostringstream oldPidsStr;
266 oldPidsStr << " NodeId: " << GetId();
267 oldPidsStr << " currentScbPid: " << currentScbPid;
268 oldPidsStr << " oldScbPids:";
269 for (auto iter = oldScbPids.begin(); iter != oldScbPids.end(); ++iter) {
270 oldPidsStr << *iter << ",";
271 }
272 DoFlushModifier();
273 ROSEN_LOGI("SetScbNodePid %{public}s", oldPidsStr.str().c_str());
274 }
275
SetVirtualScreenMuteStatus(bool virtualScreenMuteStatus)276 void RSDisplayNode::SetVirtualScreenMuteStatus(bool virtualScreenMuteStatus)
277 {
278 std::unique_ptr<RSCommand> command =
279 std::make_unique<RSDisplayNodeSetVirtualScreenMuteStatus>(GetId(), virtualScreenMuteStatus);
280 AddCommand(command, true);
281 ROSEN_LOGI("RSDisplayNode::SetVirtualScreenMuteStatus, displayNodeId:[%{public}" PRIu64 "] "
282 "virtualScreenMuteStatus: %{public}d", GetId(), virtualScreenMuteStatus);
283 }
284
285 RSDisplayNode::~RSDisplayNode() = default;
286
287 } // namespace Rosen
288 } // namespace OHOS
289