1 /* 2 * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_TASK_EXECUTOR_H 17 #define FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_TASK_EXECUTOR_H 18 19 #include <thread> 20 #include <unordered_map> 21 22 #include "flutter/common/task_runners.h" 23 #include "flutter/fml/thread.h" 24 25 #include "base/thread/task_executor.h" 26 #include "base/utils/macros.h" 27 28 namespace OHOS::Ace { 29 30 class ACE_EXPORT FlutterTaskExecutor final : public TaskExecutor { 31 DECLARE_ACE_TYPE(FlutterTaskExecutor, TaskExecutor); 32 33 public: 34 explicit FlutterTaskExecutor(const RefPtr<FlutterTaskExecutor>& taskExecutors); 35 explicit FlutterTaskExecutor(const flutter::TaskRunners& taskRunners); 36 FlutterTaskExecutor() = default; 37 ~FlutterTaskExecutor() final; 38 // Must call this method on platform thread 39 void InitPlatformThread(bool useCurrentEventRunner = false); 40 void InitJsThread(bool newThread = true); 41 void InitOtherThreads(const flutter::TaskRunners& taskRunners); 42 43 void AddTaskObserver(Task&& callback) override; 44 void RemoveTaskObserver() override; 45 bool WillRunOnCurrentThread(TaskType type) const final; 46 GetTid(TaskType type)47 int32_t GetTid(TaskType type) final 48 { 49 return taskTypeTable_[type].tid; 50 } 51 52 private: 53 bool OnPostTask(Task&& task, TaskType type, uint32_t delayTime) const final; 54 Task WrapTaskWithTraceId(Task&& task, int32_t id) const final; 55 56 #ifdef ACE_DEBUG 57 bool OnPreSyncTask(TaskType type) const final; 58 void OnPostSyncTask() const final; 59 60 void DumpDeadSyncTask(TaskType from, TaskType to) const; 61 mutable std::unordered_map<std::thread::id, std::thread::id> syncTaskTable_; 62 #endif 63 64 void FillTaskTypeTable(TaskType type); 65 static void FillTaskTypeTable(const WeakPtr<FlutterTaskExecutor>& weak, TaskType type); 66 67 struct ThreadInfo { 68 std::thread::id threadId; 69 int32_t tid = 0; 70 std::string threadName; 71 }; 72 73 mutable std::mutex tableMutex_; 74 std::unordered_map<TaskType, ThreadInfo> taskTypeTable_; 75 static thread_local TaskType localTaskType; 76 77 std::unique_ptr<fml::Thread> jsThread_; 78 79 fml::RefPtr<fml::TaskRunner> platformRunner_; 80 fml::RefPtr<fml::TaskRunner> uiRunner_; 81 fml::RefPtr<fml::TaskRunner> ioRunner_; 82 fml::RefPtr<fml::TaskRunner> jsRunner_; 83 fml::RefPtr<fml::TaskRunner> gpuRunner_; 84 }; 85 86 } // namespace OHOS::Ace 87 88 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_TASK_EXECUTOR_H 89