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 #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 <utility> 23 24 #include "base/memory/referenced.h" 25 #include "core/common/frontend.h" 26 #include "core/components_ng/base/frame_node.h" 27 #include "core/components_ng/manager/drag_drop/drag_drop_manager.h" 28 #include "core/components_ng/manager/full_screen/full_screen_manager.h" 29 #include "core/components_ng/manager/select_overlay/select_overlay_manager.h" 30 #include "core/components_ng/manager/shared_overlay/shared_overlay_manager.h" 31 #include "core/components_ng/pattern/custom/custom_node.h" 32 #include "core/components_ng/pattern/overlay/overlay_manager.h" 33 #include "core/components_ng/pattern/stage/stage_manager.h" 34 #include "core/event/touch_event.h" 35 #include "core/pipeline/pipeline_base.h" 36 37 namespace OHOS::Ace::NG { 38 39 class ACE_EXPORT PipelineContext final : public PipelineBase { 40 DECLARE_ACE_TYPE(NG::PipelineContext, PipelineBase); 41 42 public: 43 using SurfaceChangedCallbackMap = 44 std::unordered_map<int32_t, std::function<void(int32_t, int32_t, int32_t, int32_t)>>; 45 using SurfacePositionChangedCallbackMap = std::unordered_map<int32_t, std::function<void(int32_t, int32_t)>>; 46 using PredictTask = std::function<void(int64_t)>; 47 PipelineContext(std::unique_ptr<Window> window, RefPtr<TaskExecutor> taskExecutor, 48 RefPtr<AssetManager> assetManager, RefPtr<PlatformResRegister> platformResRegister, 49 const RefPtr<Frontend>& frontend, int32_t instanceId); 50 PipelineContext(std::unique_ptr<Window> window, RefPtr<TaskExecutor> taskExecutor, 51 RefPtr<AssetManager> assetManager, const RefPtr<Frontend>& frontend, int32_t instanceId); 52 PipelineContext() = default; 53 54 ~PipelineContext() override = default; 55 56 static RefPtr<PipelineContext> GetCurrentContext(); 57 58 static float GetCurrentRootWidth(); 59 60 static float GetCurrentRootHeight(); 61 62 void SetupRootElement() override; 63 64 void SetupSubRootElement(); 65 GetRootElement()66 const RefPtr<FrameNode>& GetRootElement() const 67 { 68 return rootNode_; 69 } 70 AddKeyFrame(float fraction,const RefPtr<Curve> & curve,const std::function<void ()> & propertyCallback)71 void AddKeyFrame(float fraction, const RefPtr<Curve>& curve, const std::function<void()>& propertyCallback) override 72 {} 73 AddKeyFrame(float fraction,const std::function<void ()> & propertyCallback)74 void AddKeyFrame(float fraction, const std::function<void()>& propertyCallback) override {} 75 76 // add schedule task and return the unique mark id. 77 uint32_t AddScheduleTask(const RefPtr<ScheduleTask>& task) override; 78 79 // remove schedule task by id. RemoveScheduleTask(uint32_t id)80 void RemoveScheduleTask(uint32_t id) override {} 81 82 // Called by view when touch event received. 83 void OnTouchEvent(const TouchEvent& point, bool isSubPipe = false) override; 84 85 // Called by container when key event received. 86 // if return false, then this event needs platform to handle it. 87 bool OnKeyEvent(const KeyEvent& event) override; 88 89 // Called by view when mouse event received. 90 void OnMouseEvent(const MouseEvent& event) override; 91 92 // Called by view when axis event received. 93 void OnAxisEvent(const AxisEvent& event) override; 94 95 // Called by container when rotation event received. 96 // if return false, then this event needs platform to handle it. OnRotationEvent(const RotationEvent & event)97 bool OnRotationEvent(const RotationEvent& event) const override 98 { 99 return false; 100 } 101 102 void OnDragEvent(int32_t x, int32_t y, DragEventAction action) override; 103 104 // Called by view when idle event. 105 void OnIdle(int64_t deadline) override; 106 SetBuildAfterCallback(const std::function<void ()> & callback)107 void SetBuildAfterCallback(const std::function<void()>& callback) override 108 { 109 buildFinishCallbacks_.emplace_back(callback); 110 } 111 SaveExplicitAnimationOption(const AnimationOption & option)112 void SaveExplicitAnimationOption(const AnimationOption& option) override {} 113 CreateExplicitAnimator(const std::function<void ()> & onFinishEvent)114 void CreateExplicitAnimator(const std::function<void()>& onFinishEvent) override {} 115 ClearExplicitAnimationOption()116 void ClearExplicitAnimationOption() override {} 117 GetExplicitAnimationOption()118 AnimationOption GetExplicitAnimationOption() const override 119 { 120 return {}; 121 } 122 123 void AddOnAreaChangeNode(int32_t nodeId); 124 125 void RemoveOnAreaChangeNode(int32_t nodeId); 126 127 void HandleOnAreaChangeEvent(); 128 129 void AddVisibleAreaChangeNode(const RefPtr<FrameNode>& node, double ratio, const VisibleRatioCallback& callback); 130 void RemoveVisibleAreaChangeNode(int32_t nodeId); 131 132 void HandleVisibleAreaChangeEvent(); 133 134 void Destroy() override; 135 136 void OnShow() override; 137 138 void OnHide() override; 139 140 void WindowFocus(bool isFocus) override; 141 142 void ShowContainerTitle(bool isShow) override; 143 144 void SetAppBgColor(const Color& color) override; 145 146 void SetAppTitle(const std::string& title) override; 147 148 void SetAppIcon(const RefPtr<PixelMap>& icon) override; 149 150 void OnSurfaceChanged( 151 int32_t width, int32_t height, WindowSizeChangeReason type = WindowSizeChangeReason::UNDEFINED) override; 152 153 void OnSurfacePositionChanged(int32_t posX, int32_t posY) override; 154 155 void OnSurfaceDensityChanged(double density) override; 156 OnSystemBarHeightChanged(double statusBar,double navigationBar)157 void OnSystemBarHeightChanged(double statusBar, double navigationBar) override {} 158 OnSurfaceDestroyed()159 void OnSurfaceDestroyed() override {} 160 NotifyOnPreDraw()161 void NotifyOnPreDraw() override {} 162 CallRouterBackToPopPage()163 bool CallRouterBackToPopPage() override 164 { 165 return OnBackPressed(); 166 } 167 168 bool OnBackPressed(); 169 170 RefPtr<FrameNode> GetNavDestinationBackButtonNode(); 171 172 void AddDirtyCustomNode(const RefPtr<UINode>& dirtyNode); 173 174 void AddDirtyLayoutNode(const RefPtr<FrameNode>& dirty); 175 176 void AddDirtyRenderNode(const RefPtr<FrameNode>& dirty); 177 178 void AddPredictTask(PredictTask&& task); 179 180 void AddAfterLayoutTask(std::function<void()>&& task); 181 182 void FlushDirtyNodeUpdate(); 183 184 void SetRootRect(double width, double height, double offset) override; 185 186 const RefPtr<FullScreenManager>& GetFullScreenManager(); 187 188 const RefPtr<StageManager>& GetStageManager(); 189 190 const RefPtr<OverlayManager>& GetOverlayManager(); 191 192 const RefPtr<SelectOverlayManager>& GetSelectOverlayManager(); 193 GetSharedOverlayManager()194 const RefPtr<SharedOverlayManager>& GetSharedOverlayManager() 195 { 196 return sharedTransitionManager_; 197 } 198 199 const RefPtr<DragDropManager>& GetDragDropManager(); 200 201 void FlushBuild() override; 202 203 void FlushPipelineImmediately() override; 204 205 void AddBuildFinishCallBack(std::function<void()>&& callback); 206 207 void AddWindowStateChangedCallback(int32_t nodeId); 208 209 void RemoveWindowStateChangedCallback(int32_t nodeId); 210 211 void AddWindowFocusChangedCallback(int32_t nodeId); 212 213 void RemoveWindowFocusChangedCallback(int32_t nodeId); 214 IsKeyInPressed(KeyCode tarCode)215 bool IsKeyInPressed(KeyCode tarCode) const 216 { 217 CHECK_NULL_RETURN(eventManager_, false); 218 return eventManager_->IsKeyInPressed(tarCode); 219 } 220 GetIsFocusingByTab()221 bool GetIsFocusingByTab() const 222 { 223 return isFocusingByTab_; 224 } 225 SetIsFocusingByTab(bool isFocusingByTab)226 void SetIsFocusingByTab(bool isFocusingByTab) 227 { 228 isFocusingByTab_ = isFocusingByTab; 229 } 230 GetIsNeedShowFocus()231 bool GetIsNeedShowFocus() const 232 { 233 return isNeedShowFocus_; 234 } 235 SetIsNeedShowFocus(bool isNeedShowFocus)236 void SetIsNeedShowFocus(bool isNeedShowFocus) 237 { 238 isNeedShowFocus_ = isNeedShowFocus; 239 } 240 GetOnShow()241 bool GetOnShow() const 242 { 243 return onShow_; 244 } 245 246 bool ChangeMouseStyle(int32_t nodeId, MouseFormat format); 247 248 bool RequestDefaultFocus(); 249 bool RequestFocus(const std::string& targetNodeId) override; 250 void AddDirtyFocus(const RefPtr<FrameNode>& node); 251 void RootLostFocus(BlurReason reason = BlurReason::FOCUS_SWITCH) const; 252 253 void AddNodesToNotifyMemoryLevel(int32_t nodeId); 254 void RemoveNodesToNotifyMemoryLevel(int32_t nodeId); 255 void NotifyMemoryLevel(int32_t level) override; 256 void FlushMessages() override; 257 void SetContainerWindow(bool isShow) override; 258 FlushUITasks()259 void FlushUITasks() override 260 { 261 taskScheduler_.FlushTask(); 262 } 263 // end pipeline, exit app 264 void Finish(bool autoFinish) const override; GetRootRect()265 RectF GetRootRect() 266 { 267 return rootNode_->GetGeometryNode()->GetFrameRect(); 268 } 269 270 void FlushReload() override; 271 RegisterSurfaceChangedCallback(std::function<void (int32_t,int32_t,int32_t,int32_t)> && callback)272 int32_t RegisterSurfaceChangedCallback(std::function<void(int32_t, int32_t, int32_t, int32_t)>&& callback) 273 { 274 if (callback) { 275 surfaceChangedCallbackMap_.emplace(++callbackId_, std::move(callback)); 276 return callbackId_; 277 } 278 return 0; 279 } 280 UnregisterSurfaceChangedCallback(int32_t callbackId)281 void UnregisterSurfaceChangedCallback(int32_t callbackId) 282 { 283 surfaceChangedCallbackMap_.erase(callbackId); 284 } 285 RegisterSurfacePositionChangedCallback(std::function<void (int32_t,int32_t)> && callback)286 int32_t RegisterSurfacePositionChangedCallback(std::function<void(int32_t, int32_t)>&& callback) 287 { 288 if (callback) { 289 surfacePositionChangedCallbackMap_.emplace(++callbackId_, std::move(callback)); 290 return callbackId_; 291 } 292 return 0; 293 } 294 UnregisterSurfacePositionChangedCallback(int32_t callbackId)295 void UnregisterSurfacePositionChangedCallback(int32_t callbackId) 296 { 297 surfacePositionChangedCallbackMap_.erase(callbackId); 298 } 299 SetMouseStyleHoldNode(int32_t id)300 void SetMouseStyleHoldNode(int32_t id) 301 { 302 if (mouseStyleNodeId_ == -1) { 303 mouseStyleNodeId_ = id; 304 } 305 } FreeMouseStyleHoldNode(int32_t id)306 void FreeMouseStyleHoldNode(int32_t id) 307 { 308 if (mouseStyleNodeId_ == id) { 309 mouseStyleNodeId_ = -1; 310 } 311 } 312 313 protected: 314 void StartWindowSizeChangeAnimate(int32_t width, int32_t height, WindowSizeChangeReason type); 315 316 void FlushVsync(uint64_t nanoTimestamp, uint32_t frameCount) override; 317 void FlushPipelineWithoutAnimation() override; 318 void FlushFocus(); 319 void FlushAnimation(uint64_t nanoTimestamp) override; 320 bool OnDumpInfo(const std::vector<std::string>& params) const override; 321 322 void OnVirtualKeyboardHeightChange(float keyboardHeight) override; 323 324 private: 325 void ExecuteSurfaceChangedCallbacks(int32_t newWidth, int32_t newHeight); 326 327 void FlushWindowStateChangedCallback(bool isShow); 328 329 void FlushWindowFocusChangedCallback(bool isFocus); 330 331 void FlushTouchEvents(); 332 333 void FlushBuildFinishCallbacks(); 334 335 void RegisterRootEvent(); 336 337 template<typename T> 338 struct NodeCompare { operatorNodeCompare339 bool operator()(const T& nodeLeft, const T& nodeRight) const 340 { 341 if (!nodeLeft || !nodeRight) { 342 return false; 343 } 344 if (nodeLeft->GetDepth() < nodeRight->GetDepth()) { 345 return true; 346 } 347 if (nodeLeft->GetDepth() == nodeRight->GetDepth()) { 348 return nodeLeft < nodeRight; 349 } 350 return false; 351 } 352 }; 353 354 UITaskScheduler taskScheduler_; 355 356 std::unordered_map<uint32_t, WeakPtr<ScheduleTask>> scheduleTasks_; 357 std::set<RefPtr<UINode>, NodeCompare<RefPtr<UINode>>> dirtyNodes_; 358 std::list<std::function<void()>> buildFinishCallbacks_; 359 360 // window on show or on hide 361 std::set<int32_t> onWindowStateChangedCallbacks_; 362 // window on focused or on unfocused 363 std::list<int32_t> onWindowFocusChangedCallbacks_; 364 365 std::list<int32_t> nodesToNotifyMemoryLevel_; 366 367 std::list<TouchEvent> touchEvents_; 368 369 RefPtr<FrameNode> rootNode_; 370 371 int32_t callbackId_ = 0; 372 SurfaceChangedCallbackMap surfaceChangedCallbackMap_; 373 SurfacePositionChangedCallbackMap surfacePositionChangedCallbackMap_; 374 375 std::unordered_set<int32_t> onAreaChangeNodeIds_; 376 std::unordered_map<int32_t, std::list<VisibleCallbackInfo>> visibleAreaChangeNodes_; 377 378 RefPtr<StageManager> stageManager_; 379 RefPtr<OverlayManager> overlayManager_; 380 RefPtr<FullScreenManager> fullScreenManager_; 381 RefPtr<SelectOverlayManager> selectOverlayManager_; 382 RefPtr<DragDropManager> dragDropManager_; 383 RefPtr<SharedOverlayManager> sharedTransitionManager_; 384 WeakPtr<FrameNode> dirtyFocusNode_; 385 WeakPtr<FrameNode> dirtyFocusScope_; 386 uint32_t nextScheduleTaskId_ = 0; 387 int32_t rotationAnimationCount_ = 0; 388 int32_t mouseStyleNodeId_ = -1; 389 bool hasIdleTasks_ = false; 390 bool isFocusingByTab_ = false; 391 bool isNeedShowFocus_ = false; 392 bool onShow_ = false; 393 bool onFocus_ = true; 394 395 ACE_DISALLOW_COPY_AND_MOVE(PipelineContext); 396 }; 397 398 } // namespace OHOS::Ace::NG 399 400 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_CONTEXT_H 401