1 /*
2  * Copyright 2019 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 "LayerInfo.h"
18 
19 #include <algorithm>
20 #include <utility>
21 
22 namespace android::scheduler {
23 
LayerInfo(float lowRefreshRate,float highRefreshRate)24 LayerInfo::LayerInfo(float lowRefreshRate, float highRefreshRate)
25       : mLowRefreshRate(lowRefreshRate), mHighRefreshRate(highRefreshRate) {}
26 
setLastPresentTime(nsecs_t lastPresentTime,nsecs_t now)27 void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now) {
28     lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
29 
30     // Buffers can come with a present time far in the future. That keeps them relevant.
31     mLastUpdatedTime = std::max(lastPresentTime, now);
32     mPresentTimeHistory.insertPresentTime(mLastUpdatedTime);
33 
34     if (mLastPresentTime == 0) {
35         // First frame
36         mLastPresentTime = lastPresentTime;
37         return;
38     }
39 
40     const nsecs_t period = lastPresentTime - mLastPresentTime;
41     mLastPresentTime = lastPresentTime;
42     // Ignore time diff that are too high - those are stale values
43     if (period > MAX_ACTIVE_LAYER_PERIOD_NS.count()) return;
44 
45     const float fps = std::min(1e9f / period, mHighRefreshRate);
46     mRefreshRateHistory.insertRefreshRate(fps);
47 }
48 
49 } // namespace android::scheduler
50