• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "command/rs_node_showing_command.h"
17 
18 #include "pipeline/rs_render_node.h"
19 #include "platform/common/rs_log.h"
20 #include "transaction/rs_marshalling_helper.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 RSNodeGetShowingPropertyAndCancelAnimation::Registrar RSNodeGetShowingPropertyAndCancelAnimation::instance_;
25 RSNodeGetShowingPropertiesAndCancelAnimation::Registrar RSNodeGetShowingPropertiesAndCancelAnimation::instance_;
26 
Marshalling(Parcel & parcel) const27 bool RSNodeGetShowingPropertyAndCancelAnimation::Marshalling(Parcel& parcel) const
28 {
29     return RSMarshallingHelper::Marshalling(parcel, commandType) &&
30            RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
31            RSMarshallingHelper::Marshalling(parcel, targetId_) &&
32            RSMarshallingHelper::Marshalling(parcel, timeoutNS_) &&
33            RSMarshallingHelper::Marshalling(parcel, isTimeout_) && RSMarshallingHelper::Marshalling(parcel, success_) &&
34            RSMarshallingHelper::Marshalling(parcel, property_);
35 }
36 
Unmarshalling(Parcel & parcel)37 RSCommand* RSNodeGetShowingPropertyAndCancelAnimation::Unmarshalling(Parcel& parcel)
38 {
39     NodeId targetId;
40     std::shared_ptr<RSRenderPropertyBase> property;
41     uint64_t timeoutNS;
42     if (!(RSMarshallingHelper::Unmarshalling(parcel, targetId) &&
43             RSMarshallingHelper::Unmarshalling(parcel, timeoutNS))) {
44         return nullptr;
45     }
46     auto command = new RSNodeGetShowingPropertyAndCancelAnimation(targetId, property, timeoutNS);
47     if (!command->ReadFromParcel(parcel)) {
48         delete command;
49         return nullptr;
50     }
51     return command;
52 }
53 
CheckHeader(Parcel & parcel) const54 bool RSNodeGetShowingPropertyAndCancelAnimation::CheckHeader(Parcel& parcel) const
55 {
56     uint16_t type;
57     uint16_t subType;
58     uint64_t timeoutNS;
59     NodeId targetId;
60     return RSMarshallingHelper::Unmarshalling(parcel, type) && type == commandType &&
61            RSMarshallingHelper::Unmarshalling(parcel, subType) && subType == commandSubType &&
62            RSMarshallingHelper::Unmarshalling(parcel, targetId) && targetId == targetId_ &&
63            RSMarshallingHelper::Unmarshalling(parcel, timeoutNS) && timeoutNS == timeoutNS_;
64 }
65 
ReadFromParcel(Parcel & parcel)66 bool RSNodeGetShowingPropertyAndCancelAnimation::ReadFromParcel(Parcel& parcel)
67 {
68     return RSMarshallingHelper::Unmarshalling(parcel, isTimeout_) &&
69            RSMarshallingHelper::Unmarshalling(parcel, success_) &&
70            RSMarshallingHelper::Unmarshalling(parcel, property_);
71 }
72 
Process(RSContext & context)73 void RSNodeGetShowingPropertyAndCancelAnimation::Process(RSContext& context)
74 {
75     isTimeout_ = false;
76     auto& nodeMap = context.GetNodeMap();
77     auto node = nodeMap.GetRenderNode<RSRenderNode>(targetId_);
78     if (!node || !property_) {
79         success_ = false;
80         ROSEN_LOGE("RSNodeGetShowingPropertyAndCancelAnimation::Process, "
81             "node [%{public}" PRIu64 "] or property is null!", targetId_);
82         return;
83     }
84     if (auto property = node->GetProperty(property_->GetId())) {
85         property_ = property;
86     }
87     success_ = (property_ != nullptr);
88     if (success_) {
89         auto& animationManager = node->GetAnimationManager();
90         animationManager.CancelAnimationByPropertyId(property_->GetId());
91     }
92 }
93 
IsCallingPidValid(pid_t callingPid,const RSRenderNodeMap & nodeMap) const94 bool RSNodeGetShowingPropertyAndCancelAnimation::IsCallingPidValid(pid_t callingPid,
95     const RSRenderNodeMap& nodeMap) const
96 {
97     if (ExtractPid(targetId_) != callingPid && !nodeMap.IsUIExtensionSurfaceNode(targetId_)) {
98         ROSEN_LOGE("RSNodeGetShowingPropertyAndCancelAnimation::IsCallingPidValid, "
99                 "callingPid [%{public}d] no permission "
100                 "EXECUTE_SYNCHRONOUS_TASK on node [%{public}" PRIu64 "] ",
101                 static_cast<int>(callingPid), targetId_);
102         return false;
103     }
104     return true;
105 }
106 
Marshalling(Parcel & parcel) const107 bool RSNodeGetShowingPropertiesAndCancelAnimation::Marshalling(Parcel& parcel) const
108 {
109     bool result = RSMarshallingHelper::Marshalling(parcel, commandType) &&
110            RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
111            RSMarshallingHelper::Marshalling(parcel, timeoutNS_) &&
112            RSMarshallingHelper::Marshalling(parcel, success_) &&
113            RSMarshallingHelper::Marshalling(parcel, propertiesMap_);
114     return result;
115 }
116 
117 // construct cancelAnimation & SetProperties
Unmarshalling(Parcel & parcel)118 RSCommand* RSNodeGetShowingPropertiesAndCancelAnimation::Unmarshalling(Parcel& parcel)
119 {
120     uint64_t timeoutNS;
121     if (!RSMarshallingHelper::Unmarshalling(parcel, timeoutNS)) {
122         return nullptr;
123     }
124     auto command = new RSNodeGetShowingPropertiesAndCancelAnimation(timeoutNS);
125     if (!command->ReadFromParcel(parcel)) {
126         delete command;
127         return nullptr;
128     }
129     return command;
130 }
131 
CheckHeader(Parcel & parcel) const132 bool RSNodeGetShowingPropertiesAndCancelAnimation::CheckHeader(Parcel& parcel) const
133 {
134     uint16_t type;
135     uint16_t subType;
136     uint64_t timeoutNS;
137 
138     return RSMarshallingHelper::Unmarshalling(parcel, type) && type == commandType &&
139            RSMarshallingHelper::Unmarshalling(parcel, subType) && subType == commandSubType &&
140            RSMarshallingHelper::Unmarshalling(parcel, timeoutNS) && timeoutNS == timeoutNS_;
141 }
142 
ReadFromParcel(Parcel & parcel)143 bool RSNodeGetShowingPropertiesAndCancelAnimation::ReadFromParcel(Parcel& parcel)
144 {
145     if (!RSMarshallingHelper::Unmarshalling(parcel, success_)) {
146         return false;
147     }
148     if (!RSMarshallingHelper::Unmarshalling(parcel, propertiesMap_)) {
149         return false;
150     }
151     return true;
152 }
153 
Process(RSContext & context)154 void RSNodeGetShowingPropertiesAndCancelAnimation::Process(RSContext& context)
155 {
156     success_ = true;
157     auto& nodeMap = context.GetNodeMap();
158     for (auto& [key, value] : propertiesMap_) {
159         // value should already initialized as nullptr
160         auto& [nodeId, propertyId] = key;
161         auto& [property, animations] = value;
162         auto node = nodeMap.GetRenderNode<RSRenderNode>(nodeId);
163         if (!node) {
164             ROSEN_LOGE("RSNodeGetShowingPropertiesAndCancelAnimation::Process, "
165                 "node [%{public}" PRIu64 "] is null!", nodeId);
166             continue;
167         }
168         if (auto prop = node->GetProperty(propertyId)) {
169             property = prop;
170         }
171         node->GetAnimationManager().AttemptCancelAnimationByAnimationId(animations);
172     }
173 }
174 
IsCallingPidValid(pid_t callingPid,const RSRenderNodeMap & nodeMap) const175 bool RSNodeGetShowingPropertiesAndCancelAnimation::IsCallingPidValid(pid_t callingPid,
176     const RSRenderNodeMap& nodeMap) const
177 {
178     for (auto& [key, _] : propertiesMap_) {
179         auto& nodeId = key.first;
180         if (ExtractPid(nodeId) != callingPid && !nodeMap.IsUIExtensionSurfaceNode(nodeId)) {
181             ROSEN_LOGE("RSNodeGetShowingPropertiesAndCancelAnimation::IsCallingPidValid, "
182                 "callingPid [%{public}d] no permission "
183                 "EXECUTE_SYNCHRONOUS_TASK on node [%{public}" PRIu64 "] ",
184                 static_cast<int>(callingPid), nodeId);
185             return false;
186         }
187     }
188     return true;
189 }
190 
Marshalling(Parcel & parcel) const191 bool RSNodeGetAnimationsValueFraction::Marshalling(Parcel& parcel) const
192 {
193     bool result = RSMarshallingHelper::Marshalling(parcel, commandType) &&
194            RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
195            RSMarshallingHelper::Marshalling(parcel, timeoutNS_) &&
196            RSMarshallingHelper::Marshalling(parcel, success_) &&
197            RSMarshallingHelper::Marshalling(parcel, nodeId_) &&
198            RSMarshallingHelper::Marshalling(parcel, animationId_);
199     return result;
200 }
201 
Unmarshalling(Parcel & parcel)202 RSCommand* RSNodeGetAnimationsValueFraction::Unmarshalling(Parcel& parcel)
203 {
204     uint64_t timeoutNS;
205     if (!RSMarshallingHelper::Unmarshalling(parcel, timeoutNS)) {
206         return nullptr;
207     }
208     auto command = new RSNodeGetAnimationsValueFraction(timeoutNS);
209     if (!command->ReadFromParcel(parcel)) {
210         delete command;
211         return nullptr;
212     }
213     return command;
214 }
215 
CheckHeader(Parcel & parcel) const216 bool RSNodeGetAnimationsValueFraction::CheckHeader(Parcel& parcel) const
217 {
218     uint16_t type;
219     uint16_t subType;
220     uint64_t timeoutNS;
221     return RSMarshallingHelper::Unmarshalling(parcel, type) && type == commandType &&
222            RSMarshallingHelper::Unmarshalling(parcel, subType) && subType == commandSubType &&
223            RSMarshallingHelper::Unmarshalling(parcel, timeoutNS) && timeoutNS == timeoutNS_;
224 }
225 
ReadFromParcel(Parcel & parcel)226 bool RSNodeGetAnimationsValueFraction::ReadFromParcel(Parcel& parcel)
227 {
228     if (!RSMarshallingHelper::Unmarshalling(parcel, success_)) {
229         return false;
230     }
231     if (!RSMarshallingHelper::UnmarshallingPidPlusId(parcel, nodeId_)) {
232         return false;
233     }
234     if (!RSMarshallingHelper::UnmarshallingPidPlusId(parcel, animationId_)) {
235         return false;
236     }
237     return true;
238 }
239 
Process(RSContext & context)240 void RSNodeGetAnimationsValueFraction::Process(RSContext& context)
241 {
242     auto& nodeMap = context.GetNodeMap();
243     auto node = nodeMap.GetRenderNode<RSRenderNode>(nodeId_);
244     if (node == nullptr) {
245         ROSEN_LOGE("RSNodeGetAnimationsValueFraction::Process, node is null!");
246         return;
247     }
248     auto animation = node->GetAnimationManager().GetAnimation(animationId_);
249     if (animation == nullptr) {
250         ROSEN_LOGE("RSNodeGetAnimationsValueFraction::Process, animation is null!");
251         return;
252     }
253     success_ = true;
254     fraction_ = animation->GetValueFraction();
255 }
256 
IsCallingPidValid(pid_t callingPid,const RSRenderNodeMap & nodeMap) const257 bool RSNodeGetAnimationsValueFraction::IsCallingPidValid(pid_t callingPid,
258     const RSRenderNodeMap& nodeMap) const
259 {
260     if (ExtractPid(nodeId_) != callingPid && !nodeMap.IsUIExtensionSurfaceNode(nodeId_)) {
261         ROSEN_LOGE("RSNodeGetAnimationsValueFraction::IsCallingPidValid, "
262                 "callingPid [%{public}d] no permission "
263                 "EXECUTE_SYNCHRONOUS_TASK on node [%{public}" PRIu64 "] ",
264                 static_cast<int>(callingPid), nodeId_);
265         return false;
266     }
267     return true;
268 }
269 } // namespace Rosen
270 } // namespace OHOS
271