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/thread_model_impl.h"
17
18 #include "base/log/log.h"
19 #include "core/common/task_runner_adapter_factory.h"
20
21 namespace OHOS::Ace {
CreateThreadModel(bool useCurrentEventRunner,bool hasUiThread,bool hasGpuThread)22 std::unique_ptr<ThreadModelImpl> ThreadModelImpl::CreateThreadModel(
23 bool useCurrentEventRunner, bool hasUiThread, bool hasGpuThread)
24 {
25 // Create threads
26 static size_t sCount = 1;
27 auto threadLabel = std::to_string(sCount++);
28 uint64_t typeMask = 0;
29
30 if (hasUiThread) {
31 typeMask |= ThreadContainer::ThreadType::UI;
32 }
33 if (hasGpuThread) {
34 typeMask |= ThreadContainer::ThreadType::GPU;
35 }
36 ThreadContainer threadContainer = { threadLabel, typeMask };
37
38 // Create Taskrunners
39 RefPtr<TaskRunnerAdapter> gpuRunner;
40 RefPtr<TaskRunnerAdapter> uiRunner;
41
42 RefPtr<TaskRunnerAdapter> platformRunner = TaskRunnerAdapterFactory::Create(useCurrentEventRunner, "");
43 if (hasUiThread) {
44 uiRunner = threadContainer.uiThread;
45 } else {
46 uiRunner = platformRunner;
47 }
48 if (hasGpuThread) {
49 gpuRunner = threadContainer.gpuThread;
50 } else {
51 gpuRunner = uiRunner;
52 }
53
54 TaskRunners taskRunners(threadLabel, platformRunner, gpuRunner, uiRunner, uiRunner);
55
56 return std::make_unique<ThreadModelImpl>(std::move(threadContainer), std::move(taskRunners));
57 }
58
GetTaskRunners() const59 const TaskRunners& ThreadModelImpl::GetTaskRunners() const
60 {
61 return taskRunners_;
62 }
63
ThreadModelImpl(ThreadContainer threadContainer,TaskRunners taskRunners)64 ThreadModelImpl::ThreadModelImpl(ThreadContainer threadContainer, TaskRunners taskRunners)
65 : threadContainer_(std::move(threadContainer)), taskRunners_(std::move(taskRunners))
66 {}
67 } // namespace OHOS::Ace
68