• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "feature/hwc/hpae_offline/rs_hpae_offline_process_syncer.h"
16 #include "feature/hwc/hpae_offline/rs_hpae_offline_util.h"
17 #include "common/rs_optional_trace.h"
18 
19 namespace OHOS {
20 namespace Rosen {
21 
RegisterPostedTask(uint64_t taskId)22 std::shared_ptr<ProcessOfflineFuture> RSHpaeOfflineProcessSyncer::RegisterPostedTask(
23     uint64_t taskId)
24 {
25     RS_OPTIONAL_TRACE_NAME_FMT("hpae_offline: Register task [%" PRIu64 "].", taskId);
26     RS_OFFLINE_LOGD("hpae_offline: Register task [%{public}" PRIu64 "].", taskId);
27     {
28         std::lock_guard<std::mutex> lock(poolMtx_);
29         if (resultPool_.find(taskId) == resultPool_.end()) {
30             auto future = std::make_shared<ProcessOfflineFuture>();
31             resultPool_.insert({taskId, future});
32             return future;
33         }
34     }
35     RS_OFFLINE_LOGW("Task with %{public}" PRIu64 " already exists.", taskId);
36     return nullptr;
37 }
38 
MarkTaskDoneAndSetResult(std::shared_ptr<ProcessOfflineFuture> futurePtr,const ProcessOfflineResult & processOfflineResult)39 void RSHpaeOfflineProcessSyncer::MarkTaskDoneAndSetResult(
40     std::shared_ptr<ProcessOfflineFuture> futurePtr, const ProcessOfflineResult& processOfflineResult)
41 {
42     RS_OFFLINE_LOGD("task done, set result and notify.");
43     {
44         std::lock_guard<std::mutex> lock(futurePtr->mtx);
45         futurePtr->done = true;
46         futurePtr->result = processOfflineResult;
47         futurePtr->cv.notify_one();
48     }
49 }
50 
GetTaskFuture(uint64_t taskId)51 std::shared_ptr<ProcessOfflineFuture> RSHpaeOfflineProcessSyncer::GetTaskFuture(uint64_t taskId)
52 {
53     std::lock_guard<std::mutex> lock(poolMtx_);
54     auto iter = resultPool_.find(taskId);
55     if (iter == resultPool_.end()) {
56         RS_OFFLINE_LOGW("%{public}s, taskPool has not wanted task.", __func__);
57         return nullptr;
58     }
59     auto future = std::move(iter->second);
60     resultPool_.erase(iter);
61     return future;
62 }
63 
WaitForTaskAndGetResult(uint64_t taskId,std::chrono::milliseconds timeout,ProcessOfflineResult & processOfflineResult)64 bool RSHpaeOfflineProcessSyncer::WaitForTaskAndGetResult(uint64_t taskId,
65     std::chrono::milliseconds timeout, ProcessOfflineResult& processOfflineResult)
66 {
67     RS_TRACE_NAME_FMT("Wait for offline task.", __func__);
68     auto future = GetTaskFuture(taskId);
69     if (!future) {
70         RS_OFFLINE_LOGE("%{public}s, task is null.", __func__);
71         return false;
72     }
73 
74     std::unique_lock<std::mutex> lock(future->mtx);
75     if (timeout == std::chrono::milliseconds::max()) {
76         future->cv.wait(lock, [future] { return future->done; });
77     } else {
78         if (!future->cv.wait_for(lock, timeout, [future] { return future->done; })) {
79             future->timeout = true;
80             RS_OFFLINE_LOGW("%{public}s, wait task[%{public}" PRIu64 "] timeout!!!", __func__, taskId);
81             return false;
82         }
83     }
84     processOfflineResult = future->result;
85     return true;
86 }
87 
GetResultPoolSize()88 int32_t RSHpaeOfflineProcessSyncer::GetResultPoolSize()
89 {
90     std::lock_guard<std::mutex> lock(poolMtx_);
91     return resultPool_.size();
92 }
93 
ClearResultPool()94 void RSHpaeOfflineProcessSyncer::ClearResultPool()
95 {
96     std::lock_guard<std::mutex> lock(poolMtx_);
97     resultPool_.clear();
98 }
99 
100 } // namespace Rosen
101 } // namespace OHOS