1 /*
2 * Copyright (c) 2022 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 "sync_fence_tracker.h"
17 #include "hilog/log.h"
18 #include "rs_trace.h"
19
20 namespace OHOS {
21 using namespace OHOS::HiviewDFX;
22 namespace {
23 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD001400, "SyncFence" };
24 }
25
SyncFenceTracker(const std::string threadName)26 SyncFenceTracker::SyncFenceTracker(const std::string threadName)
27 : threadName_(threadName),
28 fencesQueued_(0),
29 fencesSignaled_(0)
30 {
31 auto runner = OHOS::AppExecFwk::EventRunner::Create(threadName_);
32 handler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner);
33 }
34
TrackFence(const sptr<SyncFence> & fence)35 void SyncFenceTracker::TrackFence(const sptr<SyncFence>& fence)
36 {
37 std::string fenceMsg;
38 if (fence->SyncFileReadTimestamp() != SyncFence::FENCE_PENDING_TIMESTAMP) {
39 fenceMsg = threadName_ + " " + std::to_string(fencesQueued_.load()) + " has signaled";
40 RS_TRACE_NAME(fenceMsg.c_str());
41 fencesQueued_++;
42 fencesSignaled_++;
43 return;
44 }
45
46 fenceMsg = threadName_ + " " + std::to_string(fencesQueued_.load());
47 RS_TRACE_NAME(fenceMsg.c_str());
48 if (handler_) {
49 handler_->PostTask([this, fence]() {
50 Loop(fence);
51 });
52 fencesQueued_++;
53 }
54 }
55
Loop(const sptr<SyncFence> & fence)56 void SyncFenceTracker::Loop(const sptr<SyncFence>& fence)
57 {
58 uint32_t fenceIndex = 0;
59 fenceIndex = fencesSignaled_.load();
60 {
61 std::string fenceMsg = "Waiting for " + threadName_ + " " + std::to_string(fenceIndex);
62 RS_TRACE_NAME(fenceMsg.c_str());
63 int32_t result = fence->Wait(SYNC_TIME_OUT);
64 if (result < 0) {
65 HiLog::Debug(LABEL, "Error waiting for SyncFence: %s", strerror(result));
66 }
67 }
68 fencesSignaled_++;
69 }
70 } // namespace OHOS