Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 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 | |
Ana Krulec | ad083c4 | 2019-06-26 16:28:08 -0700 | [diff] [blame] | 27 | LayerInfo::LayerInfo(const std::string name, float minRefreshRate, float maxRefreshRate) |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 28 | : mName(name), |
| 29 | mMinRefreshDuration(1e9f / maxRefreshRate), |
Ana Krulec | ad083c4 | 2019-06-26 16:28:08 -0700 | [diff] [blame] | 30 | mLowActivityRefreshDuration(1e9f / minRefreshRate), |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 31 | mRefreshRateHistory(mMinRefreshDuration) {} |
| 32 | |
| 33 | LayerInfo::~LayerInfo() = default; |
| 34 | |
| 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 | |
Ady Abraham | 616b832 | 2019-06-12 13:30:07 -0700 | [diff] [blame] | 42 | if (mLastPresentTime == 0) { |
| 43 | // First frame |
| 44 | mLastPresentTime = lastPresentTime; |
| 45 | return; |
| 46 | } |
| 47 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 48 | const nsecs_t timeDiff = lastPresentTime - mLastPresentTime; |
| 49 | mLastPresentTime = lastPresentTime; |
| 50 | // Ignore time diff that are too high - those are stale values |
Ana Krulec | ad083c4 | 2019-06-26 16:28:08 -0700 | [diff] [blame] | 51 | if (timeDiff > OBSOLETE_TIME_EPSILON_NS.count()) return; |
Ady Abraham | cdd0f9d | 2019-08-09 10:44:59 -0700 | [diff] [blame] | 52 | const nsecs_t refreshDuration = std::max(timeDiff, mMinRefreshDuration); |
Ady Abraham | 187d2d8 | 2019-07-10 16:18:48 -0700 | [diff] [blame] | 53 | const int fps = 1e9f / refreshDuration; |
| 54 | mRefreshRateHistory.insertRefreshRate(fps); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } // namespace scheduler |
| 58 | } // namespace android |