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