!16349 确保pipeline析构在UI线程中执行且frontend析构在JS线程中执行

Merge pull request !16349 from Caoruihong/fix_pipeline_frontend_destruction
This commit is contained in:
openharmony_ci 2023-07-24 02:06:47 +00:00 committed by Gitee
commit 0fb1d0963b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
19 changed files with 66 additions and 59 deletions

View File

@ -127,11 +127,6 @@ public:
return eventHandler_;
}
FrontendType GetType() override
{
return type_;
}
// judge frontend is foreground frontend.
bool IsForeground() override
{
@ -205,7 +200,6 @@ protected:
void ParseManifest() const;
ColorMode colorMode_ = ColorMode::LIGHT;
FrontendType type_ = FrontendType::JS_CARD;
bool foregroundFrontend_ = false;
bool pageLoaded_ = false;
double density_ = 1.0;
@ -217,7 +211,6 @@ protected:
OnFormVisibleCallback onFormVisibleCallback_;
mutable std::once_flag onceFlag_;
RefPtr<TaskExecutor> taskExecutor_;
RefPtr<AceEventHandler> eventHandler_;
Framework::PageIdPool pageIdPool_;
std::string formSrc_;

View File

@ -182,6 +182,7 @@ bool DeclarativeFrontend::Initialize(FrontendType type, const RefPtr<TaskExecuto
{
LOGD("DeclarativeFrontend initialize begin.");
type_ = type;
taskExecutor_ = taskExecutor;
ACE_DCHECK(type_ == FrontendType::DECLARATIVE_JS);
InitializeFrontendDelegate(taskExecutor);

View File

@ -62,12 +62,12 @@ public:
void PushPage(const std::string& url, const std::string& params) override;
// Js frontend manages all pages self.
void AddPage(const RefPtr<AcePage>& page) override {};
void AddPage(const RefPtr<AcePage>& page) override {}
RefPtr<AcePage> GetPage(int32_t /*pageId*/) const override
{
return nullptr;
};
}
std::string GetCurrentPageUrl() const override;
@ -153,11 +153,6 @@ public:
RefPtr<AceEventHandler> GetEventHandler() override
{
return handler_;
};
FrontendType GetType() override
{
return type_;
}
// judge frontend is foreground frontend.
@ -237,7 +232,6 @@ protected:
private:
void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor);
FrontendType type_ = FrontendType::DECLARATIVE_JS;
RefPtr<AceEventHandler> handler_;
RefPtr<Framework::JsEngine> jsEngine_;
RefPtr<AccessibilityManager> accessibilityManager_;

View File

@ -41,6 +41,7 @@ bool DeclarativeFrontendNG::Initialize(FrontendType type, const RefPtr<TaskExecu
{
LOGI("DeclarativeFrontendNG initialize begin.");
type_ = type;
taskExecutor_ = taskExecutor;
ACE_DCHECK(type_ == FrontendType::DECLARATIVE_JS);
InitializeDelegate(taskExecutor);
bool needPostJsTask = true;
@ -61,7 +62,6 @@ bool DeclarativeFrontendNG::Initialize(FrontendType type, const RefPtr<TaskExecu
} else {
initJSEngineTask();
}
taskExecutor_ = taskExecutor;
LOGI("DeclarativeFrontendNG initialize end.");
return true;
}

View File

@ -116,11 +116,6 @@ public:
RefPtr<AceEventHandler> GetEventHandler() override
{
return nullptr;
};
FrontendType GetType() override
{
return type_;
}
// judge frontend is foreground frontend.
@ -192,11 +187,9 @@ public:
private:
void InitializeDelegate(const RefPtr<TaskExecutor>& taskExecutor);
FrontendType type_ = FrontendType::DECLARATIVE_JS;
RefPtr<Framework::JsEngine> jsEngine_;
RefPtr<Framework::FrontendDelegateDeclarativeNG> delegate_;
RefPtr<AccessibilityManager> accessibilityManager_;
RefPtr<TaskExecutor> taskExecutor_;
std::string pageProfile_;
bool foregroundFrontend_ = false;

View File

@ -235,6 +235,19 @@ RefPtr<Frontend> Frontend::Create()
return AceType::MakeRefPtr<JsFrontend>();
}
bool Frontend::MaybeRelease()
{
CHECK_RUN_ON(JS);
CHECK_NULL_RETURN(taskExecutor_, true);
if (taskExecutor_->WillRunOnCurrentThread(TaskExecutor::TaskType::JS)) {
LOGI("Destroy Frontend on JS thread.");
return true;
} else {
LOGI("Post Destroy Frontend Task to JS thread.");
return !taskExecutor_->PostTask([this] { delete this; }, TaskExecutor::TaskType::JS);
}
}
JsFrontend::~JsFrontend() noexcept
{
LOG_DESTROY();
@ -255,6 +268,7 @@ bool JsFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskE
{
LOGI("JsFrontend initialize begin.");
type_ = type;
taskExecutor_ = taskExecutor;
ACE_DCHECK(type_ == FrontendType::JS);
InitializeFrontendDelegate(taskExecutor);
auto weakEngine = AceType::WeakClaim(AceType::RawPtr(jsEngine_));

View File

@ -60,12 +60,12 @@ public:
void PushPage(const std::string& url, const std::string& params) override;
// Js frontend manages all pages self.
void AddPage(const RefPtr<AcePage>& page) override {};
void AddPage(const RefPtr<AcePage>& page) override {}
RefPtr<AcePage> GetPage(int32_t pageId) const override
{
return delegate_->GetPage(pageId);
};
}
WeakPtr<AcePage> GetCurrentReadyPage() const
{
@ -130,11 +130,6 @@ public:
RefPtr<AceEventHandler> GetEventHandler() override
{
return handler_;
};
FrontendType GetType() override
{
return type_;
}
// judge frontend is foreground frontend.
@ -179,7 +174,6 @@ public:
private:
void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor);
FrontendType type_ = FrontendType::JS;
RefPtr<Framework::FrontendDelegateImpl> delegate_;
RefPtr<AceEventHandler> handler_;
RefPtr<Framework::JsEngine> jsEngine_;

View File

@ -176,6 +176,7 @@ void PluginFrontend::Destroy()
bool PluginFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
{
type_ = type;
taskExecutor_ = taskExecutor;
ACE_DCHECK(type_ == FrontendType::JS_PLUGIN);
InitializeFrontendDelegate(taskExecutor);
taskExecutor->PostSyncTask(

View File

@ -60,12 +60,12 @@ public:
void PushPage(const std::string& url, const std::string& params) override;
// Js frontend manages all pages self.
void AddPage(const RefPtr<AcePage>& page) override {};
void AddPage(const RefPtr<AcePage>& page) override {}
RefPtr<AcePage> GetPage(int32_t pageId) const override
{
return nullptr;
};
}
void TriggerGarbageCollection() override;
@ -119,11 +119,6 @@ public:
RefPtr<AceEventHandler> GetEventHandler() override
{
return handler_;
};
FrontendType GetType() override
{
return type_;
}
// judge frontend is foreground frontend.
@ -216,7 +211,6 @@ public:
private:
void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor);
FrontendType type_ = FrontendType::DECLARATIVE_JS;
RefPtr<Framework::PluginFrontendDelegate> delegate_;
RefPtr<AceEventHandler> handler_;
RefPtr<Framework::JsEngine> jsEngine_;

View File

@ -30,13 +30,13 @@ public:
~MockPlatformWindow() override = default;
// Request next vsync.
void RequestFrame() override {};
void RequestFrame() override {}
// Register Vsync callback.
void RegisterVsyncCallback(AceVsyncCallback&& callback) override {};
void RegisterVsyncCallback(AceVsyncCallback&& callback) override {}
// Attach root render node to container
void SetRootRenderNode(const RefPtr<RenderNode>& root) override {};
void SetRootRenderNode(const RefPtr<RenderNode>& root) override {}
};
class MockFrontend : public Frontend {
@ -66,11 +66,6 @@ public:
return windowConfig_;
}
FrontendType GetType() override
{
return FrontendType::JS;
}
bool OnBackPressed() override
{
return false;

View File

@ -110,7 +110,7 @@ public:
// Get the stage mode sourceMap.
virtual void GetStageSourceMap(
std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const {};
std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const {}
virtual void RunPage(int32_t pageId, const std::string& content, const std::string& params) = 0;
@ -124,7 +124,15 @@ public:
// Get window config of front end, which is used to calculate the pixel ratio of the real device.
virtual WindowConfig& GetWindowConfig() = 0;
virtual FrontendType GetType() = 0;
FrontendType GetType() const
{
return type_;
}
RefPtr<TaskExecutor> GetTaskExecutor() const
{
return taskExecutor_;
}
// inform the frontend that onCreate or onDestroy
virtual void UpdateState(State) = 0;
@ -268,7 +276,7 @@ public:
virtual void FlushReload() {}
// flush frontend for HotReload feature in NG
virtual void HotReload() {};
virtual void HotReload() {}
State GetState() const
{
@ -278,6 +286,9 @@ public:
virtual void SetErrorEventHandler(std::function<void(const std::string&, const std::string&)>&& errorCallback) {}
protected:
virtual bool MaybeRelease() override;
FrontendType type_ = FrontendType::JS;
RefPtr<TaskExecutor> taskExecutor_;
bool disallowPopLastPage_ = false;
FrontendDialogCallback dialogCallback_ = nullptr;
State state_ = State::UNDEFINE;

View File

@ -26,10 +26,10 @@ RefPtr<Frontend> Frontend::CreateDefault()
return AceType::MakeRefPtr<JsonFrontend>();
}
bool JsonFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>&)
bool JsonFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
{
// This interface is inherited from base class 'Frontend', but we never use 'TaskExecutor'.
type_ = type;
taskExecutor_ = taskExecutor;
return true;
}

View File

@ -120,11 +120,6 @@ public:
return windowConfig_;
}
FrontendType GetType() override
{
return type_;
}
void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override
{
// maybe will implement later
@ -172,7 +167,6 @@ public:
}
private:
FrontendType type_ { FrontendType::JSON };
static constexpr int32_t JSON_DESIGN_WIDTH = 1080;
std::unordered_map<int, RefPtr<AcePage>> pageMap_;
RefPtr<PipelineBase> pipelineContext_;

View File

@ -22,6 +22,8 @@ void PluginFrontend::Destroy() {}
bool PluginFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
{
type_ = type;
taskExecutor_ = taskExecutor;
return true;
}

View File

@ -723,6 +723,19 @@ void PipelineBase::RemoveSubWindowVsyncCallback(int32_t subWindowId)
subWindowVsyncCallbacks_.erase(subWindowId);
}
bool PipelineBase::MaybeRelease()
{
CHECK_RUN_ON(UI);
CHECK_NULL_RETURN(taskExecutor_, true);
if (taskExecutor_->WillRunOnCurrentThread(TaskExecutor::TaskType::UI)) {
LOGI("Destroy Pipeline on UI thread.");
return true;
} else {
LOGI("Post Destroy Pipeline Task to UI thread.");
return !taskExecutor_->PostTask([this] { delete this; }, TaskExecutor::TaskType::UI);
}
}
void PipelineBase::Destroy()
{
CHECK_RUN_ON(UI);

View File

@ -910,6 +910,7 @@ public:
}
protected:
virtual bool MaybeRelease() override;
void TryCallNextFrameLayoutCallback()
{
if (isForegroundCalled_ && nextFrameLayoutCallback_) {

View File

@ -409,6 +409,10 @@ bool PipelineBase::Animate(const AnimationOption& option, const RefPtr<Curve>& c
}
void PipelineBase::Destroy() {}
bool PipelineBase::MaybeRelease()
{
return AceType::MaybeRelease();
}
void PipelineBase::AddEtsCardTouchEventCallback(int32_t ponitId, EtsCardTouchEventCallback&& callback) {}

View File

@ -23,6 +23,10 @@ int32_t g_id = 0;
} // namespace
RefPtr<MockContainer> MockContainer::container_;
bool Frontend::MaybeRelease()
{
return AceType::MaybeRelease();
}
int32_t Container::CurrentId()
{

View File

@ -40,7 +40,6 @@ public:
MOCK_METHOD(void, UpdateState, (Frontend::State state), (override));
MOCK_METHOD(RefPtr<AccessibilityManager>, GetAccessibilityManager, (), (const override));
MOCK_METHOD(bool, IsForeground, (), (override));
MOCK_METHOD(FrontendType, GetType, (), (override));
MOCK_METHOD(RefPtr<AceEventHandler>, GetEventHandler, (), (override));
MOCK_METHOD(void, RebuildAllPages, (), (override));
MOCK_METHOD(void, SetColorMode, (ColorMode colorMode), (override));