blob: 406066c92bab27b942b2b39689fd4c91719aa6a4 [file] [log] [blame]
John Reck18f16e62014-05-02 16:46:41 -07001/*
2 * Copyright (C) 2014 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#include "TimeLord.h"
Ady Abrahamdfb13982020-10-05 17:59:09 -070017#include <limits>
John Reck18f16e62014-05-02 16:46:41 -070018
19namespace android {
20namespace uirenderer {
21namespace renderthread {
22
Steven Thomas6fabb5a2020-08-21 16:56:08 -070023TimeLord::TimeLord() : mFrameIntervalNanos(milliseconds_to_nanoseconds(16)),
24 mFrameTimeNanos(0),
25 mFrameIntendedTimeNanos(0),
Ady Abrahamdfb13982020-10-05 17:59:09 -070026 mFrameVsyncId(-1),
27 mFrameDeadline(std::numeric_limits<int64_t>::max()){}
John Reck18f16e62014-05-02 16:46:41 -070028
Ady Abrahamdfb13982020-10-05 17:59:09 -070029bool TimeLord::vsyncReceived(nsecs_t vsync, nsecs_t intendedVsync, int64_t vsyncId,
Jorim Jaggi10f328c2021-01-19 00:08:02 +010030 int64_t frameDeadline, nsecs_t frameInterval) {
Steven Thomas6fabb5a2020-08-21 16:56:08 -070031 if (intendedVsync > mFrameIntendedTimeNanos) {
32 mFrameIntendedTimeNanos = intendedVsync;
33 mFrameVsyncId = vsyncId;
Ady Abrahamdfb13982020-10-05 17:59:09 -070034 mFrameDeadline = frameDeadline;
Jorim Jaggi10f328c2021-01-19 00:08:02 +010035 if (frameInterval > 0) {
36 mFrameIntervalNanos = frameInterval;
37 }
Steven Thomas6fabb5a2020-08-21 16:56:08 -070038 }
39
John Reck18f16e62014-05-02 16:46:41 -070040 if (vsync > mFrameTimeNanos) {
41 mFrameTimeNanos = vsync;
John Recka733f892014-12-19 11:37:21 -080042 return true;
John Reck18f16e62014-05-02 16:46:41 -070043 }
John Recka733f892014-12-19 11:37:21 -080044 return false;
John Reck18f16e62014-05-02 16:46:41 -070045}
46
John Reckba6adf62015-02-19 14:36:50 -080047nsecs_t TimeLord::computeFrameTimeNanos() {
John Reck18f16e62014-05-02 16:46:41 -070048 // Logic copied from Choreographer.java
Jerome Gaillarde218c692019-06-14 12:58:57 +010049 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070050 nsecs_t jitterNanos = now - mFrameTimeNanos;
51 if (jitterNanos >= mFrameIntervalNanos) {
52 nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalNanos;
53 mFrameTimeNanos = now - lastFrameOffset;
Steven Thomas6fabb5a2020-08-21 16:56:08 -070054 // mFrameVsyncId is not adjusted here as we still want to send
55 // the vsync id that started this frame to the Surface Composer
John Reck18f16e62014-05-02 16:46:41 -070056 }
John Reckba6adf62015-02-19 14:36:50 -080057 return mFrameTimeNanos;
58}
59
John Reck18f16e62014-05-02 16:46:41 -070060} /* namespace renderthread */
61} /* namespace uirenderer */
62} /* namespace android */