1 /* 2 * Copyright (c) 2022-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_CONTEXT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_CONTEXT_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <list> 22 #include <unordered_map> 23 #include <utility> 24 25 #include "base/geometry/ng/rect_t.h" 26 #include "base/log/frame_info.h" 27 #include "base/log/frame_report.h" 28 #include "base/memory/referenced.h" 29 #include "core/common/frontend.h" 30 #include "core/components_ng/base/frame_node.h" 31 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h" 32 #include "core/components_ng/manager/drag_drop/drag_drop_manager.h" 33 #include "core/components_ng/manager/full_screen/full_screen_manager.h" 34 #include "core/components_ng/manager/safe_area/safe_area_manager.h" 35 #include "core/components_ng/manager/select_overlay/select_overlay_manager.h" 36 #include "core/components_ng/manager/shared_overlay/shared_overlay_manager.h" 37 #include "core/components_ng/pattern/custom/custom_node.h" 38 #include "core/components_ng/pattern/overlay/overlay_manager.h" 39 #include "core/components_ng/pattern/stage/stage_manager.h" 40 #include "core/components_ng/property/safe_area_insets.h" 41 #include "core/event/touch_event.h" 42 #include "core/pipeline/pipeline_base.h" 43 44 namespace OHOS::Ace::NG { 45 46 using WindowSceneTouchEventCallback = std::function<void(const std::shared_ptr<MMI::PointerEvent>&)>; 47 48 class ACE_EXPORT PipelineContext : public PipelineBase { 49 DECLARE_ACE_TYPE(NG::PipelineContext, PipelineBase); 50 51 public: 52 using SurfaceChangedCallbackMap = 53 std::unordered_map<int32_t, std::function<void(int32_t, int32_t, int32_t, int32_t, WindowSizeChangeReason)>>; 54 using SurfacePositionChangedCallbackMap = std::unordered_map<int32_t, std::function<void(int32_t, int32_t)>>; 55 using PredictTask = std::function<void(int64_t, bool)>; 56 PipelineContext(std::shared_ptr<Window> window, RefPtr<TaskExecutor> taskExecutor, 57 RefPtr<AssetManager> assetManager, RefPtr<PlatformResRegister> platformResRegister, 58 const RefPtr<Frontend>& frontend, int32_t instanceId); 59 PipelineContext(std::shared_ptr<Window> window, RefPtr<TaskExecutor> taskExecutor, 60 RefPtr<AssetManager> assetManager, const RefPtr<Frontend>& frontend, int32_t instanceId); 61 PipelineContext() = default; 62 63 ~PipelineContext() override = default; 64 65 static RefPtr<PipelineContext> GetCurrentContext(); 66 67 static float GetCurrentRootWidth(); 68 69 static float GetCurrentRootHeight(); 70 71 void SetupRootElement() override; 72 73 void SetupSubRootElement(); 74 GetRootElement()75 const RefPtr<FrameNode>& GetRootElement() const 76 { 77 return rootNode_; 78 } 79 AddKeyFrame(float fraction,const RefPtr<Curve> & curve,const std::function<void ()> & propertyCallback)80 void AddKeyFrame(float fraction, const RefPtr<Curve>& curve, const std::function<void()>& propertyCallback) override 81 {} 82 AddKeyFrame(float fraction,const std::function<void ()> & propertyCallback)83 void AddKeyFrame(float fraction, const std::function<void()>& propertyCallback) override {} 84 85 // add schedule task and return the unique mark id. 86 uint32_t AddScheduleTask(const RefPtr<ScheduleTask>& task) override; 87 88 // remove schedule task by id. 89 void RemoveScheduleTask(uint32_t id) override; 90 91 // Called by view when touch event received. 92 void OnTouchEvent(const TouchEvent& point, bool isSubPipe = false) override; 93 94 // Called by container when key event received. 95 // if return false, then this event needs platform to handle it. 96 bool OnKeyEvent(const KeyEvent& event) override; 97 98 // Called by view when mouse event received. 99 void OnMouseEvent(const MouseEvent& event) override; 100 101 // Do mouse event actively. 102 void FlushMouseEvent(); 103 104 // Called by view when axis event received. 105 void OnAxisEvent(const AxisEvent& event) override; 106 107 // Called by container when rotation event received. 108 // if return false, then this event needs platform to handle it. OnRotationEvent(const RotationEvent & event)109 bool OnRotationEvent(const RotationEvent& event) const override 110 { 111 return false; 112 } 113 114 void OnDragEvent(int32_t x, int32_t y, DragEventAction action) override; 115 116 // Called by view when idle event. 117 void OnIdle(int64_t deadline) override; 118 SetBuildAfterCallback(const std::function<void ()> & callback)119 void SetBuildAfterCallback(const std::function<void()>& callback) override 120 { 121 buildFinishCallbacks_.emplace_back(callback); 122 } 123 SaveExplicitAnimationOption(const AnimationOption & option)124 void SaveExplicitAnimationOption(const AnimationOption& option) override {} 125 CreateExplicitAnimator(const std::function<void ()> & onFinishEvent)126 void CreateExplicitAnimator(const std::function<void()>& onFinishEvent) override {} 127 ClearExplicitAnimationOption()128 void ClearExplicitAnimationOption() override {} 129 GetExplicitAnimationOption()130 AnimationOption GetExplicitAnimationOption() const override 131 { 132 return {}; 133 } 134 135 void AddOnAreaChangeNode(int32_t nodeId); 136 137 void RemoveOnAreaChangeNode(int32_t nodeId); 138 139 void HandleOnAreaChangeEvent(); 140 141 void NotifyConfigurationChange(const OnConfigurationChange& configurationChange) override; 142 143 void AddVisibleAreaChangeNode( 144 const RefPtr<FrameNode>& node, double ratio, const VisibleRatioCallback& callback, bool isUserCallback = true); 145 void RemoveVisibleAreaChangeNode(int32_t nodeId); 146 147 void HandleVisibleAreaChangeEvent(); 148 149 void Destroy() override; 150 151 void OnShow() override; 152 153 void OnHide() override; 154 155 void WindowFocus(bool isFocus) override; 156 157 void ShowContainerTitle(bool isShow, bool hasDeco = true) override; 158 159 void SetAppBgColor(const Color& color) override; 160 161 void SetAppTitle(const std::string& title) override; 162 163 void SetAppIcon(const RefPtr<PixelMap>& icon) override; 164 165 void OnSurfaceChanged(int32_t width, int32_t height, 166 WindowSizeChangeReason type = WindowSizeChangeReason::UNDEFINED, 167 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr) override; 168 169 void OnLayoutCompleted(const std::string& componentId); 170 void OnDrawCompleted(const std::string& componentId); 171 172 void OnSurfacePositionChanged(int32_t posX, int32_t posY) override; 173 174 void OnSurfaceDensityChanged(double density) override; 175 OnSystemBarHeightChanged(double statusBar,double navigationBar)176 void OnSystemBarHeightChanged(double statusBar, double navigationBar) override {} 177 OnSurfaceDestroyed()178 void OnSurfaceDestroyed() override {} 179 NotifyOnPreDraw()180 void NotifyOnPreDraw() override {} 181 CallRouterBackToPopPage()182 bool CallRouterBackToPopPage() override 183 { 184 return OnBackPressed(); 185 } 186 187 bool OnBackPressed(); 188 189 RefPtr<FrameNode> FindNavigationNodeToHandleBack(const RefPtr<UINode>& node); 190 191 void AddDirtyCustomNode(const RefPtr<UINode>& dirtyNode); 192 193 void AddDirtyLayoutNode(const RefPtr<FrameNode>& dirty); 194 195 void AddDirtyRenderNode(const RefPtr<FrameNode>& dirty); 196 197 void AddPredictTask(PredictTask&& task); 198 199 void AddAfterLayoutTask(std::function<void()>&& task); 200 201 void AddAfterRenderTask(std::function<void()>&& task); 202 203 void FlushDirtyNodeUpdate(); 204 205 void SetRootRect(double width, double height, double offset) override; 206 207 void SetWindowSceneConsumed(bool isConsumed); 208 209 bool IsWindowSceneConsumed(); 210 211 void UpdateSystemSafeArea(const SafeAreaInsets& systemSafeArea) override; 212 void UpdateCutoutSafeArea(const SafeAreaInsets& cutoutSafeArea) override; GetSafeAreaManager()213 const RefPtr<SafeAreaManager>& GetSafeAreaManager() const 214 { 215 return safeAreaManager_; 216 } 217 SafeAreaInsets GetSafeArea() const; 218 219 const RefPtr<FullScreenManager>& GetFullScreenManager(); 220 221 const RefPtr<StageManager>& GetStageManager(); 222 223 const RefPtr<OverlayManager>& GetOverlayManager(); 224 225 const RefPtr<SelectOverlayManager>& GetSelectOverlayManager(); 226 GetSharedOverlayManager()227 const RefPtr<SharedOverlayManager>& GetSharedOverlayManager() 228 { 229 return sharedTransitionManager_; 230 } 231 232 const RefPtr<DragDropManager>& GetDragDropManager(); 233 234 void FlushBuild() override; 235 236 void FlushPipelineImmediately() override; 237 238 void AddBuildFinishCallBack(std::function<void()>&& callback); 239 240 void AddWindowStateChangedCallback(int32_t nodeId); 241 242 void RemoveWindowStateChangedCallback(int32_t nodeId); 243 244 void AddWindowFocusChangedCallback(int32_t nodeId); 245 246 void RemoveWindowFocusChangedCallback(int32_t nodeId); 247 248 void AddWindowSizeChangeCallback(int32_t nodeId); 249 250 void RemoveWindowSizeChangeCallback(int32_t nodeId); 251 IsKeyInPressed(KeyCode tarCode)252 bool IsKeyInPressed(KeyCode tarCode) const 253 { 254 CHECK_NULL_RETURN(eventManager_, false); 255 return eventManager_->IsKeyInPressed(tarCode); 256 } 257 GetIsFocusingByTab()258 bool GetIsFocusingByTab() const 259 { 260 return isFocusingByTab_; 261 } 262 SetIsFocusingByTab(bool isFocusingByTab)263 void SetIsFocusingByTab(bool isFocusingByTab) 264 { 265 isFocusingByTab_ = isFocusingByTab; 266 } 267 GetIsFocusActive()268 bool GetIsFocusActive() const 269 { 270 return isFocusActive_; 271 } 272 273 bool SetIsFocusActive(bool isFocusActive); 274 IsTabJustTriggerOnKeyEvent()275 bool IsTabJustTriggerOnKeyEvent() const 276 { 277 return isTabJustTriggerOnKeyEvent_; 278 } 279 GetOnShow()280 bool GetOnShow() const 281 { 282 return onShow_; 283 } 284 MarkRootFocusNeedUpdate()285 void MarkRootFocusNeedUpdate() 286 { 287 isRootFocusNeedUpdate_ = true; 288 } 289 290 bool ChangeMouseStyle(int32_t nodeId, MouseFormat format); 291 292 bool RequestDefaultFocus(); 293 bool RequestFocus(const std::string& targetNodeId) override; 294 void AddDirtyFocus(const RefPtr<FrameNode>& node); 295 void AddDirtyDefaultFocus(const RefPtr<FrameNode>& node); 296 void RootLostFocus(BlurReason reason = BlurReason::FOCUS_SWITCH) const; 297 298 void SetContainerWindow(bool isShow) override; 299 void SetContainerButtonHide(bool hideSplit, bool hideMaximize, bool hideMinimize) override; 300 void SetCloseButtonStatus(bool isEnabled); 301 302 void AddNodesToNotifyMemoryLevel(int32_t nodeId); 303 void RemoveNodesToNotifyMemoryLevel(int32_t nodeId); 304 void NotifyMemoryLevel(int32_t level) override; 305 void FlushMessages() override; 306 FlushUITasks()307 void FlushUITasks() override 308 { 309 taskScheduler_->FlushTask(); 310 } 311 IsLayouting()312 bool IsLayouting() const override 313 { 314 return taskScheduler_->IsLayouting(); 315 } 316 // end pipeline, exit app 317 void Finish(bool autoFinish) const override; GetRootRect()318 RectF GetRootRect() 319 { 320 CHECK_NULL_RETURN(rootNode_, RectF()); 321 auto geometryNode = rootNode_->GetGeometryNode(); 322 CHECK_NULL_RETURN(geometryNode, RectF()); 323 return geometryNode->GetFrameRect(); 324 } 325 326 void FlushReload() override; 327 RegisterSurfaceChangedCallback(std::function<void (int32_t,int32_t,int32_t,int32_t,WindowSizeChangeReason)> && callback)328 int32_t RegisterSurfaceChangedCallback( 329 std::function<void(int32_t, int32_t, int32_t, int32_t, WindowSizeChangeReason)>&& callback) 330 { 331 if (callback) { 332 surfaceChangedCallbackMap_.emplace(++callbackId_, std::move(callback)); 333 return callbackId_; 334 } 335 return 0; 336 } 337 UnregisterSurfaceChangedCallback(int32_t callbackId)338 void UnregisterSurfaceChangedCallback(int32_t callbackId) 339 { 340 surfaceChangedCallbackMap_.erase(callbackId); 341 } 342 RegisterSurfacePositionChangedCallback(std::function<void (int32_t,int32_t)> && callback)343 int32_t RegisterSurfacePositionChangedCallback(std::function<void(int32_t, int32_t)>&& callback) 344 { 345 if (callback) { 346 surfacePositionChangedCallbackMap_.emplace(++callbackId_, std::move(callback)); 347 return callbackId_; 348 } 349 return 0; 350 } 351 UnregisterSurfacePositionChangedCallback(int32_t callbackId)352 void UnregisterSurfacePositionChangedCallback(int32_t callbackId) 353 { 354 surfacePositionChangedCallbackMap_.erase(callbackId); 355 } 356 SetMouseStyleHoldNode(int32_t id)357 void SetMouseStyleHoldNode(int32_t id) 358 { 359 if (mouseStyleNodeId_ == -1) { 360 mouseStyleNodeId_ = id; 361 } 362 } FreeMouseStyleHoldNode(int32_t id)363 void FreeMouseStyleHoldNode(int32_t id) 364 { 365 if (mouseStyleNodeId_ == id) { 366 mouseStyleNodeId_ = -1; 367 } 368 } 369 MarkNeedFlushMouseEvent()370 void MarkNeedFlushMouseEvent() 371 { 372 isNeedFlushMouseEvent_ = true; 373 } 374 375 // font 376 void AddFontNodeNG(const WeakPtr<UINode>& node); 377 void RemoveFontNodeNG(const WeakPtr<UINode>& node); 378 379 // restore 380 void RestoreNodeInfo(std::unique_ptr<JsonValue> nodeInfo) override; 381 std::unique_ptr<JsonValue> GetStoredNodeInfo() override; 382 void StoreNode(int32_t restoreId, const WeakPtr<FrameNode>& node); 383 bool GetRestoreInfo(int32_t restoreId, std::string& restoreInfo); RemoveStoredNode(int32_t restoreId)384 void RemoveStoredNode(int32_t restoreId) 385 { 386 storeNode_.erase(restoreId); 387 } 388 void SetNeedRenderNode(const RefPtr<FrameNode>& node); 389 390 void SetIgnoreViewSafeArea(bool value) override; 391 void SetIsLayoutFullScreen(bool value) override; 392 393 void AddAnimationClosure(std::function<void()>&& animation); 394 void FlushAnimationClosure(); 395 SetDragCleanTask(std::function<void ()> && task)396 void SetDragCleanTask(std::function<void()>&& task) 397 { 398 dragCleanTask_ = std::move(task); 399 } 400 AddGestureTask(const DelayedTask & task)401 void AddGestureTask(const DelayedTask& task) 402 { 403 delayedTasks_.emplace_back(task); 404 } 405 RemoveGestureTask(const DelayedTask & task)406 void RemoveGestureTask(const DelayedTask& task) 407 { 408 for (auto iter = delayedTasks_.begin(); iter != delayedTasks_.end();) { 409 if (iter->recognizer == task.recognizer) { 410 delayedTasks_.erase(iter++); 411 } else { 412 ++iter; 413 } 414 } 415 } 416 417 protected: 418 void StartWindowSizeChangeAnimate(int32_t width, int32_t height, WindowSizeChangeReason type, 419 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr); 420 421 void FlushVsync(uint64_t nanoTimestamp, uint32_t frameCount) override; 422 void FlushPipelineWithoutAnimation() override; 423 void FlushFocus(); 424 void FlushFrameTrace(); 425 void FlushAnimation(uint64_t nanoTimestamp) override; 426 bool OnDumpInfo(const std::vector<std::string>& params) const override; 427 428 void OnVirtualKeyboardHeightChange( 429 float keyboardHeight, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr) override; 430 431 private: 432 void ExecuteSurfaceChangedCallbacks(int32_t newWidth, int32_t newHeight, WindowSizeChangeReason type); 433 434 void FlushWindowStateChangedCallback(bool isShow); 435 436 void FlushWindowFocusChangedCallback(bool isFocus); 437 438 void FlushWindowSizeChangeCallback(int32_t width, int32_t height, WindowSizeChangeReason type); 439 440 void FlushTouchEvents(); 441 442 void ProcessDelayTasks(); 443 444 void InspectDrew(); 445 446 void FlushBuildFinishCallbacks(); 447 448 void DumpPipelineInfo() const; 449 450 void RegisterRootEvent(); 451 452 FrameInfo* GetCurrentFrameInfo(uint64_t recvTime, uint64_t timeStamp); 453 454 void SyncSafeArea(bool onKeyboard = false); 455 456 // only used for static form. 457 void UpdateFormLinkInfos(); 458 459 template<typename T> 460 struct NodeCompare { operatorNodeCompare461 bool operator()(const T& nodeLeft, const T& nodeRight) const 462 { 463 if (!nodeLeft || !nodeRight) { 464 return false; 465 } 466 if (nodeLeft->GetDepth() < nodeRight->GetDepth()) { 467 return true; 468 } 469 if (nodeLeft->GetDepth() == nodeRight->GetDepth()) { 470 return nodeLeft < nodeRight; 471 } 472 return false; 473 } 474 }; 475 476 std::unique_ptr<UITaskScheduler> taskScheduler_ = std::make_unique<UITaskScheduler>(); 477 478 std::unordered_map<uint32_t, WeakPtr<ScheduleTask>> scheduleTasks_; 479 std::set<RefPtr<UINode>, NodeCompare<RefPtr<UINode>>> dirtyNodes_; 480 std::list<std::function<void()>> buildFinishCallbacks_; 481 482 // window on show or on hide 483 std::set<int32_t> onWindowStateChangedCallbacks_; 484 // window on focused or on unfocused 485 std::list<int32_t> onWindowFocusChangedCallbacks_; 486 // window on drag 487 std::list<int32_t> onWindowSizeChangeCallbacks_; 488 489 std::list<int32_t> nodesToNotifyMemoryLevel_; 490 491 std::list<TouchEvent> touchEvents_; 492 493 RefPtr<FrameNode> rootNode_; 494 495 std::set<RefPtr<FrameNode>> needRenderNode_; 496 497 int32_t callbackId_ = 0; 498 SurfaceChangedCallbackMap surfaceChangedCallbackMap_; 499 SurfacePositionChangedCallbackMap surfacePositionChangedCallbackMap_; 500 std::unordered_map<int32_t, WindowSceneTouchEventCallback> windowSceneTouchEventCallback_; 501 502 std::unordered_set<int32_t> onAreaChangeNodeIds_; 503 std::unordered_set<int32_t> onVisibleAreaChangeNodeIds_; 504 505 RefPtr<StageManager> stageManager_; 506 RefPtr<OverlayManager> overlayManager_; 507 RefPtr<FullScreenManager> fullScreenManager_; 508 RefPtr<SelectOverlayManager> selectOverlayManager_; 509 RefPtr<DragDropManager> dragDropManager_; 510 RefPtr<SharedOverlayManager> sharedTransitionManager_; 511 RefPtr<SafeAreaManager> safeAreaManager_ = MakeRefPtr<SafeAreaManager>(); 512 WeakPtr<FrameNode> dirtyFocusNode_; 513 WeakPtr<FrameNode> dirtyFocusScope_; 514 WeakPtr<FrameNode> dirtyDefaultFocusNode_; 515 uint32_t nextScheduleTaskId_ = 0; 516 int32_t mouseStyleNodeId_ = -1; 517 bool hasIdleTasks_ = false; 518 bool isFocusingByTab_ = false; 519 bool isFocusActive_ = false; 520 bool isTabJustTriggerOnKeyEvent_ = false; 521 bool isRootFocusNeedUpdate_ = false; 522 bool onShow_ = false; 523 bool onFocus_ = true; 524 bool isNeedFlushMouseEvent_ = false; 525 bool canUseLongPredictTask_ = false; 526 bool isWindowSceneConsumed_ = false; 527 std::unique_ptr<MouseEvent> lastMouseEvent_; 528 529 std::unordered_map<int32_t, WeakPtr<FrameNode>> storeNode_; 530 std::unordered_map<int32_t, std::string> restoreNodeInfo_; 531 532 std::list<FrameInfo> dumpFrameInfos_; 533 std::list<std::function<void()>> animationClosuresList_; 534 std::function<void()> dragCleanTask_; 535 std::list<DelayedTask> delayedTasks_; 536 537 ACE_DISALLOW_COPY_AND_MOVE(PipelineContext); 538 }; 539 } // namespace OHOS::Ace::NG 540 541 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_CONTEXT_H 542