1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <scheduler/PresentLatencyTracker.h>
18 
19 #include <cutils/compiler.h>
20 #include <log/log.h>
21 #include <ui/FenceTime.h>
22 
23 namespace android::scheduler {
24 
trackPendingFrame(TimePoint compositeTime,std::shared_ptr<FenceTime> presentFenceTime)25 Duration PresentLatencyTracker::trackPendingFrame(TimePoint compositeTime,
26                                                   std::shared_ptr<FenceTime> presentFenceTime) {
27     Duration presentLatency = Duration::zero();
28     while (!mPendingFrames.empty()) {
29         const auto& pendingFrame = mPendingFrames.front();
30         const auto presentTime =
31                 TimePoint::fromNs(pendingFrame.presentFenceTime->getCachedSignalTime());
32 
33         if (presentTime == TimePoint::fromNs(Fence::SIGNAL_TIME_PENDING)) {
34             break;
35         }
36 
37         if (presentTime == TimePoint::fromNs(Fence::SIGNAL_TIME_INVALID)) {
38             ALOGE("%s: Invalid present fence", __func__);
39         } else {
40             presentLatency = presentTime - pendingFrame.compositeTime;
41         }
42 
43         mPendingFrames.pop();
44     }
45 
46     mPendingFrames.emplace(compositeTime, std::move(presentFenceTime));
47 
48     if (CC_UNLIKELY(mPendingFrames.size() > kMaxPendingFrames)) {
49         ALOGE("%s: Too many pending frames", __func__);
50         mPendingFrames.pop();
51     }
52 
53     return presentLatency;
54 }
55 
56 } // namespace android::scheduler
57