From 0cdcf1a6de541ba8e89c5a8782736ac21257abcd Mon Sep 17 00:00:00 2001 From: sunbees Date: Mon, 30 Jun 2025 21:55:39 +0800 Subject: [PATCH 1/3] xcomponent delete XComponentPatternV2 Signed-off-by: sunbees --- .../xcomponent/xcomponent_model_ng.cpp | 38 ++++++++++------ .../pattern/xcomponent/xcomponent_pattern.cpp | 43 ++++++++++++++++--- .../pattern/xcomponent/xcomponent_pattern.h | 14 +++--- .../xcomponent/xcomponent_pattern_v2.cpp | 25 ++++++++--- .../xcomponent/xcomponent_pattern_v2.h | 6 ++- .../xcomponent_property_test_ng.cpp | 15 ++++--- 6 files changed, 104 insertions(+), 37 deletions(-) diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp index ef0c28c5773..b840b28da87 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp @@ -14,6 +14,7 @@ */ #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" +#include "ui/base/utils/utils.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" @@ -28,7 +29,9 @@ void XComponentModelNG::Create(XComponentType type) auto nodeId = stack->ClaimNodeId(); ACE_LAYOUT_SCOPED_TRACE("Create[%sNative][self:%d]", V2::XCOMPONENT_ETS_TAG, nodeId); auto frameNode = FrameNode::GetOrCreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, - [type]() { return AceType::MakeRefPtr(type, XComponentNodeType::DECLARATIVE_NODE); }); + []() { return AceType::MakeRefPtr(XComponentNodeType::DECLARATIVE_NODE); }); + auto pattern = frameNode->GetPattern(); + pattern->InitParams(type); stack->Push(frameNode); ACE_UPDATE_LAYOUT_PROPERTY(XComponentLayoutProperty, XComponentType, type); } @@ -41,9 +44,12 @@ void XComponentModelNG::Create(const std::optional& id, XComponentT auto nodeId = stack->ClaimNodeId(); ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::XCOMPONENT_ETS_TAG, nodeId); auto frameNode = FrameNode::GetOrCreateFrameNode( - V2::XCOMPONENT_ETS_TAG, nodeId, [id, type, libraryname, xcomponentController]() { - return AceType::MakeRefPtr(id, type, libraryname, xcomponentController); + V2::XCOMPONENT_ETS_TAG, nodeId, []() { + return AceType::MakeRefPtr(false); }); + auto pattern = frameNode->GetPattern(); + CHECK_NULL_VOID(pattern); + pattern->InitParams(id, type, libraryname, xcomponentController); stack->Push(frameNode); ACE_UPDATE_LAYOUT_PROPERTY(XComponentLayoutProperty, XComponentType, type); } @@ -56,12 +62,13 @@ RefPtr XComponentModelNG::Create(int32_t nodeId, float width, float hei auto calcWidth = CalcLength(width, DimensionUnit::VP); auto calcHeight = CalcLength(height, DimensionUnit::VP); auto frameNode = FrameNode::GetOrCreateFrameNode( - V2::XCOMPONENT_ETS_TAG, nodeId, [id, type, libraryname, xcomponentController, calcWidth, calcHeight]() { - return AceType::MakeRefPtr(id, type, libraryname, xcomponentController, - calcWidth.GetDimension().ConvertToPx(), calcHeight.GetDimension().ConvertToPx()); - }); + V2::XCOMPONENT_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr(false); }); CHECK_NULL_RETURN(frameNode, nullptr); + auto pattern = frameNode->GetPattern(); + CHECK_NULL_RETURN(pattern, nullptr); + pattern->InitParams(id, type, libraryname, xcomponentController, calcWidth.GetDimension().ConvertToPx(), + calcHeight.GetDimension().ConvertToPx()); auto layoutProperty = frameNode->GetLayoutProperty(); CHECK_NULL_RETURN(layoutProperty, frameNode); layoutProperty->UpdateXComponentType(type); @@ -327,8 +334,9 @@ XComponentType XComponentModelNG::GetType(FrameNode* frameNode) RefPtr XComponentModelNG::CreateFrameNode(int32_t nodeId, const std::string& id, XComponentType type, const std::optional& libraryname) { - auto pattern = AceType::MakeRefPtr(type, XComponentNodeType::CNODE); + auto pattern = AceType::MakeRefPtr(XComponentNodeType::CNODE); auto frameNode = FrameNode::CreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, pattern); + pattern->InitParams(type); auto layoutProperty = frameNode->GetLayoutProperty(); CHECK_NULL_RETURN(layoutProperty, frameNode); layoutProperty->UpdateXComponentType(type); @@ -345,11 +353,17 @@ RefPtr XComponentModelNG::CreateTypeNode(int32_t nodeId, ArkUI_XCompo RefPtr frameNode; if (id.empty() && controller == nullptr && (type == XComponentType::SURFACE || type == XComponentType::TEXTURE)) { - frameNode = FrameNode::CreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, - AceType::MakeRefPtr(type, XComponentNodeType::TYPE_NODE)); + frameNode = FrameNode::CreateFrameNode( + V2::XCOMPONENT_ETS_TAG, nodeId, AceType::MakeRefPtr(XComponentNodeType::TYPE_NODE)); + auto pattern = frameNode->GetPattern(); + CHECK_NULL_RETURN(pattern, nullptr); + pattern->InitParams(type); } else { - frameNode = FrameNode::CreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, - AceType::MakeRefPtr(id, type, libraryName, controller, 0.0, 0.0, true)); + frameNode = + FrameNode::CreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, AceType::MakeRefPtr(true)); + auto pattern = frameNode->GetPattern(); + CHECK_NULL_RETURN(pattern, nullptr); + pattern->InitParams(id, type, libraryName, controller); } auto layoutProperty = frameNode->GetLayoutProperty(); if (layoutProperty) { diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp index ad286ea83da..9bfe198c861 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp @@ -20,6 +20,7 @@ #include "interfaces/native/event/ui_input_event_impl.h" #include "interfaces/native/ui_input_event.h" +#include "ui/base/utils/utils.h" #include "base/geometry/ng/point_t.h" #include "base/geometry/ng/size_t.h" @@ -71,20 +72,50 @@ namespace { const std::string BUFFER_USAGE_XCOMPONENT = "xcomponent"; } // namespace -XComponentPattern::XComponentPattern(const std::optional& id, XComponentType type, +XComponentPattern::XComponentPattern(bool isTypeNode) : isTypedNode_(isTypeNode) +{ + RegisterSurfaceCallbackModeEvent(); +} + +void XComponentPattern::InitParams(const std::optional& id, XComponentType type, const std::optional& libraryname, - const std::shared_ptr& xcomponentController, float initWidth, float initHeight, - bool isTypedNode) - : id_(id), type_(type), xcomponentController_(xcomponentController), initSize_(initWidth, initHeight), - isTypedNode_(isTypedNode) + const std::shared_ptr& xcomponentController, float initWidth, float initHeight) { + if (type != XComponentType::UNKNOWN) { + return; + } + id_=id; + type_=type; + if(type == XComponentType::NODE){ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto focusHub = host->GetOrCreateFocusHub(); + focusHub->SetFocusType(FocusType::SCOPE); + focusHub->SetFocusable(true); + } + xcomponentController_ = xcomponentController; + initSize_ = SizeF { initWidth, initHeight }; SetLibraryName(libraryname); if (!isTypedNode_) { InitNativeXComponent(); } - RegisterSurfaceCallbackModeEvent(); + Initialize(); } +// XComponentPattern::XComponentPattern(const std::optional& id, XComponentType type, +// const std::optional& libraryname, +// const std::shared_ptr& xcomponentController, float initWidth, float initHeight, +// bool isTypedNode) +// : id_(id), type_(type), xcomponentController_(xcomponentController), initSize_(initWidth, initHeight), +// isTypedNode_(isTypedNode) +// { +// SetLibraryName(libraryname); +// if (!isTypedNode_) { +// InitNativeXComponent(); +// } +// RegisterSurfaceCallbackModeEvent(); +// } + std::string XComponentPattern::XComponentTypeToString(XComponentType type) { switch (type) { diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h index 328318b006a..6cd8be658d4 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h @@ -54,12 +54,14 @@ class XComponentPattern : public Pattern { DECLARE_ACE_TYPE(XComponentPattern, Pattern); public: - XComponentPattern() = default; - XComponentPattern(const std::optional& id, XComponentType type, + XComponentPattern() = delete; + explicit XComponentPattern(bool isTypeNode); + ~XComponentPattern() override = default; + + void InitParams(const std::optional& id, XComponentType type, const std::optional& libraryname, const std::shared_ptr& xcomponentController, float initWidth = 0.0f, - float initHeight = 0.0f, bool isTypedNode = false); - ~XComponentPattern() override = default; + float initHeight = 0.0f); bool IsEnableMatchParent() override { @@ -380,9 +382,10 @@ protected: void AdjustNativeWindowSize(float width, float height); bool IsSupportImageAnalyzerFeature(); void UpdateAnalyzerUIConfig(const RefPtr& geometryNode); + void Initialize(); std::optional id_; - XComponentType type_; + XComponentType type_ = XComponentType::UNKNOWN; bool hasGotSurfaceHolder_ = false; bool hasGotNativeXComponent_ = false; bool isNativeXComponentDisabled_ = false; @@ -433,7 +436,6 @@ private: void NativeSurfaceShow(); void NativeSurfaceHide(); - void Initialize(); void InitController(); void InitSurface(); void InitNativeNodeCallbacks(); diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp index a453215ba05..003dc61880a 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp @@ -18,10 +18,12 @@ #include "base/log/dump_log.h" #include "base/utils/utils.h" #include "core/accessibility/accessibility_session_adapter.h" +#include "core/components/common/layout/constants.h" #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_child_tree_callback.h" #include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_session_adapter.h" #include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h" #include "core/components_ng/pattern/xcomponent/xcomponent_inner_surface_controller.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" #ifdef ENABLE_ROSEN_BACKEND #include "transaction/rs_transaction.h" #include "transaction/rs_transaction_handler.h" @@ -43,16 +45,29 @@ inline std::string BoolToString(bool value) } } // namespace -XComponentPatternV2::XComponentPatternV2(XComponentType type, XComponentNodeType nodeType) - : XComponentPattern((nodeType == XComponentNodeType::CNODE) ? std::make_optional("") : std::nullopt, - type, (nodeType == XComponentNodeType::CNODE) ? std::make_optional("") : std::nullopt, nullptr) +XComponentPatternV2::XComponentPatternV2(XComponentNodeType nodeType) : XComponentPattern(false), nodeType_(nodeType) { - nodeType_ = nodeType; if (nodeType == XComponentNodeType::CNODE) { isCNode_ = true; } } +void XComponentPatternV2::InitParams(XComponentType type) +{ + if(type!=XComponentType::UNKNOWN) { + return; + } + type_=type; + if(isCNode_) { + id_ = ""; + SetLibraryName(""); + InitNativeXComponent(); + XComponentPattern::Initialize(); + } + InitSurface(); + UpdateTransformHint(); +} + void XComponentPatternV2::SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder) { surfaceHolder_ = surfaceHolder; @@ -80,11 +95,9 @@ void XComponentPatternV2::OnAttachToFrameNode() renderContext->SetClipToFrame(true); renderContext->SetClipToBounds(true); - InitSurface(); if (FrameReport::GetInstance().GetEnable()) { FrameReport::GetInstance().EnableSelfRender(); } - UpdateTransformHint(); } void XComponentPatternV2::OnAttachToMainTree() diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h index 70e47215667..3fc28715a30 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h @@ -15,6 +15,7 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_V2_H #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_V2_H +#include "core/components/common/layout/constants.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" #include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/accessibility/native_interface_accessibility_provider.h" @@ -31,8 +32,9 @@ namespace OHOS::Ace::NG { class XComponentPatternV2 : public XComponentPattern { DECLARE_ACE_TYPE(XComponentPatternV2, XComponentPattern); public: - XComponentPatternV2() = default; - XComponentPatternV2(XComponentType type, XComponentNodeType nodeType); + XComponentPatternV2() = delete; + explicit XComponentPatternV2(XComponentNodeType nodeType); + void InitParams(XComponentType type); ~XComponentPatternV2() override = default; void SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder); OH_ArkUI_SurfaceHolder* GetSurfaceHolder(); diff --git a/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp b/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp index 2537afab650..bb640d60150 100644 --- a/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp +++ b/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp @@ -20,6 +20,7 @@ #include "gtest/gtest.h" #include "base/geometry/ng/size_t.h" +#include "core/components/common/layout/constants.h" #define private public #define protected public @@ -507,10 +508,11 @@ HWTEST_F(XComponentPropertyTestNg, XComponentControllerSetExtControllerTest013, * case: pattern->GetType() != XComponentType::SURFACE * @tc.expected: result = XCOMPONENT_CONTROLLER_TYPE_ERROR */ - XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_SURFACE_TYPE_VALUE); + XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XComponentType::UNKNOWN); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); pattern->OnAttachToFrameNode(); + pattern->InitParams(XCOMPONENT_ID, XCOMPONENT_NODE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController); XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_TEXTURE_TYPE_VALUE); result = xComponentController->SetExtController(xComponentController); EXPECT_EQ(result, XCOMPONENT_CONTROLLER_TYPE_ERROR); @@ -520,8 +522,9 @@ HWTEST_F(XComponentPropertyTestNg, XComponentControllerSetExtControllerTest013, * case: !extPattern * @tc.expected: result = XCOMPONENT_CONTROLLER_BAD_PARAMETER */ - XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_SURFACE_TYPE_VALUE); + XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XComponentType::UNKNOWN); pattern->OnAttachToFrameNode(); + pattern->InitParams(XCOMPONENT_ID, XCOMPONENT_NODE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController); auto xComponentController2 = std::make_shared(); result = xComponentController->SetExtController(xComponentController2); EXPECT_EQ(result, XCOMPONENT_CONTROLLER_BAD_PARAMETER); @@ -575,11 +578,12 @@ HWTEST_F(XComponentPropertyTestNg, XComponentControllerResetExtControllerTest014 * case: pattern->GetType() != XComponentType::SURFACE * @tc.expected: result = XCOMPONENT_CONTROLLER_TYPE_ERROR */ - XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_SURFACE_TYPE_VALUE); + XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XComponentType::UNKNOWN); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); pattern->OnAttachToFrameNode(); - XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_TEXTURE_TYPE_VALUE); + pattern->InitParams(XCOMPONENT_ID, XCOMPONENT_SURFACE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController); + XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XComponentType::UNKNOWN); result = xComponentController->ResetExtController(xComponentController); EXPECT_EQ(result, XCOMPONENT_CONTROLLER_TYPE_ERROR); @@ -588,8 +592,9 @@ HWTEST_F(XComponentPropertyTestNg, XComponentControllerResetExtControllerTest014 * case: !extPattern * @tc.expected: result = XCOMPONENT_CONTROLLER_BAD_PARAMETER */ - XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_SURFACE_TYPE_VALUE); + XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XComponentType::UNKNOWN); pattern->OnAttachToFrameNode(); + pattern->InitParams(XCOMPONENT_ID, XCOMPONENT_SURFACE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController); auto xComponentController2 = std::make_shared(); result = xComponentController->ResetExtController(xComponentController2); EXPECT_EQ(result, XCOMPONENT_CONTROLLER_BAD_PARAMETER); -- Gitee From 7ff8f5992c955e8ba1d7fe0e5f7b25ffd3acfe73 Mon Sep 17 00:00:00 2001 From: sunbees Date: Wed, 2 Jul 2025 15:37:14 +0800 Subject: [PATCH 2/3] delete v2_2 Signed-off-by: sunbees --- .../components_ng/pattern/xcomponent/BUILD.gn | 2 +- .../xcomponent/xcomponent_model_ng.cpp | 51 +- .../pattern/xcomponent/xcomponent_pattern.cpp | 768 +++++++++++++++--- .../pattern/xcomponent/xcomponent_pattern.h | 118 ++- .../xcomponent/xcomponent_pattern_v2.cpp | 6 +- .../xcomponent/xcomponent_surface_holder.h | 1 + .../native/node/node_xcomponent_modifier.cpp | 14 +- 7 files changed, 809 insertions(+), 151 deletions(-) diff --git a/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn b/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn index 1bdf2029639..cb9180219f9 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn +++ b/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn @@ -25,7 +25,7 @@ build_component_ng("xcomponent_pattern_ng") { "xcomponent_model_ng.cpp", "xcomponent_paint_method.cpp", "xcomponent_pattern.cpp", - "xcomponent_pattern_v2.cpp", + # "xcomponent_pattern_v2.cpp", "xcomponent_utils.cpp", "xcomponent_inner_surface_controller.cpp", ] diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp index b840b28da87..166d042b4a8 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp @@ -14,10 +14,12 @@ */ #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" +#include #include "ui/base/utils/utils.h" +#include "core/components/common/layout/constants.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" +// #include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" #include "base/display_manager/display_manager.h" #include "base/error/error_code.h" @@ -29,9 +31,9 @@ void XComponentModelNG::Create(XComponentType type) auto nodeId = stack->ClaimNodeId(); ACE_LAYOUT_SCOPED_TRACE("Create[%sNative][self:%d]", V2::XCOMPONENT_ETS_TAG, nodeId); auto frameNode = FrameNode::GetOrCreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, - []() { return AceType::MakeRefPtr(XComponentNodeType::DECLARATIVE_NODE); }); - auto pattern = frameNode->GetPattern(); - pattern->InitParams(type); + []() { return AceType::MakeRefPtr(XComponentNodeType::DECLARATIVE_NODE); }); + auto pattern = frameNode->GetPattern(); + pattern->InitParams(std::nullopt, type, std::nullopt, nullptr); stack->Push(frameNode); ACE_UPDATE_LAYOUT_PROPERTY(XComponentLayoutProperty, XComponentType, type); } @@ -45,7 +47,7 @@ void XComponentModelNG::Create(const std::optional& id, XComponentT ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::XCOMPONENT_ETS_TAG, nodeId); auto frameNode = FrameNode::GetOrCreateFrameNode( V2::XCOMPONENT_ETS_TAG, nodeId, []() { - return AceType::MakeRefPtr(false); + return AceType::MakeRefPtr(XComponentNodeType::DECLARATIVE_NODE); }); auto pattern = frameNode->GetPattern(); CHECK_NULL_VOID(pattern); @@ -54,6 +56,7 @@ void XComponentModelNG::Create(const std::optional& id, XComponentT ACE_UPDATE_LAYOUT_PROPERTY(XComponentLayoutProperty, XComponentType, type); } +// for XComponentNode which is deprecated RefPtr XComponentModelNG::Create(int32_t nodeId, float width, float height, const std::string& id, XComponentType type, const std::string& libraryname, const std::shared_ptr& xcomponentController) @@ -61,8 +64,8 @@ RefPtr XComponentModelNG::Create(int32_t nodeId, float width, float hei ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::XCOMPONENT_ETS_TAG, nodeId); auto calcWidth = CalcLength(width, DimensionUnit::VP); auto calcHeight = CalcLength(height, DimensionUnit::VP); - auto frameNode = FrameNode::GetOrCreateFrameNode( - V2::XCOMPONENT_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr(false); }); + auto frameNode = FrameNode::GetOrCreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, + []() { return AceType::MakeRefPtr(XComponentNodeType::DECLARATIVE_NODE); }); CHECK_NULL_RETURN(frameNode, nullptr); auto pattern = frameNode->GetPattern(); @@ -331,12 +334,12 @@ XComponentType XComponentModelNG::GetType(FrameNode* frameNode) } // For CAPI XComponent -RefPtr XComponentModelNG::CreateFrameNode(int32_t nodeId, const std::string& id, XComponentType type, - const std::optional& libraryname) +RefPtr XComponentModelNG::CreateFrameNode( + int32_t nodeId, const std::string& id, XComponentType type, const std::optional& libraryname) { - auto pattern = AceType::MakeRefPtr(XComponentNodeType::CNODE); + auto pattern = AceType::MakeRefPtr(XComponentNodeType::CNODE); auto frameNode = FrameNode::CreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, pattern); - pattern->InitParams(type); + pattern->InitParams(id, type, libraryname, nullptr); auto layoutProperty = frameNode->GetLayoutProperty(); CHECK_NULL_RETURN(layoutProperty, frameNode); layoutProperty->UpdateXComponentType(type); @@ -354,13 +357,13 @@ RefPtr XComponentModelNG::CreateTypeNode(int32_t nodeId, ArkUI_XCompo RefPtr frameNode; if (id.empty() && controller == nullptr && (type == XComponentType::SURFACE || type == XComponentType::TEXTURE)) { frameNode = FrameNode::CreateFrameNode( - V2::XCOMPONENT_ETS_TAG, nodeId, AceType::MakeRefPtr(XComponentNodeType::TYPE_NODE)); - auto pattern = frameNode->GetPattern(); + V2::XCOMPONENT_ETS_TAG, nodeId, AceType::MakeRefPtr(XComponentNodeType::TYPE_NODE)); + auto pattern = frameNode->GetPattern(); CHECK_NULL_RETURN(pattern, nullptr); - pattern->InitParams(type); + pattern->InitParams(std::nullopt, type, libraryName, controller); } else { - frameNode = - FrameNode::CreateFrameNode(V2::XCOMPONENT_ETS_TAG, nodeId, AceType::MakeRefPtr(true)); + frameNode = FrameNode::CreateFrameNode( + V2::XCOMPONENT_ETS_TAG, nodeId, AceType::MakeRefPtr(XComponentNodeType::TYPE_NODE)); auto pattern = frameNode->GetPattern(); CHECK_NULL_RETURN(pattern, nullptr); pattern->InitParams(id, type, libraryName, controller); @@ -638,7 +641,7 @@ bool XComponentModelNG::GetXComponentEnableAnalyzer(FrameNode* frameNode) int32_t XComponentModelNG::SetExpectedRateRange(FrameNode* frameNode, int32_t min, int32_t max, int32_t expected) { CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); if (xcPattern->HasGotNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; @@ -651,7 +654,7 @@ int32_t XComponentModelNG::SetOnFrameCallback(FrameNode* frameNode, void(*callback)(void*, uint64_t, uint64_t), void* arkuiNode) { CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); if (xcPattern->HasGotNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; @@ -663,7 +666,7 @@ int32_t XComponentModelNG::SetOnFrameCallback(FrameNode* frameNode, int32_t XComponentModelNG::UnregisterOnFrameCallback(FrameNode* frameNode) { CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); if (xcPattern->HasGotNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; @@ -676,7 +679,7 @@ int32_t XComponentModelNG::UnregisterOnFrameCallback(FrameNode* frameNode) int32_t XComponentModelNG::SetNeedSoftKeyboard(FrameNode* frameNode, bool needSoftKeyboard) { CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); if (xcPattern->HasGotNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; @@ -688,7 +691,7 @@ int32_t XComponentModelNG::SetNeedSoftKeyboard(FrameNode* frameNode, bool needSo void* XComponentModelNG::CreateAccessibilityProvider(FrameNode* frameNode) { CHECK_NULL_RETURN(frameNode, nullptr); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, nullptr); if (xcPattern->HasGotNativeXComponent()) { return nullptr; @@ -700,13 +703,13 @@ void XComponentModelNG::DisposeAccessibilityProvider(ArkUI_AccessibilityProvider { CHECK_NULL_VOID(provider); bool isProviderValied = false; - auto frameNode = XComponentPatternV2::QueryAccessibilityProviderHost(provider, isProviderValied); + auto frameNode = XComponentPattern::QueryAccessibilityProviderHost(provider, isProviderValied); if (!isProviderValied) { return; } - RefPtr xcPattern = (frameNode == nullptr) + RefPtr xcPattern = (frameNode == nullptr) ? (nullptr) - : (frameNode->GetPattern()); + : (frameNode->GetPattern()); if (xcPattern) { xcPattern->DisposeAccessibilityProvider(provider); return; diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp index 9bfe198c861..e9bb47beeaf 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp @@ -17,8 +17,10 @@ #include #include +#include #include "interfaces/native/event/ui_input_event_impl.h" +#include "interfaces/native/native_interface_xcomponent.h" #include "interfaces/native/ui_input_event.h" #include "ui/base/utils/utils.h" @@ -72,7 +74,7 @@ namespace { const std::string BUFFER_USAGE_XCOMPONENT = "xcomponent"; } // namespace -XComponentPattern::XComponentPattern(bool isTypeNode) : isTypedNode_(isTypeNode) +XComponentPattern::XComponentPattern(XComponentNodeType nodeType) : nodeType_(nodeType) { RegisterSurfaceCallbackModeEvent(); } @@ -81,40 +83,208 @@ void XComponentPattern::InitParams(const std::optional& id, XCompon const std::optional& libraryname, const std::shared_ptr& xcomponentController, float initWidth, float initHeight) { - if (type != XComponentType::UNKNOWN) { + if (type_ != XComponentType::UNKNOWN) { return; } - id_=id; - type_=type; - if(type == XComponentType::NODE){ + id_ = id; + type_ = type; + SetLibraryName(libraryname); + xcomponentController_ = xcomponentController; + if (type == XComponentType::SURFACE || type == XComponentType::TEXTURE) { + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto renderContext = host->GetRenderContext(); + CHECK_NULL_VOID(renderContext); + + renderContext->SetClipToFrame(true); + renderContext->SetClipToBounds(true); + if (FrameReport::GetInstance().GetEnable()) { + FrameReport::GetInstance().EnableSelfRender(); + } + } + if (id == std::nullopt && xcomponentController == nullptr && + (type == XComponentType::SURFACE || type == XComponentType::TEXTURE) && + nodeType_ != XComponentNodeType::CNODE) { + // ProcessV2 + InitV2(); + isV2_ = true; + } else { + initSize_ = SizeF { initWidth, initHeight }; // only for XComponentNode which is deprecated + InitV1(); + } +} + +void XComponentPattern::InitV1() +{ + if (type_ == XComponentType::NODE) { auto host = GetHost(); CHECK_NULL_VOID(host); auto focusHub = host->GetOrCreateFocusHub(); focusHub->SetFocusType(FocusType::SCOPE); focusHub->SetFocusable(true); } - xcomponentController_ = xcomponentController; - initSize_ = SizeF { initWidth, initHeight }; - SetLibraryName(libraryname); - if (!isTypedNode_) { + if (nodeType_ != XComponentNodeType::TYPE_NODE) { InitNativeXComponent(); } - Initialize(); + if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) { + InitSurface(); + if (nodeType_ == XComponentNodeType::TYPE_NODE || nodeType_ == XComponentNodeType::CNODE) { + InitNativeWindow(initSize_.Width(), initSize_.Height()); + } + // only xcomponent created by capi will set successfully, others will be set in FireExternalEvent + SetExpectedRateRangeInit(); + OnFrameEventInit(); + UnregisterOnFrameEventInit(); + InitEvent(); + InitController(); + } else if (type_ == XComponentType::NODE && id_.has_value()) { + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto context = host->GetContextRefPtr(); + if (context) { + FireExternalEvent(context, id_.value(), host->GetId(), false); + InitNativeNodeCallbacks(); + } + } + if (nodeType_ != XComponentNodeType::TYPE_NODE) { + InitializeAccessibility(); + } + + RegisterCallbackV1(); } -// XComponentPattern::XComponentPattern(const std::optional& id, XComponentType type, -// const std::optional& libraryname, -// const std::shared_ptr& xcomponentController, float initWidth, float initHeight, -// bool isTypedNode) -// : id_(id), type_(type), xcomponentController_(xcomponentController), initSize_(initWidth, initHeight), -// isTypedNode_(isTypedNode) -// { -// SetLibraryName(libraryname); -// if (!isTypedNode_) { -// InitNativeXComponent(); -// } -// RegisterSurfaceCallbackModeEvent(); -// } +void XComponentPattern::InitV2() +{ + InitSurface(); + renderSurface_->RegisterSurface(); + InitNativeWindow(paintRect_.Width(), paintRect_.Height()); + if (surfaceHolder_) { + surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); + } + RegisterCallbackV2(); +} + +void XComponentPattern::RegisterCallbackV1() { + onAttachToMainTree_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + if (xcPattern->GetNodeType() == XComponentNodeType::TYPE_NODE && + xcPattern->GetSurfaceCallbackMode() == SurfaceCallbackMode::DEFAULT) { + xcPattern->HandleSurfaceCreated(); + } + }; + onDetachFromMainTree_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + if (xcPattern->GetNodeType() == XComponentNodeType::TYPE_NODE && + xcPattern->GetSurfaceCallbackMode() == SurfaceCallbackMode::DEFAULT) { + xcPattern->HandleSurfaceDestroyed(); + } + }; + onSizeChange_ = [weak = WeakClaim(this)]( + bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->OnSizeChangeV1(offsetChanged, sizeChanged, frameOffsetChange, needFireNativeEvent); + }; + onWindowShow_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->NativeSurfaceShow(); + }; + onWindowHide_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->NativeSurfaceHide(); + }; +} + +void XComponentPattern::RegisterCallbackV2() { + onAttachToMainTree_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + if (xcPattern->IsAutoInitialize()) { + xcPattern->HandleSurfaceCreatedV2(); + } + }; + onDetachFromMainTree_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + if (xcPattern->IsAutoInitialize()) { + xcPattern->HandleSurfaceDestroyedV2(); + } + }; + onSizeChange_ = [weak = WeakClaim(this)]([[maybe_unused]] bool offsetChanged, bool sizeChanged, + [[maybe_unused]] bool frameOffsetChange, [[maybe_unused]] bool needFireNativeEvent) { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->HandleSurfaceChangeEventV2(sizeChanged); + }; + onWindowShow_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->OnSurfaceShowV2(); + }; + onWindowHide_ = [weak = WeakClaim(this)]() { + auto xcPattern = weak.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->OnSurfaceHideV2(); + }; +} + +void XComponentPattern::ReinitializeSurfaceProperties() +{ + CHECK_NULL_VOID(renderContextForSurface_); + CHECK_NULL_VOID(renderSurface_); + if (!paintRect_.IsEmpty()) { + auto width = paintRect_.Width(); + auto height = paintRect_.Height(); + renderContextForSurface_->SetBounds(paintRect_.GetX(), paintRect_.GetY(), width, height); + renderSurface_->UpdateSurfaceSizeInUserData(static_cast(width), static_cast(height)); + renderSurface_->SetSurfaceDefaultSize(static_cast(width), static_cast(height)); + } + renderContextForSurface_->UpdateBackgroundColor(bkColor_); + renderContextForSurface_->SetHDRBrightness(hdrBrightness_); + renderContextForSurface_->SetTransparentLayer(isTransparentLayer_); + renderContextForSurface_->SetSecurityLayer(isEnableSecure_); + renderContextForSurface_->SetSurfaceRotation(isSurfaceLock_); + renderContextForSurface_->SetRenderFit(renderFit_); + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto renderContext = host->GetRenderContext(); + CHECK_NULL_VOID(renderContext); + renderContext->AddChild(renderContextForSurface_, 0); + needReinitialize_ = false; +} + +void XComponentPattern::OnSizeChangeV1( + bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) +{ + if (!hasXComponentInit_) { + initSize_ = paintRect_.GetSize(); + if (!SystemProperties::GetExtSurfaceEnabled() && nodeType_ != XComponentNodeType::TYPE_NODE) { + XComponentSizeInit(); + } + auto offset = globalPosition_ + paintRect_.GetOffset(); + NativeXComponentOffset(offset.GetX(), offset.GetY()); + hasXComponentInit_ = true; + } +#ifndef RENDER_EXTRACT_SUPPORTED + auto host = GetHost(); + CHECK_NULL_VOID(host); + if (SystemProperties::GetExtSurfaceEnabled()) { + auto transformRelativeOffset = host->GetTransformRelativeOffset(); + renderSurface_->SetExtSurfaceBounds( + static_cast(transformRelativeOffset.GetX() + localPosition_.GetX()), + static_cast(transformRelativeOffset.GetY() + localPosition_.GetY()), + static_cast(drawSize_.Width()), static_cast(drawSize_.Height())); + } + HandleSurfaceChangeEvent(false, offsetChanged, sizeChanged, needFireNativeEvent, frameOffsetChange); +#endif + if (type_ == XComponentType::SURFACE && renderType_ == NodeRenderType::RENDER_TYPE_TEXTURE) { + AddAfterLayoutTaskForExportTexture(); + } +} std::string XComponentPattern::XComponentTypeToString(XComponentType type) { @@ -174,7 +344,7 @@ void XComponentPattern::InitNativeXComponent() void XComponentPattern::InitXComponent() { // used for TypedNode, not for declareative - if (isTypedNode_) { + if (nodeType_ == XComponentNodeType::TYPE_NODE) { InitNativeXComponent(); if (isNativeXComponent_) { InitializeAccessibility(); @@ -185,18 +355,14 @@ void XComponentPattern::InitXComponent() void XComponentPattern::InitSurface() { + if (renderSurface_) { + return; + } auto host = GetHost(); CHECK_NULL_VOID(host); auto renderContext = host->GetRenderContext(); CHECK_NULL_VOID(renderContext); - // only xcomponent created by capi will set successfully, others will be set in FireExternalEvent - SetExpectedRateRangeInit(); - OnFrameEventInit(); - UnregisterOnFrameEventInit(); - - renderContext->SetClipToFrame(true); - renderContext->SetClipToBounds(true); #ifdef RENDER_EXTRACT_SUPPORTED renderSurface_ = RenderSurface::Create(CovertToRenderSurfaceType(type_)); #else @@ -229,9 +395,6 @@ void XComponentPattern::InitSurface() if (type_ == XComponentType::TEXTURE) { renderSurface_->RegisterBufferCallback(); } - if (isTypedNode_ || isCNode_) { - InitNativeWindow(initSize_.Width(), initSize_.Height()); - } surfaceId_ = renderSurface_->GetUniqueId(); initialSurfaceId_ = surfaceId_; UpdateTransformHint(); @@ -278,34 +441,34 @@ void XComponentPattern::RegisterTransformHintCallback(PipelineContext* context) UpdateTransformHintChangedCallbackId(callbackId); } -void XComponentPattern::Initialize() -{ - if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) { - InitSurface(); - InitEvent(); - InitController(); - } else if (type_ == XComponentType::NODE && id_.has_value()) { - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto context = host->GetContextRefPtr(); - if (context) { - FireExternalEvent(context, id_.value(), host->GetId(), false); - InitNativeNodeCallbacks(); - } - } - if (!isTypedNode_) { - InitializeAccessibility(); - } -} +// void XComponentPattern::Initialize() +// { +// if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) { +// InitSurface(); +// InitEvent(); +// InitController(); +// } else if (type_ == XComponentType::NODE && id_.has_value()) { +// auto host = GetHost(); +// CHECK_NULL_VOID(host); +// auto context = host->GetContextRefPtr(); +// if (context) { +// FireExternalEvent(context, id_.value(), host->GetId(), false); +// InitNativeNodeCallbacks(); +// } +// } +// if (nodeType_ != XComponentNodeType::TYPE_NODE) { +// InitializeAccessibility(); +// } +// } void XComponentPattern::OnAttachToMainTree() { TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] AttachToMainTree", GetId().c_str()); ACE_SCOPED_TRACE("XComponent[%s] AttachToMainTree", GetId().c_str()); isOnTree_ = true; - if (isTypedNode_ && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) { - HandleSurfaceCreated(); - } + // if (nodeType_ == XComponentNodeType::TYPE_NODE && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) { + // HandleSurfaceCreated(); + // } auto host = GetHost(); CHECK_NULL_VOID(host); if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { @@ -316,6 +479,9 @@ void XComponentPattern::OnAttachToMainTree() needRecoverDisplaySync_ = false; } } + if(onAttachToMainTree_) { + onAttachToMainTree_(); + } } void XComponentPattern::OnDetachFromMainTree() @@ -323,9 +489,9 @@ void XComponentPattern::OnDetachFromMainTree() TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] DetachFromMainTree", GetId().c_str()); ACE_SCOPED_TRACE("XComponent[%s] DetachFromMainTree", GetId().c_str()); isOnTree_ = false; - if (isTypedNode_ && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) { - HandleSurfaceDestroyed(); - } + // if (nodeType_ == XComponentNodeType::TYPE_NODE && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) { + // HandleSurfaceDestroyed(); + // } auto host = GetHost(); CHECK_NULL_VOID(host); if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { @@ -336,10 +502,16 @@ void XComponentPattern::OnDetachFromMainTree() needRecoverDisplaySync_ = true; } } + if (onDetachFromMainTree_) { + onDetachFromMainTree_(); + } } void XComponentPattern::InitializeRenderContext() { + if (renderContextForSurface_) { + return; + } renderContextForSurface_ = RenderContext::Create(); #ifdef RENDER_EXTRACT_SUPPORTED auto contextType = type_ == XComponentType::TEXTURE ? RenderContext::ContextType::HARDWARE_TEXTURE @@ -352,7 +524,9 @@ void XComponentPattern::InitializeRenderContext() renderContextForSurface_->InitContext(false, param); - renderContextForSurface_->UpdateBackgroundColor(Color::BLACK); + if (needReinitialize_) { + ReinitializeSurfaceProperties(); + } } #ifdef RENDER_EXTRACT_SUPPORTED @@ -407,16 +581,17 @@ void XComponentPattern::RequestFocus() void XComponentPattern::OnAttachToFrameNode() { - Initialize(); - if (FrameReport::GetInstance().GetEnable()) { - FrameReport::GetInstance().EnableSelfRender(); - } + // Initialize(); + // if (FrameReport::GetInstance().GetEnable()) { + // FrameReport::GetInstance().EnableSelfRender(); + // } } void XComponentPattern::OnModifyDone() { Pattern::OnModifyDone(); // if surface has been reset by pip, do not set backgourndColor + // need do sth for pip if (handlingSurfaceRenderContext_ != renderContextForSurface_) { return; } @@ -500,7 +675,7 @@ void XComponentPattern::OnDetachFromFrameNode(FrameNode* frameNode) UnregisterSurfaceRenderContext(); CHECK_NULL_VOID(frameNode); UninitializeAccessibility(frameNode); - if (isTypedNode_) { + if (nodeType_ == XComponentNodeType::TYPE_NODE) { if (surfaceCallbackMode_ == SurfaceCallbackMode::PIP) { HandleSurfaceDestroyed(frameNode); } @@ -562,7 +737,7 @@ void XComponentPattern::InitController() }, "ArkUIXComponentSurfaceConfigChange"); }); - if (!isTypedNode_) { + if (nodeType_ != XComponentNodeType::TYPE_NODE) { xcomponentController_->SetSurfaceId(surfaceId_); } } @@ -647,27 +822,8 @@ void XComponentPattern::BeforeSyncGeometryProperties(const DirtySwapConfig& conf UpdateAnalyzerUIConfig(geometryNode); } const auto& [offsetChanged, sizeChanged, needFireNativeEvent] = UpdateSurfaceRect(); - if (!hasXComponentInit_) { - initSize_ = paintRect_.GetSize(); - if (!SystemProperties::GetExtSurfaceEnabled() && !isTypedNode_) { - XComponentSizeInit(); - } - auto offset = globalPosition_ + paintRect_.GetOffset(); - NativeXComponentOffset(offset.GetX(), offset.GetY()); - hasXComponentInit_ = true; - } -#ifndef RENDER_EXTRACT_SUPPORTED - if (SystemProperties::GetExtSurfaceEnabled()) { - auto transformRelativeOffset = host->GetTransformRelativeOffset(); - renderSurface_->SetExtSurfaceBounds( - static_cast(transformRelativeOffset.GetX() + localPosition_.GetX()), - static_cast(transformRelativeOffset.GetY() + localPosition_.GetY()), - static_cast(drawSize_.Width()), static_cast(drawSize_.Height())); - } - HandleSurfaceChangeEvent(false, offsetChanged, sizeChanged, needFireNativeEvent, config.frameOffsetChange); -#endif - if (type_ == XComponentType::SURFACE && renderType_ == NodeRenderType::RENDER_TYPE_TEXTURE) { - AddAfterLayoutTaskForExportTexture(); + if (onSizeChange_) { + onSizeChange_(offsetChanged, sizeChanged, needFireNativeEvent, config.frameOffsetChange); } host->MarkNeedSyncRenderTree(); } @@ -725,19 +881,21 @@ void XComponentPattern::NativeXComponentDispatchTouchEvent( void XComponentPattern::InitNativeWindow(float textureWidth, float textureHeight) { - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto context = host->GetContextRefPtr(); - CHECK_NULL_VOID(context); CHECK_NULL_VOID(renderSurface_); if (renderSurface_->GetNativeWindow()) { return; } if (renderSurface_->IsSurfaceValid() && (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE)) { - float viewScale = context->GetViewScale(); renderSurface_->CreateNativeWindow(); - renderSurface_->AdjustNativeWindowSize( - static_cast(textureWidth * viewScale), static_cast(textureHeight * viewScale)); + if (textureWidth > 0 && textureHeight > 0) { + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto context = host->GetContextRefPtr(); + CHECK_NULL_VOID(context); + float viewScale = context->GetViewScale(); + renderSurface_->AdjustNativeWindowSize( + static_cast(textureWidth * viewScale), static_cast(textureHeight * viewScale)); + } nativeWindow_ = renderSurface_->GetNativeWindow(); } } @@ -747,12 +905,12 @@ void XComponentPattern::XComponentSizeInit() CHECK_RUN_ON(UI); auto host = GetHost(); CHECK_NULL_VOID(host); - if (!isCNode_) { + if (nodeType_ != XComponentNodeType::CNODE) { InitNativeWindow(initSize_.Width(), initSize_.Height()); } else { AdjustNativeWindowSize(initSize_.Width(), initSize_.Height()); } - + #ifdef RENDER_EXTRACT_SUPPORTED if (xcomponentController_ && renderSurface_) { surfaceId_ = renderSurface_->GetUniqueId(); @@ -779,7 +937,7 @@ void XComponentPattern::XComponentSizeChange(const RectF& surfaceRect, bool need // In declarative mode: Native onSurfaceCreated callback is triggred // when the component finish it's first layout, so do not trigger the native onSurfaceChanged callback - if (!isTypedNode_ && isNativeXComponent_ && !needFireNativeEvent) { + if (nodeType_ != XComponentNodeType::TYPE_NODE && isNativeXComponent_ && !needFireNativeEvent) { return; } // When creating the surface for the first time, needFireNativeEvent = false, other time needFireNativeEvent = true @@ -1627,6 +1785,42 @@ void XComponentPattern::HandleSurfaceChangeEvent( } } +void XComponentPattern::HandleSurfaceChangeEventV2(bool sizeChanged) +{ + if (!drawSize_.IsPositive()) { + return; + } + if (sizeChanged) { + XComponentSizeChangeV2(paintRect_); + } + if (renderContextForSurface_) { + renderContextForSurface_->SetBounds( + paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height()); + } + if (renderSurface_) { + renderSurface_->SetSurfaceDefaultSize( + static_cast(paintRect_.Width()), static_cast(paintRect_.Height())); + } +} + +void XComponentPattern::XComponentSizeChangeV2(const RectF& surfaceRect) +{ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto context = host->GetContextRefPtr(); + CHECK_NULL_VOID(context); + auto viewScale = context->GetViewScale(); + CHECK_NULL_VOID(renderSurface_); + auto width = surfaceRect.Width(); + auto height = surfaceRect.Height(); + needNotifySizeChanged_ = true; + renderSurface_->UpdateSurfaceSizeInUserData( + static_cast(width), static_cast(height)); + renderSurface_->AdjustNativeWindowSize( + static_cast(width * viewScale), static_cast(height * viewScale)); + OnSurfaceChangedV2(surfaceRect); +} + std::tuple XComponentPattern::UpdateSurfaceRect() { if (!drawSize_.IsPositive()) { @@ -1744,6 +1938,22 @@ void XComponentPattern::OnSurfaceChanged(const RectF& surfaceRect, bool needResi } } +void XComponentPattern::OnSurfaceChangedV2(const RectF& surfaceRect) +{ + CHECK_RUN_ON(UI); + CHECK_NULL_VOID(surfaceHolder_); + CHECK_EQUAL_VOID(isInitialized_, false); + ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceChanged", GetId().c_str()); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + for (const auto& iter : callbackList) { + auto callback = iter->OnSurfaceChanged; + if (callback) { + callback(surfaceHolder_, surfaceRect.Width(), surfaceRect.Height()); + } + } + needNotifySizeChanged_ = false; +} + void XComponentPattern::OnSurfaceDestroyed(FrameNode* frameNode) { if (isNativeXComponent_) { @@ -1758,7 +1968,7 @@ void XComponentPattern::OnSurfaceDestroyed(FrameNode* frameNode) ACE_SCOPED_TRACE("XComponent[%s] native OnSurfaceDestroyed", GetId().c_str()); callback->OnSurfaceDestroyed(nativeXComponent_.get(), surface); nativeXComponentImpl_->SetSurface(nullptr); - } else if (isTypedNode_) { + } else if (nodeType_ == XComponentNodeType::TYPE_NODE) { RefPtr host; if (!frameNode) { host = GetHost(); @@ -1773,7 +1983,7 @@ void XComponentPattern::OnSurfaceDestroyed(FrameNode* frameNode) void XComponentPattern::RegisterSurfaceCallbackModeEvent() { - if (isTypedNode_ && !surfaceCallbackModeChangeEvent_) { + if (nodeType_ == XComponentNodeType::TYPE_NODE && !surfaceCallbackModeChangeEvent_) { surfaceCallbackModeChangeEvent_ = [weak = WeakClaim(this)](SurfaceCallbackMode mode) { auto xcPattern = weak.Upgrade(); CHECK_NULL_VOID(xcPattern); @@ -1807,6 +2017,34 @@ void XComponentPattern::HandleSurfaceCreated() OnSurfaceCreated(); } +int32_t XComponentPattern::HandleSurfaceCreatedV2() +{ + CHECK_EQUAL_RETURN(isInitialized_, true, ERROR_CODE_XCOMPONENT_STATE_INVALID); + InitSurface(); + renderSurface_->RegisterSurface(); + InitNativeWindow(paintRect_.Width(), paintRect_.Height()); + if (surfaceHolder_) { + surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); + } + CHECK_NULL_RETURN(renderSurface_, ERROR_CODE_PARAM_INVALID); + isInitialized_ = true; + if (surfaceHolder_) { + auto callbackList = surfaceHolder_->surfaceCallbackList_; + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceCreated", GetId().c_str()); + ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceCreated", GetId().c_str()); + for (const auto& iter : callbackList) { + auto callback = iter->OnSurfaceCreated; + if (callback) { + callback(surfaceHolder_); + } + } + } + if (needNotifySizeChanged_) { + OnSurfaceChangedV2(paintRect_); + } + return ERROR_CODE_NO_ERROR; +} + void XComponentPattern::HandleSurfaceDestroyed(FrameNode* frameNode) { CHECK_NULL_VOID(renderSurface_); @@ -1817,6 +2055,95 @@ void XComponentPattern::HandleSurfaceDestroyed(FrameNode* frameNode) xcomponentController_->SetSurfaceId(""); } +int32_t XComponentPattern::HandleSurfaceDestroyedV2() +{ + CHECK_EQUAL_RETURN(isInitialized_, false, ERROR_CODE_XCOMPONENT_STATE_INVALID); + isInitialized_ = false; + if (surfaceHolder_) { + auto callbackList = surfaceHolder_->surfaceCallbackList_; + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); + ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); + for (const auto& iter : callbackList) { + auto callback = iter->OnSurfaceDestroyed; + if (callback) { + callback(surfaceHolder_); + } + } + } + DisposeSurface(); + return ERROR_CODE_NO_ERROR; +} + +void XComponentPattern::DisposeSurface() +{ + if (type_ == XComponentType::SURFACE) { + XComponentInnerSurfaceController::UnregisterSurfaceRenderContext(surfaceId_); + surfaceId_ = ""; + } + if (renderSurface_) { + renderSurface_->ReleaseSurfaceBuffers(); + renderSurface_->UnregisterSurface(); + renderSurface_ = nullptr; + } + if (surfaceHolder_) { + surfaceHolder_->nativeWindow_ = nullptr; + } + nativeWindow_ = nullptr; + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto renderContext = host->GetRenderContext(); + CHECK_NULL_VOID(renderContext); + // for surface type + CHECK_NULL_VOID(renderContextForSurface_); + renderContext->RemoveChild(renderContextForSurface_); + renderContextForSurface_ = nullptr; + needReinitialize_ = true; +#ifdef ENABLE_ROSEN_BACKEND + FlushImplicitTransaction(host); +#endif +} + +std::shared_ptr XComponentPattern::GetRSTransactionHandler( + const RefPtr& frameNode) +{ +#ifdef ENABLE_ROSEN_BACKEND + if (!SystemProperties::GetMultiInstanceEnabled()) { + return nullptr; + } + auto rsUIContext = GetRSUIContext(frameNode); + CHECK_NULL_RETURN(rsUIContext, nullptr); + return rsUIContext->GetRSTransaction(); +#endif + return nullptr; +} + +std::shared_ptr XComponentPattern::GetRSUIContext(const RefPtr& frameNode) +{ +#ifdef ENABLE_ROSEN_BACKEND + CHECK_NULL_RETURN(frameNode, nullptr); + auto pipeline = frameNode->GetContext(); + CHECK_NULL_RETURN(pipeline, nullptr); + auto window = pipeline->GetWindow(); + CHECK_NULL_RETURN(window, nullptr); + auto rsUIDirector = window->GetRSUIDirector(); + CHECK_NULL_RETURN(rsUIDirector, nullptr); + auto rsUIContext = rsUIDirector->GetRSUIContext(); + return rsUIContext; +#endif + return nullptr; +} + +void XComponentPattern::FlushImplicitTransaction(const RefPtr& frameNode) +{ +#ifdef ENABLE_ROSEN_BACKEND + if (auto transactionHandler = GetRSTransactionHandler(frameNode)) { + transactionHandler->FlushImplicitTransaction(); + } else { + Rosen::RSTransactionProxy::GetInstance()->FlushImplicitTransaction(); + } +#endif +} + void XComponentPattern::NativeSurfaceShow() { CHECK_RUN_ON(UI); @@ -1850,7 +2177,9 @@ void XComponentPattern::OnWindowHide() if (renderSurface_) { renderSurface_->OnWindowStateChange(false); } - NativeSurfaceHide(); + if (onWindowHide_) { + onWindowHide_(); + } hasReleasedSurface_ = true; } @@ -1863,7 +2192,9 @@ void XComponentPattern::OnWindowShow() if (renderSurface_) { renderSurface_->OnWindowStateChange(true); } - NativeSurfaceShow(); + if (onWindowShow_) { + onWindowShow_(); + } hasReleasedSurface_ = false; } @@ -2204,4 +2535,239 @@ void XComponentPattern::UnlockCanvasAndPost(RSCanvas* canvas) TAG_LOGW(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] UnlockCanvasAndPost failed", GetId().c_str()); } } + +void XComponentPattern::SetExpectedRateRange(int32_t min, int32_t max, int32_t expected) +{ + if (hasGotNativeXComponent_) { + return; + } + isNativeXComponentDisabled_ = true; + CHECK_NULL_VOID(displaySync_); + FrameRateRange frameRateRange; + frameRateRange.Set(min, max, expected); + displaySync_->SetExpectedFrameRateRange(frameRateRange); + TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " SetExpectedFrameRateRange" + "{%{public}d, %{public}d, %{public}d}", displaySync_->GetId(), min, max, expected); +} + +void XComponentPattern::UpdateOnFrameEvent(void(*callback)(void*, uint64_t, uint64_t), void* arkuiNode) +{ + if (hasGotNativeXComponent_) { + return; + } + isNativeXComponentDisabled_ = true; + CHECK_NULL_VOID(displaySync_); + displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this), + callback, arkuiNode](RefPtr displaySyncData) { + auto xComponentPattern = weak.Upgrade(); + CHECK_NULL_VOID(xComponentPattern); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(arkuiNode); + CHECK_NULL_VOID(displaySyncData); + callback(arkuiNode, displaySyncData->GetTimestamp(), displaySyncData->GetTargetTimestamp()); + }); + TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " RegisterOnFrame", + displaySync_->GetId()); + displaySync_->AddToPipelineOnContainer(); +} + +void XComponentPattern::UnregisterOnFrameEvent() +{ + if (hasGotNativeXComponent_) { + return; + } + isNativeXComponentDisabled_ = true; + CHECK_NULL_VOID(displaySync_); + displaySync_->UnregisterOnFrame(); + TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame", + displaySync_->GetId()); + displaySync_->DelFromPipelineOnContainer(); + needRecoverDisplaySync_ = false; +} + +void XComponentPattern::SetNeedSoftKeyboard(bool isNeedSoftKeyboard) +{ + if (hasGotNativeXComponent_) { + return; + } + isNativeXComponentDisabled_ = true; + isNeedSoftKeyboard_ = isNeedSoftKeyboard; +} + +void XComponentPattern::OnSurfaceShowV2() +{ + CHECK_RUN_ON(UI); + CHECK_NULL_VOID(surfaceHolder_); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + for (const auto& iter : callbackList) { + auto callback = iter->onSurfaceShow; + if (callback) { + callback(surfaceHolder_); + } + } +} + +void XComponentPattern::OnSurfaceHideV2() +{ + CHECK_RUN_ON(UI); + CHECK_NULL_VOID(surfaceHolder_); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + bool hasCallback = false; + for (const auto& iter : callbackList) { + auto callback = iter->onSurfaceHide; + if (callback) { + callback(surfaceHolder_); + hasCallback = true; + } + } + if (hasCallback) { + CHECK_NULL_VOID(renderSurface_); + renderSurface_->ReleaseSurfaceBuffers(); + } +} + +void XComponentPattern::SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder) +{ + surfaceHolder_ = surfaceHolder; + if (surfaceHolder_) { + surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); + hasGotSurfaceHolder_ = true; + } + if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { + isV2_ = true; + RegisterCallbackV2(); + } +} + +int32_t XComponentPattern::Initialize() +{ + if (IsCreateSurfaceHolderForbidden()) { + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; + } + if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { + isV2_ = true; + RegisterCallbackV2(); + } + return HandleSurfaceCreatedV2(); +} +int32_t XComponentPattern::Finalize() +{ + if (IsCreateSurfaceHolderForbidden()) { + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; + } + if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { + isV2_ = true; + RegisterCallbackV2(); + } + return HandleSurfaceDestroyedV2(); +} +int32_t XComponentPattern::SetAutoInitialize(bool autoInitialize) +{ + if (IsCreateSurfaceHolderForbidden()) { + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; + } + if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { + isV2_ = true; + RegisterCallbackV2(); + } + autoInitialize_ = autoInitialize; + return ERROR_CODE_NO_ERROR; +} +int32_t XComponentPattern::IsInitialized(bool& isInitialized) +{ + if (IsCreateSurfaceHolderForbidden()) { + return OHOS::Ace::ERROR_CODE_PARAM_INVALID; + } + if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { + isV2_ = true; + RegisterCallbackV2(); + } + isInitialized = isInitialized_; + return ERROR_CODE_NO_ERROR; +} + +void XComponentPattern::ResetAndInitializeNodeHandleAccessibility() +{ + auto host = GetHost(); + CHECK_NULL_VOID(host); + auto pipeline = host->GetContextRefPtr(); + CHECK_NULL_VOID(pipeline); + auto accessibilityManager = pipeline->GetAccessibilityManager(); + CHECK_NULL_VOID(accessibilityManager); + if (accessibilityChildTreeCallback_) { + accessibilityManager->DeregisterAccessibilityChildTreeCallback( + accessibilityChildTreeCallback_->GetAccessibilityId() + ); + accessibilityChildTreeCallback_.reset(); + accessibilityChildTreeCallback_ = nullptr; + } + + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeNodeHandleAccessibility"); + CHECK_NULL_VOID(arkuiAccessibilityProvider_); + arkuiAccessibilityProvider_->SetRegisterCallback( + [weak = WeakClaim(this)] (bool isRegister) { + auto pattern = weak.Upgrade(); + CHECK_NULL_VOID(pattern); + pattern->HandleRegisterAccessibilityEvent(isRegister); + }); + int64_t accessibilityId = host->GetAccessibilityId(); + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, + "InitializeNodeHandleAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId); + accessibilityChildTreeCallback_ = std::make_shared( + WeakClaim(this), host->GetAccessibilityId()); + accessibilityManager->RegisterAccessibilityChildTreeCallback( + accessibilityId, accessibilityChildTreeCallback_); + if (accessibilityManager->IsRegister()) { + accessibilityChildTreeCallback_->OnRegister( + pipeline->GetWindowId(), accessibilityManager->GetTreeId()); + } +} + +ArkUI_AccessibilityProvider* XComponentPattern::CreateAccessibilityProvider() +{ + if (hasGotNativeXComponent_) { + return nullptr; + } + isNativeXComponentDisabled_ = true; + useNodeHandleAccessibilityProvider_ = true; + if (arkuiAccessibilityProvider_) { + return arkuiAccessibilityProvider_; + } + auto host = GetHost(); + CHECK_NULL_RETURN(host, nullptr); + arkuiAccessibilityProvider_ = new ArkUI_AccessibilityProvider(); + XComponentPattern::XComponentAccessibilityProviderMap[arkuiAccessibilityProvider_] = WeakPtr(host); + ResetAndInitializeNodeHandleAccessibility(); + return arkuiAccessibilityProvider_; +} + +void XComponentPattern::DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider) +{ + if (hasGotNativeXComponent_ || (provider != arkuiAccessibilityProvider_)) { + return; + } + CHECK_NULL_VOID(arkuiAccessibilityProvider_); + isNativeXComponentDisabled_ = true; + XComponentPattern::XComponentAccessibilityProviderMap.erase(arkuiAccessibilityProvider_); + auto host = GetHost(); + UninitializeAccessibility(AceType::RawPtr(host)); + delete arkuiAccessibilityProvider_; + arkuiAccessibilityProvider_ = nullptr; +} + +std::unordered_map> XComponentPattern::XComponentAccessibilityProviderMap; + +FrameNode* XComponentPattern::QueryAccessibilityProviderHost(void* provider, bool& isProviderValied) +{ + auto it = XComponentAccessibilityProviderMap.find(provider); + if (it == XComponentAccessibilityProviderMap.end()) { + isProviderValied = false; + return nullptr; + } + isProviderValied = true; + auto weakHost = it->second; + auto host = weakHost.Upgrade(); + CHECK_NULL_RETURN(host, nullptr); + return AceType::RawPtr(host); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h index 6cd8be658d4..63c7f71b0cd 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h @@ -39,15 +39,22 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" #include "core/components_ng/pattern/xcomponent/xcomponent_layout_property.h" #include "core/components_ng/pattern/xcomponent/xcomponent_paint_method.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/components_ng/property/property.h" #include "core/components_ng/render/render_surface.h" #include "core/pipeline_ng/pipeline_context.h" -#include "core/components_ng/manager/display_sync/ui_display_sync.h" -#include "core/gestures/velocity.h" + +namespace OHOS::Rosen { +class Session; +class RSNode; +class RSTransaction; +class RSTransactionHandler; +class RSUIContext; +} // namespace OHOS::Rosen namespace OHOS::Ace { class ImageAnalyzerManager; -} +} // namespace OHOS::Ace namespace OHOS::Ace::NG { class XComponentExtSurfaceCallbackClient; class XComponentPattern : public Pattern { @@ -55,7 +62,7 @@ class XComponentPattern : public Pattern { public: XComponentPattern() = delete; - explicit XComponentPattern(bool isTypeNode); + explicit XComponentPattern(XComponentNodeType nodeType); ~XComponentPattern() override = default; void InitParams(const std::optional& id, XComponentType type, @@ -133,9 +140,10 @@ public: void InitNativeWindow(float textureWidth, float textureHeight); void XComponentSizeInit(); void XComponentSizeChange(const RectF& surfaceRect, bool needFireNativeEvent); + void XComponentSizeChangeV2(const RectF& surfaceRect); void NativeXComponentInit() { - if (!isTypedNode_) { + if (nodeType_ != XComponentNodeType::TYPE_NODE) { OnSurfaceCreated(); } } @@ -213,11 +221,6 @@ public: return isSurfaceLock_; } - void SetIsTypeNode(bool isTypeNode) - { - isTypedNode_ = isTypeNode; - } - std::shared_ptr GetXComponentController() { return xcomponentController_; @@ -284,7 +287,7 @@ public: bool NeedTriggerLoadEventImmediately() const { - return isTypedNode_ && isNativeXComponent_ && hasLoadNativeDone_; + return nodeType_ == XComponentNodeType::TYPE_NODE && isNativeXComponent_ && hasLoadNativeDone_; } bool HasGotSurfaceHolder() const @@ -297,9 +300,9 @@ public: return hasGotNativeXComponent_; } - virtual bool IsBindNative() + bool IsBindNative() { - return false; + return isV2_ || nodeType_ == XComponentNodeType::CNODE; } bool IsNativeXComponentDisabled() const @@ -312,6 +315,53 @@ public: hasGotNativeXComponent_ = hasGotNativeXComponent; } + XComponentNodeType GetNodeType() const + { + return nodeType_; + } + + SurfaceCallbackMode GetSurfaceCallbackMode() const + { + return surfaceCallbackMode_; + } + + bool IsAutoInitialize() const + { + return autoInitialize_; + } + + OH_ArkUI_SurfaceHolder* GetSurfaceHolder() const + { + return surfaceHolder_; + } + + void SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder); + + bool IsCreateSurfaceHolderForbidden() const + { + // isV2_ || (nodeType_ == XComponentNodeType::CNODE && !hasGotNativeXComponent_) can create SurfaceHolder + return !isV2_ && (nodeType_ != XComponentNodeType::CNODE || hasGotNativeXComponent_); + } + + XComponentNodeType GetXComponentNodeType() const + { + return nodeType_; + } + + // need to do sth + int32_t Initialize(); + int32_t Finalize(); + int32_t SetAutoInitialize(bool autoInitialize); + int32_t IsInitialized(bool& isInitialized); + + void SetExpectedRateRange(int32_t min, int32_t max, int32_t expected); + void UpdateOnFrameEvent(void (*callback)(void*, uint64_t, uint64_t), void* arkuiNode); + void UnregisterOnFrameEvent(); + void SetNeedSoftKeyboard(bool isNeedSoftKeyboard); + ArkUI_AccessibilityProvider* CreateAccessibilityProvider(); + void DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider); + static FrameNode* QueryAccessibilityProviderHost(void* provider, bool& isProviderValied); + void SetExportTextureSurfaceId(const std::string& surfaceId); void FireExternalEvent(RefPtr context, const std::string& componentId, const uint32_t nodeId, const bool isDestroy); @@ -336,6 +386,7 @@ public: std::tuple UpdateSurfaceRect(); void HandleSurfaceChangeEvent(bool needForceRender, bool offsetChanged, bool sizeChanged, bool needFireNativeEvent, bool frameOffsetChange = false); + void HandleSurfaceChangeEventV2(bool sizeChanged); void EnableAnalyzer(bool enable); void SetImageAIOptions(void* options); void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed); @@ -346,7 +397,10 @@ public: void SetRenderFit(RenderFit renderFit); void SetScreenId(uint64_t screenId); void HandleSurfaceCreated(); + int32_t HandleSurfaceCreatedV2(); void HandleSurfaceDestroyed(FrameNode* frameNode = nullptr); + int32_t HandleSurfaceDestroyedV2(); + void DisposeSurface(); void ChangeSurfaceCallbackMode(SurfaceCallbackMode mode) { if (surfaceCallbackModeChangeEvent_) { @@ -382,7 +436,7 @@ protected: void AdjustNativeWindowSize(float width, float height); bool IsSupportImageAnalyzerFeature(); void UpdateAnalyzerUIConfig(const RefPtr& geometryNode); - void Initialize(); + // void Initialize(); std::optional id_; XComponentType type_ = XComponentType::UNKNOWN; @@ -432,6 +486,7 @@ private: void OnSurfaceCreated(); void OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow); + void OnSurfaceChangedV2(const RectF& surfaceRect); void NativeSurfaceShow(); void NativeSurfaceHide(); @@ -455,6 +510,20 @@ private: void HandleBlurEvent(); ExternalEvent CreateExternalEvent(); + void InitV1(); + void InitV2(); + void ReinitializeSurfaceProperties(); + void RegisterCallbackV1(); + void RegisterCallbackV2(); + std::shared_ptr GetRSUIContext(const RefPtr& frameNode); + void FlushImplicitTransaction(const RefPtr& frameNode); + std::shared_ptr GetRSTransactionHandler(const RefPtr& frameNode); + void OnSizeChangeV1(bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent); + void OnSurfaceShowV2(); + void OnSurfaceHideV2(); + + void ResetAndInitializeNodeHandleAccessibility(); + void SetTouchPoint( const std::list& touchInfoList, int64_t timeStamp, const TouchType& touchType); void HandleSetExpectedRateRangeEvent(); @@ -525,7 +594,6 @@ private: std::shared_ptr imageAnalyzerManager_; bool isEnableAnalyzer_ = false; uint32_t rotation_ = 0; - bool isTypedNode_ = false; bool isNativeXComponent_ = false; bool hasLoadNativeDone_ = false; SurfaceCallbackMode surfaceCallbackMode_ = SurfaceCallbackMode::DEFAULT; @@ -535,6 +603,26 @@ private: WeakPtr initialContext_ = nullptr; // record the initial surfaceId_ in InitSurface, this variable should not be modified after the initial assignment std::string initialSurfaceId_; + + OH_ArkUI_SurfaceHolder* surfaceHolder_ = nullptr; + XComponentNodeType nodeType_ = XComponentNodeType::UNKNOWN; + Color bkColor_ = Color::BLACK; + bool autoInitialize_ = true; + bool needReinitialize_ = false; + bool isInitialized_ = false; + bool needNotifySizeChanged_ = false; + bool isV2_ = false; + std::function onAttachToMainTree_ = nullptr; + std::function onDetachFromMainTree_ = nullptr; + std::function onSizeChange_ = nullptr; + // std::function onRebuildFrame_ = nullptr; + std::function onWindowHide_ = nullptr; + std::function onWindowShow_ = nullptr; + // std::function onModifyDone_ = nullptr; + // std::function dumpInfo_ = nullptr; + // std::function needSoftKeyboard_ = nullptr; + + static std::unordered_map> XComponentAccessibilityProviderMap; }; } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp index 003dc61880a..2a7342c2319 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp @@ -54,11 +54,11 @@ XComponentPatternV2::XComponentPatternV2(XComponentNodeType nodeType) : XCompone void XComponentPatternV2::InitParams(XComponentType type) { - if(type!=XComponentType::UNKNOWN) { + if (type_ != XComponentType::UNKNOWN) { return; } - type_=type; - if(isCNode_) { + type_ = type; + if (isCNode_) { id_ = ""; SetLibraryName(""); InitNativeXComponent(); diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h index ffff5a6b3ac..ec7b8529c69 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h @@ -19,6 +19,7 @@ #include #include #include "interfaces/native/native_interface_xcomponent.h" +#include "base/error/error_code.h" struct OH_ArkUI_SurfaceCallback { /** Called when the surface is created. */ diff --git a/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp b/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp index 949537c18d4..735e710a422 100644 --- a/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp +++ b/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp @@ -16,7 +16,7 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" +// #include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" #include "core/components_ng/base/view_abstract.h" namespace OHOS::Ace::NG { @@ -354,7 +354,7 @@ void* CreateSurfaceHolder(ArkUINodeHandle node) { auto* frameNode = reinterpret_cast(node); CHECK_NULL_RETURN(frameNode, nullptr); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, nullptr); if (xcPattern->IsCreateSurfaceHolderForbidden()) { return nullptr; @@ -371,7 +371,7 @@ void Dispose(ArkUINodeHandle node) { auto* frameNode = reinterpret_cast(node); CHECK_NULL_VOID(frameNode); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_VOID(xcPattern); xcPattern->SetSurfaceHolder(nullptr); } @@ -380,7 +380,7 @@ ArkUI_Int32 SetAutoInitialize(ArkUINodeHandle node, ArkUI_Bool autoInitialize) { auto* frameNode = reinterpret_cast(node); CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); auto nodeType = xcPattern->GetXComponentNodeType(); if (nodeType != XComponentNodeType::TYPE_NODE && nodeType != XComponentNodeType::CNODE) { @@ -393,7 +393,7 @@ ArkUI_Int32 Initialize(ArkUINodeHandle node) { auto* frameNode = reinterpret_cast(node); CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); auto nodeType = xcPattern->GetXComponentNodeType(); if (nodeType != XComponentNodeType::TYPE_NODE && nodeType != XComponentNodeType::CNODE) { @@ -406,7 +406,7 @@ ArkUI_Int32 IsInitialized(ArkUINodeHandle node, ArkUI_Bool* isInitialized) { auto* frameNode = reinterpret_cast(node); CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); auto nodeType = xcPattern->GetXComponentNodeType(); if (nodeType != XComponentNodeType::TYPE_NODE && nodeType != XComponentNodeType::CNODE) { @@ -422,7 +422,7 @@ ArkUI_Int32 Finalize(ArkUINodeHandle node) { auto* frameNode = reinterpret_cast(node); CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); - auto xcPattern = frameNode->GetPattern(); + auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); auto nodeType = xcPattern->GetXComponentNodeType(); if (nodeType != XComponentNodeType::TYPE_NODE && nodeType != XComponentNodeType::CNODE) { -- Gitee From edd87b3edea74d1c1c6f23752736b578cc176a9d Mon Sep 17 00:00:00 2001 From: sunbees Date: Thu, 3 Jul 2025 22:33:09 +0800 Subject: [PATCH 3/3] xcomponent refactoring_3 Signed-off-by: sunbees --- .../native_interface_xcomponent_impl.cpp | 2 +- .../native_interface_xcomponent_impl.h | 3 +- .../components_ng/pattern/xcomponent/BUILD.gn | 6 +- .../xcomponent_accessibility_provider.cpp | 16 +- .../xcomponent/xcomponent_model_ng.cpp | 10 +- .../pattern/xcomponent/xcomponent_pattern.cpp | 798 +++++------------- .../pattern/xcomponent/xcomponent_pattern.h | 208 ++--- .../xcomponent/xcomponent_pattern_v2.cpp | 732 ---------------- .../xcomponent_proxy/xcomponent_proxy.cpp | 112 +++ .../xcomponent_proxy/xcomponent_proxy.h | 74 ++ .../xcomponent_proxy_native_xcomponent.cpp | 304 +++++++ .../xcomponent_proxy_native_xcomponent.h | 88 ++ .../xcomponent_proxy_surface_holder.cpp | 187 ++++ .../xcomponent_proxy_surface_holder.h} | 128 +-- .../native/node/node_xcomponent_modifier.cpp | 15 +- test/unittest/BUILD.gn | 4 +- test/unittest/core/pattern/web/BUILD.gn | 1 - .../unittest/core/pattern/xcomponent/BUILD.gn | 1 - .../xcomponent_property_test_ng.cpp | 1 - .../pattern/xcomponent/xcomponent_test_ng.cpp | 13 +- .../xcomponent/xcomponent_testtwo_ng.cpp | 28 +- .../xcomponent/xcomponent_v2_test_ng.cpp | 1 - 22 files changed, 1132 insertions(+), 1600 deletions(-) delete mode 100644 frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp create mode 100644 frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.cpp create mode 100644 frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h create mode 100644 frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.cpp create mode 100644 frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h create mode 100644 frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.cpp rename frameworks/core/components_ng/pattern/xcomponent/{xcomponent_pattern_v2.h => xcomponent_proxy/xcomponent_proxy_surface_holder.h} (31%) diff --git a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp index 545fef8d2d1..71d18ca0052 100644 --- a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp +++ b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.cpp @@ -366,7 +366,7 @@ int32_t OH_NativeXComponent::GetAccessibilityProvider(ArkUI_AccessibilityProvide return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER; } - (*handle) = xcomponentImpl_->GetAccessbilityProvider().get(); + (*handle) = xcomponentImpl_->GetAccessibilityProvider().get(); return OH_NATIVEXCOMPONENT_RESULT_SUCCESS; } diff --git a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h index a15cd8a66b3..12fd4a36026 100644 --- a/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h +++ b/frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h @@ -79,6 +79,7 @@ class NativeXComponentImpl : public virtual AceType { public: NativeXComponentImpl() { + // can be created as needed accessbilityProvider_ = std::make_shared(); } @@ -300,7 +301,7 @@ public: return &keyEvent_; } - std::shared_ptr GetAccessbilityProvider() + std::shared_ptr GetAccessibilityProvider() { return accessbilityProvider_; } diff --git a/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn b/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn index cb9180219f9..906c200e53a 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn +++ b/frameworks/core/components_ng/pattern/xcomponent/BUILD.gn @@ -21,13 +21,15 @@ build_component_ng("xcomponent_pattern_ng") { "xcomponent_accessibility_session_adapter.cpp", "xcomponent_controller_ng.cpp", "xcomponent_ext_surface_callback_client.cpp", + "xcomponent_inner_surface_controller.cpp", "xcomponent_layout_algorithm.cpp", "xcomponent_model_ng.cpp", "xcomponent_paint_method.cpp", "xcomponent_pattern.cpp", - # "xcomponent_pattern_v2.cpp", + "xcomponent_proxy/xcomponent_proxy.cpp", + "xcomponent_proxy/xcomponent_proxy_native_xcomponent.cpp", + "xcomponent_proxy/xcomponent_proxy_surface_holder.cpp", "xcomponent_utils.cpp", - "xcomponent_inner_surface_controller.cpp", ] if (is_ohos_standard_system) { diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_accessibility_provider.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_accessibility_provider.cpp index 5cf06063f6b..9498fd043c4 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_accessibility_provider.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_accessibility_provider.cpp @@ -24,7 +24,7 @@ namespace OHOS::Ace::NG { namespace { constexpr int32_t SEND_EVENT_FAILED = -1; -ArkUI_AccessibilityProvider* GetAccessbilityProvider( +ArkUI_AccessibilityProvider* GetAccessibilityProvider( const WeakPtr& weakPattern) { auto pattern = weakPattern.Upgrade(); @@ -37,7 +37,7 @@ int32_t XComponentAccessibilityProvider::FindAccessibilityNodeInfosById( std::vector& infos) { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->FindAccessibilityNodeInfosById( elementId, mode, requestId, infos); @@ -50,7 +50,7 @@ int32_t XComponentAccessibilityProvider::FindAccessibilityNodeInfosByText( std::vector& infos) { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->FindAccessibilityNodeInfosByText( elementId, text, requestId, infos); @@ -63,7 +63,7 @@ int32_t XComponentAccessibilityProvider::FindFocusedAccessibilityNode( ArkUI_AccessibilityElementInfo& info) { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->FindFocusedAccessibilityNode( elementId, focusType, requestId, info); @@ -76,7 +76,7 @@ int32_t XComponentAccessibilityProvider::FindNextFocusAccessibilityNode( ArkUI_AccessibilityElementInfo& info) { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->FindNextFocusAccessibilityNode( elementId, direction, requestId, info); @@ -89,7 +89,7 @@ int32_t XComponentAccessibilityProvider::ExecuteAccessibilityAction( const std::map& actionArguments) { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->ExecuteAccessibilityAction( elementId, action, requestId, actionArguments); @@ -100,7 +100,7 @@ int32_t XComponentAccessibilityProvider::ExecuteAccessibilityAction( int32_t XComponentAccessibilityProvider::ClearFocusedAccessibilityNode() { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->ClearFocusedAccessibilityNode(); CHECK_EQUAL_RETURN(ret, AccessibilityProviderOperatorErrorCode::NOT_REGISTERED, errorCode); @@ -111,7 +111,7 @@ int32_t XComponentAccessibilityProvider::GetAccessibilityNodeCursorPosition( const int64_t elementId, const int32_t requestId, int32_t &cursorPosition) { int32_t errorCode = 0; - auto accessbilityProvider = GetAccessbilityProvider(weakPattern_); + auto accessbilityProvider = GetAccessibilityProvider(weakPattern_); CHECK_NULL_RETURN(accessbilityProvider, errorCode); int32_t ret = accessbilityProvider->GetAccessibilityNodeCursorPosition( elementId, requestId, cursorPosition); diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp index 166d042b4a8..98f12dcb572 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp @@ -643,7 +643,7 @@ int32_t XComponentModelNG::SetExpectedRateRange(FrameNode* frameNode, int32_t mi CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); - if (xcPattern->HasGotNativeXComponent()) { + if (xcPattern->IsUsingNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; } xcPattern->SetExpectedRateRange(min, max, expected); @@ -656,7 +656,7 @@ int32_t XComponentModelNG::SetOnFrameCallback(FrameNode* frameNode, CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); - if (xcPattern->HasGotNativeXComponent()) { + if (xcPattern->IsUsingNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; } xcPattern->UpdateOnFrameEvent(callback, arkuiNode); @@ -668,7 +668,7 @@ int32_t XComponentModelNG::UnregisterOnFrameCallback(FrameNode* frameNode) CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); - if (xcPattern->HasGotNativeXComponent()) { + if (xcPattern->IsUsingNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; } xcPattern->UpdateOnFrameEvent(nullptr, nullptr); @@ -681,7 +681,7 @@ int32_t XComponentModelNG::SetNeedSoftKeyboard(FrameNode* frameNode, bool needSo CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, ERROR_CODE_PARAM_INVALID); - if (xcPattern->HasGotNativeXComponent()) { + if (xcPattern->IsUsingNativeXComponent()) { return ERROR_CODE_PARAM_INVALID; } xcPattern->SetNeedSoftKeyboard(needSoftKeyboard); @@ -693,7 +693,7 @@ void* XComponentModelNG::CreateAccessibilityProvider(FrameNode* frameNode) CHECK_NULL_RETURN(frameNode, nullptr); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, nullptr); - if (xcPattern->HasGotNativeXComponent()) { + if (xcPattern->IsUsingNativeXComponent()) { return nullptr; } return xcPattern->CreateAccessibilityProvider(); diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp index e9bb47beeaf..ba4ec5dad63 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp @@ -20,6 +20,7 @@ #include #include "interfaces/native/event/ui_input_event_impl.h" +#include "interfaces/native/native_interface_accessibility.h" #include "interfaces/native/native_interface_xcomponent.h" #include "interfaces/native/ui_input_event.h" #include "ui/base/utils/utils.h" @@ -39,6 +40,8 @@ #include "core/components/common/layout/constants.h" #include "core/components_ng/event/gesture_event_hub.h" #include "core/components_ng/pattern/xcomponent/xcomponent_controller_ng.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.h" #include "core/event/axis_event.h" #ifdef NG_BUILD #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h" @@ -105,16 +108,14 @@ void XComponentPattern::InitParams(const std::optional& id, XCompon if (id == std::nullopt && xcomponentController == nullptr && (type == XComponentType::SURFACE || type == XComponentType::TEXTURE) && nodeType_ != XComponentNodeType::CNODE) { - // ProcessV2 - InitV2(); - isV2_ = true; + InitForSurfaceHolder(); } else { initSize_ = SizeF { initWidth, initHeight }; // only for XComponentNode which is deprecated - InitV1(); + InitForNonSurfaceHolder(); } } -void XComponentPattern::InitV1() +void XComponentPattern::InitForNonSurfaceHolder() { if (type_ == XComponentType::NODE) { auto host = GetHost(); @@ -123,6 +124,10 @@ void XComponentPattern::InitV1() focusHub->SetFocusType(FocusType::SCOPE); focusHub->SetFocusable(true); } + xcomponentProxy_ = MakeRefPtr(WeakClaim(this)); + if (nodeType_ == XComponentNodeType::CNODE) { + xcomponentProxy_->SetToggleEnabled(true); + } if (nodeType_ != XComponentNodeType::TYPE_NODE) { InitNativeXComponent(); } @@ -137,11 +142,14 @@ void XComponentPattern::InitV1() UnregisterOnFrameEventInit(); InitEvent(); InitController(); - } else if (type_ == XComponentType::NODE && id_.has_value()) { + } else if (type_ == XComponentType::NODE) { auto host = GetHost(); CHECK_NULL_VOID(host); + auto focusHub = host->GetOrCreateFocusHub(); + focusHub->SetFocusType(FocusType::SCOPE); + focusHub->SetFocusable(true); auto context = host->GetContextRefPtr(); - if (context) { + if (id_.has_value() && context) { FireExternalEvent(context, id_.value(), host->GetId(), false); InitNativeNodeCallbacks(); } @@ -149,87 +157,17 @@ void XComponentPattern::InitV1() if (nodeType_ != XComponentNodeType::TYPE_NODE) { InitializeAccessibility(); } - - RegisterCallbackV1(); } -void XComponentPattern::InitV2() +void XComponentPattern::InitForSurfaceHolder() { + xcomponentProxy_ = MakeRefPtr(WeakClaim(this)); InitSurface(); renderSurface_->RegisterSurface(); InitNativeWindow(paintRect_.Width(), paintRect_.Height()); - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); - } - RegisterCallbackV2(); -} - -void XComponentPattern::RegisterCallbackV1() { - onAttachToMainTree_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - if (xcPattern->GetNodeType() == XComponentNodeType::TYPE_NODE && - xcPattern->GetSurfaceCallbackMode() == SurfaceCallbackMode::DEFAULT) { - xcPattern->HandleSurfaceCreated(); - } - }; - onDetachFromMainTree_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - if (xcPattern->GetNodeType() == XComponentNodeType::TYPE_NODE && - xcPattern->GetSurfaceCallbackMode() == SurfaceCallbackMode::DEFAULT) { - xcPattern->HandleSurfaceDestroyed(); - } - }; - onSizeChange_ = [weak = WeakClaim(this)]( - bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - xcPattern->OnSizeChangeV1(offsetChanged, sizeChanged, frameOffsetChange, needFireNativeEvent); - }; - onWindowShow_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - xcPattern->NativeSurfaceShow(); - }; - onWindowHide_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - xcPattern->NativeSurfaceHide(); - }; -} - -void XComponentPattern::RegisterCallbackV2() { - onAttachToMainTree_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - if (xcPattern->IsAutoInitialize()) { - xcPattern->HandleSurfaceCreatedV2(); - } - }; - onDetachFromMainTree_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - if (xcPattern->IsAutoInitialize()) { - xcPattern->HandleSurfaceDestroyedV2(); - } - }; - onSizeChange_ = [weak = WeakClaim(this)]([[maybe_unused]] bool offsetChanged, bool sizeChanged, - [[maybe_unused]] bool frameOffsetChange, [[maybe_unused]] bool needFireNativeEvent) { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - xcPattern->HandleSurfaceChangeEventV2(sizeChanged); - }; - onWindowShow_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - xcPattern->OnSurfaceShowV2(); - }; - onWindowHide_ = [weak = WeakClaim(this)]() { - auto xcPattern = weak.Upgrade(); - CHECK_NULL_VOID(xcPattern); - xcPattern->OnSurfaceHideV2(); - }; + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxySurfaceHolder); + xcomponentProxySurfaceHolder->SetNativeWindow(nativeWindow_); } void XComponentPattern::ReinitializeSurfaceProperties() @@ -257,7 +195,7 @@ void XComponentPattern::ReinitializeSurfaceProperties() needReinitialize_ = false; } -void XComponentPattern::OnSizeChangeV1( +void XComponentPattern::OnSizeChange( bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) { if (!hasXComponentInit_) { @@ -336,8 +274,8 @@ void XComponentPattern::InitNativeXComponent() { if ((type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) && libraryname_.has_value()) { isNativeXComponent_ = true; - nativeXComponentImpl_ = AceType::MakeRefPtr(); - nativeXComponent_ = std::make_shared(AceType::RawPtr(nativeXComponentImpl_)); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + xcomponentProxyNativeXComponent->GetOrCreateNativeXComponent(); } } @@ -441,34 +379,11 @@ void XComponentPattern::RegisterTransformHintCallback(PipelineContext* context) UpdateTransformHintChangedCallbackId(callbackId); } -// void XComponentPattern::Initialize() -// { -// if (type_ == XComponentType::SURFACE || type_ == XComponentType::TEXTURE) { -// InitSurface(); -// InitEvent(); -// InitController(); -// } else if (type_ == XComponentType::NODE && id_.has_value()) { -// auto host = GetHost(); -// CHECK_NULL_VOID(host); -// auto context = host->GetContextRefPtr(); -// if (context) { -// FireExternalEvent(context, id_.value(), host->GetId(), false); -// InitNativeNodeCallbacks(); -// } -// } -// if (nodeType_ != XComponentNodeType::TYPE_NODE) { -// InitializeAccessibility(); -// } -// } - void XComponentPattern::OnAttachToMainTree() { TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] AttachToMainTree", GetId().c_str()); ACE_SCOPED_TRACE("XComponent[%s] AttachToMainTree", GetId().c_str()); isOnTree_ = true; - // if (nodeType_ == XComponentNodeType::TYPE_NODE && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) { - // HandleSurfaceCreated(); - // } auto host = GetHost(); CHECK_NULL_VOID(host); if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { @@ -479,8 +394,8 @@ void XComponentPattern::OnAttachToMainTree() needRecoverDisplaySync_ = false; } } - if(onAttachToMainTree_) { - onAttachToMainTree_(); + if (xcomponentProxy_) { + xcomponentProxy_->OnAttachToMainTree(); } } @@ -489,9 +404,6 @@ void XComponentPattern::OnDetachFromMainTree() TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] DetachFromMainTree", GetId().c_str()); ACE_SCOPED_TRACE("XComponent[%s] DetachFromMainTree", GetId().c_str()); isOnTree_ = false; - // if (nodeType_ == XComponentNodeType::TYPE_NODE && surfaceCallbackMode_ == SurfaceCallbackMode::DEFAULT) { - // HandleSurfaceDestroyed(); - // } auto host = GetHost(); CHECK_NULL_VOID(host); if (host->GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { @@ -502,8 +414,8 @@ void XComponentPattern::OnDetachFromMainTree() needRecoverDisplaySync_ = true; } } - if (onDetachFromMainTree_) { - onDetachFromMainTree_(); + if (xcomponentProxy_) { + xcomponentProxy_->OnDetachFromMainTree(); } } @@ -579,14 +491,6 @@ void XComponentPattern::RequestFocus() } #endif -void XComponentPattern::OnAttachToFrameNode() -{ - // Initialize(); - // if (FrameReport::GetInstance().GetEnable()) { - // FrameReport::GetInstance().EnableSelfRender(); - // } -} - void XComponentPattern::OnModifyDone() { Pattern::OnModifyDone(); @@ -822,8 +726,8 @@ void XComponentPattern::BeforeSyncGeometryProperties(const DirtySwapConfig& conf UpdateAnalyzerUIConfig(geometryNode); } const auto& [offsetChanged, sizeChanged, needFireNativeEvent] = UpdateSurfaceRect(); - if (onSizeChange_) { - onSizeChange_(offsetChanged, sizeChanged, needFireNativeEvent, config.frameOffsetChange); + if (xcomponentProxy_) { + xcomponentProxy_->OnSizeChange(offsetChanged, sizeChanged, needFireNativeEvent, config.frameOffsetChange); } host->MarkNeedSyncRenderTree(); } @@ -850,33 +754,25 @@ void XComponentPattern::DumpAdvanceInfo() } } +std::pair, std::weak_ptr> +XComponentPattern::GetNativeXComponent() +{ + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_RETURN(xcomponentProxyNativeXComponent, {}); + return xcomponentProxyNativeXComponent->GetOrCreateNativeXComponent(); +} + void XComponentPattern::NativeXComponentOffset(double x, double y) { CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); auto host = GetHost(); CHECK_NULL_VOID(host); auto pipelineContext = host->GetContextRefPtr(); CHECK_NULL_VOID(pipelineContext); float scale = pipelineContext->GetViewScale(); - nativeXComponentImpl_->SetXComponentOffsetX(x * scale); - nativeXComponentImpl_->SetXComponentOffsetY(y * scale); -} - -void XComponentPattern::NativeXComponentDispatchTouchEvent( - const OH_NativeXComponent_TouchEvent& touchEvent, const std::vector& xComponentTouchPoints) -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - nativeXComponentImpl_->SetTouchEvent(touchEvent); - nativeXComponentImpl_->SetTouchPoint(xComponentTouchPoints); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto* callback = nativeXComponentImpl_->GetCallback(); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(callback->DispatchTouchEvent); - callback->DispatchTouchEvent(nativeXComponent_.get(), surface); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->SetNativeXComponentOffset(x * scale, y * scale); } void XComponentPattern::InitNativeWindow(float textureWidth, float textureHeight) @@ -934,7 +830,6 @@ void XComponentPattern::XComponentSizeChange(const RectF& surfaceRect, bool need CHECK_NULL_VOID(host); renderSurface_->UpdateSurfaceSizeInUserData( static_cast(surfaceRect.Width()), static_cast(surfaceRect.Height())); - // In declarative mode: Native onSurfaceCreated callback is triggred // when the component finish it's first layout, so do not trigger the native onSurfaceChanged callback if (nodeType_ != XComponentNodeType::TYPE_NODE && isNativeXComponent_ && !needFireNativeEvent) { @@ -942,7 +837,10 @@ void XComponentPattern::XComponentSizeChange(const RectF& surfaceRect, bool need } // When creating the surface for the first time, needFireNativeEvent = false, other time needFireNativeEvent = true // the first time change size no need to resize nativeWindow - OnSurfaceChanged(surfaceRect, needFireNativeEvent); + if (needFireNativeEvent) { + AdjustNativeWindowSize(surfaceRect.Width(), surfaceRect.Height()); + } + OnSurfaceChanged(surfaceRect); } RefPtr XComponentPattern::GetAccessibilitySessionAdapter() @@ -952,55 +850,22 @@ RefPtr XComponentPattern::GetAccessibilitySessionAd void XComponentPattern::InitializeAccessibility() { - if (accessibilityChildTreeCallback_) { - return; - } - - InitializeAccessibilityCallback(); - auto host = GetHost(); - CHECK_NULL_VOID(host); - int64_t accessibilityId = host->GetAccessibilityId(); - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, - "InitializeAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId); - auto pipeline = host->GetContextRefPtr(); - CHECK_NULL_VOID(pipeline); - auto accessibilityManager = pipeline->GetAccessibilityManager(); - CHECK_NULL_VOID(accessibilityManager); - accessibilityChildTreeCallback_ = std::make_shared( - WeakClaim(this), host->GetAccessibilityId()); - accessibilityManager->RegisterAccessibilityChildTreeCallback( - accessibilityId, accessibilityChildTreeCallback_); - if (accessibilityManager->IsRegister()) { - accessibilityChildTreeCallback_->OnRegister( - pipeline->GetWindowId(), accessibilityManager->GetTreeId()); + if (xcomponentProxy_) { + xcomponentProxy_->InitializeAccessibility(); } } void XComponentPattern::UninitializeAccessibility(FrameNode* frameNode) { - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "UninitializeAccessibility"); - CHECK_NULL_VOID(frameNode); - int64_t accessibilityId = frameNode->GetAccessibilityId(); - auto pipeline = frameNode->GetContextRefPtr(); - CHECK_NULL_VOID(pipeline); - auto accessibilityManager = pipeline->GetAccessibilityManager(); - CHECK_NULL_VOID(accessibilityManager); - if (accessibilityManager->IsRegister() && accessibilityChildTreeCallback_) { - accessibilityChildTreeCallback_->OnDeregister(); + if (xcomponentProxy_) { + xcomponentProxy_->UninitializeAccessibility(frameNode); } - accessibilityManager->DeregisterAccessibilityChildTreeCallback(accessibilityId); - accessibilityChildTreeCallback_ = nullptr; } ArkUI_AccessibilityProvider* XComponentPattern::GetNativeProvider() { - if(useNodeHandleAccessibilityProvider_) { - return arkuiAccessibilityProvider_; - } - auto pair = GetNativeXComponent(); - auto nativeXComponentImpl = pair.first; - CHECK_NULL_RETURN(nativeXComponentImpl, nullptr); - return nativeXComponentImpl->GetAccessbilityProvider().get(); + CHECK_NULL_RETURN(xcomponentProxy_, nullptr); + return xcomponentProxy_->GetAccessibilityProvider(); } bool XComponentPattern::OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId) @@ -1051,7 +916,7 @@ bool XComponentPattern::OnAccessibilityChildTreeDeregister() CHECK_NULL_RETURN(pipeline, false); auto accessibilityManager = pipeline->GetAccessibilityManager(); CHECK_NULL_RETURN(accessibilityManager, false); - auto nativeProvider = GetNativeProvider(); + auto* nativeProvider = GetNativeProvider(); CHECK_NULL_RETURN(nativeProvider, false); nativeProvider->SetInnerAccessibilityProvider(nullptr); accessibilitySessionAdapter_ = nullptr; @@ -1074,72 +939,13 @@ void XComponentPattern::OnSetAccessibilityChildTree( accessibilityProperty->SetChildTreeId(childTreeId); } -void XComponentPattern::InitializeAccessibilityCallback() -{ - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeAccessibilityCallback"); - CHECK_NULL_VOID(nativeXComponentImpl_); - auto nativeProvider = nativeXComponentImpl_->GetAccessbilityProvider(); - CHECK_NULL_VOID(nativeProvider); - nativeProvider->SetRegisterCallback( - [weak = WeakClaim(this)] (bool isRegister) { - auto pattern = weak.Upgrade(); - CHECK_NULL_VOID(pattern); - pattern->HandleRegisterAccessibilityEvent(isRegister); - }); -} - -void XComponentPattern::HandleRegisterAccessibilityEvent(bool isRegister) -{ - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "HandleRegisterAccessibilityEvent, " - "isRegister: %{public}d.", isRegister); - CHECK_NULL_VOID(accessibilityChildTreeCallback_); - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto pipeline = host->GetContextRefPtr(); - CHECK_NULL_VOID(pipeline); - auto accessibilityManager = pipeline->GetAccessibilityManager(); - CHECK_NULL_VOID(accessibilityManager); - if (!isRegister) { - accessibilityChildTreeCallback_->OnDeregister(); - return; - } - - if (accessibilityManager->IsRegister()) { - accessibilityChildTreeCallback_->OnRegister( - pipeline->GetWindowId(), accessibilityManager->GetTreeId()); - } -} - void XComponentPattern::InitNativeNodeCallbacks() { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - auto host = GetHost(); CHECK_NULL_VOID(host); - nativeXComponentImpl_->registerContaner(AceType::RawPtr(host)); - - auto OnAttachRootNativeNode = [](void* container, void* root) { - ContainerScope scope(Container::CurrentIdSafely()); - auto node = AceType::Claim(reinterpret_cast(root)); - CHECK_NULL_VOID(node); - auto host = AceType::Claim(reinterpret_cast(container)); - CHECK_NULL_VOID(host); - host->AddChild(node); - host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE); - }; - - auto OnDetachRootNativeNode = [](void* container, void* root) { - ContainerScope scope(Container::CurrentIdSafely()); - auto node = AceType::Claim(reinterpret_cast(root)); - CHECK_NULL_VOID(node); - auto host = AceType::Claim(reinterpret_cast(container)); - CHECK_NULL_VOID(host); - host->RemoveChild(node); - }; - - nativeXComponentImpl_->registerNativeNodeCallbacks( - std::move(OnAttachRootNativeNode), std::move(OnDetachRootNativeNode)); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->RegisterNodeCallback(AceType::RawPtr(host)); } void XComponentPattern::InitEvent() @@ -1195,41 +1001,23 @@ void XComponentPattern::InitFocusEvent(const RefPtr& focusHub) } void XComponentPattern::HandleFocusEvent() { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto focusEventCallback = nativeXComponentImpl_->GetFocusEventCallback(); - CHECK_NULL_VOID(focusEventCallback); - focusEventCallback(nativeXComponent_.get(), surface); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchFocusEvent(); } bool XComponentPattern::HandleKeyEvent(const KeyEvent& event) { - CHECK_NULL_RETURN(nativeXComponent_, false); - CHECK_NULL_RETURN(nativeXComponentImpl_, false); - - OH_NativeXComponent_KeyEvent keyEvent = XComponentUtils::ConvertNativeXComponentKeyEvent(event); - nativeXComponentImpl_->SetKeyEvent(keyEvent); - - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto keyEventCallbackWithResult = nativeXComponentImpl_->GetKeyEventCallbackWithResult(); - if (keyEventCallbackWithResult) { - return keyEventCallbackWithResult(nativeXComponent_.get(), surface); - } - const auto keyEventCallback = nativeXComponentImpl_->GetKeyEventCallback(); - CHECK_NULL_RETURN(keyEventCallback, false); - keyEventCallback(nativeXComponent_.get(), surface); - return false; + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_RETURN(xcomponentProxyNativeXComponent, false); + return xcomponentProxyNativeXComponent->DispatchKeyEvent(event); } void XComponentPattern::HandleBlurEvent() { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto blurEventCallback = nativeXComponentImpl_->GetBlurEventCallback(); - CHECK_NULL_VOID(blurEventCallback); - blurEventCallback(nativeXComponent_.get(), surface); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchBlurEvent(); } void XComponentPattern::InitTouchEvent(const RefPtr& gestureHub) @@ -1270,13 +1058,10 @@ void XComponentPattern::InitOnTouchIntercept(const RefPtr& gest CHECK_NULL_RETURN(pattern, NG::HitTestMode::HTMDEFAULT); auto hostNode = pattern->GetHost(); CHECK_NULL_RETURN(hostNode, NG::HitTestMode::HTMDEFAULT); - CHECK_NULL_RETURN(pattern->nativeXComponentImpl_, hostNode->GetHitTestMode()); - const auto onTouchInterceptCallback = pattern->nativeXComponentImpl_->GetOnTouchInterceptCallback(); - CHECK_NULL_RETURN(onTouchInterceptCallback, hostNode->GetHitTestMode()); - auto event = touchEvent.ConvertToTouchEvent(); - ArkUI_UIInputEvent uiEvent { ARKUI_UIINPUTEVENT_TYPE_TOUCH, TOUCH_EVENT_ID, &event, false, - Container::GetCurrentApiTargetVersion() }; - return static_cast(onTouchInterceptCallback(pattern->nativeXComponent_.get(), &uiEvent)); + auto xcomponentProxyNativeXComponent = DynamicCast(pattern->xcomponentProxy_); + auto defaultHitTestMode = hostNode->GetHitTestMode(); + CHECK_NULL_RETURN(xcomponentProxyNativeXComponent, defaultHitTestMode); + return xcomponentProxyNativeXComponent->InitOnTouchIntercept(touchEvent, defaultHitTestMode); }); } @@ -1338,14 +1123,15 @@ void XComponentPattern::HandleTouchEvent(const TouchEventInfo& info) localOffset.GetX(), localOffset.GetY(), touchInfo.GetFingerId(), touchInfoList.front().GetTouchType(), static_cast(touchInfo.GetSize())); SetTouchPoint(info.GetTouches(), timeStamp, touchType); - - if (nativeXComponent_ && nativeXComponentImpl_) { - nativeXComponentImpl_->SetHistoricalPoint(SetHistoryPoint(info.GetHistory())); - nativeXComponentImpl_->SetCurrentSourceType({ touchInfo.GetFingerId(), - XComponentUtils::ConvertNativeXComponentEventSourceType(info.GetSourceDevice()) }); + { + CHECK_RUN_ON(UI); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchTouchEvent(touchEventPoint_, nativeXComponentTouchPoints_, + SetHistoryPoint(info.GetHistory()), + { touchInfo.GetFingerId(), + XComponentUtils::ConvertNativeXComponentEventSourceType(info.GetSourceDevice()) }); } - NativeXComponentDispatchTouchEvent(touchEventPoint_, nativeXComponentTouchPoints_); - #ifdef RENDER_EXTRACT_SUPPORTED if (touchType == TouchType::DOWN) { RequestFocus(); @@ -1365,51 +1151,31 @@ void XComponentPattern::HandleMouseEvent(const MouseInfo& info) mouseEventPoint.timestamp = info.GetTimeStamp().time_since_epoch().count(); OH_NativeXComponent_ExtraMouseEventInfo extraMouseEventInfo; extraMouseEventInfo.modifierKeyStates = CalculateModifierKeyState(info.GetPressedKeyCodes()); - NativeXComponentDispatchMouseEvent(mouseEventPoint, extraMouseEventInfo); + { + CHECK_RUN_ON(UI); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchMouseEvent(mouseEventPoint, extraMouseEventInfo); + } } void XComponentPattern::HandleAxisEvent(const AxisInfo& info) { auto axisEvent = info.ConvertToAxisEvent(); - NativeXComponentDispatchAxisEvent(&axisEvent); + { + CHECK_RUN_ON(UI); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchAxisEvent(&axisEvent); + } } void XComponentPattern::HandleMouseHoverEvent(bool isHover) { CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - const auto* callback = nativeXComponentImpl_->GetMouseEventCallback(); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(callback->DispatchHoverEvent); - callback->DispatchHoverEvent(nativeXComponent_.get(), isHover); -} - -void XComponentPattern::NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent, - const OH_NativeXComponent_ExtraMouseEventInfo& extraMouseEventInfo) -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - nativeXComponentImpl_->SetMouseEvent(mouseEvent); - nativeXComponentImpl_->SetExtraMouseEventInfo(extraMouseEventInfo); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto* callback = nativeXComponentImpl_->GetMouseEventCallback(); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(callback->DispatchMouseEvent); - callback->DispatchMouseEvent(nativeXComponent_.get(), surface); -} - -void XComponentPattern::NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent) -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - const auto callback = nativeXComponentImpl_->GetUIAxisEventCallback(); - CHECK_NULL_VOID(callback); - ArkUI_UIInputEvent uiEvent { ARKUI_UIINPUTEVENT_TYPE_AXIS, AXIS_EVENT_ID, axisEvent, false, - Container::GetCurrentApiTargetVersion() }; - callback(nativeXComponent_.get(), &uiEvent, ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchMouseHoverEvent(isHover); } void XComponentPattern::SetTouchPoint( @@ -1577,10 +1343,10 @@ XComponentControllerErrorCode XComponentPattern::ResetExtController(const RefPtr void XComponentPattern::HandleSetExpectedRateRangeEvent() { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); CHECK_NULL_VOID(displaySync_); - OH_NativeXComponent_ExpectedRateRange* range = nativeXComponentImpl_->GetRateRange(); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + OH_NativeXComponent_ExpectedRateRange* range = xcomponentProxyNativeXComponent->GetRateRange(); CHECK_NULL_VOID(range); FrameRateRange frameRateRange; frameRateRange.Set(range->min, range->max, range->expected); @@ -1597,29 +1363,26 @@ void XComponentPattern::HandleSetExpectedRateRangeEvent() void XComponentPattern::HandleOnFrameEvent() { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); CHECK_NULL_VOID(displaySync_); displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this)](RefPtr displaySyncData) { + CHECK_NULL_VOID(displaySyncData); auto xComponentPattern = weak.Upgrade(); CHECK_NULL_VOID(xComponentPattern); - CHECK_NULL_VOID(xComponentPattern->nativeXComponentImpl_->GetOnFrameCallback()); - xComponentPattern->nativeXComponentImpl_->GetOnFrameCallback()(xComponentPattern->nativeXComponent_.get(), + auto xcomponentProxyNativeXComponent = + DynamicCast(xComponentPattern->xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchOnFrameEvent( displaySyncData->GetTimestamp(), displaySyncData->GetTargetTimestamp()); }); - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " RegisterOnFrame", - displaySync_->GetId()); + TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " RegisterOnFrame", displaySync_->GetId()); displaySync_->AddToPipelineOnContainer(); } void XComponentPattern::HandleUnregisterOnFrameEvent() { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); CHECK_NULL_VOID(displaySync_); displaySync_->UnregisterOnFrame(); - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame", - displaySync_->GetId()); + TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame", displaySync_->GetId()); displaySync_->DelFromPipelineOnContainer(); needRecoverDisplaySync_ = false; } @@ -1785,13 +1548,13 @@ void XComponentPattern::HandleSurfaceChangeEvent( } } -void XComponentPattern::HandleSurfaceChangeEventV2(bool sizeChanged) +void XComponentPattern::HandleSurfaceChangeEventForSurfaceHolder(bool sizeChanged) { if (!drawSize_.IsPositive()) { return; } if (sizeChanged) { - XComponentSizeChangeV2(paintRect_); + XComponentSizeChangeForSurfaceHolder(paintRect_); } if (renderContextForSurface_) { renderContextForSurface_->SetBounds( @@ -1803,7 +1566,7 @@ void XComponentPattern::HandleSurfaceChangeEventV2(bool sizeChanged) } } -void XComponentPattern::XComponentSizeChangeV2(const RectF& surfaceRect) +void XComponentPattern::XComponentSizeChangeForSurfaceHolder(const RectF& surfaceRect) { auto host = GetHost(); CHECK_NULL_VOID(host); @@ -1818,7 +1581,7 @@ void XComponentPattern::XComponentSizeChangeV2(const RectF& surfaceRect) static_cast(width), static_cast(height)); renderSurface_->AdjustNativeWindowSize( static_cast(width * viewScale), static_cast(height * viewScale)); - OnSurfaceChangedV2(surfaceRect); + OnSurfaceChangedForSurfaceHolder(surfaceRect); } std::tuple XComponentPattern::UpdateSurfaceRect() @@ -1888,17 +1651,11 @@ void XComponentPattern::OnSurfaceCreated() auto width = initSize_.Width(); auto height = initSize_.Height(); if (isNativeXComponent_) { - CHECK_NULL_VOID(nativeXComponentImpl_); - CHECK_NULL_VOID(nativeXComponent_); - nativeXComponentImpl_->SetXComponentWidth(static_cast(width)); - nativeXComponentImpl_->SetXComponentHeight(static_cast(height)); - nativeXComponentImpl_->SetSurface(nativeWindow_); - const auto* callback = nativeXComponentImpl_->GetCallback(); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(callback->OnSurfaceCreated); TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] native OnSurfaceCreated", GetId().c_str()); ACE_SCOPED_TRACE("XComponent[%s] NativeSurfaceCreated[w:%f,h:%f]", GetId().c_str(), width, height); - callback->OnSurfaceCreated(nativeXComponent_.get(), nativeWindow_); + CHECK_NULL_VOID(xcomponentProxy_); + xcomponentProxy_->DispatchSurfaceCreatedEvent( + static_cast(width), static_cast(height), nativeWindow_); } else { auto host = GetHost(); CHECK_NULL_VOID(host); @@ -1908,29 +1665,17 @@ void XComponentPattern::OnSurfaceCreated() } } -void XComponentPattern::OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow) +void XComponentPattern::OnSurfaceChanged(const RectF& surfaceRect) { CHECK_RUN_ON(UI); auto host = GetHost(); CHECK_NULL_VOID(host); - auto width = surfaceRect.Width(); - auto height = surfaceRect.Height(); - if (needResizeNativeWindow) { - AdjustNativeWindowSize(width, height); - } if (isNativeXComponent_) { - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - nativeXComponentImpl_->SetXComponentWidth(static_cast(width)); - nativeXComponentImpl_->SetXComponentHeight(static_cast(height)); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto* callback = nativeXComponentImpl_->GetCallback(); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(callback->OnSurfaceChanged); - { - ACE_SCOPED_TRACE("XComponent[%s] native OnSurfaceChanged[w:%f,h:%f]", GetId().c_str(), width, height); - callback->OnSurfaceChanged(nativeXComponent_.get(), surface); - } + auto width = surfaceRect.Width(); + auto height = surfaceRect.Height(); + ACE_SCOPED_TRACE("XComponent[%s] native OnSurfaceChanged[w:%f,h:%f]", GetId().c_str(), width, height); + CHECK_NULL_VOID(xcomponentProxy_); + xcomponentProxy_->DispatchSurfaceChangedEvent(static_cast(width), static_cast(height)); } else { auto eventHub = host->GetOrCreateEventHub(); CHECK_NULL_VOID(eventHub); @@ -1938,19 +1683,14 @@ void XComponentPattern::OnSurfaceChanged(const RectF& surfaceRect, bool needResi } } -void XComponentPattern::OnSurfaceChangedV2(const RectF& surfaceRect) +void XComponentPattern::OnSurfaceChangedForSurfaceHolder(const RectF& surfaceRect) { CHECK_RUN_ON(UI); - CHECK_NULL_VOID(surfaceHolder_); - CHECK_EQUAL_VOID(isInitialized_, false); + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxySurfaceHolder); + CHECK_EQUAL_VOID(xcomponentProxySurfaceHolder->IsInitialized(), false); ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceChanged", GetId().c_str()); - auto callbackList = surfaceHolder_->surfaceCallbackList_; - for (const auto& iter : callbackList) { - auto callback = iter->OnSurfaceChanged; - if (callback) { - callback(surfaceHolder_, surfaceRect.Width(), surfaceRect.Height()); - } - } + xcomponentProxySurfaceHolder->DispatchSurfaceChangedEvent(surfaceRect.Width(), surfaceRect.Height()); needNotifySizeChanged_ = false; } @@ -1958,16 +1698,10 @@ void XComponentPattern::OnSurfaceDestroyed(FrameNode* frameNode) { if (isNativeXComponent_) { CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto* callback = nativeXComponentImpl_->GetCallback(); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(callback->OnSurfaceDestroyed); TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] native OnSurfaceDestroyed", GetId().c_str()); ACE_SCOPED_TRACE("XComponent[%s] native OnSurfaceDestroyed", GetId().c_str()); - callback->OnSurfaceDestroyed(nativeXComponent_.get(), surface); - nativeXComponentImpl_->SetSurface(nullptr); + CHECK_NULL_VOID(xcomponentProxy_); + xcomponentProxy_->DispatchSurfaceDestroyedEvent(); } else if (nodeType_ == XComponentNodeType::TYPE_NODE) { RefPtr host; if (!frameNode) { @@ -2017,30 +1751,20 @@ void XComponentPattern::HandleSurfaceCreated() OnSurfaceCreated(); } -int32_t XComponentPattern::HandleSurfaceCreatedV2() +int32_t XComponentPattern::HandleSurfaceCreatedForSurfaceHolder() { - CHECK_EQUAL_RETURN(isInitialized_, true, ERROR_CODE_XCOMPONENT_STATE_INVALID); + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + CHECK_NULL_RETURN(xcomponentProxySurfaceHolder, ERROR_CODE_XCOMPONENT_STATE_INVALID); + CHECK_EQUAL_RETURN(xcomponentProxySurfaceHolder->IsInitialized(), true, ERROR_CODE_XCOMPONENT_STATE_INVALID); InitSurface(); renderSurface_->RegisterSurface(); InitNativeWindow(paintRect_.Width(), paintRect_.Height()); - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); - } - CHECK_NULL_RETURN(renderSurface_, ERROR_CODE_PARAM_INVALID); - isInitialized_ = true; - if (surfaceHolder_) { - auto callbackList = surfaceHolder_->surfaceCallbackList_; - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceCreated", GetId().c_str()); - ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceCreated", GetId().c_str()); - for (const auto& iter : callbackList) { - auto callback = iter->OnSurfaceCreated; - if (callback) { - callback(surfaceHolder_); - } - } - } + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceCreated", GetId().c_str()); + ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceCreated", GetId().c_str()); + // width&height is unnecessary + xcomponentProxySurfaceHolder->DispatchSurfaceCreatedEvent(-1, -1, nativeWindow_); if (needNotifySizeChanged_) { - OnSurfaceChangedV2(paintRect_); + OnSurfaceChangedForSurfaceHolder(paintRect_); } return ERROR_CODE_NO_ERROR; } @@ -2055,21 +1779,14 @@ void XComponentPattern::HandleSurfaceDestroyed(FrameNode* frameNode) xcomponentController_->SetSurfaceId(""); } -int32_t XComponentPattern::HandleSurfaceDestroyedV2() +int32_t XComponentPattern::HandleSurfaceDestroyedForSurfaceHolder() { - CHECK_EQUAL_RETURN(isInitialized_, false, ERROR_CODE_XCOMPONENT_STATE_INVALID); - isInitialized_ = false; - if (surfaceHolder_) { - auto callbackList = surfaceHolder_->surfaceCallbackList_; - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); - ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); - for (const auto& iter : callbackList) { - auto callback = iter->OnSurfaceDestroyed; - if (callback) { - callback(surfaceHolder_); - } - } - } + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + CHECK_NULL_RETURN(xcomponentProxySurfaceHolder, ERROR_CODE_XCOMPONENT_STATE_INVALID); + CHECK_EQUAL_RETURN(xcomponentProxySurfaceHolder->IsInitialized(), false, ERROR_CODE_XCOMPONENT_STATE_INVALID); + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); + ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); + xcomponentProxySurfaceHolder->DispatchSurfaceDestroyedEvent(); DisposeSurface(); return ERROR_CODE_NO_ERROR; } @@ -2085,9 +1802,9 @@ void XComponentPattern::DisposeSurface() renderSurface_->UnregisterSurface(); renderSurface_ = nullptr; } - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = nullptr; - } + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxySurfaceHolder); + xcomponentProxySurfaceHolder->SetNativeWindow(nullptr); nativeWindow_ = nullptr; auto host = GetHost(); CHECK_NULL_VOID(host); @@ -2144,30 +1861,6 @@ void XComponentPattern::FlushImplicitTransaction(const RefPtr& frameN #endif } -void XComponentPattern::NativeSurfaceShow() -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponentImpl_); - CHECK_NULL_VOID(nativeXComponent_); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto surfaceShowCallback = nativeXComponentImpl_->GetSurfaceShowCallback(); - CHECK_NULL_VOID(surfaceShowCallback); - surfaceShowCallback(nativeXComponent_.get(), surface); -} - -void XComponentPattern::NativeSurfaceHide() -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(nativeXComponent_); - CHECK_NULL_VOID(nativeXComponentImpl_); - auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); - const auto surfaceHideCallback = nativeXComponentImpl_->GetSurfaceHideCallback(); - CHECK_NULL_VOID(surfaceHideCallback); - surfaceHideCallback(nativeXComponent_.get(), surface); - CHECK_NULL_VOID(renderSurface_); - renderSurface_->ReleaseSurfaceBuffers(); -} - void XComponentPattern::OnWindowHide() { if (!hasXComponentInit_ || hasReleasedSurface_ || @@ -2177,8 +1870,10 @@ void XComponentPattern::OnWindowHide() if (renderSurface_) { renderSurface_->OnWindowStateChange(false); } - if (onWindowHide_) { - onWindowHide_(); + if (xcomponentProxy_ && xcomponentProxy_->DispatchSurfaceHideEvent()) { + if (renderSurface_) { + renderSurface_->ReleaseSurfaceBuffers(); + } } hasReleasedSurface_ = true; } @@ -2192,8 +1887,8 @@ void XComponentPattern::OnWindowShow() if (renderSurface_) { renderSurface_->OnWindowStateChange(true); } - if (onWindowShow_) { - onWindowShow_(); + if (xcomponentProxy_) { + xcomponentProxy_->DispatchSurfaceShowEvent(); } hasReleasedSurface_ = false; } @@ -2538,24 +2233,24 @@ void XComponentPattern::UnlockCanvasAndPost(RSCanvas* canvas) void XComponentPattern::SetExpectedRateRange(int32_t min, int32_t max, int32_t expected) { - if (hasGotNativeXComponent_) { + if (IsUsingNativeXComponent()) { return; } - isNativeXComponentDisabled_ = true; CHECK_NULL_VOID(displaySync_); FrameRateRange frameRateRange; frameRateRange.Set(min, max, expected); displaySync_->SetExpectedFrameRateRange(frameRateRange); - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " SetExpectedFrameRateRange" - "{%{public}d, %{public}d, %{public}d}", displaySync_->GetId(), min, max, expected); + TAG_LOGD(AceLogTag::ACE_XCOMPONENT, + "Id: %{public}" PRIu64 " SetExpectedFrameRateRange" + "{%{public}d, %{public}d, %{public}d}", + displaySync_->GetId(), min, max, expected); } void XComponentPattern::UpdateOnFrameEvent(void(*callback)(void*, uint64_t, uint64_t), void* arkuiNode) { - if (hasGotNativeXComponent_) { + if (IsUsingNativeXComponent()) { return; } - isNativeXComponentDisabled_ = true; CHECK_NULL_VOID(displaySync_); displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this), callback, arkuiNode](RefPtr displaySyncData) { @@ -2573,10 +2268,9 @@ void XComponentPattern::UpdateOnFrameEvent(void(*callback)(void*, uint64_t, uint void XComponentPattern::UnregisterOnFrameEvent() { - if (hasGotNativeXComponent_) { + if (IsUsingNativeXComponent()) { return; } - isNativeXComponentDisabled_ = true; CHECK_NULL_VOID(displaySync_); displaySync_->UnregisterOnFrame(); TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame", @@ -2587,187 +2281,133 @@ void XComponentPattern::UnregisterOnFrameEvent() void XComponentPattern::SetNeedSoftKeyboard(bool isNeedSoftKeyboard) { - if (hasGotNativeXComponent_) { + if (IsUsingNativeXComponent()) { return; } - isNativeXComponentDisabled_ = true; - isNeedSoftKeyboard_ = isNeedSoftKeyboard; -} - -void XComponentPattern::OnSurfaceShowV2() -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(surfaceHolder_); - auto callbackList = surfaceHolder_->surfaceCallbackList_; - for (const auto& iter : callbackList) { - auto callback = iter->onSurfaceShow; - if (callback) { - callback(surfaceHolder_); - } + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + xcomponentProxySurfaceHolder->SetNeedSoftKeyboard(isNeedSoftKeyboard); } -void XComponentPattern::OnSurfaceHideV2() +OH_ArkUI_SurfaceHolder* XComponentPattern::GetSurfaceHolder() { - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(surfaceHolder_); - auto callbackList = surfaceHolder_->surfaceCallbackList_; - bool hasCallback = false; - for (const auto& iter : callbackList) { - auto callback = iter->onSurfaceHide; - if (callback) { - callback(surfaceHolder_); - hasCallback = true; - } + if (IsUsingNativeXComponent()) { + return nullptr; } - if (hasCallback) { - CHECK_NULL_VOID(renderSurface_); - renderSurface_->ReleaseSurfaceBuffers(); + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); + return nullptr; } + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + CHECK_NULL_RETURN(xcomponentProxySurfaceHolder, nullptr); + return xcomponentProxySurfaceHolder->GetSurfaceHolder(); } void XComponentPattern::SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder) { - surfaceHolder_ = surfaceHolder; - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); - hasGotSurfaceHolder_ = true; - } - if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { - isV2_ = true; - RegisterCallbackV2(); + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + xcomponentProxySurfaceHolder->SetSurfaceHolder(surfaceHolder); + xcomponentProxySurfaceHolder->SetNativeWindow(nativeWindow_); } int32_t XComponentPattern::Initialize() { - if (IsCreateSurfaceHolderForbidden()) { + if (IsUsingNativeXComponent()) { return OHOS::Ace::ERROR_CODE_PARAM_INVALID; } - if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { - isV2_ = true; - RegisterCallbackV2(); + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } - return HandleSurfaceCreatedV2(); + return HandleSurfaceCreatedForSurfaceHolder(); } int32_t XComponentPattern::Finalize() { - if (IsCreateSurfaceHolderForbidden()) { + if (IsUsingNativeXComponent()) { return OHOS::Ace::ERROR_CODE_PARAM_INVALID; } - if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { - isV2_ = true; - RegisterCallbackV2(); + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } - return HandleSurfaceDestroyedV2(); + return HandleSurfaceDestroyedForSurfaceHolder(); } int32_t XComponentPattern::SetAutoInitialize(bool autoInitialize) { - if (IsCreateSurfaceHolderForbidden()) { + if (IsUsingNativeXComponent()) { return OHOS::Ace::ERROR_CODE_PARAM_INVALID; } - if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { - isV2_ = true; - RegisterCallbackV2(); + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } - autoInitialize_ = autoInitialize; return ERROR_CODE_NO_ERROR; } int32_t XComponentPattern::IsInitialized(bool& isInitialized) { - if (IsCreateSurfaceHolderForbidden()) { + if (IsUsingNativeXComponent()) { return OHOS::Ace::ERROR_CODE_PARAM_INVALID; } - if (!isV2_ && nodeType_ == XComponentNodeType::CNODE) { - isV2_ = true; - RegisterCallbackV2(); + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } - isInitialized = isInitialized_; + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + isInitialized = xcomponentProxySurfaceHolder->IsInitialized(); return ERROR_CODE_NO_ERROR; } -void XComponentPattern::ResetAndInitializeNodeHandleAccessibility() +bool XComponentPattern::IsUsingNativeXComponent() const { - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto pipeline = host->GetContextRefPtr(); - CHECK_NULL_VOID(pipeline); - auto accessibilityManager = pipeline->GetAccessibilityManager(); - CHECK_NULL_VOID(accessibilityManager); - if (accessibilityChildTreeCallback_) { - accessibilityManager->DeregisterAccessibilityChildTreeCallback( - accessibilityChildTreeCallback_->GetAccessibilityId() - ); - accessibilityChildTreeCallback_.reset(); - accessibilityChildTreeCallback_ = nullptr; - } - - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeNodeHandleAccessibility"); - CHECK_NULL_VOID(arkuiAccessibilityProvider_); - arkuiAccessibilityProvider_->SetRegisterCallback( - [weak = WeakClaim(this)] (bool isRegister) { - auto pattern = weak.Upgrade(); - CHECK_NULL_VOID(pattern); - pattern->HandleRegisterAccessibilityEvent(isRegister); - }); - int64_t accessibilityId = host->GetAccessibilityId(); - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, - "InitializeNodeHandleAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId); - accessibilityChildTreeCallback_ = std::make_shared( - WeakClaim(this), host->GetAccessibilityId()); - accessibilityManager->RegisterAccessibilityChildTreeCallback( - accessibilityId, accessibilityChildTreeCallback_); - if (accessibilityManager->IsRegister()) { - accessibilityChildTreeCallback_->OnRegister( - pipeline->GetWindowId(), accessibilityManager->GetTreeId()); + CHECK_NULL_RETURN(xcomponentProxy_, true); + return InstanceOf(xcomponentProxy_) && !xcomponentProxy_->IsToggleEnabled(); +} + +void XComponentPattern::SetHasGotNativeXComponent(bool hasGotNativeXComponent) +{ + if (hasGotNativeXComponent) { + CHECK_NULL_VOID(xcomponentProxy_); + xcomponentProxy_->SetToggleEnabled(false); } } +bool XComponentPattern::IsBindNative() const +{ + return InstanceOf(xcomponentProxy_) || nodeType_ == XComponentNodeType::CNODE; +} + ArkUI_AccessibilityProvider* XComponentPattern::CreateAccessibilityProvider() { - if (hasGotNativeXComponent_) { + if (IsUsingNativeXComponent()) { return nullptr; } - isNativeXComponentDisabled_ = true; - useNodeHandleAccessibilityProvider_ = true; - if (arkuiAccessibilityProvider_) { - return arkuiAccessibilityProvider_; + if (xcomponentProxy_->IsToggleEnabled()) { + xcomponentProxy_.Swap(MakeRefPtr(WeakClaim(this))); + xcomponentProxy_->SetToggleEnabled(false); } - auto host = GetHost(); - CHECK_NULL_RETURN(host, nullptr); - arkuiAccessibilityProvider_ = new ArkUI_AccessibilityProvider(); - XComponentPattern::XComponentAccessibilityProviderMap[arkuiAccessibilityProvider_] = WeakPtr(host); - ResetAndInitializeNodeHandleAccessibility(); - return arkuiAccessibilityProvider_; + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + return xcomponentProxySurfaceHolder->CreateAccessibilityProvider(); } void XComponentPattern::DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider) { - if (hasGotNativeXComponent_ || (provider != arkuiAccessibilityProvider_)) { + if (IsUsingNativeXComponent()) { return; } - CHECK_NULL_VOID(arkuiAccessibilityProvider_); - isNativeXComponentDisabled_ = true; - XComponentPattern::XComponentAccessibilityProviderMap.erase(arkuiAccessibilityProvider_); - auto host = GetHost(); - UninitializeAccessibility(AceType::RawPtr(host)); - delete arkuiAccessibilityProvider_; - arkuiAccessibilityProvider_ = nullptr; + auto xcomponentProxySurfaceHolder = DynamicCast(xcomponentProxy_); + xcomponentProxySurfaceHolder->DisposeAccessibilityProvider(provider); } -std::unordered_map> XComponentPattern::XComponentAccessibilityProviderMap; - FrameNode* XComponentPattern::QueryAccessibilityProviderHost(void* provider, bool& isProviderValied) { - auto it = XComponentAccessibilityProviderMap.find(provider); - if (it == XComponentAccessibilityProviderMap.end()) { - isProviderValied = false; - return nullptr; - } - isProviderValied = true; - auto weakHost = it->second; - auto host = weakHost.Upgrade(); - CHECK_NULL_RETURN(host, nullptr); - return AceType::RawPtr(host); + return XComponentProxySurfaceHolder::QueryAccessibilityProviderHost(provider, isProviderValied); } } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h index 63c7f71b0cd..ecf0fa22b0e 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h @@ -20,14 +20,7 @@ #include #include -#include "base/geometry/dimension.h" -#include "base/geometry/ng/offset_t.h" #include "base/geometry/ng/rect_t.h" -#include "base/geometry/ng/size_t.h" -#include "base/geometry/size.h" -#include "base/memory/referenced.h" -#include "base/utils/utils.h" -#include "core/common/thread_checker.h" #include "core/components/common/layout/constants.h" #include "core/components/xcomponent/native_interface_xcomponent_impl.h" #include "core/components_ng/event/focus_hub.h" @@ -39,6 +32,8 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" #include "core/components_ng/pattern/xcomponent/xcomponent_layout_property.h" #include "core/components_ng/pattern/xcomponent/xcomponent_paint_method.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h" #include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/components_ng/property/property.h" #include "core/components_ng/render/render_surface.h" @@ -116,31 +111,17 @@ public: bool NeedSoftKeyboard() const override { - return (nativeXComponentImpl_ ? nativeXComponentImpl_->IsNeedSoftKeyboard() : false) || isNeedSoftKeyboard_; + return xcomponentProxy_ ? xcomponentProxy_->NeedSoftKeyboard() : false; } - std::pair, std::weak_ptr> GetNativeXComponent() - { - if (!nativeXComponent_ || !nativeXComponentImpl_) { - // for XComponentType::NODE - nativeXComponentImpl_ = AceType::MakeRefPtr(); - nativeXComponent_ = std::make_shared(AceType::RawPtr(nativeXComponentImpl_)); - } - return std::make_pair(nativeXComponentImpl_, nativeXComponent_); - } - - void NativeXComponentDispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent, - const std::vector& xComponentTouchPoints); - void NativeXComponentDispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent, - const OH_NativeXComponent_ExtraMouseEventInfo& extraMouseEventInfo); - void NativeXComponentDispatchAxisEvent(AxisEvent* axisEvent); + std::pair, std::weak_ptr> GetNativeXComponent(); void InitXComponent(); void InitNativeXComponent(); void InitNativeWindow(float textureWidth, float textureHeight); void XComponentSizeInit(); void XComponentSizeChange(const RectF& surfaceRect, bool needFireNativeEvent); - void XComponentSizeChangeV2(const RectF& surfaceRect); + void XComponentSizeChangeForSurfaceHolder(const RectF& surfaceRect); void NativeXComponentInit() { if (nodeType_ != XComponentNodeType::TYPE_NODE) { @@ -216,7 +197,7 @@ public: void SetSurfaceRotation(bool isLock); - bool GetSurfaceRotation() + bool GetSurfaceRotation() const { return isSurfaceLock_; } @@ -235,32 +216,23 @@ public: void SetExpectedRateRangeInit() { - CHECK_NULL_VOID(nativeXComponentImpl_); - nativeXComponentImpl_->SetExpectedRateRangeEventCallback([weak = AceType::WeakClaim(this)]() { - auto xComponentPattern = weak.Upgrade(); - CHECK_NULL_VOID(xComponentPattern); - xComponentPattern->HandleSetExpectedRateRangeEvent(); - }); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->SetExpectedRateRangeInit(); } void OnFrameEventInit() { - CHECK_NULL_VOID(nativeXComponentImpl_); - nativeXComponentImpl_->SetOnFrameEventCallback([weak = AceType::WeakClaim(this)]() { - auto xComponentPattern = weak.Upgrade(); - CHECK_NULL_VOID(xComponentPattern); - xComponentPattern->HandleOnFrameEvent(); - }); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->OnFrameEventInit(); } void UnregisterOnFrameEventInit() { - CHECK_NULL_VOID(nativeXComponentImpl_); - nativeXComponentImpl_->SetUnregisterOnFrameEventCallback([weak = AceType::WeakClaim(this)]() { - auto xComponentPattern = weak.Upgrade(); - CHECK_NULL_VOID(xComponentPattern); - xComponentPattern->HandleUnregisterOnFrameEvent(); - }); + auto xcomponentProxyNativeXComponent = DynamicCast(xcomponentProxy_); + CHECK_NULL_VOID(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->UnregisterOnFrameEventInit(); } void SetXcomponentInit(bool isInit) @@ -290,30 +262,11 @@ public: return nodeType_ == XComponentNodeType::TYPE_NODE && isNativeXComponent_ && hasLoadNativeDone_; } - bool HasGotSurfaceHolder() const - { - return hasGotSurfaceHolder_; - } - - bool HasGotNativeXComponent() const - { - return hasGotNativeXComponent_; - } - - bool IsBindNative() - { - return isV2_ || nodeType_ == XComponentNodeType::CNODE; - } + bool IsUsingNativeXComponent() const; - bool IsNativeXComponentDisabled() const - { - return isNativeXComponentDisabled_; - } + bool IsBindNative() const; - void SetHasGotNativeXComponent(bool hasGotNativeXComponent) - { - hasGotNativeXComponent_ = hasGotNativeXComponent; - } + void SetHasGotNativeXComponent(bool hasGotNativeXComponent); XComponentNodeType GetNodeType() const { @@ -325,30 +278,15 @@ public: return surfaceCallbackMode_; } - bool IsAutoInitialize() const - { - return autoInitialize_; - } - - OH_ArkUI_SurfaceHolder* GetSurfaceHolder() const - { - return surfaceHolder_; - } + OH_ArkUI_SurfaceHolder* GetSurfaceHolder(); void SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder); - bool IsCreateSurfaceHolderForbidden() const - { - // isV2_ || (nodeType_ == XComponentNodeType::CNODE && !hasGotNativeXComponent_) can create SurfaceHolder - return !isV2_ && (nodeType_ != XComponentNodeType::CNODE || hasGotNativeXComponent_); - } - XComponentNodeType GetXComponentNodeType() const { return nodeType_; } - // need to do sth int32_t Initialize(); int32_t Finalize(); int32_t SetAutoInitialize(bool autoInitialize); @@ -375,8 +313,6 @@ public: void OnSetAccessibilityChildTree(int32_t childWindowId, int32_t childTreeId); void SetAccessibilityState(bool state) {} RefPtr GetAccessibilitySessionAdapter() override; - void InitializeAccessibilityCallback(); - void HandleRegisterAccessibilityEvent(bool isRegister); void SetIdealSurfaceWidth(float surfaceWidth); void SetIdealSurfaceHeight(float surfaceHeight); @@ -386,7 +322,7 @@ public: std::tuple UpdateSurfaceRect(); void HandleSurfaceChangeEvent(bool needForceRender, bool offsetChanged, bool sizeChanged, bool needFireNativeEvent, bool frameOffsetChange = false); - void HandleSurfaceChangeEventV2(bool sizeChanged); + void HandleSurfaceChangeEventForSurfaceHolder(bool sizeChanged); void EnableAnalyzer(bool enable); void SetImageAIOptions(void* options); void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed); @@ -397,9 +333,9 @@ public: void SetRenderFit(RenderFit renderFit); void SetScreenId(uint64_t screenId); void HandleSurfaceCreated(); - int32_t HandleSurfaceCreatedV2(); + int32_t HandleSurfaceCreatedForSurfaceHolder(); void HandleSurfaceDestroyed(FrameNode* frameNode = nullptr); - int32_t HandleSurfaceDestroyedV2(); + int32_t HandleSurfaceDestroyedForSurfaceHolder(); void DisposeSurface(); void ChangeSurfaceCallbackMode(SurfaceCallbackMode mode) { @@ -418,10 +354,14 @@ public: void UnlockCanvasAndPost(RSCanvas* canvas); ArkUI_AccessibilityProvider* GetNativeProvider(); + void OnSizeChange(bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent); + void HandleSetExpectedRateRangeEvent(); + void HandleOnFrameEvent(); + void HandleUnregisterOnFrameEvent(); + protected: void OnAttachToMainTree() override; void OnDetachFromMainTree() override; - void OnAttachToFrameNode() override; void OnDetachFromFrameNode(FrameNode* frameNode) override; void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override; void OnRebuildFrame() override; @@ -436,37 +376,6 @@ protected: void AdjustNativeWindowSize(float width, float height); bool IsSupportImageAnalyzerFeature(); void UpdateAnalyzerUIConfig(const RefPtr& geometryNode); - // void Initialize(); - - std::optional id_; - XComponentType type_ = XComponentType::UNKNOWN; - bool hasGotSurfaceHolder_ = false; - bool hasGotNativeXComponent_ = false; - bool isNativeXComponentDisabled_ = false; - bool isCNode_ = false; - bool useNodeHandleAccessibilityProvider_ = false; - RefPtr renderSurface_; - OffsetF localPosition_; - OffsetF surfaceOffset_; - SizeF drawSize_; - SizeF surfaceSize_; - RectF paintRect_; - void* nativeWindow_ = nullptr; - bool hasReleasedSurface_ = false; - RefPtr renderContextForSurface_; - std::optional transformHintChangedCallbackId_; - std::string surfaceId_; - bool isOnTree_ = false; - float hdrBrightness_ = 1.0f; - bool isTransparentLayer_ = false; - bool isEnableSecure_ = false; - bool isSurfaceLock_ = false; - RenderFit renderFit_ = RenderFit::RESIZE_FILL; - RefPtr displaySync_ = AceType::MakeRefPtr(UIObjectType::DISPLAYSYNC_XCOMPONENT); - bool needRecoverDisplaySync_ = false; - std::shared_ptr accessibilityChildTreeCallback_; - ArkUI_AccessibilityProvider* arkuiAccessibilityProvider_ = nullptr; - bool isNeedSoftKeyboard_ = false; private: void OnAreaChangedInner() override; @@ -485,11 +394,8 @@ private: void OnNativeUnload(FrameNode* frameNode); void OnSurfaceCreated(); - void OnSurfaceChanged(const RectF& surfaceRect, bool needResizeNativeWindow); - void OnSurfaceChangedV2(const RectF& surfaceRect); - - void NativeSurfaceShow(); - void NativeSurfaceHide(); + void OnSurfaceChanged(const RectF& surfaceRect); + void OnSurfaceChangedForSurfaceHolder(const RectF& surfaceRect); void InitController(); void InitSurface(); @@ -510,25 +416,15 @@ private: void HandleBlurEvent(); ExternalEvent CreateExternalEvent(); - void InitV1(); - void InitV2(); + void InitForNonSurfaceHolder(); + void InitForSurfaceHolder(); void ReinitializeSurfaceProperties(); - void RegisterCallbackV1(); - void RegisterCallbackV2(); std::shared_ptr GetRSUIContext(const RefPtr& frameNode); void FlushImplicitTransaction(const RefPtr& frameNode); std::shared_ptr GetRSTransactionHandler(const RefPtr& frameNode); - void OnSizeChangeV1(bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent); - void OnSurfaceShowV2(); - void OnSurfaceHideV2(); - - void ResetAndInitializeNodeHandleAccessibility(); void SetTouchPoint( const std::list& touchInfoList, int64_t timeStamp, const TouchType& touchType); - void HandleSetExpectedRateRangeEvent(); - void HandleOnFrameEvent(); - void HandleUnregisterOnFrameEvent(); bool ExportTextureAvailable(); bool DoTextureExport(); bool StopTextureExport(); @@ -551,17 +447,36 @@ private: #endif std::vector SetHistoryPoint(const std::list& touchInfoList); + + std::optional id_; + XComponentType type_ = XComponentType::UNKNOWN; std::optional libraryname_; std::shared_ptr xcomponentController_; std::optional soPath_; + RefPtr renderSurface_; + OffsetF localPosition_; + OffsetF surfaceOffset_; + SizeF drawSize_; + SizeF surfaceSize_; + RectF paintRect_; + void* nativeWindow_ = nullptr; + bool hasReleasedSurface_ = false; + RefPtr renderContextForSurface_; + std::optional transformHintChangedCallbackId_; + std::string surfaceId_; + bool isOnTree_ = false; + float hdrBrightness_ = 1.0f; + bool isTransparentLayer_ = false; + bool isEnableSecure_ = false; + bool isSurfaceLock_ = false; + RenderFit renderFit_ = RenderFit::RESIZE_FILL; + RefPtr displaySync_ = AceType::MakeRefPtr(UIObjectType::DISPLAYSYNC_XCOMPONENT); + bool needRecoverDisplaySync_ = false; std::optional screenId_; RefPtr handlingSurfaceRenderContext_; WeakPtr extPattern_; - std::shared_ptr nativeXComponent_; - RefPtr nativeXComponentImpl_; - bool hasXComponentInit_ = false; RefPtr touchEvent_; @@ -604,25 +519,12 @@ private: // record the initial surfaceId_ in InitSurface, this variable should not be modified after the initial assignment std::string initialSurfaceId_; - OH_ArkUI_SurfaceHolder* surfaceHolder_ = nullptr; XComponentNodeType nodeType_ = XComponentNodeType::UNKNOWN; Color bkColor_ = Color::BLACK; - bool autoInitialize_ = true; bool needReinitialize_ = false; - bool isInitialized_ = false; bool needNotifySizeChanged_ = false; - bool isV2_ = false; - std::function onAttachToMainTree_ = nullptr; - std::function onDetachFromMainTree_ = nullptr; - std::function onSizeChange_ = nullptr; - // std::function onRebuildFrame_ = nullptr; - std::function onWindowHide_ = nullptr; - std::function onWindowShow_ = nullptr; - // std::function onModifyDone_ = nullptr; - // std::function dumpInfo_ = nullptr; - // std::function needSoftKeyboard_ = nullptr; - - static std::unordered_map> XComponentAccessibilityProviderMap; + + RefPtr xcomponentProxy_ = nullptr; }; } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp deleted file mode 100644 index 2a7342c2319..00000000000 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp +++ /dev/null @@ -1,732 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" - -#include "base/log/dump_log.h" -#include "base/utils/utils.h" -#include "core/accessibility/accessibility_session_adapter.h" -#include "core/components/common/layout/constants.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_child_tree_callback.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_session_adapter.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_inner_surface_controller.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#ifdef ENABLE_ROSEN_BACKEND -#include "transaction/rs_transaction.h" -#include "transaction/rs_transaction_handler.h" -#include "transaction/rs_transaction_proxy.h" -#include "transaction/rs_sync_transaction_controller.h" -#include "transaction/rs_sync_transaction_handler.h" -#include "ui/rs_ui_context.h" -#include "ui/rs_ui_director.h" -#endif - -namespace OHOS::Ace::NG { - -namespace { -const std::string BUFFER_USAGE_XCOMPONENT = "xcomponent"; - -inline std::string BoolToString(bool value) -{ - return value ? "true" : "false"; -} -} // namespace - -XComponentPatternV2::XComponentPatternV2(XComponentNodeType nodeType) : XComponentPattern(false), nodeType_(nodeType) -{ - if (nodeType == XComponentNodeType::CNODE) { - isCNode_ = true; - } -} - -void XComponentPatternV2::InitParams(XComponentType type) -{ - if (type_ != XComponentType::UNKNOWN) { - return; - } - type_ = type; - if (isCNode_) { - id_ = ""; - SetLibraryName(""); - InitNativeXComponent(); - XComponentPattern::Initialize(); - } - InitSurface(); - UpdateTransformHint(); -} - -void XComponentPatternV2::SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder) -{ - surfaceHolder_ = surfaceHolder; - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); - hasGotSurfaceHolder_ = true; - } -} - -OH_ArkUI_SurfaceHolder* XComponentPatternV2::GetSurfaceHolder() -{ - return surfaceHolder_; -} - -void XComponentPatternV2::OnAttachToFrameNode() -{ - if (isCNode_) { - XComponentPattern::OnAttachToFrameNode(); - return; - } - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto renderContext = host->GetRenderContext(); - CHECK_NULL_VOID(renderContext); - - renderContext->SetClipToFrame(true); - renderContext->SetClipToBounds(true); - if (FrameReport::GetInstance().GetEnable()) { - FrameReport::GetInstance().EnableSelfRender(); - } -} - -void XComponentPatternV2::OnAttachToMainTree() -{ - UpdateUsesSuperMethod(); - if (usesSuperMethod_) { - XComponentPattern::OnAttachToMainTree(); - return; - } - isOnTree_ = true; - if (autoInitialize_) { - HandleSurfaceCreated(); - } - if (needRecoverDisplaySync_ && displaySync_ && !displaySync_->IsOnPipeline()) { - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "OnAttachToMainTree:recover displaySync: " - "%{public}s(%{public}" PRIu64 ")", GetId().c_str(), displaySync_->GetId()); - displaySync_->AddToPipelineOnContainer(); - needRecoverDisplaySync_ = false; - } -} - -void XComponentPatternV2::BeforeSyncGeometryProperties(const DirtySwapConfig& config) -{ - UpdateUsesSuperMethod(); - if (usesSuperMethod_) { - XComponentPattern::BeforeSyncGeometryProperties(config); - return; - } - CHECK_EQUAL_VOID(config.skipMeasure, true); - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto geometryNode = host->GetGeometryNode(); - CHECK_NULL_VOID(geometryNode); - drawSize_ = geometryNode->GetContentSize(); - if (!drawSize_.IsPositive()) { - TAG_LOGW( - AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s]'s size is not positive", GetId().c_str()); - return; - } - localPosition_ = geometryNode->GetContentOffset(); - if (IsSupportImageAnalyzerFeature()) { - UpdateAnalyzerUIConfig(geometryNode); - } - const auto& [offsetChanged, sizeChanged] = UpdateSurfaceRect(); - HandleSurfaceChangeEvent(offsetChanged, sizeChanged, config.frameOffsetChange); - AddAfterLayoutTaskForExportTexture(); - host->MarkNeedSyncRenderTree(); -} - -std::pair XComponentPatternV2::UpdateSurfaceRect() -{ - if (!drawSize_.IsPositive()) { - return { false, false }; - } - auto preSurfaceSize = surfaceSize_; - auto preSurfaceOffset = surfaceOffset_; - - surfaceSize_ = drawSize_; - surfaceOffset_ = localPosition_; - auto offsetChanged = preSurfaceOffset != surfaceOffset_; - auto sizeChanged = preSurfaceSize != surfaceSize_; - if (offsetChanged || sizeChanged) { - paintRect_ = AdjustPaintRect( - surfaceOffset_.GetX(), surfaceOffset_.GetY(), surfaceSize_.Width(), surfaceSize_.Height(), true); - } - return { offsetChanged, sizeChanged }; -} - -void XComponentPatternV2::HandleSurfaceChangeEvent(bool offsetChanged, bool sizeChanged, bool frameOffsetChange) -{ - if (!drawSize_.IsPositive()) { - return; - } - if (sizeChanged) { - XComponentSizeChange(paintRect_); - } - if (renderContextForSurface_) { - renderContextForSurface_->SetBounds( - paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height()); - } - if (renderSurface_) { - renderSurface_->SetSurfaceDefaultSize( - static_cast(paintRect_.Width()), static_cast(paintRect_.Height())); - } -} - -void XComponentPatternV2::XComponentSizeChange(const RectF& surfaceRect) -{ - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto context = host->GetContextRefPtr(); - CHECK_NULL_VOID(context); - auto viewScale = context->GetViewScale(); - CHECK_NULL_VOID(renderSurface_); - auto width = surfaceRect.Width(); - auto height = surfaceRect.Height(); - needNotifySizeChanged_ = true; - renderSurface_->UpdateSurfaceSizeInUserData( - static_cast(width), static_cast(height)); - renderSurface_->AdjustNativeWindowSize( - static_cast(width * viewScale), static_cast(height * viewScale)); - OnSurfaceChanged(surfaceRect); -} - -void XComponentPatternV2::OnSurfaceChanged(const RectF& surfaceRect) -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(surfaceHolder_); - CHECK_EQUAL_VOID(isInitialized_, false); - ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceChanged", GetId().c_str()); - auto callbackList = surfaceHolder_->surfaceCallbackList_; - for (const auto& iter : callbackList) { - auto callback = iter->OnSurfaceChanged; - if (callback) { - callback(surfaceHolder_, surfaceRect.Width(), surfaceRect.Height()); - } - } - needNotifySizeChanged_ = false; -} - -void XComponentPatternV2::OnDetachFromMainTree() -{ - if (usesSuperMethod_) { - XComponentPattern::OnDetachFromMainTree(); - return; - } - isOnTree_ = false; - if (autoInitialize_) { - HandleSurfaceDestroyed(); - } - if (displaySync_ && displaySync_->IsOnPipeline()) { - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "OnDetachFromMainTree:remove displaySync: " - "%{public}s(%{public}" PRIu64 ")", GetId().c_str(), displaySync_->GetId()); - displaySync_->DelFromPipelineOnContainer(); - needRecoverDisplaySync_ = true; - } -} - -void XComponentPatternV2::OnDetachFromFrameNode(FrameNode* frameNode) -{ - UpdateUsesSuperMethod(); - if (usesSuperMethod_) { - XComponentPattern::OnDetachFromFrameNode(frameNode); - return; - } - auto id = frameNode->GetId(); - auto pipeline = frameNode->GetContextRefPtr(); - CHECK_NULL_VOID(pipeline); - pipeline->RemoveWindowStateChangedCallback(id); - if (HasTransformHintChangedCallbackId()) { - pipeline->UnregisterTransformHintChangedCallback(transformHintChangedCallbackId_.value_or(-1)); - } - if (FrameReport::GetInstance().GetEnable()) { - FrameReport::GetInstance().DisableSelfRender(); - } - HandleSurfaceDestroyed(); - - CHECK_NULL_VOID(surfaceHolder_); - surfaceHolder_->nativeWindow_ = nullptr; - surfaceHolder_->node_ = nullptr; -} - -void XComponentPatternV2::InitSurface() -{ - if (renderSurface_) { - return; - } - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto renderContext = host->GetRenderContext(); - CHECK_NULL_VOID(renderContext); - - renderSurface_ = RenderSurface::Create(); - renderSurface_->SetInstanceId(GetHostInstanceId()); - renderSurface_->SetBufferUsage(BUFFER_USAGE_XCOMPONENT); - if (type_ == XComponentType::SURFACE) { - InitializeRenderContext(); - renderSurface_->SetRenderContext(renderContextForSurface_); - renderContext->AddChild(renderContextForSurface_, 0); - } else if (type_ == XComponentType::TEXTURE) { - renderSurface_->SetRenderContext(renderContext); - renderSurface_->SetIsTexture(true); - renderContext->OnNodeNameUpdate(GetId()); - } - renderSurface_->InitSurface(); - renderSurface_->UpdateSurfaceConfig(); - if (type_ == XComponentType::TEXTURE) { - renderSurface_->RegisterBufferCallback(); - } - auto width = paintRect_.Width(); - auto height = paintRect_.Height(); - if (!paintRect_.IsEmpty()) { - renderSurface_->UpdateSurfaceSizeInUserData( - static_cast(width), static_cast(height)); - renderSurface_->SetSurfaceDefaultSize( - static_cast(width), static_cast(height)); - } - renderSurface_->RegisterSurface(); - InitNativeWindow(width, height); - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow_); - } - surfaceId_ = renderSurface_->GetUniqueId(); - if (type_ == XComponentType::SURFACE) { - XComponentInnerSurfaceController::RegisterSurfaceRenderContext( - surfaceId_, WeakPtr(renderContextForSurface_)); - } -} - -void XComponentPatternV2::DisposeSurface() -{ - if (type_ == XComponentType::SURFACE) { - XComponentInnerSurfaceController::UnregisterSurfaceRenderContext( - surfaceId_); - surfaceId_ = ""; - } - if (renderSurface_) { - renderSurface_->ReleaseSurfaceBuffers(); - renderSurface_->UnregisterSurface(); - renderSurface_ = nullptr; - } - if (surfaceHolder_) { - surfaceHolder_->nativeWindow_ = nullptr; - } - nativeWindow_ = nullptr; - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto renderContext = host->GetRenderContext(); - CHECK_NULL_VOID(renderContext); - // for surface type - CHECK_NULL_VOID(renderContextForSurface_); - renderContext->RemoveChild(renderContextForSurface_); - renderContextForSurface_ = nullptr; -#ifdef ENABLE_ROSEN_BACKEND - FlushImplicitTransaction(host); -#endif -} - -std::shared_ptr XComponentPatternV2::GetRSTransactionHandler( - const RefPtr& frameNode) -{ -#ifdef ENABLE_ROSEN_BACKEND - if (!SystemProperties::GetMultiInstanceEnabled()) { - return nullptr; - } - auto rsUIContext = GetRSUIContext(frameNode); - CHECK_NULL_RETURN(rsUIContext, nullptr); - return rsUIContext->GetRSTransaction(); -#endif - return nullptr; -} - -std::shared_ptr XComponentPatternV2::GetRSUIContext(const RefPtr& frameNode) -{ -#ifdef ENABLE_ROSEN_BACKEND - CHECK_NULL_RETURN(frameNode, nullptr); - auto pipeline = frameNode->GetContext(); - CHECK_NULL_RETURN(pipeline, nullptr); - auto window = pipeline->GetWindow(); - CHECK_NULL_RETURN(window, nullptr); - auto rsUIDirector = window->GetRSUIDirector(); - CHECK_NULL_RETURN(rsUIDirector, nullptr); - auto rsUIContext = rsUIDirector->GetRSUIContext(); - return rsUIContext; -#endif - return nullptr; -} - -void XComponentPatternV2::FlushImplicitTransaction(const RefPtr& frameNode) -{ -#ifdef ENABLE_ROSEN_BACKEND - if (auto transactionHandler = GetRSTransactionHandler(frameNode)) { - transactionHandler->FlushImplicitTransaction(); - } else { - Rosen::RSTransactionProxy::GetInstance()->FlushImplicitTransaction(); - } -#endif -} - -int32_t XComponentPatternV2::HandleSurfaceCreated() -{ - CHECK_EQUAL_RETURN(isInitialized_, true, ERROR_CODE_XCOMPONENT_STATE_INVALID); - InitSurface(); - CHECK_NULL_RETURN(renderSurface_, ERROR_CODE_PARAM_INVALID); - isInitialized_ = true; - if (surfaceHolder_) { - auto callbackList = surfaceHolder_->surfaceCallbackList_; - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceCreated", GetId().c_str()); - ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceCreated", GetId().c_str()); - for (const auto& iter : callbackList) { - auto callback = iter->OnSurfaceCreated; - if (callback) { - callback(surfaceHolder_); - } - } - } - if (needNotifySizeChanged_) { - OnSurfaceChanged(paintRect_); - } - return ERROR_CODE_NO_ERROR; -} - -int32_t XComponentPatternV2::HandleSurfaceDestroyed() -{ - CHECK_EQUAL_RETURN(isInitialized_, false, ERROR_CODE_XCOMPONENT_STATE_INVALID); - isInitialized_ = false; - if (surfaceHolder_) { - auto callbackList = surfaceHolder_->surfaceCallbackList_; - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); - ACE_SCOPED_TRACE("XComponent[%s] surfaceHolder OnSurfaceDestroyed", GetId().c_str()); - for (const auto& iter : callbackList) { - auto callback = iter->OnSurfaceDestroyed; - if (callback) { - callback(surfaceHolder_); - } - } - } - DisposeSurface(); - return ERROR_CODE_NO_ERROR; -} - -int32_t XComponentPatternV2::Initialize() -{ - CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID); - isLifecycleInterfaceCalled_ = true; - return HandleSurfaceCreated(); -} - -int32_t XComponentPatternV2::Finalize() -{ - CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID); - isLifecycleInterfaceCalled_ = true; - return HandleSurfaceDestroyed(); -} - -int32_t XComponentPatternV2::SetAutoInitialize(bool autoInitialize) -{ - CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID); - isLifecycleInterfaceCalled_ = true; - autoInitialize_ = autoInitialize; - return ERROR_CODE_NO_ERROR; -} - -int32_t XComponentPatternV2::IsInitialized(bool& isInitialized) -{ - CHECK_EQUAL_RETURN(usesSuperMethod_, true, ERROR_CODE_PARAM_INVALID); - isLifecycleInterfaceCalled_ = true; - isInitialized = isInitialized_; - return ERROR_CODE_NO_ERROR; -} - -void XComponentPatternV2::OnWindowHide() -{ - if (usesSuperMethod_) { - XComponentPattern::OnWindowHide(); - return; - } - CHECK_EQUAL_VOID(hasReleasedSurface_, true); - if (renderSurface_) { - renderSurface_->OnWindowStateChange(false); - } - OnSurfaceHide(); - hasReleasedSurface_ = true; -} - -void XComponentPatternV2::OnWindowShow() -{ - if (usesSuperMethod_) { - XComponentPattern::OnWindowShow(); - return; - } - CHECK_EQUAL_VOID(hasReleasedSurface_, false); - if (renderSurface_) { - renderSurface_->OnWindowStateChange(true); - } - OnSurfaceShow(); - hasReleasedSurface_ = false; -} - -void XComponentPatternV2::OnRebuildFrame() -{ - if (usesSuperMethod_) { - XComponentPattern::OnRebuildFrame(); - return; - } - if (type_ != XComponentType::SURFACE) { - return; - } - CHECK_NULL_VOID(renderSurface_); - if (!renderSurface_->IsSurfaceValid()) { - return; - } - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto renderContext = host->GetRenderContext(); - CHECK_NULL_VOID(renderContext); - CHECK_NULL_VOID(renderContextForSurface_); - renderContext->AddChild(renderContextForSurface_, 0); -} - -void XComponentPatternV2::InitializeRenderContext() -{ - if (renderContextForSurface_) { - return; - } - renderContextForSurface_ = RenderContext::Create(); - RenderContext::ContextParam param = { RenderContext::ContextType::HARDWARE_SURFACE, GetId() + "Surface", - RenderContext::PatternType::XCOM }; - renderContextForSurface_->InitContext(false, param); - if (!paintRect_.IsEmpty()) { - renderContextForSurface_->SetBounds( - paintRect_.GetX(), paintRect_.GetY(), paintRect_.Width(), paintRect_.Height()); - } - renderContextForSurface_->UpdateBackgroundColor(bkColor_); - renderContextForSurface_->SetHDRBrightness(hdrBrightness_); - renderContextForSurface_->SetTransparentLayer(isTransparentLayer_); - renderContextForSurface_->SetSecurityLayer(isEnableSecure_); - renderContextForSurface_->SetSurfaceRotation(isSurfaceLock_); - renderContextForSurface_->SetRenderFit(renderFit_); -} - -void XComponentPatternV2::OnModifyDone() -{ - if (usesSuperMethod_) { - XComponentPattern::OnModifyDone(); - return; - } - Pattern::OnModifyDone(); - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto renderContext = host->GetRenderContext(); - CHECK_NULL_VOID(renderContext); - CHECK_NULL_VOID(renderContextForSurface_); - auto bkColor = renderContext->GetBackgroundColor(); - if (bkColor.has_value()) { - bool isTransparent = bkColor.value().GetAlpha() < UINT8_MAX; - bkColor_ = isTransparent ? Color::TRANSPARENT : bkColor.value(); - } - renderContextForSurface_->UpdateBackgroundColor(bkColor_); -} - -void XComponentPatternV2::DumpInfo() -{ - if (usesSuperMethod_) { - XComponentPattern::DumpInfo(); - return; - } - DumpLog::GetInstance().AddDesc(std::string("autoInitialize: ").append(BoolToString(autoInitialize_))); - DumpLog::GetInstance().AddDesc(std::string("isInitialized: ").append(BoolToString(isInitialized_))); - DumpLog::GetInstance().AddDesc( - std::string("xcomponentNodeType: ").append(XComponentPattern::XComponentNodeTypeToString(nodeType_))); - DumpLog::GetInstance().AddDesc( - std::string("xcomponentType: ").append(XComponentPattern::XComponentTypeToString(type_))); - DumpLog::GetInstance().AddDesc(std::string("surfaceId: ").append(surfaceId_)); - DumpLog::GetInstance().AddDesc(std::string("surfaceRect: ").append(paintRect_.ToString())); -} - -void XComponentPatternV2::SetExpectedRateRange(int32_t min, int32_t max, int32_t expected) -{ - if (hasGotNativeXComponent_) { - return; - } - isNativeXComponentDisabled_ = true; - CHECK_NULL_VOID(displaySync_); - FrameRateRange frameRateRange; - frameRateRange.Set(min, max, expected); - displaySync_->SetExpectedFrameRateRange(frameRateRange); - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " SetExpectedFrameRateRange" - "{%{public}d, %{public}d, %{public}d}", displaySync_->GetId(), min, max, expected); -} - -void XComponentPatternV2::UpdateOnFrameEvent(void(*callback)(void*, uint64_t, uint64_t), void* arkuiNode) -{ - if (hasGotNativeXComponent_) { - return; - } - isNativeXComponentDisabled_ = true; - CHECK_NULL_VOID(displaySync_); - displaySync_->RegisterOnFrameWithData([weak = AceType::WeakClaim(this), - callback, arkuiNode](RefPtr displaySyncData) { - auto xComponentPattern = weak.Upgrade(); - CHECK_NULL_VOID(xComponentPattern); - CHECK_NULL_VOID(callback); - CHECK_NULL_VOID(arkuiNode); - CHECK_NULL_VOID(displaySyncData); - callback(arkuiNode, displaySyncData->GetTimestamp(), displaySyncData->GetTargetTimestamp()); - }); - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " RegisterOnFrame", - displaySync_->GetId()); - displaySync_->AddToPipelineOnContainer(); -} - -void XComponentPatternV2::UnregisterOnFrameEvent() -{ - if (hasGotNativeXComponent_) { - return; - } - isNativeXComponentDisabled_ = true; - CHECK_NULL_VOID(displaySync_); - displaySync_->UnregisterOnFrame(); - TAG_LOGD(AceLogTag::ACE_XCOMPONENT, "Id: %{public}" PRIu64 " UnregisterOnFrame", - displaySync_->GetId()); - displaySync_->DelFromPipelineOnContainer(); - needRecoverDisplaySync_ = false; -} - -void XComponentPatternV2::SetNeedSoftKeyboard(bool isNeedSoftKeyboard) -{ - if (hasGotNativeXComponent_) { - return; - } - isNativeXComponentDisabled_ = true; - isNeedSoftKeyboard_ = isNeedSoftKeyboard; -} - -void XComponentPatternV2::OnSurfaceShow() -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(surfaceHolder_); - auto callbackList = surfaceHolder_->surfaceCallbackList_; - for (const auto& iter : callbackList) { - auto callback = iter->onSurfaceShow; - if (callback) { - callback(surfaceHolder_); - } - } -} - -void XComponentPatternV2::OnSurfaceHide() -{ - CHECK_RUN_ON(UI); - CHECK_NULL_VOID(surfaceHolder_); - auto callbackList = surfaceHolder_->surfaceCallbackList_; - bool hasCallback = false; - for (const auto& iter : callbackList) { - auto callback = iter->onSurfaceHide; - if (callback) { - callback(surfaceHolder_); - hasCallback = true; - } - } - if (hasCallback) { - CHECK_NULL_VOID(renderSurface_); - renderSurface_->ReleaseSurfaceBuffers(); - } -} - -void XComponentPatternV2::ResetAndInitializeNodeHandleAccessibility() -{ - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto pipeline = host->GetContextRefPtr(); - CHECK_NULL_VOID(pipeline); - auto accessibilityManager = pipeline->GetAccessibilityManager(); - CHECK_NULL_VOID(accessibilityManager); - if (accessibilityChildTreeCallback_) { - accessibilityManager->DeregisterAccessibilityChildTreeCallback( - accessibilityChildTreeCallback_->GetAccessibilityId() - ); - accessibilityChildTreeCallback_.reset(); - accessibilityChildTreeCallback_ = nullptr; - } - - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeNodeHandleAccessibility"); - CHECK_NULL_VOID(arkuiAccessibilityProvider_); - arkuiAccessibilityProvider_->SetRegisterCallback( - [weak = WeakClaim(this)] (bool isRegister) { - auto pattern = weak.Upgrade(); - CHECK_NULL_VOID(pattern); - pattern->HandleRegisterAccessibilityEvent(isRegister); - }); - int64_t accessibilityId = host->GetAccessibilityId(); - TAG_LOGI(AceLogTag::ACE_XCOMPONENT, - "InitializeNodeHandleAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId); - accessibilityChildTreeCallback_ = std::make_shared( - WeakClaim(this), host->GetAccessibilityId()); - accessibilityManager->RegisterAccessibilityChildTreeCallback( - accessibilityId, accessibilityChildTreeCallback_); - if (accessibilityManager->IsRegister()) { - accessibilityChildTreeCallback_->OnRegister( - pipeline->GetWindowId(), accessibilityManager->GetTreeId()); - } -} - -ArkUI_AccessibilityProvider* XComponentPatternV2::CreateAccessibilityProvider() -{ - if (hasGotNativeXComponent_) { - return nullptr; - } - isNativeXComponentDisabled_ = true; - useNodeHandleAccessibilityProvider_ = true; - if (arkuiAccessibilityProvider_) { - return arkuiAccessibilityProvider_; - } - auto host = GetHost(); - CHECK_NULL_RETURN(host, nullptr); - arkuiAccessibilityProvider_ = new ArkUI_AccessibilityProvider(); - XComponentPatternV2::XComponentAccessibilityProviderMap[arkuiAccessibilityProvider_] = WeakPtr(host); - ResetAndInitializeNodeHandleAccessibility(); - return arkuiAccessibilityProvider_; -} - -void XComponentPatternV2::DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider) -{ - if (hasGotNativeXComponent_ || (provider != arkuiAccessibilityProvider_)) { - return; - } - CHECK_NULL_VOID(arkuiAccessibilityProvider_); - isNativeXComponentDisabled_ = true; - XComponentPatternV2::XComponentAccessibilityProviderMap.erase(arkuiAccessibilityProvider_); - auto host = GetHost(); - UninitializeAccessibility(AceType::RawPtr(host)); - delete arkuiAccessibilityProvider_; - arkuiAccessibilityProvider_ = nullptr; -} - -std::unordered_map> XComponentPatternV2::XComponentAccessibilityProviderMap; - -FrameNode* XComponentPatternV2::QueryAccessibilityProviderHost(void* provider, bool& isProviderValied) -{ - auto it = XComponentAccessibilityProviderMap.find(provider); - if (it == XComponentAccessibilityProviderMap.end()) { - isProviderValied = false; - return nullptr; - } - isProviderValied = true; - auto weakHost = it->second; - auto host = weakHost.Upgrade(); - CHECK_NULL_RETURN(host, nullptr); - return AceType::RawPtr(host); -} -} // namespace OHOS::Ace::NG \ No newline at end of file diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.cpp new file mode 100644 index 00000000000..40277fb8be0 --- /dev/null +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h" + +#include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_child_tree_callback.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" + +namespace OHOS::Ace::NG { +void XComponentProxy::InitializeAccessibility() +{ + if (accessibilityChildTreeCallback_) { + return; + } + InitializeAccessibilityCallback(); + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + auto host = xcPattern->GetHost(); + CHECK_NULL_VOID(host); + int64_t accessibilityId = host->GetAccessibilityId(); + TAG_LOGI( + AceLogTag::ACE_XCOMPONENT, "InitializeAccessibility accessibilityId: %{public}" PRId64 "", accessibilityId); + auto pipeline = host->GetContextRefPtr(); + CHECK_NULL_VOID(pipeline); + auto accessibilityManager = pipeline->GetAccessibilityManager(); + CHECK_NULL_VOID(accessibilityManager); + accessibilityChildTreeCallback_ = + std::make_shared(pattern_, host->GetAccessibilityId()); + accessibilityManager->RegisterAccessibilityChildTreeCallback(accessibilityId, accessibilityChildTreeCallback_); + if (accessibilityManager->IsRegister()) { + accessibilityChildTreeCallback_->OnRegister(pipeline->GetWindowId(), accessibilityManager->GetTreeId()); + } +} + +void XComponentProxy::InitializeAccessibilityCallback() +{ + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "InitializeAccessibilityCallback"); + auto* nativeProvider = GetAccessibilityProvider(); + CHECK_NULL_VOID(nativeProvider); + nativeProvider->SetRegisterCallback([weak = WeakClaim(this)](bool isRegister) { + auto proxy = weak.Upgrade(); + CHECK_NULL_VOID(proxy); + proxy->HandleRegisterAccessibilityEvent(isRegister); + }); +} + +void XComponentProxy::HandleRegisterAccessibilityEvent(bool isRegister) +{ + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "HandleRegisterAccessibilityEvent, isRegister: %{public}d.", isRegister); + CHECK_NULL_VOID(accessibilityChildTreeCallback_); + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + auto host = xcPattern->GetHost(); + CHECK_NULL_VOID(host); + auto pipeline = host->GetContextRefPtr(); + CHECK_NULL_VOID(pipeline); + auto accessibilityManager = pipeline->GetAccessibilityManager(); + CHECK_NULL_VOID(accessibilityManager); + if (!isRegister) { + accessibilityChildTreeCallback_->OnDeregister(); + return; + } + if (accessibilityManager->IsRegister()) { + accessibilityChildTreeCallback_->OnRegister(pipeline->GetWindowId(), accessibilityManager->GetTreeId()); + } +} + +void XComponentProxy::ResetAccessibilityChildTreeCallback() +{ + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + auto host = xcPattern->GetHost(); + CHECK_NULL_VOID(host); + auto pipeline = host->GetContextRefPtr(); + CHECK_NULL_VOID(pipeline); + auto accessibilityManager = pipeline->GetAccessibilityManager(); + if (accessibilityChildTreeCallback_) { + accessibilityManager->DeregisterAccessibilityChildTreeCallback( + accessibilityChildTreeCallback_->GetAccessibilityId()); + accessibilityChildTreeCallback_.reset(); + accessibilityChildTreeCallback_ = nullptr; + } +} + +void XComponentProxy::UninitializeAccessibility(FrameNode* frameNode) +{ + TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "UninitializeAccessibility"); + CHECK_NULL_VOID(frameNode); + int64_t accessibilityId = frameNode->GetAccessibilityId(); + auto pipeline = frameNode->GetContextRefPtr(); + CHECK_NULL_VOID(pipeline); + auto accessibilityManager = pipeline->GetAccessibilityManager(); + CHECK_NULL_VOID(accessibilityManager); + if (accessibilityManager->IsRegister() && accessibilityChildTreeCallback_) { + accessibilityChildTreeCallback_->OnDeregister(); + } + accessibilityManager->DeregisterAccessibilityChildTreeCallback(accessibilityId); + accessibilityChildTreeCallback_ = nullptr; +} +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h new file mode 100644 index 00000000000..ca3bb986677 --- /dev/null +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_H + +#include "ui/base/ace_type.h" +#include "ui/base/referenced.h" + +#include "core/accessibility/accessibility_manager.h" +#include "core/accessibility/native_interface_accessibility_provider.h" +#include "core/components_ng/base/frame_node.h" + +namespace OHOS::Ace::NG { + +class XComponentPattern; +class XComponentProxy : public AceType { + DECLARE_ACE_TYPE(XComponentProxy, AceType); + +public: + XComponentProxy() = delete; + explicit XComponentProxy(const WeakPtr& pattern) : pattern_(pattern) {}; + ~XComponentProxy() override = default; + + virtual void OnAttachToMainTree() = 0; + virtual void OnDetachFromMainTree() = 0; + virtual void OnSizeChange( + bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) = 0; + virtual bool NeedSoftKeyboard() = 0; + virtual ArkUI_AccessibilityProvider* GetAccessibilityProvider() const = 0; + virtual void DispatchSurfaceCreatedEvent(int32_t width, int32_t height, void* nativeWindow) = 0; + virtual void DispatchSurfaceChangedEvent(int32_t width, int32_t height) = 0; + virtual void DispatchSurfaceDestroyedEvent() = 0; + virtual void DispatchSurfaceShowEvent() = 0; + // return true means need to release buffer + virtual bool DispatchSurfaceHideEvent() = 0; + + // only CNODE&XComponentProxyNativeXComponent may return true + bool IsToggleEnabled() const + { + return toggleEnabled_; + } + void SetToggleEnabled(bool toggleEnabled) + { + toggleEnabled_ = toggleEnabled; + } + void InitializeAccessibility(); + void UninitializeAccessibility(FrameNode* frameNode); + +protected: + void HandleRegisterAccessibilityEvent(bool isRegister); + void ResetAccessibilityChildTreeCallback(); + WeakPtr pattern_ = nullptr; + +private: + void InitializeAccessibilityCallback(); + // true means XComponentProxy can toggle between XComponentProxyNativeXComponent and XComponentProxySurfaceHolder + bool toggleEnabled_ = false; + std::shared_ptr accessibilityChildTreeCallback_; +}; +} // namespace OHOS::Ace::NG +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_H diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.cpp new file mode 100644 index 00000000000..961a16ecdc3 --- /dev/null +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.cpp @@ -0,0 +1,304 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h" + +#include "interfaces/native/event/ui_input_event_impl.h" +#include "interfaces/native/ui_input_event.h" +#include "ui/base/utils/utils.h" + +#include "core/accessibility/native_interface_accessibility_provider.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_utils.h" + +namespace OHOS::Ace::NG { +void XComponentProxyNativeXComponent::OnAttachToMainTree() +{ + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + if (xcPattern->GetNodeType() == XComponentNodeType::TYPE_NODE && + xcPattern->GetSurfaceCallbackMode() == SurfaceCallbackMode::DEFAULT) { + xcPattern->HandleSurfaceCreated(); + } +} +void XComponentProxyNativeXComponent::OnDetachFromMainTree() +{ + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + if (xcPattern->GetNodeType() == XComponentNodeType::TYPE_NODE && + xcPattern->GetSurfaceCallbackMode() == SurfaceCallbackMode::DEFAULT) { + xcPattern->HandleSurfaceDestroyed(); + } +} +void XComponentProxyNativeXComponent::OnSizeChange( + bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) +{ + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->OnSizeChange(offsetChanged, sizeChanged, frameOffsetChange, needFireNativeEvent); +} + +bool XComponentProxyNativeXComponent::NeedSoftKeyboard() +{ + return nativeXComponentImpl_ ? nativeXComponentImpl_->IsNeedSoftKeyboard() : false; +} + +void XComponentProxyNativeXComponent::SetExpectedRateRangeInit() +{ + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetExpectedRateRangeEventCallback([weak = pattern_]() { + auto xComponentPattern = weak.Upgrade(); + CHECK_NULL_VOID(xComponentPattern); + xComponentPattern->HandleSetExpectedRateRangeEvent(); + }); +} +void XComponentProxyNativeXComponent::OnFrameEventInit() +{ + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetOnFrameEventCallback([weak = pattern_]() { + auto xComponentPattern = weak.Upgrade(); + CHECK_NULL_VOID(xComponentPattern); + xComponentPattern->HandleOnFrameEvent(); + }); +} +void XComponentProxyNativeXComponent::UnregisterOnFrameEventInit() +{ + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetUnregisterOnFrameEventCallback([weak = pattern_]() { + auto xComponentPattern = weak.Upgrade(); + CHECK_NULL_VOID(xComponentPattern); + xComponentPattern->HandleUnregisterOnFrameEvent(); + }); +} + +void XComponentProxyNativeXComponent::SetNativeXComponentOffset(double offsetX, double offsetY) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetXComponentOffsetX(offsetX); + nativeXComponentImpl_->SetXComponentOffsetY(offsetY); +} + +void XComponentProxyNativeXComponent::DispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent, + const std::vector& xComponentTouchPoints, + const std::vector& historicalPoints, + std::pair&& curSourceType) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetHistoricalPoint(historicalPoints); + nativeXComponentImpl_->SetCurrentSourceType(std::move(curSourceType)); + nativeXComponentImpl_->SetTouchEvent(touchEvent); + nativeXComponentImpl_->SetTouchPoint(xComponentTouchPoints); + auto* nativeWindow = const_cast(nativeXComponentImpl_->GetSurface()); + const auto* callback = nativeXComponentImpl_->GetCallback(); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(callback->DispatchTouchEvent); + callback->DispatchTouchEvent(nativeXComponent_.get(), nativeWindow); +} + +void XComponentProxyNativeXComponent::RegisterNodeCallback(void* xcFrameNode) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + + nativeXComponentImpl_->registerContaner(xcFrameNode); + + auto OnAttachRootNativeNode = [](void* container, void* root) { + ContainerScope scope(Container::CurrentIdSafely()); + auto node = AceType::Claim(reinterpret_cast(root)); + CHECK_NULL_VOID(node); + auto host = AceType::Claim(reinterpret_cast(container)); + CHECK_NULL_VOID(host); + host->AddChild(node); + host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE); + }; + + auto OnDetachRootNativeNode = [](void* container, void* root) { + ContainerScope scope(Container::CurrentIdSafely()); + auto node = AceType::Claim(reinterpret_cast(root)); + CHECK_NULL_VOID(node); + auto host = AceType::Claim(reinterpret_cast(container)); + CHECK_NULL_VOID(host); + host->RemoveChild(node); + }; + + nativeXComponentImpl_->registerNativeNodeCallbacks( + std::move(OnAttachRootNativeNode), std::move(OnDetachRootNativeNode)); +} + +void XComponentProxyNativeXComponent::DispatchFocusEvent() +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + auto* nativeWindow = const_cast(nativeXComponentImpl_->GetSurface()); + const auto focusEventCallback = nativeXComponentImpl_->GetFocusEventCallback(); + CHECK_NULL_VOID(focusEventCallback); + focusEventCallback(nativeXComponent_.get(), nativeWindow); +} +bool XComponentProxyNativeXComponent::DispatchKeyEvent(const KeyEvent& event) +{ + CHECK_NULL_RETURN(nativeXComponent_, false); + CHECK_NULL_RETURN(nativeXComponentImpl_, false); + + OH_NativeXComponent_KeyEvent keyEvent = XComponentUtils::ConvertNativeXComponentKeyEvent(event); + nativeXComponentImpl_->SetKeyEvent(keyEvent); + + auto* nativeWindow = const_cast(nativeXComponentImpl_->GetSurface()); + const auto keyEventCallbackWithResult = nativeXComponentImpl_->GetKeyEventCallbackWithResult(); + if (keyEventCallbackWithResult) { + return keyEventCallbackWithResult(nativeXComponent_.get(), nativeWindow); + } + const auto keyEventCallback = nativeXComponentImpl_->GetKeyEventCallback(); + CHECK_NULL_RETURN(keyEventCallback, false); + keyEventCallback(nativeXComponent_.get(), nativeWindow); + return false; +} +void XComponentProxyNativeXComponent::DispatchBlurEvent() +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); + const auto blurEventCallback = nativeXComponentImpl_->GetBlurEventCallback(); + CHECK_NULL_VOID(blurEventCallback); + blurEventCallback(nativeXComponent_.get(), surface); +} + +HitTestMode XComponentProxyNativeXComponent::InitOnTouchIntercept( + const TouchEventInfo& touchEvent, HitTestMode defaultHitTestMode) +{ + CHECK_NULL_RETURN(nativeXComponent_, defaultHitTestMode); + CHECK_NULL_RETURN(nativeXComponentImpl_, defaultHitTestMode); + const auto onTouchInterceptCallback = nativeXComponentImpl_->GetOnTouchInterceptCallback(); + CHECK_NULL_RETURN(onTouchInterceptCallback, defaultHitTestMode); + auto event = touchEvent.ConvertToTouchEvent(); + ArkUI_UIInputEvent uiEvent { ARKUI_UIINPUTEVENT_TYPE_TOUCH, TOUCH_EVENT_ID, &event, false, + Container::GetCurrentApiTargetVersion() }; + return static_cast(onTouchInterceptCallback(nativeXComponent_.get(), &uiEvent)); +} + +void XComponentProxyNativeXComponent::DispatchMouseHoverEvent(bool isHover) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + const auto* callback = nativeXComponentImpl_->GetMouseEventCallback(); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(callback->DispatchHoverEvent); + callback->DispatchHoverEvent(nativeXComponent_.get(), isHover); +} + +void XComponentProxyNativeXComponent::DispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent, + const OH_NativeXComponent_ExtraMouseEventInfo& extraMouseEventInfo) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetMouseEvent(mouseEvent); + nativeXComponentImpl_->SetExtraMouseEventInfo(extraMouseEventInfo); + auto* nativeWindow = const_cast(nativeXComponentImpl_->GetSurface()); + const auto* callback = nativeXComponentImpl_->GetMouseEventCallback(); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(callback->DispatchMouseEvent); + callback->DispatchMouseEvent(nativeXComponent_.get(), nativeWindow); +} + +void XComponentProxyNativeXComponent::DispatchAxisEvent(AxisEvent* axisEvent) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + const auto callback = nativeXComponentImpl_->GetUIAxisEventCallback(); + CHECK_NULL_VOID(callback); + ArkUI_UIInputEvent uiEvent { ARKUI_UIINPUTEVENT_TYPE_AXIS, AXIS_EVENT_ID, axisEvent, false, + Container::GetCurrentApiTargetVersion() }; + callback(nativeXComponent_.get(), &uiEvent, ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS); +} + +OH_NativeXComponent_ExpectedRateRange* XComponentProxyNativeXComponent::GetRateRange() +{ + CHECK_NULL_RETURN(nativeXComponent_, nullptr); + CHECK_NULL_RETURN(nativeXComponentImpl_, nullptr); + return nativeXComponentImpl_->GetRateRange(); +} + +void XComponentProxyNativeXComponent::DispatchOnFrameEvent(int64_t timestamp, int64_t targetTimestamp) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + const auto callback = nativeXComponentImpl_->GetOnFrameCallback(); + CHECK_NULL_VOID(callback); + callback(nativeXComponent_.get(), timestamp, targetTimestamp); +} + +void XComponentProxyNativeXComponent::DispatchSurfaceCreatedEvent(int32_t width, int32_t height, void* nativeWindow) +{ + CHECK_NULL_VOID(nativeXComponentImpl_); + CHECK_NULL_VOID(nativeXComponent_); + nativeXComponentImpl_->SetXComponentWidth(static_cast(width)); + nativeXComponentImpl_->SetXComponentHeight(static_cast(height)); + nativeXComponentImpl_->SetSurface(nativeWindow); + const auto* callback = nativeXComponentImpl_->GetCallback(); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(callback->OnSurfaceCreated); + callback->OnSurfaceCreated(nativeXComponent_.get(), nativeWindow); +} +void XComponentProxyNativeXComponent::DispatchSurfaceChangedEvent(int32_t width, int32_t height) +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + nativeXComponentImpl_->SetXComponentWidth(static_cast(width)); + nativeXComponentImpl_->SetXComponentHeight(static_cast(height)); + auto* nativeWindow = const_cast(nativeXComponentImpl_->GetSurface()); + const auto* callback = nativeXComponentImpl_->GetCallback(); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(callback->OnSurfaceChanged); + callback->OnSurfaceChanged(nativeXComponent_.get(), nativeWindow); +} +void XComponentProxyNativeXComponent::DispatchSurfaceDestroyedEvent() +{ + CHECK_NULL_VOID(nativeXComponent_); + CHECK_NULL_VOID(nativeXComponentImpl_); + auto* nativeWindow = const_cast(nativeXComponentImpl_->GetSurface()); + const auto* callback = nativeXComponentImpl_->GetCallback(); + CHECK_NULL_VOID(callback); + CHECK_NULL_VOID(callback->OnSurfaceDestroyed); + callback->OnSurfaceDestroyed(nativeXComponent_.get(), nativeWindow); + nativeXComponentImpl_->SetSurface(nullptr); +} +void XComponentProxyNativeXComponent::DispatchSurfaceShowEvent() +{ + CHECK_NULL_VOID(nativeXComponentImpl_); + CHECK_NULL_VOID(nativeXComponent_); + auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); + const auto surfaceShowCallback = nativeXComponentImpl_->GetSurfaceShowCallback(); + CHECK_NULL_VOID(surfaceShowCallback); + surfaceShowCallback(nativeXComponent_.get(), surface); +} +bool XComponentProxyNativeXComponent::DispatchSurfaceHideEvent() +{ + CHECK_NULL_RETURN(nativeXComponent_, false); + CHECK_NULL_RETURN(nativeXComponentImpl_, false); + auto* surface = const_cast(nativeXComponentImpl_->GetSurface()); + const auto surfaceHideCallback = nativeXComponentImpl_->GetSurfaceHideCallback(); + CHECK_NULL_RETURN(surfaceHideCallback, false); + surfaceHideCallback(nativeXComponent_.get(), surface); + return true; +} + +ArkUI_AccessibilityProvider* XComponentProxyNativeXComponent::GetAccessibilityProvider() const +{ + CHECK_NULL_RETURN(nativeXComponent_, nullptr); + CHECK_NULL_RETURN(nativeXComponentImpl_, nullptr); + return nativeXComponentImpl_->GetAccessibilityProvider().get(); +} +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h new file mode 100644 index 00000000000..3a76476535f --- /dev/null +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_NATIVE_XCOMPONENT_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_NATIVE_XCOMPONENT_H + +#include "interfaces/native/native_interface_xcomponent.h" +#include "ui/base/ace_type.h" + +#include "core/components/xcomponent/native_interface_xcomponent_impl.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h" + +namespace OHOS::Ace::NG { + +class XComponentProxyNativeXComponent : public XComponentProxy { + DECLARE_ACE_TYPE(XComponentProxyNativeXComponent, XComponentProxy); + +public: + explicit XComponentProxyNativeXComponent(const WeakPtr& pattern) : XComponentProxy(pattern) {} + ~XComponentProxyNativeXComponent() override = default; + + void OnAttachToMainTree() override; + void OnDetachFromMainTree() override; + void OnSizeChange(bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) override; + bool NeedSoftKeyboard() override; + ArkUI_AccessibilityProvider* GetAccessibilityProvider() const override; + void DispatchSurfaceCreatedEvent(int32_t width, int32_t height, void* nativeWindow) override; + void DispatchSurfaceChangedEvent(int32_t width, int32_t height) override; + void DispatchSurfaceDestroyedEvent() override; + void DispatchSurfaceShowEvent() override; + // return true means need to release buffer + bool DispatchSurfaceHideEvent() override; + + std::pair, std::weak_ptr> GetOrCreateNativeXComponent() + { + if (!nativeXComponent_ || !nativeXComponentImpl_) { + nativeXComponentImpl_ = AceType::MakeRefPtr(); + nativeXComponent_ = std::make_shared(AceType::RawPtr(nativeXComponentImpl_)); + } + return std::make_pair(nativeXComponentImpl_, nativeXComponent_); + } + void SetExpectedRateRangeInit(); + void OnFrameEventInit(); + void UnregisterOnFrameEventInit(); + + void SetNativeXComponentOffset(double offsetX, double offsetY); + + void DispatchTouchEvent(const OH_NativeXComponent_TouchEvent& touchEvent, + const std::vector& xComponentTouchPoints, + const std::vector& historicalPoints, + std::pair&& curSourceType); + + // for type is node which is deprecated + void RegisterNodeCallback(void* xcFrameNode); + + void DispatchFocusEvent(); + bool DispatchKeyEvent(const KeyEvent& event); + void DispatchBlurEvent(); + + HitTestMode InitOnTouchIntercept(const TouchEventInfo& touchEvent, HitTestMode defaultHitTestMode); + + void DispatchMouseHoverEvent(bool isHover); + void DispatchMouseEvent(const OH_NativeXComponent_MouseEvent& mouseEvent, + const OH_NativeXComponent_ExtraMouseEventInfo& extraMouseEventInfo); + void DispatchAxisEvent(AxisEvent* axisEvent); + + OH_NativeXComponent_ExpectedRateRange* GetRateRange(); + + void DispatchOnFrameEvent(int64_t timestamp, int64_t targetTimestamp); + +private: + std::shared_ptr nativeXComponent_ = nullptr; + RefPtr nativeXComponentImpl_ = nullptr; +}; +} // namespace OHOS::Ace::NG +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_NATIVE_XCOMPONENT_H diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.cpp b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.cpp new file mode 100644 index 00000000000..280002af6cb --- /dev/null +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.cpp @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.h" + +#include "ui/base/utils/utils.h" + +#include "core/components_ng/pattern/xcomponent/xcomponent_accessibility_child_tree_callback.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" + +namespace OHOS::Ace::NG { +void XComponentProxySurfaceHolder::OnAttachToMainTree() +{ + if (autoInitialize_) { + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->HandleSurfaceCreatedForSurfaceHolder(); + } +} +void XComponentProxySurfaceHolder::OnDetachFromMainTree() +{ + if (autoInitialize_) { + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->HandleSurfaceDestroyedForSurfaceHolder(); + } +} +void XComponentProxySurfaceHolder::OnSizeChange([[maybe_unused]] bool offsetChanged, bool sizeChanged, + [[maybe_unused]] bool frameOffsetChange, [[maybe_unused]] bool needFireNativeEvent) +{ + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + xcPattern->HandleSurfaceChangeEventForSurfaceHolder(sizeChanged); +} + +bool XComponentProxySurfaceHolder::NeedSoftKeyboard() +{ + return isNeedSoftKeyboard_; +} + +void XComponentProxySurfaceHolder::SetNativeWindow(void* nativeWindow) +{ + if (surfaceHolder_) { + surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow); + } +} + +void XComponentProxySurfaceHolder::DispatchSurfaceCreatedEvent( + [[maybe_unused]] int32_t width, [[maybe_unused]] int32_t height, void* nativeWindow) +{ + CHECK_NULL_VOID(surfaceHolder_); + surfaceHolder_->nativeWindow_ = reinterpret_cast(nativeWindow); + isInitialized_ = true; + auto callbackList = surfaceHolder_->surfaceCallbackList_; + for (const auto& iter : callbackList) { + auto callback = iter->OnSurfaceCreated; + if (callback) { + callback(surfaceHolder_); + } + } +} +void XComponentProxySurfaceHolder::DispatchSurfaceChangedEvent(int32_t width, int32_t height) +{ + CHECK_NULL_VOID(surfaceHolder_); + CHECK_EQUAL_VOID(isInitialized_, false); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + for (const auto& iter : callbackList) { + auto callback = iter->OnSurfaceChanged; + if (callback) { + callback(surfaceHolder_, width, height); + } + } +} +void XComponentProxySurfaceHolder::DispatchSurfaceDestroyedEvent() +{ + isInitialized_ = false; + CHECK_NULL_VOID(surfaceHolder_); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + for (const auto& iter : callbackList) { + auto callback = iter->OnSurfaceDestroyed; + if (callback) { + callback(surfaceHolder_); + } + } +} +void XComponentProxySurfaceHolder::DispatchSurfaceShowEvent() +{ + CHECK_NULL_VOID(surfaceHolder_); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + for (const auto& iter : callbackList) { + auto callback = iter->onSurfaceShow; + if (callback) { + callback(surfaceHolder_); + } + } +} +// return true means need to release buffer +bool XComponentProxySurfaceHolder::DispatchSurfaceHideEvent() +{ + CHECK_NULL_RETURN(surfaceHolder_, false); + auto callbackList = surfaceHolder_->surfaceCallbackList_; + bool hasCallback = false; + for (const auto& iter : callbackList) { + auto callback = iter->onSurfaceHide; + if (callback) { + callback(surfaceHolder_); + hasCallback = true; + } + } + return hasCallback; +} + +void XComponentProxySurfaceHolder::SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder) +{ + surfaceHolder_ = surfaceHolder; +} + +ArkUI_AccessibilityProvider* XComponentProxySurfaceHolder::GetAccessibilityProvider() const +{ + return arkuiAccessibilityProvider_; +} + +void XComponentProxySurfaceHolder::SetAutoInitialize(bool autoInitialize) +{ + autoInitialize_ = autoInitialize; +} + +std::unordered_map> XComponentProxySurfaceHolder::XComponentAccessibilityProviderMap; + +ArkUI_AccessibilityProvider* XComponentProxySurfaceHolder::CreateAccessibilityProvider() +{ + if (arkuiAccessibilityProvider_) { + return arkuiAccessibilityProvider_; + } + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_RETURN(xcPattern, nullptr); + auto host = xcPattern->GetHost(); + CHECK_NULL_RETURN(host, nullptr); + arkuiAccessibilityProvider_ = new ArkUI_AccessibilityProvider(); + XComponentAccessibilityProviderMap[arkuiAccessibilityProvider_] = WeakPtr(host); + ResetAccessibilityChildTreeCallback(); + InitializeAccessibility(); + return arkuiAccessibilityProvider_; +} + +void XComponentProxySurfaceHolder::DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider) +{ + if (provider != arkuiAccessibilityProvider_) { + return; + } + CHECK_NULL_VOID(arkuiAccessibilityProvider_); + XComponentAccessibilityProviderMap.erase(arkuiAccessibilityProvider_); + auto xcPattern = pattern_.Upgrade(); + CHECK_NULL_VOID(xcPattern); + auto host = xcPattern->GetHost(); + UninitializeAccessibility(AceType::RawPtr(host)); + delete arkuiAccessibilityProvider_; + arkuiAccessibilityProvider_ = nullptr; +} + +FrameNode* XComponentProxySurfaceHolder::QueryAccessibilityProviderHost(void* provider, bool& isProviderValied) +{ + auto it = XComponentAccessibilityProviderMap.find(provider); + if (it == XComponentAccessibilityProviderMap.end()) { + isProviderValied = false; + return nullptr; + } + isProviderValied = true; + auto weakHost = it->second; + auto host = weakHost.Upgrade(); + CHECK_NULL_RETURN(host, nullptr); + return AceType::RawPtr(host); +} + +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.h similarity index 31% rename from frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h rename to frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.h index 3fc28715a30..7207eec46b1 100644 --- a/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h +++ b/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.h @@ -13,114 +13,64 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_V2_H -#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_V2_H -#include "core/components/common/layout/constants.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" -#include "core/accessibility/native_interface_accessibility_provider.h" +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_SURFACE_HOLDER_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_SURFACE_HOLDER_H + +#include "ui/base/ace_type.h" -namespace OHOS::Rosen { -class Session; -class RSNode; -class RSTransaction; -class RSTransactionHandler; -class RSUIContext; -} // namespace OHOS::Rosen +#include "core/accessibility/accessibility_manager.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.h" +#include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" namespace OHOS::Ace::NG { -class XComponentPatternV2 : public XComponentPattern { - DECLARE_ACE_TYPE(XComponentPatternV2, XComponentPattern); -public: - XComponentPatternV2() = delete; - explicit XComponentPatternV2(XComponentNodeType nodeType); - void InitParams(XComponentType type); - ~XComponentPatternV2() override = default; - void SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder); - OH_ArkUI_SurfaceHolder* GetSurfaceHolder(); - int32_t Initialize(); - int32_t Finalize(); - int32_t SetAutoInitialize(bool autoInitialize); - int32_t IsInitialized(bool& isInitialized); +class FrameNode; +class XComponentProxySurfaceHolder : public XComponentProxy { + DECLARE_ACE_TYPE(XComponentProxySurfaceHolder, XComponentProxy); - bool IsCreateSurfaceHolderForbidden() +public: + explicit XComponentProxySurfaceHolder(const WeakPtr& pattern) : XComponentProxy(pattern) {} + ~XComponentProxySurfaceHolder() override = default; + static FrameNode* QueryAccessibilityProviderHost(void* provider, bool& isProviderValied); + void OnAttachToMainTree() override; + void OnDetachFromMainTree() override; + void OnSizeChange(bool offsetChanged, bool sizeChanged, bool frameOffsetChange, bool needFireNativeEvent) override; + bool NeedSoftKeyboard() override; + ArkUI_AccessibilityProvider* GetAccessibilityProvider() const override; + void DispatchSurfaceCreatedEvent(int32_t width, int32_t height, void* nativeWindow) override; + void DispatchSurfaceChangedEvent(int32_t width, int32_t height) override; + void DispatchSurfaceDestroyedEvent() override; + void DispatchSurfaceShowEvent() override; + // return true means need to release buffer + bool DispatchSurfaceHideEvent() override; + + void SetNeedSoftKeyboard(bool isNeedSoftKeyboard) { - return (hasGotNativeXComponent_ || usesSuperMethod_); + isNeedSoftKeyboard_ = isNeedSoftKeyboard; } - - XComponentNodeType GetXComponentNodeType() const + bool IsInitialized() const { - return nodeType_; + return isInitialized_; } - bool IsBindNative() override + OH_ArkUI_SurfaceHolder* GetSurfaceHolder() const { - return true; + return surfaceHolder_; } - - void SetExpectedRateRange(int32_t min, int32_t max, int32_t expected); - - void UpdateOnFrameEvent(void(*callback)(void*, uint64_t, uint64_t), void* arkuiNode); - - void UnregisterOnFrameEvent(); - - void SetNeedSoftKeyboard(bool isNeedSoftKeyboard); + void SetSurfaceHolder(OH_ArkUI_SurfaceHolder* surfaceHolder); + void SetNativeWindow(void* nativeWindow); + void SetAutoInitialize(bool autoInitialize); ArkUI_AccessibilityProvider* CreateAccessibilityProvider(); - void DisposeAccessibilityProvider(ArkUI_AccessibilityProvider* provider); - static FrameNode* QueryAccessibilityProviderHost(void* provider, bool& isProviderValied); - private: - void OnAttachToFrameNode() override; - void OnAttachToMainTree() override; - void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override; - void OnDetachFromMainTree() override; - void OnDetachFromFrameNode(FrameNode* frameNode) override; - void OnWindowHide() override; - void OnWindowShow() override; - void OnRebuildFrame() override; - void OnModifyDone() override; - void DumpInfo() override; - - void InitSurface(); - void DisposeSurface(); - int32_t HandleSurfaceCreated(); - int32_t HandleSurfaceDestroyed(); - void InitializeRenderContext(); - std::pair UpdateSurfaceRect(); - void HandleSurfaceChangeEvent(bool offsetChanged, bool sizeChanged, bool frameOffsetChange); - void XComponentSizeChange(const RectF& surfaceRect); - void OnSurfaceChanged(const RectF& surfaceRect); - std::shared_ptr GetRSUIContext(const RefPtr& frameNode); - void FlushImplicitTransaction(const RefPtr& frameNode); - std::shared_ptr GetRSTransactionHandler(const RefPtr& frameNode); - - void OnSurfaceShow(); - void OnSurfaceHide(); - void ResetAndInitializeNodeHandleAccessibility(); - - void UpdateUsesSuperMethod() - { - if (usesSuperMethod_) { - return; - } - usesSuperMethod_ = (isCNode_ && !isLifecycleInterfaceCalled_ && !surfaceHolder_); - } - + OH_ArkUI_SurfaceHolder* surfaceHolder_ = nullptr; bool autoInitialize_ = true; bool isInitialized_ = false; - bool isLifecycleInterfaceCalled_ = false; - bool usesSuperMethod_ = false; - bool needNotifySizeChanged_ = false; - OH_ArkUI_SurfaceHolder* surfaceHolder_ = nullptr; - XComponentNodeType nodeType_ = XComponentNodeType::UNKNOWN; - Color bkColor_ = Color::BLACK; - + bool isNeedSoftKeyboard_ = false; + ArkUI_AccessibilityProvider* arkuiAccessibilityProvider_ = nullptr; static std::unordered_map> XComponentAccessibilityProviderMap; }; } // namespace OHOS::Ace::NG - -#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_PATTERN_V2_H +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_XCOMPONENT_XCOMPONENT_PROXY_SURFACE_HOLDER_H diff --git a/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp b/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp index 735e710a422..8ca65b4b6bb 100644 --- a/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp +++ b/frameworks/core/interfaces/native/node/node_xcomponent_modifier.cpp @@ -155,16 +155,17 @@ ArkUI_Uint32 GetXComponentSurfaceHeight(ArkUINodeHandle node) void* GetNativeXComponent(ArkUINodeHandle node) { - auto *frameNode = reinterpret_cast(node); + auto* frameNode = reinterpret_cast(node); CHECK_NULL_RETURN(frameNode, nullptr); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, nullptr); - if (xcPattern->HasGotSurfaceHolder() || xcPattern->IsNativeXComponentDisabled()) { - return nullptr; - } auto pair = xcPattern->GetNativeXComponent(); - xcPattern->SetHasGotNativeXComponent(true); - return reinterpret_cast(pair.second.lock().get()); + auto nativeXComponent = pair.second.lock(); + if (nativeXComponent) { + xcPattern->SetHasGotNativeXComponent(true); + return reinterpret_cast(nativeXComponent.get()); + } + return nullptr; } void SetXComponentLibraryname(ArkUINodeHandle node, const char* libraryname) @@ -356,7 +357,7 @@ void* CreateSurfaceHolder(ArkUINodeHandle node) CHECK_NULL_RETURN(frameNode, nullptr); auto xcPattern = frameNode->GetPattern(); CHECK_NULL_RETURN(xcPattern, nullptr); - if (xcPattern->IsCreateSurfaceHolderForbidden()) { + if (xcPattern->IsUsingNativeXComponent()) { return nullptr; } OH_ArkUI_SurfaceHolder* surfaceHolder = xcPattern->GetSurfaceHolder(); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 51925a89af2..fd0647133dc 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1468,7 +1468,9 @@ ohos_source_set("ace_components_pattern") { "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp", "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_paint_method.cpp", "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp", - "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp", + "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy.cpp", + "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_native_xcomponent.cpp", + "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_proxy/xcomponent_proxy_surface_holder.cpp", "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_utils.cpp", "$ace_root/test/mock/core/pattern/mock_indexer_vibrator.cpp", "$ace_root/test/mock/core/pattern/mock_picker_haptic_factory.cpp", diff --git a/test/unittest/core/pattern/web/BUILD.gn b/test/unittest/core/pattern/web/BUILD.gn index 85fbef52886..86e49cf4388 100755 --- a/test/unittest/core/pattern/web/BUILD.gn +++ b/test/unittest/core/pattern/web/BUILD.gn @@ -631,7 +631,6 @@ ohos_unittest("web_pattern_unit_test_ohos") { "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_model_ng.cpp", "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_paint_method.cpp", "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.cpp", - "$ace_root/frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.cpp", "$ace_root/test/mock/adapter/mock_ace_container.cpp", "$ace_root/test/mock/core/pattern/mock_indexer_vibrator.cpp", "$ace_root/test/mock/core/pattern/mock_picker_haptic_factory.cpp", diff --git a/test/unittest/core/pattern/xcomponent/BUILD.gn b/test/unittest/core/pattern/xcomponent/BUILD.gn index 0f6e48464cb..9ce23a789ed 100644 --- a/test/unittest/core/pattern/xcomponent/BUILD.gn +++ b/test/unittest/core/pattern/xcomponent/BUILD.gn @@ -21,7 +21,6 @@ ace_unittest("xcomponent_test_ng") { "xcomponent_test_ng.cpp", "xcomponent_testtwo_ng.cpp", "xcomponent_utils_test_ng.cpp", - "xcomponent_v2_test_ng.cpp", ] external_deps = [ "bounds_checking_function:libsec_shared" ] diff --git a/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp b/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp index bb640d60150..f168c4df256 100644 --- a/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp +++ b/test/unittest/core/pattern/xcomponent/xcomponent_property_test_ng.cpp @@ -37,7 +37,6 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" #include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/components_ng/property/measure_property.h" #include "core/components_v2/inspector/inspector_constants.h" diff --git a/test/unittest/core/pattern/xcomponent/xcomponent_test_ng.cpp b/test/unittest/core/pattern/xcomponent/xcomponent_test_ng.cpp index c85ffc94df8..17decae57fc 100644 --- a/test/unittest/core/pattern/xcomponent/xcomponent_test_ng.cpp +++ b/test/unittest/core/pattern/xcomponent/xcomponent_test_ng.cpp @@ -35,7 +35,6 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" #include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/components_ng/pattern/xcomponent/xcomponent_inner_surface_controller.h" #include "core/components_ng/property/measure_property.h" @@ -1296,8 +1295,10 @@ HWTEST_F(XComponentTestNg, XComponentSourceTypeTest, TestSize.Level1) for (SourceType& sourceType : sourceTypes) { touchEventInfoSourceType.SetSourceDevice(sourceType); pattern->HandleTouchEvent(touchEventInfoSourceType); - EXPECT_EQ(pattern->nativeXComponentImpl_->curSourceType_.first, 0); - EXPECT_EQ(static_cast(pattern->nativeXComponentImpl_->curSourceType_.second), + auto xcomponentProxyNativeXComponent = AceType::DynamicCast(pattern->xcomponentProxy_); + EXPECT_TRUE(xcomponentProxyNativeXComponent); + EXPECT_EQ(xcomponentProxyNativeXComponent->nativeXComponentImpl_->curSourceType_.first, 0); + EXPECT_EQ(static_cast(xcomponentProxyNativeXComponent->nativeXComponentImpl_->curSourceType_.second), static_cast(ConvertNativeXComponentEventSourceType(sourceType))); } } @@ -1395,8 +1396,10 @@ HWTEST_F(XComponentTestNg, XComponentSurfaceLifeCycleCallback, TestSize.Level1) pattern->BeforeSyncGeometryProperties(config); EXPECT_STREQ(SURFACE_ID.c_str(), onSurfaceCreatedSurfaceId.c_str()); EXPECT_STREQ(SURFACE_ID.c_str(), onSurfaceChangedSurfaceId.c_str()); - EXPECT_FALSE(pattern->nativeXComponent_); - EXPECT_FALSE(pattern->nativeXComponentImpl_); + auto xcomponentProxyNativeXComponent = AceType::DynamicCast(pattern->xcomponentProxy_); + EXPECT_TRUE(xcomponentProxyNativeXComponent); + EXPECT_FALSE(xcomponentProxyNativeXComponent->nativeXComponent_); + EXPECT_FALSE(xcomponentProxyNativeXComponent->nativeXComponentImpl_); /** * @tc.steps: step3. call OnDetachFromFrameNode diff --git a/test/unittest/core/pattern/xcomponent/xcomponent_testtwo_ng.cpp b/test/unittest/core/pattern/xcomponent/xcomponent_testtwo_ng.cpp index 8fce1b2cfab..5d39b0d6787 100644 --- a/test/unittest/core/pattern/xcomponent/xcomponent_testtwo_ng.cpp +++ b/test/unittest/core/pattern/xcomponent/xcomponent_testtwo_ng.cpp @@ -38,7 +38,6 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" #include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/components_ng/property/measure_property.h" #include "core/components_v2/inspector/inspector_constants.h" @@ -487,7 +486,7 @@ HWTEST_F(XComponentTestTwoNg, ChangeSurfaceCallbackModeTest, TestSize.Level1) ASSERT_TRUE(frameNode); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); - pattern->isTypedNode_ = true; + pattern->nodeType_ = XComponentNodeType::TYPE_NODE; pattern->RegisterSurfaceCallbackModeEvent(); EXPECT_EQ(pattern->surfaceCallbackMode_, SurfaceCallbackMode::DEFAULT); @@ -766,10 +765,13 @@ HWTEST_F(XComponentTestTwoNg, NativeXComponentCallbackTest, TestSize.Level1) pattern->OnSurfaceCreated(); ASSERT_TRUE(hasSurfaceCreated); - pattern->OnSurfaceChanged(MAX_SURFACE_RECT, true); + pattern->OnSurfaceChanged(MAX_SURFACE_RECT); ASSERT_TRUE(hasSurfaceChanged); - pattern->NativeXComponentDispatchTouchEvent({}, {}); + // pattern->NativeXComponentDispatchTouchEvent({}, {}); + auto xcomponentProxyNativeXComponent = AceType::DynamicCast(pattern->xcomponentProxy_); + ASSERT_TRUE(xcomponentProxyNativeXComponent); + xcomponentProxyNativeXComponent->DispatchTouchEvent({}, {}, {}, {}); ASSERT_TRUE(hasDispatchTouchEvent); pattern->OnSurfaceDestroyed(); @@ -912,7 +914,7 @@ HWTEST_F(XComponentTestTwoNg, InitXComponentShouldCallInitNativeXComponentAndLoa ASSERT_TRUE(frameNode); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); - pattern->isTypedNode_ = true; + pattern->nodeType_ = XComponentNodeType::TYPE_NODE; XComponentModelNG().InitXComponent(Referenced::RawPtr(frameNode)); EXPECT_EQ(pattern->isNativeXComponent_, true); @@ -932,7 +934,7 @@ HWTEST_F(XComponentTestTwoNg, InitXComponentShouldCallInitNativeXComponentTest, ASSERT_TRUE(frameNode); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); - pattern->isTypedNode_ = true; + pattern->nodeType_ = XComponentNodeType::TYPE_NODE; XComponentModelNG().InitXComponent(Referenced::RawPtr(frameNode)); EXPECT_EQ(pattern->isNativeXComponent_, false); @@ -970,11 +972,11 @@ HWTEST_F(XComponentTestTwoNg, InitXComponentShouldCallInitAccessibilty, TestSize ASSERT_TRUE(frameNode); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); - pattern->isTypedNode_ = true; + pattern->nodeType_ = XComponentNodeType::TYPE_NODE; XComponentModelNG().InitXComponent(Referenced::RawPtr(frameNode)); EXPECT_EQ(pattern->isNativeXComponent_, false); - EXPECT_NE(pattern->accessibilityChildTreeCallback_, nullptr); + EXPECT_NE(pattern->xcomponentProxy_->accessibilityChildTreeCallback_, nullptr); } /** @@ -991,11 +993,11 @@ HWTEST_F(XComponentTestTwoNg, InitializeNotCallInitAccessibilty, TestSize.Level1 ASSERT_TRUE(frameNode); auto pattern = frameNode->GetPattern(); ASSERT_TRUE(pattern); - pattern->isTypedNode_ = true; - pattern->accessibilityChildTreeCallback_ = nullptr; + pattern->nodeType_ = XComponentNodeType::TYPE_NODE; + pattern->xcomponentProxy_->accessibilityChildTreeCallback_ = nullptr; pattern->Initialize(); - EXPECT_EQ(pattern->accessibilityChildTreeCallback_, nullptr); + EXPECT_EQ(pattern->xcomponentProxy_->accessibilityChildTreeCallback_, nullptr); } /** @@ -1881,7 +1883,7 @@ HWTEST_F(XComponentTestTwoNg, SetHasGotNativeXComponentTest, TestSize.Level1) * @tc.steps2: Check Param hasGotNativeXComponent_'s value. * @tc.expected: Param hasGotNativeXComponent_ equals true. */ - pattern->SetHasGotNativeXComponent(true); - EXPECT_TRUE(pattern->hasGotNativeXComponent_); + // pattern->SetHasGotNativeXComponent(true); + // EXPECT_TRUE(pattern->hasGotNativeXComponent_); } } // namespace OHOS::Ace::NG diff --git a/test/unittest/core/pattern/xcomponent/xcomponent_v2_test_ng.cpp b/test/unittest/core/pattern/xcomponent/xcomponent_v2_test_ng.cpp index 270f6cd7691..002de5f2213 100644 --- a/test/unittest/core/pattern/xcomponent/xcomponent_v2_test_ng.cpp +++ b/test/unittest/core/pattern/xcomponent/xcomponent_v2_test_ng.cpp @@ -32,7 +32,6 @@ #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h" #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h" #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h" -#include "core/components_ng/pattern/xcomponent/xcomponent_pattern_v2.h" #include "core/components_ng/pattern/xcomponent/xcomponent_surface_holder.h" #include "core/components_ng/property/measure_property.h" -- Gitee