blob: 723d71ff39ef3bd88b6145f259c23296bc179f35 [file] [log] [blame]
Ady Abraham09bd3922019-04-08 10:44:56 -07001/*
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
24namespace android {
25namespace scheduler {
26
Ana Krulecad083c42019-06-26 16:28:08 -070027LayerInfo::LayerInfo(const std::string name, float minRefreshRate, float maxRefreshRate)
Ady Abraham09bd3922019-04-08 10:44:56 -070028 : mName(name),
29 mMinRefreshDuration(1e9f / maxRefreshRate),
Ana Krulecad083c42019-06-26 16:28:08 -070030 mLowActivityRefreshDuration(1e9f / minRefreshRate),
Ady Abraham09bd3922019-04-08 10:44:56 -070031 mRefreshRateHistory(mMinRefreshDuration) {}
32
33LayerInfo::~LayerInfo() = default;
34
35void 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 Abraham616b8322019-06-12 13:30:07 -070042 if (mLastPresentTime == 0) {
43 // First frame
44 mLastPresentTime = lastPresentTime;
45 return;
46 }
47
Ady Abraham09bd3922019-04-08 10:44:56 -070048 const nsecs_t timeDiff = lastPresentTime - mLastPresentTime;
49 mLastPresentTime = lastPresentTime;
50 // Ignore time diff that are too high - those are stale values
Ana Krulecad083c42019-06-26 16:28:08 -070051 if (timeDiff > OBSOLETE_TIME_EPSILON_NS.count()) return;
Ady Abrahamcdd0f9d2019-08-09 10:44:59 -070052 const nsecs_t refreshDuration = std::max(timeDiff, mMinRefreshDuration);
Ady Abraham187d2d82019-07-10 16:18:48 -070053 const int fps = 1e9f / refreshDuration;
54 mRefreshRateHistory.insertRefreshRate(fps);
Ady Abraham09bd3922019-04-08 10:44:56 -070055}
56
57} // namespace scheduler
58} // namespace android