blob: a43fcdc6e202e02092703101ada0b7eb78d416af [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>
Ady Abraham505e7302021-06-23 15:03:48 -070018#include "FrameInfo.h"
John Reck18f16e62014-05-02 16:46:41 -070019
20namespace android {
21namespace uirenderer {
22namespace renderthread {
23
Ady Abraham505e7302021-06-23 15:03:48 -070024TimeLord::TimeLord()
25 : mFrameIntervalNanos(milliseconds_to_nanoseconds(16))
26 , mFrameTimeNanos(0)
27 , mFrameIntendedTimeNanos(0)
28 , mFrameVsyncId(UiFrameInfoBuilder::INVALID_VSYNC_ID)
29 , mFrameDeadline(std::numeric_limits<int64_t>::max()) {}
John Reck18f16e62014-05-02 16:46:41 -070030
Ady Abrahamdfb13982020-10-05 17:59:09 -070031bool TimeLord::vsyncReceived(nsecs_t vsync, nsecs_t intendedVsync, int64_t vsyncId,
Jorim Jaggi10f328c2021-01-19 00:08:02 +010032 int64_t frameDeadline, nsecs_t frameInterval) {
Steven Thomas6fabb5a2020-08-21 16:56:08 -070033 if (intendedVsync > mFrameIntendedTimeNanos) {
34 mFrameIntendedTimeNanos = intendedVsync;
Ady Abraham505e7302021-06-23 15:03:48 -070035
36 // The intendedVsync might have been advanced to account for scheduling
37 // jitter. Since we don't have a way to advance the vsync id we just
38 // reset it.
39 mFrameVsyncId = (vsyncId > mFrameVsyncId) ? vsyncId : UiFrameInfoBuilder::INVALID_VSYNC_ID;
Ady Abrahamdfb13982020-10-05 17:59:09 -070040 mFrameDeadline = frameDeadline;
Jorim Jaggi10f328c2021-01-19 00:08:02 +010041 if (frameInterval > 0) {
42 mFrameIntervalNanos = frameInterval;
43 }
Steven Thomas6fabb5a2020-08-21 16:56:08 -070044 }
45
John Reck18f16e62014-05-02 16:46:41 -070046 if (vsync > mFrameTimeNanos) {
47 mFrameTimeNanos = vsync;
John Recka733f892014-12-19 11:37:21 -080048 return true;
John Reck18f16e62014-05-02 16:46:41 -070049 }
John Recka733f892014-12-19 11:37:21 -080050 return false;
John Reck18f16e62014-05-02 16:46:41 -070051}
52
John Reckba6adf62015-02-19 14:36:50 -080053nsecs_t TimeLord::computeFrameTimeNanos() {
John Reck18f16e62014-05-02 16:46:41 -070054 // Logic copied from Choreographer.java
Jerome Gaillarde218c692019-06-14 12:58:57 +010055 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck18f16e62014-05-02 16:46:41 -070056 nsecs_t jitterNanos = now - mFrameTimeNanos;
57 if (jitterNanos >= mFrameIntervalNanos) {
58 nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalNanos;
59 mFrameTimeNanos = now - lastFrameOffset;
Steven Thomas6fabb5a2020-08-21 16:56:08 -070060 // mFrameVsyncId is not adjusted here as we still want to send
61 // the vsync id that started this frame to the Surface Composer
John Reck18f16e62014-05-02 16:46:41 -070062 }
John Reckba6adf62015-02-19 14:36:50 -080063 return mFrameTimeNanos;
64}
65
John Reck18f16e62014-05-02 16:46:41 -070066} /* namespace renderthread */
67} /* namespace uirenderer */
68} /* namespace android */