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