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 #include "core/components_ng/image_provider/image_utils.h"
16 #include "core/common/container.h"
17 #include "core/common/container_scope.h"
18 
19 namespace OHOS::Ace::NG {
PostTask(std::function<void ()> && task,TaskExecutor::TaskType taskType,const char * taskTypeName,PriorityType priorityType)20 void ImageUtils::PostTask(
21     std::function<void()>&& task, TaskExecutor::TaskType taskType, const char* taskTypeName, PriorityType priorityType)
22 {
23     auto taskExecutor = Container::CurrentTaskExecutorSafelyWithCheck();
24     if (!taskExecutor) {
25         LOGE("taskExecutor is null when try post task to %{public}s", taskTypeName);
26         return;
27     }
28     taskExecutor->PostTask(
29         [task, id = Container::CurrentId()] {
30             ContainerScope scope(id);
31             CHECK_NULL_VOID(task);
32             task();
33         },
34         taskType, std::string(taskTypeName), priorityType);
35 }
36 
PostToUI(std::function<void ()> && task,const std::string & name,const int32_t containerId,PriorityType priorityType)37 void ImageUtils::PostToUI(
38     std::function<void()>&& task, const std::string& name, const int32_t containerId, PriorityType priorityType)
39 {
40     ContainerScope scope(containerId);
41 
42     CHECK_NULL_VOID(task);
43     ImageUtils::PostTask(std::move(task), TaskExecutor::TaskType::UI, name.c_str(), priorityType);
44 }
45 
PostToBg(std::function<void ()> && task,const std::string & name,const int32_t containerId,PriorityType priorityType)46 void ImageUtils::PostToBg(
47     std::function<void()>&& task, const std::string& name, const int32_t containerId, PriorityType priorityType)
48 {
49     ContainerScope scope(containerId);
50 
51     CHECK_NULL_VOID(task);
52     ImageUtils::PostTask(std::move(task), TaskExecutor::TaskType::BACKGROUND, name.c_str(), priorityType);
53 }
54 } // namespace OHOS::Ace::NG
55