點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們
吳文璐
深圳開(kāi)鴻數(shù)字產(chǎn)業(yè)發(fā)展有限公司
OS框架開(kāi)發(fā)工程師
一、前言
二、ArkUI中的線程
void AceContainer::InitializeTask()
{
auto flutterTaskExecutor = Referenced::MakeRefPtr();
flutterTaskExecutor->InitPlatformThread(useCurrentEventRunner_);
taskExecutor_ = flutterTaskExecutor;
// No need to create JS Thread for DECLARATIVE_JS
if (type_ == FrontendType::DECLARATIVE_JS) {
GetSettings().useUIAsJSThread = true;
} else {
flutterTaskExecutor->InitJsThread();
}
}
enum class TaskType : uint32_t {
PLATFORM = 0,
UI,
IO,
GPU,
JS,
BACKGROUND,
UNKNOWN,
};
auto&& touchEventCallback = [context = pipelineContext_, id = instanceId_](
const TouchEvent& event, const std::function& markProcess) {
ContainerScope scope(id);
context->GetTaskExecutor()->PostTask(
[context, event, markProcess]() {
context->OnTouchEvent(event);
CHECK_NULL_VOID_NOLOG(markProcess);
markProcess();
},
TaskExecutor::UI);
};
三、各種類型的TaskRunner如何初始化?
void FlutterTaskExecutor::InitPlatformThread(bool useCurrentEventRunner)
{
#ifdef OHOS_STANDARD_SYSTEM
platformRunner_ = flutter::CurrentTaskRunner(useCurrentEventRunner);
#else
fml::EnsureInitializedForCurrentThread();
platformRunner_ = fml::GetCurrent().GetTaskRunner();
#endif
FillTaskTypeTable(TaskType::PLATFORM);
}
fml::RefPtr PlatformTaskRunner::CurrentTaskRunner(bool useCurrentEventRunner)
{
return PlatformTaskRunnerAdapter::CurrentTaskRunner(useCurrentEventRunner);
}
fml::RefPtr PlatformTaskRunnerAdapter::CurrentTaskRunner(bool useCurrentEventRunner)
{
if (useCurrentEventRunner) {
return fml::MakeRefCounted(useCurrentEventRunner);
}
if (taskRunner_) {
return taskRunner_;
}
taskRunner_ = fml::MakeRefCounted(useCurrentEventRunner);
return taskRunner_;
}
PlatformTaskRunnerAdapter::PlatformTaskRunnerAdapter(bool useCurrentEventRunner)
: fml::TaskRunner(nullptr)
{
if (useCurrentEventRunner) {
eventRunner_ = OHOS::Current();
} else {
eventRunner_ = OHOS::GetMainEventRunner();
}
eventHandler_ = std::make_shared(eventRunner_);
}
void PlatformTaskRunnerAdapter::closure task)
{
eventHandler_->PostTask(std::move(task));
}
void FlutterTaskExecutor::InitOtherThreads(const flutter::TaskRunners& taskRunners)
{
uiRunner_ = taskRunners.GetUITaskRunner();
ioRunner_ = taskRunners.GetIOTaskRunner();
#ifdef NG_BUILD
gpuRunner_ = taskRunners.GetRasterTaskRunner();
#else
gpuRunner_ = taskRunners.GetGPUTaskRunner();
#endif
//...此處省略若干行
}
FlutterAceView* FlutterAceView::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread)
{
FlutterAceView* aceSurface = new Platform::FlutterAceView(instanceId);
if (aceSurface != nullptr) {
aceSurface->IncRefCount();
}
flutter::Settings settings;
settings.instanceId = instanceId;
settings.platform = flutter::ACE_PLATFORM_OHOS;
#ifndef GPU_DISABLED
settings.enable_software_rendering = false;
#else
settings.enable_software_rendering = true;
#endif
#ifdef ENABLE_ROSEN_BACKEND
settings.use_system_render_thread = SystemProperties::GetRosenBackendEnabled();
#endif
settings.platform_as_ui_thread = usePlatformThread;
settings.use_current_event_runner = useCurrentEventRunner;
// ...此處省略若干行
auto shell_holder = std::make_unique(settings, false);
if (aceSurface != nullptr) {
aceSurface->SetShellHolder(std::move(shell_holder));
}
return aceSurface;
}
OhosShellHolder::OhosShellHolder(
flutter::Settings settings,
bool is_background_view)
: settings_(std::move(settings))
{
// ...此處省略若干行
// The current thread will be used as the platform thread. Ensure that the
// message loop is initialized.
fml::EnsureInitializedForCurrentThread();
fml::RefPtr gpu_runner;
fml::RefPtr ui_runner;
fml::RefPtr io_runner;
fml::RefPtr platform_runner =
PlatformTaskRunnerAdapter::CurrentTaskRunner(settings_.use_current_event_runner);
if (is_background_view) {
auto single_task_runner = thread_host_.ui_thread->GetTaskRunner();
gpu_runner = single_task_runner;
ui_runner = single_task_runner;
io_runner = single_task_runner;
} else {
if (settings_.platform_as_ui_thread) {
ui_runner = platform_runner;
} else {
ui_runner = thread_host_.ui_thread->GetTaskRunner();
}
if (!settings_.use_system_render_thread) {
gpu_runner = thread_host_.gpu_thread->GetTaskRunner();
} else {
gpu_runner = ui_runner;
}
if (settings_.use_io_thread) {
io_runner = thread_host_.io_thread->GetTaskRunner();
} else {
io_runner = ui_runner;
}
}
flutter::TaskRunners task_runners(thread_label, // label
platform_runner, // platform
gpu_runner, // gpu
ui_runner, // ui
io_runner // io
);
void FlutterTaskExecutor::InitJsThread(bool newThread)
{
if (newThread) {
jsThread_ = std::make_unique(GenJsThreadName());
jsRunner_ = jsThread_->GetTaskRunner();
} else {
jsRunner_ = uiRunner_;
}
PostTaskToTaskRunner(
jsRunner_, [weak = AceType::WeakClaim(this)] { FillTaskTypeTable(weak, TaskType::JS); }, 0);
}
void RosenFontLoader::LoadFromNetwork(const OHOS::RefPtr& context)
{
auto weakContext = AceType::RawPtr(context));
context->GetTaskExecutor()->PostTask(
[weak = AceType::WeakClaim(this), weakContext] {
auto fontLoader = weak.Upgrade();
auto context = weakContext.Upgrade();
if (!fontLoader || !context) {
return;
}
std::vector fontData;
if (!DownloadManager::GetInstance().Download(fontLoader->familySrc_, fontData) || fontData.empty()) {
return;
}
//...此處省略若干行
},
TaskExecutor::BACKGROUND);
}
四、ArkUI中的看門狗
原文標(biāo)題:ArkUI中的線程和看門狗機(jī)制
文章出處:【微信公眾號(hào):OpenAtom OpenHarmony】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
鴻蒙
+關(guān)注
關(guān)注
57文章
2383瀏覽量
42948 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3732瀏覽量
16447
原文標(biāo)題:ArkUI中的線程和看門狗機(jī)制
文章出處:【微信號(hào):gh_e4f28cfa3159,微信公眾號(hào):OpenAtom OpenHarmony】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論