From a83d53a9020e9c0ab69eeb1fc8d54b6cbab78099 Mon Sep 17 00:00:00 2001 From: ZhangYu Date: Tue, 23 May 2023 06:39:52 +0000 Subject: [PATCH] Modify the construction of the Rosen window. Signed-off-by: ZhangYu Change-Id: I87b4569f5b68a119b09675e6c40976de2ced8101 --- adapter/preview/entrance/ace_ability.cpp | 46 ++++++---- adapter/preview/entrance/ace_ability.h | 12 +++ adapter/preview/entrance/ace_container.cpp | 51 ++++------- adapter/preview/entrance/ace_container.h | 21 ++--- adapter/preview/external/BUILD.gn | 24 ++++-- adapter/preview/external/window/window.h | 46 ++++++++++ .../preview/external/window/window_impl.cpp | 86 +++++++++++++++++++ .../{window_preview.h => window_impl.h} | 80 +++-------------- .../render/adapter/rosen_window.h | 2 +- 9 files changed, 229 insertions(+), 139 deletions(-) create mode 100644 adapter/preview/external/window/window.h create mode 100644 adapter/preview/external/window/window_impl.cpp rename adapter/preview/external/window/{window_preview.h => window_impl.h} (34%) diff --git a/adapter/preview/entrance/ace_ability.cpp b/adapter/preview/entrance/ace_ability.cpp index d6c5740e078..1616322baec 100644 --- a/adapter/preview/entrance/ace_ability.cpp +++ b/adapter/preview/entrance/ace_ability.cpp @@ -15,8 +15,6 @@ #include "adapter/preview/entrance/ace_ability.h" -#include "base/utils/utils.h" - #ifdef INIT_ICU_DATA_PATH #include "unicode/putil.h" #endif @@ -28,12 +26,16 @@ #include "adapter/preview/entrance/event_dispatcher.h" #include "adapter/preview/entrance/rs_dir_asset_provider.h" #include "adapter/preview/inspector/inspector_client.h" +#include "frameworks/base/utils/utils.h" #include "frameworks/bridge/common/utils/utils.h" #include "frameworks/bridge/js_frontend/js_frontend.h" #ifndef ENABLE_ROSEN_BACKEND #include "frameworks/core/components/common/painter/flutter_svg_painter.h" #else +#include +#include + #include "adapter/preview/external/flutter/main_event_loop.h" #endif @@ -237,6 +239,7 @@ std::unique_ptr AceAbility::CreateInstance(AceRunArgs& runArgs) SetFontMgrConfig(runArgs.containerSdkPath); EventDispatcher::GetInstance().Initialize(); auto aceAbility = std::make_unique(runArgs); + aceAbility->SetWindow(Rosen::Window::Create(runArgs.onRender)); aceAbility->SetGlfwWindowController(ctx); return aceAbility; } @@ -282,6 +285,8 @@ void AceAbility::InitEnv() AceContainer::RunPage(ACE_INSTANCE_ID, UNUSED_PAGE_ID, runArgs_.url, ""); AceContainer::SetView(view, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight); } + // Drive the native engine with the platform thread. + container->RunNativeEngineLoop(); if (runArgs_.projectModel == ProjectModel::STAGE) { container->InitializeStageAppConfig(runArgs_.assetPath, runArgs_.formsEnabled); } @@ -335,15 +340,36 @@ void AceAbility::InitEnv() runArgs_.appResourcesPath, runArgs_.themeId, runArgs_.deviceConfig.colorMode); auto view = AceViewPreview::CreateView(ACE_INSTANCE_ID); + auto window = GetWindow(); + UIEnvCallback callback = [window, id = ACE_INSTANCE_ID](const OHOS::Ace::RefPtr& context) mutable { + CHECK_NULL_VOID(context); + CHECK_NULL_VOID(window); + auto director = OHOS::Rosen::RSUIDirector::Create(); + CHECK_NULL_VOID(director); + director->SetRSSurfaceNode(window->GetSurfaceNode()); + auto container = AceContainer::GetContainerInstance(id); + CHECK_NULL_VOID(container); + auto func = [taskExecutor = container->GetTaskExecutor(), id](const std::function& task) { + CHECK_NULL_VOID(taskExecutor); + ContainerScope scope(id); + taskExecutor->PostTask(task, TaskExecutor::TaskType::UI); + }; + director->SetUITaskRunner(func); + director->Init(); + context->SetRSUIDirector(director); + }; + if (runArgs_.aceVersion == AceVersion::ACE_2_0) { AceContainer::SetView( - view, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight, runArgs_.onRender); + view, window, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight, callback); AceContainer::RunPage(ACE_INSTANCE_ID, UNUSED_PAGE_ID, runArgs_.url, ""); } else { AceContainer::RunPage(ACE_INSTANCE_ID, UNUSED_PAGE_ID, runArgs_.url, ""); AceContainer::SetView( - view, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight, runArgs_.onRender); + view, window, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight, callback); } + // Drive the native engine with the platform thread. + container->RunNativeEngineLoop(); if (runArgs_.projectModel == ProjectModel::STAGE) { container->InitializeStageAppConfig(runArgs_.assetPath, runArgs_.formsEnabled); } @@ -385,11 +411,6 @@ void AceAbility::RunEventLoop() SurfaceChanged(runArgs_.deviceConfig.orientation, runArgs_.deviceConfig.density, width, height); } #endif - auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID); - if (container) { - container->RunNativeEngineLoop(); - } - std::this_thread::sleep_for(std::chrono::milliseconds(1)); } loopRunning_ = true; @@ -430,13 +451,6 @@ void AceAbility::RunEventLoop() SurfaceChanged(runArgs_.deviceConfig.orientation, runArgs_.deviceConfig.density, width, height); } #endif - - // Drive the native engine. - auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID); - if (container) { - container->RunNativeEngineLoop(); - } - // Process the event of glfw controller_->PollEvents(); std::this_thread::sleep_for(std::chrono::milliseconds(1)); diff --git a/adapter/preview/entrance/ace_ability.h b/adapter/preview/entrance/ace_ability.h index 84f13d30dd9..c19852e13c5 100644 --- a/adapter/preview/entrance/ace_ability.h +++ b/adapter/preview/entrance/ace_ability.h @@ -26,6 +26,7 @@ #endif #include "adapter/preview/entrance/ace_run_args.h" +#include "adapter/preview/external/window/window.h" #include "base/utils/macros.h" #include "core/event/key_event.h" #include "core/event/touch_event.h" @@ -83,6 +84,16 @@ public: return controller_; } + void SetWindow(sptr rsWindow) + { + rsWindow_ = rsWindow; + } + + sptr GetWindow() + { + return rsWindow_; + } + private: void RunEventLoop(); @@ -98,6 +109,7 @@ private: AceRunArgs runArgs_; ConfigChanges configChanges_; GlfwController controller_ = nullptr; + sptr rsWindow_; }; } // namespace OHOS::Ace::Platform diff --git a/adapter/preview/entrance/ace_container.cpp b/adapter/preview/entrance/ace_container.cpp index 6c141ce2644..d7619097011 100644 --- a/adapter/preview/entrance/ace_container.cpp +++ b/adapter/preview/entrance/ace_container.cpp @@ -24,9 +24,6 @@ #include "core/common/flutter/flutter_asset_manager.h" #include "core/common/flutter/flutter_task_executor.h" #else // ENABLE_ROSEN_BACKEND == true -#include -#include - #include "adapter/preview/entrance/rs_dir_asset_provider.h" #include "core/common/flutter/flutter_task_executor.h" #include "core/common/rosen/rosen_asset_manager.h" @@ -34,7 +31,6 @@ #include "adapter/ohos/entrance/ace_new_pipe_judgement.h" #include "adapter/preview/entrance/ace_application_info.h" -#include "adapter/preview/external/window/window_preview.h" #include "adapter/preview/osal/stage_card_parser.h" #include "base/log/ace_trace.h" #include "base/log/event_report.h" @@ -238,6 +234,9 @@ void AceContainer::InitializeFrontend() void AceContainer::RunNativeEngineLoop() { taskExecutor_->PostTask([frontend = frontend_]() { frontend->RunNativeEngineLoop(); }, TaskExecutor::TaskType::JS); + // After the JS thread executes frontend ->RunNativeEngineLoop(), + // it is thrown back into the Platform thread queue to form a loop. + taskExecutor_->PostTask([this]() { RunNativeEngineLoop(); }, TaskExecutor::TaskType::PLATFORM); } void AceContainer::InitializeStageAppConfig(const std::string& assetPath, bool formsEnabled) @@ -804,17 +803,16 @@ void AceContainer::SetView(AceViewPreview* view, double density, int32_t width, container->AttachView(std::move(window), view, density, width, height); } #else -void AceContainer::SetView( - AceViewPreview* view, double density, int32_t width, int32_t height, SendRenderDataCallback onRender) +void AceContainer::SetView(AceViewPreview* view, sptr rsWindow, double density, int32_t width, + int32_t height, UIEnvCallback callback) { CHECK_NULL_VOID(view); auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(view->GetInstanceId())); CHECK_NULL_VOID(container); auto taskExecutor = container->GetTaskExecutor(); CHECK_NULL_VOID(taskExecutor); - auto rsWindow = new Rosen::Window(onRender); auto window = std::make_unique(rsWindow, taskExecutor, view->GetInstanceId()); - container->AttachView(std::move(window), view, density, width, height, onRender); + container->AttachView(std::move(window), view, density, width, height, callback); } #endif @@ -905,7 +903,7 @@ void AceContainer::AttachView( } #else void AceContainer::AttachView(std::unique_ptr window, AceViewPreview* view, double density, int32_t width, - int32_t height, SendRenderDataCallback onRender) + int32_t height, UIEnvCallback callback) { ContainerScope scope(instanceId_); aceView_ = view; @@ -973,33 +971,14 @@ void AceContainer::AttachView(std::unique_ptr window, AceViewPreview* vi }, TaskExecutor::TaskType::UI); } - - taskExecutor_->PostTask( - [pipelineContext = AceType::DynamicCast(pipelineContext_), onRender, this]() { - CHECK_NULL_VOID(pipelineContext); - auto director = Rosen::RSUIDirector::Create(); - if (director == nullptr) { - return; - } - - struct Rosen::RSSurfaceNodeConfig rsSurfaceNodeConfig = { - .SurfaceNodeName = "preview_surface", - .additionalData = reinterpret_cast(onRender), - }; - static auto snode = Rosen::RSSurfaceNode::Create(rsSurfaceNodeConfig); - director->SetRSSurfaceNode(snode); - - auto func = [taskExecutor = taskExecutor_, id = instanceId_](const std::function& task) { - ContainerScope scope(id); - taskExecutor->PostTask(task, TaskExecutor::TaskType::UI); - }; - director->SetUITaskRunner(func); - - director->Init(); - pipelineContext->SetRSUIDirector(director); - LOGI("Init Rosen Backend"); - }, - TaskExecutor::TaskType::UI); + if (!useNewPipeline_) { + taskExecutor_->PostTask( + [context = pipelineContext_, callback]() { + CHECK_NULL_VOID(callback); + callback(AceType::DynamicCast(context)); + }, + TaskExecutor::TaskType::UI); + } auto weak = AceType::WeakClaim(AceType::RawPtr(pipelineContext_)); taskExecutor_->PostTask( diff --git a/adapter/preview/entrance/ace_container.h b/adapter/preview/entrance/ace_container.h index caa64fb2181..614d577db4c 100644 --- a/adapter/preview/entrance/ace_container.h +++ b/adapter/preview/entrance/ace_container.h @@ -22,6 +22,11 @@ #include #include "adapter/preview/entrance/ace_run_args.h" +#include "adapter/preview/entrance/ace_view_preview.h" +#include "adapter/preview/external/ability/context.h" +#include "adapter/preview/external/ability/fa/fa_context.h" +#include "adapter/preview/external/ability/stage/stage_context.h" +#include "adapter/preview/external/window/window.h" #include "adapter/preview/osal/fetch_manager.h" #include "base/resource/asset_manager.h" #include "base/thread/task_executor.h" @@ -31,11 +36,6 @@ #include "core/common/js_message_dispatcher.h" #include "core/common/platform_bridge.h" #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" -#include "adapter/preview/external/ability/context.h" -#include "adapter/preview/external/ability/fa/fa_context.h" -#include "adapter/preview/external/ability/stage/stage_context.h" - -#include "adapter/preview/entrance/ace_view_preview.h" namespace OHOS::Ace::Platform { @@ -45,6 +45,7 @@ namespace { constexpr int32_t ACE_INSTANCE_ID = 0; } // namespace +using UIEnvCallback = std::function& context)>; using OnRouterChangeCallback = bool (*)(const std::string currentRouterPath); // AceContainer is the instance have its own pipeline and thread models, it can contains multiple pages. @@ -63,8 +64,8 @@ public: #ifndef ENABLE_ROSEN_BACKEND static void SetView(AceViewPreview* view, double density, int32_t width, int32_t height); #else - static void SetView( - AceViewPreview* view, double density, int32_t width, int32_t height, SendRenderDataCallback onRender); + static void SetView(AceViewPreview* view, sptr rsWindow, double density, int32_t width, + int32_t height, UIEnvCallback callback); #endif static void InitDeviceInfo(int32_t instanceId, const AceRunArgs& runArgs); @@ -259,8 +260,8 @@ private: void AttachView( std::unique_ptr window, AceViewPreview* view, double density, int32_t width, int32_t height); #else - void AttachView(std::unique_ptr window, - AceViewPreview* view, double density, int32_t width, int32_t height, SendRenderDataCallback onRender); + void AttachView(std::unique_ptr window, AceViewPreview* view, double density, int32_t width, int32_t height, + UIEnvCallback callback); #endif AceViewPreview* aceView_ = nullptr; @@ -283,7 +284,7 @@ private: mutable std::mutex cardPipelineMutex_; RefPtr context_; - //app bar to use + // app bar to use bool installationFree_ = false; int32_t labelId_; diff --git a/adapter/preview/external/BUILD.gn b/adapter/preview/external/BUILD.gn index ffe361063ba..77ea8b3f106 100644 --- a/adapter/preview/external/BUILD.gn +++ b/adapter/preview/external/BUILD.gn @@ -42,13 +42,25 @@ ohos_source_set("preview_external_source") { "ability/stage/stage_app_info.cpp", "ability/stage/stage_context.cpp", "ability/stage/stage_hap_module_info.cpp", - - # mock event_loop - "flutter/main_event_loop.cpp", - "flutter/platform_task_runner.cpp", - "flutter/platform_task_runner_adapter.cpp", ] - include_dirs = [ "//third_party/flutter/engine" ] + # for rosen backend + if (defined(config.enable_rosen_backend) && config.enable_rosen_backend) { + # for event loop + include_dirs = [ "//third_party/flutter/engine" ] + + # for window + deps = [ "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client" ] + + sources += [ + # for event loop + "flutter/main_event_loop.cpp", + "flutter/platform_task_runner.cpp", + "flutter/platform_task_runner_adapter.cpp", + + # for window + "window/window_impl.cpp", + ] + } } } diff --git a/adapter/preview/external/window/window.h b/adapter/preview/external/window/window.h new file mode 100644 index 00000000000..2178db58b62 --- /dev/null +++ b/adapter/preview/external/window/window.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 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_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_H +#define FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_H + +#include +#include + +namespace OHOS { +namespace Rosen { + +class RSSurfaceNode; + +using SendRenderDataCallback = bool (*)(const void*, const size_t, const int32_t, const int32_t); +using OnCallback = std::function; +struct VsyncCallback { + OnCallback onCallback; +}; + +class Window : public RefBase { +public: + Window() = default; + virtual ~Window() = default; + + static sptr Create(SendRenderDataCallback onRender); + + virtual void RequestVsync(const std::shared_ptr& vsyncCallback); + virtual std::shared_ptr GetSurfaceNode() const; +}; + +} // namespace Rosen +} // namespace OHOS +#endif // FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_H diff --git a/adapter/preview/external/window/window_impl.cpp b/adapter/preview/external/window/window_impl.cpp new file mode 100644 index 00000000000..dcbb18c39cd --- /dev/null +++ b/adapter/preview/external/window/window_impl.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023 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 "adapter/preview/external/window/window_impl.h" + +#include + +#include "base/utils/time_util.h" + +namespace OHOS { +namespace Rosen { + +sptr Window::Create(SendRenderDataCallback onRender) +{ + return sptr(new WindowImpl(onRender)); +} + +WindowImpl::WindowImpl(SendRenderDataCallback onRender) +{ + struct Rosen::RSSurfaceNodeConfig rsSurfaceNodeConfig = { + .SurfaceNodeName = "preview_surface", + .additionalData = reinterpret_cast(onRender), + }; + surfaceNode_ = Rosen::RSSurfaceNode::Create(rsSurfaceNodeConfig); +} + +WindowImpl::~WindowImpl() +{ + vsyncRequests_.Push(false); + vsyncThread_->join(); +} + +void WindowImpl::RequestVsync(const std::shared_ptr& vsyncCallback) +{ + { + std::unique_lock lock(mutex_); + pendingVsyncCallbacks_.emplace_back(std::move(vsyncCallback)); + } + if (vsyncThread_ == nullptr) { + auto func = [this] { VsyncThreadMain(); }; + vsyncThread_ = std::make_unique(func); + } + vsyncRequests_.Push(true); +} + +std::shared_ptr WindowImpl::GetSurfaceNode() const +{ + return surfaceNode_; +} + +void WindowImpl::VsyncThreadMain() +{ + while (true) { + bool next = false; + vsyncRequests_.PopFront(next); + if (next == false) { + break; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + { + std::unique_lock lock(mutex_); + vsyncCallbacks_.swap(pendingVsyncCallbacks_); + } + int64_t now = Ace::GetSysTimestamp(); + for (auto& callback : vsyncCallbacks_) { + callback->onCallback(now); + } + vsyncCallbacks_.clear(); + } +} + +} // namespace Rosen +} // namespace OHOS diff --git a/adapter/preview/external/window/window_preview.h b/adapter/preview/external/window/window_impl.h similarity index 34% rename from adapter/preview/external/window/window_preview.h rename to adapter/preview/external/window/window_impl.h index b0c8ff192be..9f251a5cc14 100644 --- a/adapter/preview/external/window/window_preview.h +++ b/adapter/preview/external/window/window_impl.h @@ -13,89 +13,29 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_PREVIEWER_H -#define FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_PREVIEWER_H +#ifndef FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_IMPL_H +#define FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_IMPL_H -#include -#include -#include #include -#include -#include #include -#include #include +#include "adapter/preview/external/window/window.h" #include "base/thread/sem_queue.h" -#include "base/utils/time_util.h" -#include "core/common/window.h" namespace OHOS { namespace Rosen { -using OnCallback = std::function; -struct VsyncCallback { - OnCallback onCallback; -}; - -using SendRenderDataCallback = bool (*)(const void*, const size_t, const int32_t, const int32_t); -class Window : public RefBase { +class WindowImpl : public Window { public: - Window(SendRenderDataCallback onRender) - { - struct Rosen::RSSurfaceNodeConfig rsSurfaceNodeConfig = { - .SurfaceNodeName = "preview_surface", - .additionalData = reinterpret_cast(onRender), - }; - surfaceNode_ = Rosen::RSSurfaceNode::Create(rsSurfaceNodeConfig); - } - - ~Window() - { - vsyncRequests_.Push(false); - vsyncThread_->join(); - } - virtual void RequestVsync(const std::shared_ptr& vsyncCallback) - { - { - std::unique_lock lock(mutex_); - pendingVsyncCallbacks_.emplace_back(std::move(vsyncCallback)); - } - if (vsyncThread_ == nullptr) { - auto func = [this] { VsyncThreadMain(); }; - vsyncThread_ = std::make_unique(func); - } - vsyncRequests_.Push(true); - } - - std::shared_ptr GetSurfaceNode() const - { - return surfaceNode_; - } - - void VsyncThreadMain() - { - while (true) { - bool next = false; - vsyncRequests_.PopFront(next); - if (next == false) { - break; - } + WindowImpl(SendRenderDataCallback onRender); + ~WindowImpl() override; - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - { - std::unique_lock lock(mutex_); - vsyncCallbacks_.swap(pendingVsyncCallbacks_); - } - int64_t now = Ace::GetSysTimestamp(); - for (auto& callback : vsyncCallbacks_) { - callback->onCallback(now); - } - vsyncCallbacks_.clear(); - } - } + void RequestVsync(const std::shared_ptr& vsyncCallback) override; + std::shared_ptr GetSurfaceNode() const override; private: + void VsyncThreadMain(); std::shared_ptr surfaceNode_; std::vector> vsyncCallbacks_; std::vector> pendingVsyncCallbacks_; @@ -106,4 +46,4 @@ private: } // namespace Rosen } // namespace OHOS -#endif // FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_PREVIEWER_H +#endif // FOUNDATION_ACE_ADAPTER_PREVIEW_EXTERNAL_WINDOW_WINDOW_IMPL_H diff --git a/frameworks/core/components_ng/render/adapter/rosen_window.h b/frameworks/core/components_ng/render/adapter/rosen_window.h index 3e7fa4137ad..dd594de3b32 100644 --- a/frameworks/core/components_ng/render/adapter/rosen_window.h +++ b/frameworks/core/components_ng/render/adapter/rosen_window.h @@ -29,7 +29,7 @@ #include "adapter/android/entrance/java/jni/virtual_rs_window.h" #endif #else -#include "adapter/preview/external/window/window_preview.h" +#include "adapter/preview/external/window/window.h" #endif #include "base/thread/task_executor.h" -- Gitee