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 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 if (fence->SyncFileReadTimestamp() != SyncFence::FENCE_PENDING_TIMESTAMP) {
38 RS_TRACE_NAME_FMT("%s %d has signaled", threadName_.c_str(), fencesQueued_.load());
39 fencesQueued_++;
40 fencesSignaled_++;
41 return;
42 }
43
44 RS_TRACE_NAME_FMT("%s %d", threadName_.c_str(), fencesQueued_.load());
45 if (handler_) {
46 handler_->PostTask([this, fence]() {
47 Loop(fence);
48 });
49 fencesQueued_++;
50 }
51 }
52
Loop(const sptr<SyncFence> & fence)53 void SyncFenceTracker::Loop(const sptr<SyncFence>& fence)
54 {
55 uint32_t fenceIndex = 0;
56 fenceIndex = fencesSignaled_.load();
57 {
58 RS_TRACE_NAME_FMT("Waiting for %s %d", threadName_.c_str(), fenceIndex);
59 int32_t result = fence->Wait(SYNC_TIME_OUT);
60 if (result < 0) {
61 HiLog::Debug(LABEL, "Error waiting for SyncFence: %s", strerror(result));
62 }
63 }
64 fencesSignaled_++;
65 }
66 } // namespace OHOS