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 <cinttypes>
20 #include <cstdint>
21 #include <numeric>
22 #include <string>
23 
24 namespace android {
25 namespace scheduler {
26 
LayerInfo(const std::string name,float minRefreshRate,float maxRefreshRate)27 LayerInfo::LayerInfo(const std::string name, float minRefreshRate, float maxRefreshRate)
28       : mName(name),
29         mMinRefreshDuration(1e9f / maxRefreshRate),
30         mLowActivityRefreshDuration(1e9f / minRefreshRate),
31         mRefreshRateHistory(mMinRefreshDuration) {}
32 
33 LayerInfo::~LayerInfo() = default;
34 
setLastPresentTime(nsecs_t lastPresentTime)35 void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime) {
36     std::lock_guard lock(mLock);
37 
38     // Buffers can come with a present time far in the future. That keeps them relevant.
39     mLastUpdatedTime = std::max(lastPresentTime, systemTime());
40     mPresentTimeHistory.insertPresentTime(mLastUpdatedTime);
41 
42     if (mLastPresentTime == 0) {
43         // First frame
44         mLastPresentTime = lastPresentTime;
45         return;
46     }
47 
48     const nsecs_t timeDiff = lastPresentTime - mLastPresentTime;
49     mLastPresentTime = lastPresentTime;
50     // Ignore time diff that are too high - those are stale values
51     if (timeDiff > OBSOLETE_TIME_EPSILON_NS.count()) return;
52     const nsecs_t refreshDuration = std::max(timeDiff, mMinRefreshDuration);
53     const int fps = 1e9f / refreshDuration;
54     mRefreshRateHistory.insertRefreshRate(fps);
55 }
56 
57 } // namespace scheduler
58 } // namespace android
59