1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/common/flutter/flutter_thread_model.h"
17
18 #include <memory>
19
20 #ifdef FML_EMBEDDER_ONLY
21 #undef FML_EMBEDDER_ONLY
22 #define FML_EMBEDDER_ONLY
23 #endif
24
25 #include "common/task_runners.h"
26 #include "flutter/fml/message_loop.h"
27 #if defined(OHOS_PLATFORM) || defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
28 #include "flutter/shell/platform/ohos/platform_task_runner_adapter.h"
29 #elif defined(PREVIEW)
30 #include "adapter/preview/external/flutter/platform_task_runner_adapter.h"
31 #endif
32 #include "shell/common/thread_host.h"
33
34 #include "base/log/log.h"
35
36 namespace OHOS::Ace {
CreateThreadModel(bool useCurrentEventRunner,bool hasUiThread,bool hasGpuThread)37 std::unique_ptr<FlutterThreadModel> FlutterThreadModel::CreateThreadModel(
38 bool useCurrentEventRunner, bool hasUiThread, bool hasGpuThread)
39 {
40 // Create threads
41 static size_t sCount = 1;
42 auto threadLabel = std::to_string(sCount++);
43 uint64_t typeMask = 0;
44
45 if (hasUiThread) {
46 typeMask |= flutter::ThreadHost::Type::UI;
47 }
48 if (hasGpuThread) {
49 typeMask |= flutter::ThreadHost::Type::GPU;
50 }
51 flutter::ThreadHost threadHost = { threadLabel, typeMask };
52
53 // Create Taskrunners
54 fml::MessageLoop::EnsureInitializedForCurrentThread();
55 fml::RefPtr<fml::TaskRunner> gpuRunner;
56 fml::RefPtr<fml::TaskRunner> uiRunner;
57
58 #if defined(OHOS_PLATFORM) || defined(PREVIEW)
59 fml::RefPtr<fml::TaskRunner> platformRunner =
60 flutter::PlatformTaskRunnerAdapter::CurrentTaskRunner(useCurrentEventRunner);
61 #else
62 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
63 fml::RefPtr<fml::TaskRunner> platformRunner;
64 if (hasUiThread) {
65 platformRunner = fml::MessageLoop::GetCurrent().GetTaskRunner();
66 } else {
67 LOGI("FlutterThreadModel create platfrom thread by eventhandler.");
68 platformRunner = flutter::PlatformTaskRunnerAdapter::CurrentTaskRunner(useCurrentEventRunner);
69 }
70 #else
71 fml::RefPtr<fml::TaskRunner> platformRunner;
72 platformRunner = fml::MessageLoop::GetCurrent().GetTaskRunner();
73 #endif
74 #endif
75
76 if (hasUiThread) {
77 uiRunner = threadHost.ui_thread->GetTaskRunner();
78 } else {
79 uiRunner = platformRunner;
80 }
81 if (hasGpuThread) {
82 gpuRunner = threadHost.gpu_thread->GetTaskRunner();
83 } else {
84 gpuRunner = uiRunner;
85 }
86
87 flutter::TaskRunners taskRunners(threadLabel, // label
88 platformRunner, // platform
89 gpuRunner, // gpu
90 uiRunner, // ui
91 uiRunner // io
92 );
93
94 return std::make_unique<FlutterThreadModel>(std::move(threadHost), std::move(taskRunners));
95 }
96
FlutterThreadModel(flutter::ThreadHost threadHost,flutter::TaskRunners taskRunners)97 FlutterThreadModel::FlutterThreadModel(flutter::ThreadHost threadHost, flutter::TaskRunners taskRunners)
98 : threadHost_(std::move(threadHost)), taskRunners_(std::move(taskRunners))
99 {}
100 } // namespace OHOS::Ace
101