• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "remote_animation.h"
17 
18 #include <ability_manager_client.h>
19 #include <common/rs_rect.h>
20 #include <hisysevent.h>
21 #include <hitrace_meter.h>
22 #include <string>
23 #include <transaction/rs_transaction.h>
24 
25 #include "display_group_info.h"
26 #include "minimize_app.h"
27 #include "parameters.h"
28 #include "starting_window.h"
29 #include "surface_draw.h"
30 #include "window_helper.h"
31 #include "window_inner_manager.h"
32 #include "window_manager_hilog.h"
33 #include "window_manager_service_utils.h"
34 #include "window_system_effect.h"
35 #include "zidl/ressched_report.h"
36 #ifdef SOC_PERF_ENABLE
37 #include "socperf_client.h"
38 #endif
39 
40 namespace OHOS {
41 namespace Rosen {
42 namespace {
43     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "RemoteAnimation"};
44     const std::string ANIMATION_TIME_OUT_TASK = "remote_animation_time_out_task";
45     constexpr int64_t ANIMATION_TIME_OUT_MILLISECONDS = 3000; // 3000 is max time
46 }
47 bool RemoteAnimation::isRemoteAnimationEnable_ = true;
48 std::atomic<uint32_t> RemoteAnimation::allocationId_ = 0;
49 sptr<RSIWindowAnimationController> RemoteAnimation::windowAnimationController_ = nullptr;
50 std::weak_ptr<AppExecFwk::EventHandler> RemoteAnimation::wmsTaskHandler_;
51 wptr<WindowRoot> RemoteAnimation::windowRoot_;
52 bool RemoteAnimation::animationFirst_ = false;
53 wptr<WindowController> RemoteAnimation::windowController_ = nullptr;
54 
55 std::map<TransitionReason, TransitionEvent> eventMap_ = {
56     {TransitionReason::CLOSE, TransitionEvent::CLOSE},
57     {TransitionReason::MINIMIZE, TransitionEvent::MINIMIZE},
58     {TransitionReason::BACK_TRANSITION, TransitionEvent::BACK_TRANSITION},
59     {TransitionReason::CLOSE_BUTTON, TransitionEvent::CLOSE_BUTTON},
60     {TransitionReason::BACKGROUND_TRANSITION, TransitionEvent::BACKGROUND_TRANSITION}
61 };
62 
SetAnimationFirst(bool animationFirst)63 void RemoteAnimation::SetAnimationFirst(bool animationFirst)
64 {
65     animationFirst_ = animationFirst;
66     WLOGFI("RSWindowAnimation:animationFirst: %{public}u!", static_cast<uint32_t>(animationFirst_));
67 }
68 
IsRemoteAnimationEnabledAndFirst(DisplayId displayId)69 bool RemoteAnimation::IsRemoteAnimationEnabledAndFirst(DisplayId displayId)
70 {
71     return animationFirst_ && CheckRemoteAnimationEnabled(displayId);
72 }
73 
SetWindowAnimationController(const sptr<RSIWindowAnimationController> & controller)74 WMError RemoteAnimation::SetWindowAnimationController(const sptr<RSIWindowAnimationController>& controller)
75 {
76     WLOGFI("RSWindowAnimation: set window animation controller!");
77     if (!isRemoteAnimationEnable_) {
78         WLOGFE("RSWindowAnimation: failed to set window animation controller, remote animation is not enabled");
79         return WMError::WM_ERROR_NO_REMOTE_ANIMATION;
80     }
81     if (controller == nullptr) {
82         WLOGFE("RSWindowAnimation: failed to set window animation controller, controller is null!");
83         return WMError::WM_ERROR_NULLPTR;
84     }
85 
86     if (windowAnimationController_ != nullptr) {
87         WLOGFI("RSWindowAnimation: maybe user switch!");
88     }
89 
90     windowAnimationController_ = controller;
91     auto winRoot = windowRoot_.promote();
92     if (winRoot) {
93         auto wallpaperNode = winRoot->FindWallpaperWindow();
94         if (wallpaperNode) {
95             NotifyAnimationUpdateWallpaper(wallpaperNode);
96         } else {
97             WLOGFW("Cannot find wallpaper window!");
98         }
99     }
100     return WMError::WM_OK;
101 }
102 
SetMainTaskHandler(std::shared_ptr<AppExecFwk::EventHandler> handler)103 void RemoteAnimation::SetMainTaskHandler(std::shared_ptr<AppExecFwk::EventHandler> handler)
104 {
105     wmsTaskHandler_ = handler;
106 }
107 
SetWindowControllerAndRoot(const sptr<WindowController> & windowController,const sptr<WindowRoot> & windowRoot)108 void RemoteAnimation::SetWindowControllerAndRoot(const sptr<WindowController>& windowController,
109     const sptr<WindowRoot>& windowRoot)
110 {
111     windowController_ = windowController;
112     windowRoot_ = windowRoot;
113 }
114 
CheckAnimationController()115 bool RemoteAnimation::CheckAnimationController()
116 {
117     if (windowAnimationController_ == nullptr) {
118         WLOGFD("RSWindowAnimation: windowAnimationController_ null!");
119         return false;
120     }
121     return true;
122 }
123 
CheckRemoteAnimationEnabled(DisplayId displayId)124 bool RemoteAnimation::CheckRemoteAnimationEnabled(DisplayId displayId)
125 {
126     // When the screen is locked, remote animation cannot take effect because the launcher is frozen.
127     auto winRoot = windowRoot_.promote();
128     if (winRoot == nullptr) {
129         return false;
130     }
131     auto container = winRoot->GetOrCreateWindowNodeContainer(displayId);
132     if (container == nullptr || container->IsScreenLocked()) {
133         return false;
134     }
135     return CheckAnimationController();
136 }
137 
CheckTransition(sptr<WindowTransitionInfo> srcInfo,const sptr<WindowNode> & srcNode,sptr<WindowTransitionInfo> dstInfo,const sptr<WindowNode> & dstNode)138 bool RemoteAnimation::CheckTransition(sptr<WindowTransitionInfo> srcInfo, const sptr<WindowNode>& srcNode,
139     sptr<WindowTransitionInfo> dstInfo, const sptr<WindowNode>& dstNode)
140 {
141     if (srcNode == nullptr && dstNode == nullptr) {
142         WLOGFE("RSWindowAnimation: srcNode and dstNode are nullptr");
143         return false;
144     }
145 
146     if (srcNode != nullptr && !srcNode->leashWinSurfaceNode_ && !srcNode->surfaceNode_) {
147         WLOGFE("RSWindowAnimation: srcNode has no surface, winId: %{public}u", srcNode->GetWindowId());
148         return false;
149     }
150 
151     if (dstNode != nullptr && !dstNode->leashWinSurfaceNode_ && !dstNode->surfaceNode_) {
152         WLOGFE("RSWindowAnimation: dstNode has no surface, winId: %{public}u", dstNode->GetWindowId());
153         return false;
154     }
155 
156     // check support window mode when one app starts another app
157     if ((dstNode != nullptr && dstInfo != nullptr) &&
158         !WindowHelper::CheckSupportWindowMode(dstNode->GetWindowMode(), dstNode->GetModeSupportInfo(), dstInfo)) {
159         WLOGFE("RSWindowAnimation: the mode of dstNode isn't supported, winId: %{public}u, mode: %{public}u, "
160             "modeSupportInfo: %{public}u", dstNode->GetWindowId(), dstNode->GetWindowMode(),
161             dstNode->GetModeSupportInfo());
162         return false;
163     }
164 
165     auto node = (dstNode != nullptr ? dstNode : srcNode);
166     return CheckRemoteAnimationEnabled(node->GetDisplayId());
167 }
168 
OnRemoteDie(const sptr<IRemoteObject> & remoteObject)169 void RemoteAnimation::OnRemoteDie(const sptr<IRemoteObject>& remoteObject)
170 {
171     WLOGFI("RSWindowAnimation: OnRemoteDie!");
172     if (windowAnimationController_ != nullptr && windowAnimationController_->AsObject() == remoteObject) {
173         windowAnimationController_ = nullptr;
174     }
175     if (animationFirst_) {
176         CallbackTimeOutProcess();
177     }
178 }
179 
GetAndDrawSnapShot(const sptr<WindowNode> & srcNode)180 static void GetAndDrawSnapShot(const sptr<WindowNode>& srcNode)
181 {
182     if (srcNode == nullptr || srcNode->leashWinSurfaceNode_ == nullptr) {
183         WLOGFD("srcNode or srcNode->leashWinSurfaceNode_ is empty");
184         return;
185     }
186     if (srcNode->firstFrameAvailable_) {
187         std::shared_ptr<Media::PixelMap> pixelMap;
188         bool snapSucc = SurfaceDraw::GetSurfaceSnapshot(srcNode->surfaceNode_, pixelMap, SNAPSHOT_TIMEOUT_MS, 1.0, 1.0);
189         if (!snapSucc) {
190             // need to draw starting window when get pixelmap failed
191             WLOGFE("get surfaceSnapshot failed for window:%{public}u", srcNode->GetWindowId());
192             return;
193         }
194         WindowInnerManager::GetInstance().UpdateMissionSnapShot(srcNode, pixelMap);
195         struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
196         rsSurfaceNodeConfig.SurfaceNodeName = "closeWin" + std::to_string(srcNode->GetWindowId());
197         srcNode->closeWinSurfaceNode_ = RSSurfaceNode::Create(rsSurfaceNodeConfig,
198             RSSurfaceNodeType::STARTING_WINDOW_NODE);
199         auto rect = srcNode->GetWindowRect();
200         srcNode->closeWinSurfaceNode_->SetBounds(0, 0, rect.width_, rect.height_);
201         SurfaceDraw::DrawImageRect(srcNode->closeWinSurfaceNode_, srcNode->GetWindowRect(),
202             pixelMap, 0x00ffffff, true);
203         srcNode->leashWinSurfaceNode_->RemoveChild(srcNode->surfaceNode_);
204         srcNode->leashWinSurfaceNode_->AddChild(srcNode->closeWinSurfaceNode_, -1);
205         RSTransaction::FlushImplicitTransaction();
206         WLOGFI("Draw surface snapshot in starting window for window:%{public}u", srcNode->GetWindowId());
207     } else if (srcNode->surfaceNode_) {
208         srcNode->surfaceNode_->SetIsNotifyUIBufferAvailable(true);
209         WLOGFI("Draw startingWindow in starting window for window:%{public}u", srcNode->GetWindowId());
210     }
211 }
212 
GetTransitionEvent(sptr<WindowTransitionInfo> srcInfo,sptr<WindowTransitionInfo> dstInfo,const sptr<WindowNode> & srcNode,const sptr<WindowNode> & dstNode)213 TransitionEvent RemoteAnimation::GetTransitionEvent(sptr<WindowTransitionInfo> srcInfo,
214     sptr<WindowTransitionInfo> dstInfo, const sptr<WindowNode>& srcNode, const sptr<WindowNode>& dstNode)
215 {
216     auto transitionReason = srcInfo->GetTransitionReason(); // src reason same as dst reason
217     if (srcNode != nullptr && eventMap_.find(transitionReason) != eventMap_.end()) {
218         WLOGFI("current window:%{public}u state: %{public}u transitionReason:%{public}u", srcNode->GetWindowId(),
219             static_cast<uint32_t>(srcNode->stateMachine_.GetCurrentState()), static_cast<uint32_t>(transitionReason));
220         if (srcNode->stateMachine_.IsWindowNodeHiddenOrHiding()) {
221             WLOGFE("srcNode is hiding or hidden id: %{public}u!", srcNode->GetWindowId());
222             return TransitionEvent::UNKNOWN;
223         }
224         return eventMap_[transitionReason];
225     }
226     WLOGFI("Ability Transition");
227     if (dstNode == nullptr) {
228         if (dstInfo->GetAbilityToken() == nullptr) {
229             WLOGFE("target window abilityToken is null");
230         }
231         return TransitionEvent::UNKNOWN;
232     } else {
233         WLOGFI("current window:%{public}u state: %{public}u", dstNode->GetWindowId(),
234             static_cast<uint32_t>(dstNode->stateMachine_.GetCurrentState()));
235         if (WindowHelper::IsMainWindow(dstInfo->GetWindowType())) {
236             if (dstNode->stateMachine_.IsWindowNodeShownOrShowing()) {
237                 WLOGFE("dstNode is showing or shown id: %{public}d state:%{public}u!",
238                     dstNode->GetWindowId(), static_cast<uint32_t>(dstNode->stateMachine_.GetCurrentState()));
239                 return TransitionEvent::UNKNOWN;
240             }
241             return TransitionEvent::APP_TRANSITION;
242         } else if (dstInfo->GetWindowType() == WindowType::WINDOW_TYPE_DESKTOP) {
243             return TransitionEvent::HOME;
244         }
245     }
246     return TransitionEvent::UNKNOWN;
247 }
248 
GetTransitionFinishedCallback(const sptr<WindowNode> & srcNode,const sptr<WindowNode> & dstNode)249 sptr<RSWindowAnimationFinishedCallback> RemoteAnimation::GetTransitionFinishedCallback(
250     const sptr<WindowNode>& srcNode, const sptr<WindowNode>& dstNode)
251 {
252     wptr<WindowNode> weak = dstNode;
253     wptr<WindowNode> weakSrc = srcNode;
254     auto callback = [weakSrc, weak]() {
255         WLOGFI("RSWindowAnimation: on finish transition with minimize pre fullscreen!");
256         auto weakNode = weak.promote();
257         if (weakNode == nullptr) {
258             WLOGFE("dst windowNode is nullptr!");
259             return;
260         }
261         FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION),
262             "wms:async:ShowRemoteAnimation");
263         if (!weakNode->stateMachine_.IsWindowNodeShownOrShowing()) {
264             WLOGFI("node:%{public}u is not play show animation with state:%{public}u!", weakNode->GetWindowId(),
265                 static_cast<uint32_t>(weakNode->stateMachine_.GetCurrentState()));
266             return;
267         }
268         MinimizeApp::ExecuteMinimizeAll(); // minimize execute in show animation
269         RSAnimationTimingProtocol timingProtocol(200); // animation time
270         RSNode::Animate(timingProtocol, RSAnimationTimingCurve::EASE_OUT, [weakNode]() {
271             auto winRect = weakNode->GetWindowRect();
272             WLOGFD("name:%{public}s id:%{public}u winRect:[x:%{public}d, y:%{public}d, w:%{public}d, h:%{public}d]",
273                 weakNode->GetWindowName().c_str(), weakNode->GetWindowId(),
274                 winRect.posX_, winRect.posY_, winRect.width_, winRect.height_);
275             if (!weakNode->leashWinSurfaceNode_) {
276                 return;
277             }
278             weakNode->leashWinSurfaceNode_->SetBounds(
279                 winRect.posX_, winRect.posY_, winRect.width_, winRect.height_);
280             RSTransaction::FlushImplicitTransaction();
281             weakNode->stateMachine_.TransitionTo(WindowNodeState::SHOW_ANIMATION_DONE);
282         });
283     };
284     return CreateAnimationFinishedCallback(callback, dstNode);
285 }
286 
NotifyAnimationStartApp(sptr<WindowTransitionInfo> srcInfo,const sptr<WindowNode> & srcNode,const sptr<WindowNode> & dstNode,sptr<RSWindowAnimationTarget> & dstTarget,sptr<RSWindowAnimationFinishedCallback> & finishedCallback)287 WMError RemoteAnimation::NotifyAnimationStartApp(sptr<WindowTransitionInfo> srcInfo,
288     const sptr<WindowNode>& srcNode, const sptr<WindowNode>& dstNode,
289     sptr<RSWindowAnimationTarget>& dstTarget, sptr<RSWindowAnimationFinishedCallback>& finishedCallback)
290 {
291     if (animationFirst_) {
292         // From Recent also need to minimize window
293         MinimizeApp::ExecuteMinimizeAll();
294     }
295     // start app from launcher
296     if (srcNode != nullptr && srcNode->GetWindowType() == WindowType::WINDOW_TYPE_DESKTOP) {
297         WLOGFI("RSWindowAnimation: start app id:%{public}u from launcher!", dstNode->GetWindowId());
298         windowAnimationController_->OnStartApp(StartingAppType::FROM_LAUNCHER, dstTarget, finishedCallback);
299         return WMError::WM_OK;
300     }
301     // start app from recent
302     if (srcInfo != nullptr && srcInfo->GetIsRecent()) {
303         WLOGFI("RSWindowAnimation: start app id:%{public}u from recent!", dstNode->GetWindowId());
304         windowAnimationController_->OnStartApp(StartingAppType::FROM_RECENT, dstTarget, finishedCallback);
305         return WMError::WM_OK;
306     }
307     // start app from other
308     WLOGFI("RSWindowAnimation: start app id:%{public}u from other!", dstNode->GetWindowId());
309     windowAnimationController_->OnStartApp(StartingAppType::FROM_OTHER, dstTarget, finishedCallback);
310     return WMError::WM_OK;
311 }
312 
GetExpectRect(const sptr<WindowNode> & dstNode,const sptr<RSWindowAnimationTarget> & dstTarget)313 void RemoteAnimation::GetExpectRect(const sptr<WindowNode>& dstNode, const sptr<RSWindowAnimationTarget>& dstTarget)
314 {
315     // when exit immersive, startingWindow (0,0,w,h), but app need avoid
316     bool needAvoid = (dstNode->GetWindowFlags() & static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_NEED_AVOID));
317     auto winRoot = windowRoot_.promote();
318     if (needAvoid && winRoot) {
319         auto avoidRect = winRoot->GetDisplayRectWithoutSystemBarAreas(dstNode);
320         if (WindowHelper::IsEmptyRect(avoidRect)) {
321             return;
322         }
323         WLOGFI("name:%{public}s id:%{public}u avoidRect:[x:%{public}d, y:%{public}d, w:%{public}d, h:%{public}d]",
324             dstNode->GetWindowName().c_str(), dstNode->GetWindowId(),
325             avoidRect.posX_, avoidRect.posY_, avoidRect.width_, avoidRect.height_);
326         if (WindowHelper::IsMainFullScreenWindow(dstNode->GetWindowType(), dstNode->GetWindowMode())) {
327             auto boundsRect = RectF(avoidRect.posX_, avoidRect.posY_, avoidRect.width_, avoidRect.height_);
328             auto displayInfo = DisplayGroupInfo::GetInstance().GetDisplayInfo(dstNode->GetDisplayId());
329             if (displayInfo && WmsUtils::IsExpectedRotatableWindow(dstNode->GetRequestedOrientation(),
330                 displayInfo->GetDisplayOrientation(), dstNode->GetWindowFlags())) {
331                 WLOGFD("[FixOrientation] window %{public}u expected rotatable, pre-cal bounds", dstNode->GetWindowId());
332                 boundsRect = RectF(avoidRect.posX_, avoidRect.posY_, avoidRect.height_, avoidRect.width_);
333             }
334             dstTarget->windowBounds_.rect_ = boundsRect;
335             if (dstNode->leashWinSurfaceNode_) {
336                 dstNode->leashWinSurfaceNode_->SetBounds(avoidRect.posX_, avoidRect.posY_,
337                     avoidRect.width_, avoidRect.height_);
338             }
339         }
340     }
341 }
342 
NotifyAnimationTransition(sptr<WindowTransitionInfo> srcInfo,sptr<WindowTransitionInfo> dstInfo,const sptr<WindowNode> & srcNode,const sptr<WindowNode> & dstNode)343 WMError RemoteAnimation::NotifyAnimationTransition(sptr<WindowTransitionInfo> srcInfo,
344     sptr<WindowTransitionInfo> dstInfo, const sptr<WindowNode>& srcNode,
345     const sptr<WindowNode>& dstNode)
346 {
347     if (!dstNode) {
348         return WMError::WM_ERROR_NULLPTR;
349     }
350     WLOGFI("RSWindowAnimation: notify animation transition with dst currId:%{public}u!", dstNode->GetWindowId());
351     bool needMinimizeSrcNode = MinimizeApp::IsNodeNeedMinimizeWithReason(srcNode, MinimizeReason::OTHER_WINDOW);
352     auto finishedCallback = CreateShowAnimationFinishedCallback(srcNode, dstNode, needMinimizeSrcNode);
353     if (finishedCallback == nullptr) {
354         return WMError::WM_ERROR_NO_MEM;
355     }
356     auto dstTarget = CreateWindowAnimationTarget(dstInfo, dstNode);
357     if (dstTarget == nullptr) {
358         WLOGFE("RSWindowAnimation: Failed to create dst target!");
359         finishedCallback->OnAnimationFinished();
360         return WMError::WM_ERROR_NO_MEM;
361     }
362 
363     std::unordered_map<std::string, std::string> payload;
364     if (srcNode) {
365         payload["srcPid"] = std::to_string(srcNode->GetCallingPid());
366     }
367     ResSchedReport::GetInstance().ResSchedDataReport(
368         Rosen::RES_TYPE_SHOW_REMOTE_ANIMATION, Rosen::REMOTE_ANIMATION_BEGIN, payload);
369     // when exit immersive, startingWindow (0,0,w,h), but app need avoid
370     GetExpectRect(dstNode, dstTarget);
371     dstNode->isPlayAnimationShow_ = true;
372     // Transition to next state and update task count will success when enable animationFirst_
373     dstNode->stateMachine_.TransitionTo(WindowNodeState::SHOW_ANIMATION_PLAYING);
374     dstNode->stateMachine_.UpdateAnimationTaskCount(true);
375     // from app to app
376     if (needMinimizeSrcNode && srcNode != nullptr) {
377         auto srcTarget = CreateWindowAnimationTarget(srcInfo, srcNode);
378         // to avoid normal animation
379         srcNode->isPlayAnimationHide_ = true;
380         srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_PLAYING);
381         srcNode->stateMachine_.UpdateAnimationTaskCount(true);
382         auto winController = windowController_.promote();
383         if (winController) {
384             winController->RemoveWindowNode(srcNode->GetWindowId(), true);
385         }
386         if (animationFirst_) {
387             // Notify minimize before animation when animationFirst is enable.
388             // Or notify minimize in animation finished callback.
389             MinimizeApp::ExecuteMinimizeAll();
390         }
391         WLOGFI("RSWindowAnimation: app transition from id:%{public}u to id:%{public}u!",
392             srcNode->GetWindowId(), dstNode->GetWindowId());
393         windowAnimationController_->OnAppTransition(srcTarget, dstTarget, finishedCallback);
394         return WMError::WM_OK;
395     }
396     return NotifyAnimationStartApp(srcInfo, srcNode, dstNode, dstTarget, finishedCallback);
397 }
398 
NotifyAnimationMinimize(sptr<WindowTransitionInfo> srcInfo,const sptr<WindowNode> & srcNode)399 WMError RemoteAnimation::NotifyAnimationMinimize(sptr<WindowTransitionInfo> srcInfo, const sptr<WindowNode>& srcNode)
400 {
401     sptr<RSWindowAnimationTarget> srcTarget = CreateWindowAnimationTarget(srcInfo, srcNode);
402     if (srcTarget == nullptr) {
403         return WMError::WM_ERROR_NO_MEM;
404     }
405     WLOGFI("RSWindowAnimation: notify animation minimize Id:%{public}u!", srcNode->GetWindowId());
406     srcNode->isPlayAnimationHide_ = true;
407     srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_PLAYING);
408     auto winController = windowController_.promote();
409     if (winController) {
410         winController->RemoveWindowNode(srcNode->GetWindowId(), true);
411     }
412     sptr<RSWindowAnimationFinishedCallback> finishedCallback = CreateHideAnimationFinishedCallback(
413         srcNode, TransitionEvent::MINIMIZE);
414     if (finishedCallback == nullptr) {
415         WLOGFE("New RSIWindowAnimationFinishedCallback failed");
416         return WMError::WM_ERROR_NO_MEM;
417     }
418 
419     srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_PLAYING);
420     srcNode->stateMachine_.UpdateAnimationTaskCount(true);
421     windowAnimationController_->OnMinimizeWindow(srcTarget, finishedCallback);
422     return WMError::WM_OK;
423 }
424 
NotifyAnimationClose(sptr<WindowTransitionInfo> srcInfo,const sptr<WindowNode> & srcNode,TransitionEvent event)425 WMError RemoteAnimation::NotifyAnimationClose(sptr<WindowTransitionInfo> srcInfo, const sptr<WindowNode>& srcNode,
426     TransitionEvent event)
427 {
428     sptr<RSWindowAnimationTarget> srcTarget = CreateWindowAnimationTarget(srcInfo, srcNode);
429     if (srcTarget == nullptr) {
430         return WMError::WM_ERROR_NO_MEM;
431     }
432     WLOGFI("RSWindowAnimation: notify animation close id:%{public}u!", srcNode->GetWindowId());
433     srcNode->isPlayAnimationHide_ = true;
434     auto winController = windowController_.promote();
435     if (winController) {
436         winController->RemoveWindowNode(srcNode->GetWindowId(), true);
437     }
438     sptr<RSWindowAnimationFinishedCallback> finishedCallback = CreateHideAnimationFinishedCallback(srcNode, event);
439     if (finishedCallback == nullptr) {
440         WLOGFE("New RSIWindowAnimationFinishedCallback failed");
441         return WMError::WM_ERROR_NO_MEM;
442     }
443     srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_PLAYING);
444     srcNode->stateMachine_.UpdateAnimationTaskCount(true);
445     windowAnimationController_->OnCloseWindow(srcTarget, finishedCallback);
446     return WMError::WM_OK;
447 }
448 
NotifyAnimationAbilityDied(sptr<WindowTransitionInfo> info)449 void RemoteAnimation::NotifyAnimationAbilityDied(sptr<WindowTransitionInfo> info)
450 {
451     if (info == nullptr) {
452         WLOGFE("Window transition info is null!");
453         return;
454     }
455     auto handler = wmsTaskHandler_.lock();
456     if (handler == nullptr) {
457         WLOGFE("wmsTaskHandler_ is nullptr");
458         return;
459     }
460     // need post task when visit windowRoot node map
461     auto task = [info]() {
462         if (!CheckAnimationController()) {
463             return;
464         }
465         WLOGFI("ability died bundleName:%{public}s, abilityName:%{public}s", info->GetBundleName().c_str(),
466             info->GetAbilityName().c_str());
467         sptr<RSWindowAnimationTarget> target = new(std::nothrow) RSWindowAnimationTarget();
468         if (target == nullptr) {
469             WLOGFE("target is nullptr");
470             return;
471         }
472         target->bundleName_ = info->GetBundleName();
473         target->abilityName_ = info->GetAbilityName();
474         target->missionId_ = info->GetMissionId();
475         target->windowId_ = INVALID_WINDOW_ID;
476         auto func = []() { WLOGFI("NotifyAbilityDied finished!"); };
477         auto finishCallback = CreateAnimationFinishedCallback(func, nullptr);
478         windowAnimationController_->OnCloseWindow(target, finishCallback);
479     };
480     bool ret = handler->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
481     if (!ret) {
482         WLOGFE("EventHandler PostTask Failed");
483         task();
484     }
485 }
486 
NotifyAnimationBackTransition(sptr<WindowTransitionInfo> srcInfo,sptr<WindowTransitionInfo> dstInfo,const sptr<WindowNode> & srcNode,const sptr<WindowNode> & dstNode,const TransitionEvent event)487 WMError RemoteAnimation::NotifyAnimationBackTransition(sptr<WindowTransitionInfo> srcInfo,
488     sptr<WindowTransitionInfo> dstInfo, const sptr<WindowNode>& srcNode,
489     const sptr<WindowNode>& dstNode, const TransitionEvent event)
490 {
491     if (!animationFirst_) {
492         WLOGFE("not animation first!");
493         return WMError::WM_ERROR_NO_REMOTE_ANIMATION;
494     }
495     if (!dstNode || !srcNode) {
496         WLOGFE("dstNode or srcNode is nullptr, no need transition animation");
497         return WMError::WM_ERROR_NULLPTR;
498     }
499     WLOGFI("RSWindowAnimation: app back transition from id:%{public}u to id:%{public}u!",
500         srcNode->GetWindowId(), dstNode->GetWindowId());
501     sptr<RSWindowAnimationTarget> srcTarget = CreateWindowAnimationTarget(srcInfo, srcNode);
502     sptr<RSWindowAnimationTarget> dstTarget = CreateWindowAnimationTarget(dstInfo, dstNode);
503     if (srcTarget == nullptr || dstTarget == nullptr) {
504         return WMError::WM_ERROR_NO_MEM;
505     }
506     srcNode->isPlayAnimationHide_ = true;
507     srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_PLAYING);
508     srcNode->stateMachine_.UpdateAnimationTaskCount(true);
509     auto winController = windowController_.promote();
510     if (winController) {
511         winController->RemoveWindowNode(srcNode->GetWindowId(), true);
512         if (event == TransitionEvent::BACK_TRANSITION) {
513             GetAndDrawSnapShot(srcNode);
514         }
515     }
516     if (animationFirst_ && event == TransitionEvent::BACKGROUND_TRANSITION) {
517         MinimizeApp::ExecuteMinimizeAll();
518     }
519     dstNode->isPlayAnimationShow_ = true;
520     dstNode->stateMachine_.TransitionTo(WindowNodeState::SHOW_ANIMATION_PLAYING);
521     dstNode->stateMachine_.UpdateAnimationTaskCount(true);
522     wptr<WindowNode> srcNodeWptr = srcNode;
523     wptr<WindowNode> dstNodeWptr = dstNode;
524     auto func = [srcNodeWptr, dstNodeWptr]() {
525         WLOGFI("RSWindowAnimation: animationFirst use state machine process AnimationBackTransition!");
526         auto srcNodeSptr = srcNodeWptr.promote();
527         auto dstNodeSptr = dstNodeWptr.promote();
528         ProcessNodeStateTask(srcNodeSptr);
529         ProcessNodeStateTask(dstNodeSptr);
530         FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION),
531             "wms:async:ShowRemoteAnimation");
532     };
533     sptr<RSWindowAnimationFinishedCallback> finishedCallback = CreateAnimationFinishedCallback(func, dstNode);
534     if (finishedCallback == nullptr) {
535         return WMError::WM_ERROR_NO_MEM;
536     }
537     windowAnimationController_->OnAppBackTransition(srcTarget, dstTarget, finishedCallback);
538     return WMError::WM_OK;
539 }
540 
GetAnimationTargetsForHome(std::vector<sptr<RSWindowAnimationTarget>> & animationTargets,std::vector<wptr<WindowNode>> needMinimizeAppNodes)541 void RemoteAnimation::GetAnimationTargetsForHome(std::vector<sptr<RSWindowAnimationTarget>>& animationTargets,
542     std::vector<wptr<WindowNode>> needMinimizeAppNodes)
543 {
544     for (auto& weakNode : needMinimizeAppNodes) {
545         auto srcNode = weakNode.promote();
546         sptr<WindowTransitionInfo> srcInfo = new(std::nothrow) WindowTransitionInfo();
547         sptr<RSWindowAnimationTarget> srcTarget = CreateWindowAnimationTarget(srcInfo, srcNode);
548         if (srcTarget == nullptr) {
549             continue;
550         }
551         WLOGFI("notify animation by home, need minimize id%{public}u", srcNode->GetWindowId());
552         if (!WindowHelper::IsMainWindow(srcNode->GetWindowType()) ||
553             srcNode->stateMachine_.IsWindowNodeHiddenOrHiding()) {
554             WLOGFE("srcNode is already hiding or hidden id: %{public}d!", srcNode->GetWindowId());
555             continue;
556         }
557         StartAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION_HOME),
558             "wms:async:RemoteAnimationHome");
559         srcNode->isPlayAnimationHide_ = true;
560         srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_PLAYING);
561         srcNode->stateMachine_.UpdateAnimationTaskCount(true);
562         auto winController = windowController_.promote();
563         if (winController) {
564             winController->RemoveWindowNode(srcNode->GetWindowId(), true);
565         }
566         animationTargets.emplace_back(srcTarget);
567     }
568 }
569 
GetAnimationHomeFinishCallback(std::function<void (void)> & func,std::vector<wptr<WindowNode>> needMinimizeAppNodes)570 static void GetAnimationHomeFinishCallback(std::function<void(void)>& func,
571     std::vector<wptr<WindowNode>> needMinimizeAppNodes)
572 {
573     func = [needMinimizeAppNodes]() {
574         WLOGFI("NotifyAnimationByHome in animation callback not animationFirst");
575         for (auto& weakNode : needMinimizeAppNodes) {
576             auto srcNode = weakNode.promote();
577             if (srcNode == nullptr || !srcNode->stateMachine_.IsWindowNodeHiddenOrHiding()) {
578                 WLOGFE("windowNode is nullptr or is not play hide animation!");
579                 continue;
580             }
581             srcNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_DONE);
582         }
583         MinimizeApp::ExecuteMinimizeTargetReasons(MinimizeReason::MINIMIZE_ALL);
584         FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION),
585             "wms:async:ShowRemoteAnimation");
586     };
587 }
588 
NotifyAnimationByHome()589 WMError RemoteAnimation::NotifyAnimationByHome()
590 {
591     if (!CheckAnimationController()) {
592         return WMError::WM_ERROR_NO_REMOTE_ANIMATION;
593     }
594     auto needMinimizeAppNodes = MinimizeApp::GetNeedMinimizeAppNodesWithReason(MinimizeReason::MINIMIZE_ALL);
595     WLOGFI("RSWindowAnimation: notify animation by home, need minimize size: %{public}u",
596         static_cast<uint32_t>(needMinimizeAppNodes.size()));
597     std::vector<sptr<RSWindowAnimationTarget>> animationTargets;
598     GetAnimationTargetsForHome(animationTargets, needMinimizeAppNodes);
599     std::function<void(void)> func;
600     if (animationFirst_) {
601         MinimizeApp::ExecuteMinimizeTargetReasons(MinimizeReason::MINIMIZE_ALL);
602         func = [needMinimizeAppNodes]() {
603             WLOGFI("NotifyAnimationByHome in animation callback in animationFirst with size:%{public}u",
604                 static_cast<uint32_t>(needMinimizeAppNodes.size()));
605             for (auto& weakNode : needMinimizeAppNodes) {
606                 auto srcNode = weakNode.promote();
607                 ProcessNodeStateTask(srcNode);
608                 FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER,
609                     static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION_HOME),
610                     "wms:async:RemoteAnimationHome");
611             }
612         };
613     } else {
614         GetAnimationHomeFinishCallback(func, needMinimizeAppNodes);
615     }
616 #ifdef SOC_PERF_ENABLE
617     constexpr int32_t ACTION_TYPE_CPU_BOOST_CMDID = 10060;
618     OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(ACTION_TYPE_CPU_BOOST_CMDID, true, "");
619 #endif
620     sptr<RSWindowAnimationFinishedCallback> finishedCallback = CreateAnimationFinishedCallback(func, nullptr);
621     if (finishedCallback == nullptr) {
622         return WMError::WM_ERROR_NO_MEM;
623     }
624     // need use OnMinimizeWindows with controller
625     windowAnimationController_->OnMinimizeAllWindow(animationTargets, finishedCallback);
626     return WMError::WM_OK;
627 }
628 
NotifyAnimationTargetsUpdate(std::vector<uint32_t> & fullScreenWinIds,std::vector<uint32_t> & floatWinIds)629 void RemoteAnimation::NotifyAnimationTargetsUpdate(std::vector<uint32_t>& fullScreenWinIds,
630     std::vector<uint32_t>& floatWinIds)
631 {
632     auto handler = wmsTaskHandler_.lock();
633     if (handler == nullptr) {
634         WLOGFE("wmsTaskHandler_ is nullptr");
635         return;
636     }
637     // need post task when visit windowRoot node map
638     auto task = [fullScreenWinIds, floatWinIds]() {
639         if (!CheckAnimationController()) {
640             return;
641         }
642         auto winRoot = windowRoot_.promote();
643         if (winRoot == nullptr) {
644             WLOGFE("window root is nullptr");
645             return;
646         }
647         std::vector<sptr<RSWindowAnimationTarget>> floatAnimationTargets;
648         std::vector<sptr<RSWindowAnimationTarget>> fullScreenAnimationTargets;
649         for (auto& id : fullScreenWinIds) {
650             auto fullScreenNode = winRoot->GetWindowNode(id);
651             sptr<RSWindowAnimationTarget> fullScreenTarget = CreateWindowAnimationTarget(nullptr, fullScreenNode);
652             if (fullScreenTarget != nullptr) {
653                 fullScreenAnimationTargets.emplace_back(fullScreenTarget);
654             }
655         }
656         for (auto& id : floatWinIds) {
657             auto floatNode = winRoot->GetWindowNode(id);
658             sptr<RSWindowAnimationTarget> floatTarget = CreateWindowAnimationTarget(nullptr, floatNode);
659             if (floatTarget != nullptr) {
660                 floatAnimationTargets.emplace_back(floatTarget);
661             }
662         }
663         // delete when need all fullscreen targets
664         sptr<RSWindowAnimationTarget> fullScreenAnimationTarget = nullptr;
665         if (!fullScreenAnimationTargets.empty()) {
666             fullScreenAnimationTarget = fullScreenAnimationTargets[0];
667         }
668         windowAnimationController_->OnWindowAnimationTargetsUpdate(fullScreenAnimationTarget,
669             floatAnimationTargets);
670     };
671     bool ret = handler->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
672     if (!ret) {
673         WLOGFE("EventHandler PostTask Failed");
674         task();
675     }
676 }
677 
NotifyAnimationScreenUnlock(std::function<void (void)> callback,sptr<WindowNode> node)678 WMError RemoteAnimation::NotifyAnimationScreenUnlock(std::function<void(void)> callback, sptr<WindowNode> node)
679 {
680     if (!CheckAnimationController()) {
681         return WMError::WM_ERROR_NO_REMOTE_ANIMATION;
682     }
683     WLOGFI("NotifyAnimationScreenUnlock");
684     sptr<RSWindowAnimationFinishedCallback> finishedCallback = CreateAnimationFinishedCallback(callback, node);
685     if (finishedCallback == nullptr) {
686         return WMError::WM_ERROR_NO_MEM;
687     }
688 
689     windowAnimationController_->OnScreenUnlock(finishedCallback);
690     return WMError::WM_OK;
691 }
692 
GetWindowAnimationTargets(std::vector<uint32_t> missionIds,std::vector<sptr<RSWindowAnimationTarget>> & targets)693 WMError RemoteAnimation::GetWindowAnimationTargets(std::vector<uint32_t> missionIds,
694     std::vector<sptr<RSWindowAnimationTarget>>& targets)
695 {
696     auto winRoot = windowRoot_.promote();
697     if (winRoot == nullptr) {
698         WLOGFE("window root is nullptr");
699         return WMError::WM_ERROR_NO_MEM;
700     }
701     for (uint32_t& missionId : missionIds) {
702         sptr<WindowNode> windowNode = winRoot->GetWindowNodeByMissionId(missionId);
703         auto target = CreateWindowAnimationTarget(nullptr, windowNode);
704         if (target == nullptr) {
705             continue;
706         }
707         targets.push_back(target);
708     }
709     return WMError::WM_OK;
710 }
711 
CreateWindowAnimationTarget(sptr<WindowTransitionInfo> info,const sptr<WindowNode> & windowNode)712 sptr<RSWindowAnimationTarget> RemoteAnimation::CreateWindowAnimationTarget(sptr<WindowTransitionInfo> info,
713     const sptr<WindowNode>& windowNode)
714 {
715     if (windowNode == nullptr) {
716         WLOGFW("Failed to create window animation target, window node is null!");
717         return nullptr;
718     }
719 
720     sptr<RSWindowAnimationTarget> windowAnimationTarget = new(std::nothrow) RSWindowAnimationTarget();
721     if (windowAnimationTarget == nullptr) {
722         WLOGFE("New RSWindowAnimationTarget failed");
723         return nullptr;
724     }
725 
726     if (WindowHelper::IsMainWindow(windowNode->GetWindowType())) { // only starting window has abilityInfo
727         windowAnimationTarget->bundleName_ = windowNode->abilityInfo_.bundleName_;
728         windowAnimationTarget->abilityName_ = windowNode->abilityInfo_.abilityName_;
729     } else if (info) { // use for back, minimize, close
730         windowAnimationTarget->bundleName_ = info->GetBundleName();
731         windowAnimationTarget->abilityName_ = info->GetAbilityName();
732     }
733 
734     windowAnimationTarget->missionId_ = windowNode->abilityInfo_.missionId_;
735     windowAnimationTarget->windowId_ = windowNode->GetWindowId();
736     windowAnimationTarget->displayId_ = windowNode->GetDisplayId();
737     // some ability not has startingWindow,e.g. oobe
738     if (WindowHelper::IsAppWindow(windowNode->GetWindowType()) && windowNode->leashWinSurfaceNode_) {
739         windowAnimationTarget->surfaceNode_ = windowNode->leashWinSurfaceNode_;
740     } else {
741         windowAnimationTarget->surfaceNode_ = windowNode->surfaceNode_;
742     }
743     if (windowAnimationTarget->surfaceNode_ == nullptr) {
744         WLOGFE("Window surface node is null id:%{public}u, type:%{public}u!",
745             windowNode->GetWindowId(), windowNode->GetWindowType());
746         return nullptr;
747     }
748 
749     auto rect = windowNode->GetWindowRect();
750     // 0, 1, 2, 3: convert bounds to RectF
751     auto boundsRect = RectF(rect.posX_, rect.posY_, rect.width_, rect.height_);
752     auto displayInfo = DisplayGroupInfo::GetInstance().GetDisplayInfo(windowNode->GetDisplayId());
753     if (displayInfo && WmsUtils::IsExpectedRotatableWindow(windowNode->GetRequestedOrientation(),
754         displayInfo->GetDisplayOrientation(), windowNode->GetWindowMode(), windowNode->GetWindowFlags())) {
755         WLOGFD("[FixOrientation] the window %{public}u is expected rotatable, pre-calculate bounds, rect:"
756             " [%{public}d, %{public}d, %{public}d, %{public}d]", windowNode->GetWindowId(), rect.posX_, rect.posY_,
757             rect.height_, rect.width_);
758         boundsRect = RectF(rect.posX_, rect.posY_, rect.height_, rect.width_);
759     }
760     auto& stagingProperties = windowAnimationTarget->surfaceNode_->GetStagingProperties();
761     auto radius = stagingProperties.GetCornerRadius();
762     windowAnimationTarget->windowBounds_ = RRect(boundsRect, radius);
763     return windowAnimationTarget;
764 }
765 
PostProcessShowCallback(const sptr<WindowNode> & node)766 void RemoteAnimation::PostProcessShowCallback(const sptr<WindowNode>& node)
767 {
768     if (node == nullptr) {
769         WLOGFD("windowNode is nullptr!");
770         return;
771     }
772     auto winRect = node->GetWindowRect();
773     if (!node->leashWinSurfaceNode_) {
774         WLOGFD("leashWinSurfaceNode_ is nullptr with id: %{public}u!", node->GetWindowId());
775         return;
776     }
777     WLOGFD("name:%{public}s id:%{public}u winRect:[x:%{public}d, y:%{public}d, w:%{public}d, h:%{public}d]",
778         node->GetWindowName().c_str(), node->GetWindowId(),
779         winRect.posX_, winRect.posY_, winRect.width_, winRect.height_);
780     node->leashWinSurfaceNode_->SetBounds(
781         winRect.posX_, winRect.posY_, winRect.width_, winRect.height_);
782     if (WindowHelper::IsRotatableWindow(node->GetWindowType(), node->GetWindowMode())) {
783         auto displayId = node->GetDisplayId();
784         auto requestOri = node->GetRequestedOrientation();
785         WLOGFD("[FixOrientation] show animation finished, update display orientation");
786         if (FIX_ORIENTATION_ENABLE) {
787             DisplayManagerServiceInner::GetInstance().SetOrientationFromWindow(displayId, requestOri, false);
788         } else {
789             DisplayManagerServiceInner::GetInstance().SetOrientationFromWindow(displayId, requestOri, true);
790         }
791     }
792     RSTransaction::FlushImplicitTransaction();
793 }
794 
ExecuteFinalStateTask(sptr<WindowNode> & node)795 void RemoteAnimation::ExecuteFinalStateTask(sptr<WindowNode>& node)
796 {
797     StateTask destroyTask = nullptr;
798     auto winRoot = windowRoot_.promote();
799     if (winRoot == nullptr || node == nullptr) {
800         WLOGFE("windowRoot or node is nullptr");
801         return;
802     }
803     if (node->stateMachine_.IsWindowNodeHiddenOrHiding()) {
804         WLOGFI("execute task removing from rs tree id:%{public}u!", node->GetWindowId());
805         winRoot->UpdateRsTree(node->GetWindowId(), false);
806     } else if (node->stateMachine_.IsWindowNodeShownOrShowing()) {
807         WLOGFI("execute task layout after show animation id:%{public}u!", node->GetWindowId());
808         winRoot->LayoutWhenAddWindowNode(node, true);
809         WindowSystemEffect::SetWindowEffect(node, false); // no need to check animationPlaying in finishCallback
810         auto winController = windowController_.promote();
811         if (winController) {
812             winController->FlushWindowInfo(node->GetWindowId());
813         }
814     } else {
815         WLOGFD("current State:%{public}u invalid", static_cast<uint32_t>(node->stateMachine_.GetCurrentState()));
816     }
817 
818     if (node->stateMachine_.GetDestroyTask(destroyTask)) {
819         destroyTask();
820     }
821 }
822 
CallbackTimeOutProcess()823 void RemoteAnimation::CallbackTimeOutProcess()
824 {
825     auto winRoot = windowRoot_.promote();
826     if (winRoot == nullptr) {
827         WLOGFE("windowRoot is nullptr");
828         return;
829     }
830     std::vector<wptr<WindowNode>> animationPlayingNodes;
831     winRoot->GetAllAnimationPlayingNodes(animationPlayingNodes);
832     WLOGFI("CallbackTimeOutProcess playingNodes:%{public}u", static_cast<uint32_t>(animationPlayingNodes.size()));
833     for (auto& weakNode : animationPlayingNodes) {
834         auto node = weakNode.promote();
835         if (node == nullptr) {
836             continue;
837         }
838         WLOGFI("callback timeout process windowId:%{public}u", node->GetWindowId());
839         node->stateMachine_.ResetAnimationTaskCount(1);
840         ProcessNodeStateTask(node);
841     }
842 }
843 
ReportWindowAnimationAbnormalInfo(sptr<WindowNode> & node)844 static void ReportWindowAnimationAbnormalInfo(sptr<WindowNode>& node)
845 {
846     std::ostringstream oss;
847     oss << "animation callback more than task: " << "window_name: " << node->GetWindowName() << ";";
848     std::string info = oss.str();
849     info += node->stateMachine_.GenStateMachineInfo();
850     int32_t ret = HiSysEventWrite(
851         OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
852         "WINDOW_ANIMATION_ABNORMAL",
853         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
854         "PID", node->GetCallingPid(),
855         "UID", node->GetCallingUid(),
856         "PACKAGE_NAME", node->abilityInfo_.abilityName_,
857         "PROCESS_NAME", node->abilityInfo_.bundleName_,
858         "MSG", info);
859     if (ret != 0) {
860         WLOGFE("Write HiSysEvent error, ret:%{public}d", ret);
861     }
862 }
863 
ProcessNodeStateTask(sptr<WindowNode> & node)864 void RemoteAnimation::ProcessNodeStateTask(sptr<WindowNode>& node)
865 {
866     // when callback come, node maybe destroyed
867     if (node == nullptr) {
868         WLOGFI("node is nullptr!");
869         return;
870     }
871     int32_t taskCount = node->stateMachine_.GetAnimationCount();
872     if (taskCount <= 0) { // no animation task but finishCallback come
873         WLOGFE("ProcessNodeStateTask failed with windowId: %{public}u, name:%{public}s taskCount:%{public}d",
874             node->GetWindowId(), node->GetWindowName().c_str(), taskCount);
875         ReportWindowAnimationAbnormalInfo(node);
876         return;
877     }
878     node->stateMachine_.UpdateAnimationTaskCount(false);
879     taskCount = node->stateMachine_.GetAnimationCount();
880     WLOGFI("ProcessNodeStateTask windowId: %{public}u, name:%{public}s state: %{public}u, taskCount:%{public}d",
881         node->GetWindowId(), node->GetWindowName().c_str(),
882         static_cast<uint32_t>(node->stateMachine_.GetCurrentState()), taskCount);
883     if (taskCount > 0 || taskCount < 0) {
884         WLOGFI("not last state task of window: %{public}d, %{public}d callback left not be executed!",
885             node->GetWindowId(), taskCount);
886         return;
887     }
888     ExecuteFinalStateTask(node);
889     if (node->stateMachine_.IsWindowNodeShownOrShowing()) {
890         // delete when immersive solution change
891         PostProcessShowCallback(node);
892         node->stateMachine_.TransitionTo(WindowNodeState::SHOW_ANIMATION_DONE);
893     } else if (node->stateMachine_.IsWindowNodeHiddenOrHiding()) {
894         node->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_DONE);
895         auto winRoot = windowRoot_.promote();
896         if (winRoot != nullptr) {
897             winRoot->UpdateDisplayOrientationWhenHideWindow(node);
898         }
899     } else if (node->stateMachine_.GetCurrentState() == WindowNodeState::DESTROYED) {
900         auto winRoot = windowRoot_.promote();
901         if (winRoot != nullptr) {
902             winRoot->UpdateDisplayOrientationWhenHideWindow(node);
903         }
904     }
905 }
906 
CreateShowAnimationFinishedCallback(const sptr<WindowNode> & srcNode,const sptr<WindowNode> & dstNode,bool needMinimizeSrcNode)907 sptr<RSWindowAnimationFinishedCallback> RemoteAnimation::CreateShowAnimationFinishedCallback(
908     const sptr<WindowNode>& srcNode, const sptr<WindowNode>& dstNode, bool needMinimizeSrcNode)
909 {
910     wptr<WindowNode> srcNodeWptr = srcNode;
911     wptr<WindowNode> dstNodeWptr = dstNode;
912     // need add timeout check
913     std::function<void(void)> func;
914     if (!animationFirst_) {
915         WLOGFI("RSWindowAnimation: not animationFirst use default callback!");
916         return GetTransitionFinishedCallback(srcNode, dstNode);
917     } else {
918         func = [srcNodeWptr, dstNodeWptr, needMinimizeSrcNode]() {
919             WLOGFI("RSWindowAnimation: animationFirst use state machine process ShowAnimationFinishedCallback!");
920             auto srcNodeSptr = srcNodeWptr.promote();
921             auto dstNodeSptr = dstNodeWptr.promote();
922             if (dstNodeSptr == nullptr) {
923                 WLOGFE("dstNode is nullptr!");
924                 return;
925             }
926             ProcessNodeStateTask(dstNodeSptr);
927             // launcher not do this
928             if (needMinimizeSrcNode) {
929                 ProcessNodeStateTask(srcNodeSptr);
930             }
931             if (dstNodeSptr->stateMachine_.GetCurrentState() == WindowNodeState::SHOW_ANIMATION_DONE &&
932                 dstNodeSptr->leashWinSurfaceNode_) {
933                 dstNodeSptr->leashWinSurfaceNode_->SetAnimationFinished();
934             }
935             WLOGFI("current window:%{public}u state: %{public}u", dstNodeSptr->GetWindowId(),
936                 static_cast<uint32_t>(dstNodeSptr->stateMachine_.GetCurrentState()));
937             FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION),
938                 "wms:async:ShowRemoteAnimation");
939             std::unordered_map<std::string, std::string> payload;
940             if (srcNodeWptr != nullptr) {
941                 payload["srcPid"] = std::to_string(srcNodeWptr->GetCallingPid());
942             }
943             ResSchedReport::GetInstance().ResSchedDataReport(
944                 Rosen::RES_TYPE_SHOW_REMOTE_ANIMATION, Rosen::REMOTE_ANIMATION_END, payload);
945         };
946     }
947     return CreateAnimationFinishedCallback(func, dstNode);
948 }
949 
ProcessAbility(const sptr<WindowNode> & srcNode,TransitionEvent event)950 static void ProcessAbility(const sptr<WindowNode>& srcNode, TransitionEvent event)
951 {
952     if (srcNode == nullptr) {
953         return;
954     }
955     switch (event) {
956         case TransitionEvent::CLOSE_BUTTON: {
957             WLOGFI("close windowId: %{public}u, name:%{public}s",
958                 srcNode->GetWindowId(), srcNode->GetWindowName().c_str());
959             WindowInnerManager::GetInstance().CloseAbility(srcNode);
960             break;
961         }
962         case TransitionEvent::MINIMIZE: {
963             WLOGFI("minimize windowId: %{public}u, name:%{public}s",
964                 srcNode->GetWindowId(), srcNode->GetWindowName().c_str());
965             WindowInnerManager::GetInstance().MinimizeAbility(srcNode, true);
966             break;
967         }
968         case TransitionEvent::CLOSE: // close by back
969         default:
970             break;
971     }
972 }
973 
NotifyAnimationUpdateWallpaper(sptr<WindowNode> node)974 void RemoteAnimation::NotifyAnimationUpdateWallpaper(sptr<WindowNode> node)
975 {
976     if (!CheckAnimationController()) {
977         return;
978     }
979     WLOGFI("NotifyAnimationUpdateWallpaper");
980     sptr<RSWindowAnimationTarget> srcTarget = CreateWindowAnimationTarget(nullptr, node);
981     // when wallpaper destroy, update with nullptr
982     windowAnimationController_->OnWallpaperUpdate(srcTarget);
983 }
984 
CreateHideAnimationFinishedCallback(const sptr<WindowNode> & srcNode,TransitionEvent event)985 sptr<RSWindowAnimationFinishedCallback> RemoteAnimation::CreateHideAnimationFinishedCallback(
986     const sptr<WindowNode>& srcNode, TransitionEvent event)
987 {
988     wptr<WindowNode> srcNodeWptr = srcNode;
989     // need add timeout check
990     std::function<void(void)> func;
991     if (!animationFirst_) {
992         func = [srcNodeWptr, event]() {
993             WLOGFI("RSWindowAnimation: not animationFirst use default callback!");
994             auto weakNode = srcNodeWptr.promote();
995             if (weakNode == nullptr || weakNode->abilityToken_ == nullptr) {
996                 WLOGFE("window node or ability token is nullptr");
997                 return;
998             }
999             if (!weakNode->stateMachine_.IsWindowNodeHiddenOrHiding()) {
1000                 WLOGFE("window is not playing hide animation");
1001                 return;
1002             }
1003             ProcessAbility(weakNode, event);
1004             weakNode->stateMachine_.TransitionTo(WindowNodeState::HIDE_ANIMATION_DONE);
1005             FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION),
1006                 "wms:async:ShowRemoteAnimation");
1007         };
1008     } else {
1009         if (event != TransitionEvent::MINIMIZE) {
1010             GetAndDrawSnapShot(srcNode);
1011         }
1012         ProcessAbility(srcNode, event); // execute first when animationFirst
1013         func = [srcNodeWptr]() {
1014             WLOGFI("RSWindowAnimation: animationFirst use state machine process HideAnimationFinishedCallback!");
1015             auto srcNodeSptr = srcNodeWptr.promote();
1016             ProcessNodeStateTask(srcNodeSptr);
1017             FinishAsyncTraceArgs(HITRACE_TAG_WINDOW_MANAGER, static_cast<int32_t>(TraceTaskId::REMOTE_ANIMATION),
1018                 "wms:async:ShowRemoteAnimation");
1019         };
1020     }
1021     return CreateAnimationFinishedCallback(func, srcNode);
1022 }
1023 
ReportWindowAnimationCallbackTimeout(sptr<WindowNode> & node,const std::string & taskName)1024 static void ReportWindowAnimationCallbackTimeout(sptr<WindowNode>& node, const std::string& taskName)
1025 {
1026     std::ostringstream oss;
1027     oss << "animation callback time out: " << "window_name: " << node->GetWindowName()
1028         << "callbackName: " << taskName << ";";
1029     std::string info = oss.str();
1030     int32_t ret = HiSysEventWrite(
1031         OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
1032         "ANIMATION_CALLBACK_TIMEOUT",
1033         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
1034         "PID", node->GetCallingPid(),
1035         "UID", node->GetCallingUid(),
1036         "PACKAGE_NAME", node->abilityInfo_.abilityName_,
1037         "PROCESS_NAME", node->abilityInfo_.bundleName_,
1038         "MSG", info);
1039     if (ret != 0) {
1040         WLOGFE("Write HiSysEvent error, ret:%{public}d", ret);
1041     }
1042 }
1043 
CreateAnimationFinishedCallback(const std::function<void (void)> & callback,sptr<WindowNode> windowNode)1044 sptr<RSWindowAnimationFinishedCallback> RemoteAnimation::CreateAnimationFinishedCallback(
1045     const std::function<void(void)>& callback, sptr<WindowNode> windowNode)
1046 {
1047     if (callback == nullptr) {
1048         WLOGFE("callback is null!");
1049         return nullptr;
1050     }
1051     auto currentId = allocationId_.fetch_add(1);
1052     std::string timeOutTaskName = ANIMATION_TIME_OUT_TASK + std::to_string(currentId);
1053     wptr<WindowNode> weakNode = windowNode;
1054     auto timeoutFunc = [callback, timeOutTaskName, weakNode]() {
1055         WLOGFE("callback %{public}s is time out!", timeOutTaskName.c_str());
1056         callback();
1057         auto node = weakNode.promote();
1058         if (node == nullptr) {
1059             WLOGFW("window node is null or is home event, not report abnormal info!");
1060             return;
1061         }
1062         ReportWindowAnimationCallbackTimeout(node, timeOutTaskName);
1063     };
1064     auto callbackTask = [callback, timeOutTaskName]() {
1065         auto handler = wmsTaskHandler_.lock();
1066         if (handler != nullptr) {
1067             handler->RemoveTask(timeOutTaskName);
1068             WLOGFD("remove task %{public}s since animationCallback Come", timeOutTaskName.c_str());
1069             handler->PostTask(callback, AppExecFwk::EventQueue::Priority::IMMEDIATE);
1070         }
1071     };
1072     auto handlerSptr = wmsTaskHandler_.lock();
1073     if (handlerSptr != nullptr) {
1074         handlerSptr->PostTask(timeoutFunc, timeOutTaskName, ANIMATION_TIME_OUT_MILLISECONDS);
1075         WLOGFD("PostTask task %{public}s", timeOutTaskName.c_str());
1076     }
1077     sptr<RSWindowAnimationFinishedCallback> finishCallback = new(std::nothrow) RSWindowAnimationFinishedCallback(
1078         callbackTask);
1079     if (finishCallback == nullptr) {
1080         WLOGFE("New RSIWindowAnimationFinishedCallback failed");
1081         callbackTask();
1082         return nullptr;
1083     }
1084     return finishCallback;
1085 }
1086 } // Rosen
1087 } // OHOS
1088