blob: abb633028363c0c294a481f1250fbdec24c59ec1 [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,
30 int64_t frameDeadline) {
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;
Steven Thomas6fabb5a2020-08-21 16:56:08 -070035 }
36
John Reck18f16e62014-05-02 16:46:41 -070037 if (vsync > mFrameTimeNanos) {
38 mFrameTimeNanos = vsync;
John Recka733f892014-12-19 11:37:21 -080039 return true;
John Reck18f16e62014-05-02 16:46:41 -070040 }
John Recka733f892014-12-19 11:37:21 -080041 return false;
John Reck18f16e62014-05-02 16:46:41 -070042}
43
John Reckba6adf62015-02-19 14:36:50 -080044nsecs_t TimeLord::computeFrameTimeNanos() {
John Reck18f16e62014-05-02 16:46:41 -070045 // Logic copied from Choreographer.java
Jerome Gaillarde218c692019-06-14 12:58:57 +010046 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070047 nsecs_t jitterNanos = now - mFrameTimeNanos;
48 if (jitterNanos >= mFrameIntervalNanos) {
49 nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalNanos;
50 mFrameTimeNanos = now - lastFrameOffset;
Steven Thomas6fabb5a2020-08-21 16:56:08 -070051 // mFrameVsyncId is not adjusted here as we still want to send
52 // the vsync id that started this frame to the Surface Composer
John Reck18f16e62014-05-02 16:46:41 -070053 }
John Reckba6adf62015-02-19 14:36:50 -080054 return mFrameTimeNanos;
55}
56
John Reck18f16e62014-05-02 16:46:41 -070057} /* namespace renderthread */
58} /* namespace uirenderer */
59} /* namespace android */