1 /*
2 * Copyright (c) 2023-2024 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
17 #include "base/log/log.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 TAG_LOGE(AceLogTag::ACE_IMAGE, "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
PostDelayedTask(std::function<void ()> && task,TaskExecutor::TaskType taskType,uint32_t delayTime,const char * taskTypeName,PriorityType priorityType)37 void ImageUtils::PostDelayedTask(std::function<void()>&& task, TaskExecutor::TaskType taskType, uint32_t delayTime,
38 const char* taskTypeName, PriorityType priorityType)
39 {
40 auto taskExecutor = Container::CurrentTaskExecutorSafelyWithCheck();
41 if (!taskExecutor) {
42 TAG_LOGE(AceLogTag::ACE_IMAGE, "taskExecutor is null when try post task to %{public}s", taskTypeName);
43 return;
44 }
45 taskExecutor->PostDelayedTask(
46 [task, id = Container::CurrentId()] {
47 ContainerScope scope(id);
48 CHECK_NULL_VOID(task);
49 task();
50 },
51 taskType, delayTime, std::string(taskTypeName), priorityType);
52 }
53
PostDelayedTaskToUI(std::function<void ()> && task,uint32_t delayTime,const std::string & name,const int32_t containerId,PriorityType priorityType)54 void ImageUtils::PostDelayedTaskToUI(std::function<void()>&& task, uint32_t delayTime, const std::string& name,
55 const int32_t containerId, PriorityType priorityType)
56 {
57 ContainerScope scope(containerId);
58 CHECK_NULL_VOID(task);
59 ImageUtils::PostDelayedTask(std::move(task), TaskExecutor::TaskType::UI, delayTime, name.c_str(), priorityType);
60 }
61
PostToUI(std::function<void ()> && task,const std::string & name,const int32_t containerId,PriorityType priorityType)62 void ImageUtils::PostToUI(
63 std::function<void()>&& task, const std::string& name, const int32_t containerId, PriorityType priorityType)
64 {
65 ContainerScope scope(containerId);
66
67 CHECK_NULL_VOID(task);
68 ImageUtils::PostTask(std::move(task), TaskExecutor::TaskType::UI, name.c_str(), priorityType);
69 }
70
PostToBg(std::function<void ()> && task,const std::string & name,const int32_t containerId,PriorityType priorityType)71 void ImageUtils::PostToBg(
72 std::function<void()>&& task, const std::string& name, const int32_t containerId, PriorityType priorityType)
73 {
74 ContainerScope scope(containerId);
75
76 CHECK_NULL_VOID(task);
77 ImageUtils::PostTask(std::move(task), TaskExecutor::TaskType::BACKGROUND, name.c_str(), priorityType);
78 }
79 } // namespace OHOS::Ace::NG
80