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_) &&
34            RSMarshallingHelper::Marshalling(parcel, success_) &&
35            (property_ == nullptr || RSRenderPropertyBase::Marshalling(parcel, property_));
36 }
37 
Unmarshalling(Parcel & parcel)38 RSCommand* RSNodeGetShowingPropertyAndCancelAnimation::Unmarshalling(Parcel& parcel)
39 {
40     NodeId targetId;
41     std::shared_ptr<RSRenderPropertyBase> property;
42     uint64_t timeoutNS;
43     if (!(RSMarshallingHelper::Unmarshalling(parcel, targetId) &&
44             RSMarshallingHelper::Unmarshalling(parcel, timeoutNS))) {
45         return nullptr;
46     }
47     auto command = new RSNodeGetShowingPropertyAndCancelAnimation(targetId, property, timeoutNS);
48     if (!command->ReadFromParcel(parcel)) {
49         delete command;
50         return nullptr;
51     }
52     return command;
53 }
54 
CheckHeader(Parcel & parcel) const55 bool RSNodeGetShowingPropertyAndCancelAnimation::CheckHeader(Parcel& parcel) const
56 {
57     uint16_t type;
58     uint16_t subType;
59     uint64_t timeoutNS;
60     NodeId targetId;
61     return RSMarshallingHelper::Unmarshalling(parcel, type) && type == commandType &&
62            RSMarshallingHelper::Unmarshalling(parcel, subType) && subType == commandSubType &&
63            RSMarshallingHelper::Unmarshalling(parcel, targetId) && targetId == targetId_ &&
64            RSMarshallingHelper::Unmarshalling(parcel, timeoutNS) && timeoutNS == timeoutNS_;
65 }
66 
ReadFromParcel(Parcel & parcel)67 bool RSNodeGetShowingPropertyAndCancelAnimation::ReadFromParcel(Parcel& parcel)
68 {
69     return RSMarshallingHelper::Unmarshalling(parcel, isTimeout_) &&
70            RSMarshallingHelper::Unmarshalling(parcel, success_) &&
71            RSRenderPropertyBase::Unmarshalling(parcel, property_);
72 }
73 
Process(RSContext & context)74 void RSNodeGetShowingPropertyAndCancelAnimation::Process(RSContext& context)
75 {
76     isTimeout_ = false;
77     auto& nodeMap = context.GetNodeMap();
78     auto node = nodeMap.GetRenderNode<RSRenderNode>(targetId_);
79     if (!node || !property_) {
80         success_ = false;
81         return;
82     }
83     auto modifier = node->GetModifier(property_->GetId());
84     if (!modifier) {
85         success_ = false;
86         return;
87     }
88     property_ = modifier->GetProperty();
89     success_ = (property_ != nullptr);
90     if (success_) {
91         auto& animationManager = node->GetAnimationManager();
92         animationManager.CancelAnimationByPropertyId(property_->GetId());
93     }
94 }
95 
Marshalling(Parcel & parcel) const96 bool RSNodeGetShowingPropertiesAndCancelAnimation::Marshalling(Parcel& parcel) const
97 {
98     bool result = RSMarshallingHelper::Marshalling(parcel, commandType) &&
99            RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
100            RSMarshallingHelper::Marshalling(parcel, timeoutNS_) &&
101            RSMarshallingHelper::Marshalling(parcel, success_) &&
102            RSMarshallingHelper::Marshalling(parcel, propertiesMap_);
103     return result;
104 }
105 
106 // construct cancelAnimation & SetProperties
Unmarshalling(Parcel & parcel)107 RSCommand* RSNodeGetShowingPropertiesAndCancelAnimation::Unmarshalling(Parcel& parcel)
108 {
109     uint64_t timeoutNS;
110     if (!RSMarshallingHelper::Unmarshalling(parcel, timeoutNS)) {
111         return nullptr;
112     }
113     auto command = new RSNodeGetShowingPropertiesAndCancelAnimation(timeoutNS);
114     if (!command->ReadFromParcel(parcel)) {
115         delete command;
116         return nullptr;
117     }
118     return command;
119 }
120 
CheckHeader(Parcel & parcel) const121 bool RSNodeGetShowingPropertiesAndCancelAnimation::CheckHeader(Parcel& parcel) const
122 {
123     uint16_t type;
124     uint16_t subType;
125     uint64_t timeoutNS;
126 
127     return RSMarshallingHelper::Unmarshalling(parcel, type) && type == commandType &&
128            RSMarshallingHelper::Unmarshalling(parcel, subType) && subType == commandSubType &&
129            RSMarshallingHelper::Unmarshalling(parcel, timeoutNS) && timeoutNS == timeoutNS_;
130 }
131 
ReadFromParcel(Parcel & parcel)132 bool RSNodeGetShowingPropertiesAndCancelAnimation::ReadFromParcel(Parcel& parcel)
133 {
134     if (!RSMarshallingHelper::Unmarshalling(parcel, success_)) {
135         return false;
136     }
137     if (!RSMarshallingHelper::Unmarshalling(parcel, propertiesMap_)) {
138         return false;
139     }
140     return true;
141 }
142 
Process(RSContext & context)143 void RSNodeGetShowingPropertiesAndCancelAnimation::Process(RSContext& context)
144 {
145     success_ = true;
146     auto& nodeMap = context.GetNodeMap();
147     for (auto& [key, value] : propertiesMap_) {
148         // value should already initialized as nullptr
149         auto& [nodeId, propertyId] = key;
150         auto& [property, animations] = value;
151         auto node = nodeMap.GetRenderNode<RSRenderNode>(nodeId);
152         if (!node) {
153             continue;
154         }
155         auto modifier = node->GetModifier(propertyId);
156         if (modifier) {
157             property = modifier->GetProperty();
158         }
159         node->GetAnimationManager().AttemptCancelAnimationByAnimationId(animations);
160     }
161 }
162 
Marshalling(Parcel & parcel) const163 bool RSNodeGetAnimationsValueFraction::Marshalling(Parcel& parcel) const
164 {
165     bool result = RSMarshallingHelper::Marshalling(parcel, commandType) &&
166            RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
167            RSMarshallingHelper::Marshalling(parcel, timeoutNS_) &&
168            RSMarshallingHelper::Marshalling(parcel, success_) &&
169            RSMarshallingHelper::Marshalling(parcel, nodeId_) &&
170            RSMarshallingHelper::Marshalling(parcel, animationId_);
171     return result;
172 }
173 
Unmarshalling(Parcel & parcel)174 RSCommand* RSNodeGetAnimationsValueFraction::Unmarshalling(Parcel& parcel)
175 {
176     uint64_t timeoutNS;
177     if (!RSMarshallingHelper::Unmarshalling(parcel, timeoutNS)) {
178         return nullptr;
179     }
180     auto command = new RSNodeGetAnimationsValueFraction(timeoutNS);
181     if (!command->ReadFromParcel(parcel)) {
182         delete command;
183         return nullptr;
184     }
185     return command;
186 }
187 
CheckHeader(Parcel & parcel) const188 bool RSNodeGetAnimationsValueFraction::CheckHeader(Parcel& parcel) const
189 {
190     uint16_t type;
191     uint16_t subType;
192     uint64_t timeoutNS;
193     return RSMarshallingHelper::Unmarshalling(parcel, type) && type == commandType &&
194            RSMarshallingHelper::Unmarshalling(parcel, subType) && subType == commandSubType &&
195            RSMarshallingHelper::Unmarshalling(parcel, timeoutNS) && timeoutNS == timeoutNS_;
196 }
197 
ReadFromParcel(Parcel & parcel)198 bool RSNodeGetAnimationsValueFraction::ReadFromParcel(Parcel& parcel)
199 {
200     if (!RSMarshallingHelper::Unmarshalling(parcel, success_)) {
201         return false;
202     }
203     if (!RSMarshallingHelper::Unmarshalling(parcel, nodeId_)) {
204         return false;
205     }
206     if (!RSMarshallingHelper::Unmarshalling(parcel, animationId_)) {
207         return false;
208     }
209     return true;
210 }
211 
Process(RSContext & context)212 void RSNodeGetAnimationsValueFraction::Process(RSContext& context)
213 {
214     auto& nodeMap = context.GetNodeMap();
215     auto node = nodeMap.GetRenderNode<RSRenderNode>(nodeId_);
216     if (node == nullptr) {
217         return;
218     }
219     auto animation = node->GetAnimationManager().GetAnimation(animationId_);
220     if (animation == nullptr) {
221         return;
222     }
223     success_ = true;
224     fraction_ = animation->GetValueFraction();
225 }
226 } // namespace Rosen
227 } // namespace OHOS
228