John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 17 | #include <limits> |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 18 | |
| 19 | namespace android { |
| 20 | namespace uirenderer { |
| 21 | namespace renderthread { |
| 22 | |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 23 | TimeLord::TimeLord() : mFrameIntervalNanos(milliseconds_to_nanoseconds(16)), |
| 24 | mFrameTimeNanos(0), |
| 25 | mFrameIntendedTimeNanos(0), |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 26 | mFrameVsyncId(-1), |
| 27 | mFrameDeadline(std::numeric_limits<int64_t>::max()){} |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 28 | |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 29 | bool TimeLord::vsyncReceived(nsecs_t vsync, nsecs_t intendedVsync, int64_t vsyncId, |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame^] | 30 | int64_t frameDeadline, nsecs_t frameInterval) { |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 31 | if (intendedVsync > mFrameIntendedTimeNanos) { |
| 32 | mFrameIntendedTimeNanos = intendedVsync; |
| 33 | mFrameVsyncId = vsyncId; |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 34 | mFrameDeadline = frameDeadline; |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame^] | 35 | if (frameInterval > 0) { |
| 36 | mFrameIntervalNanos = frameInterval; |
| 37 | } |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 38 | } |
| 39 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 40 | if (vsync > mFrameTimeNanos) { |
| 41 | mFrameTimeNanos = vsync; |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 42 | return true; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 43 | } |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 44 | return false; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 45 | } |
| 46 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 47 | nsecs_t TimeLord::computeFrameTimeNanos() { |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 48 | // Logic copied from Choreographer.java |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 49 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 50 | nsecs_t jitterNanos = now - mFrameTimeNanos; |
| 51 | if (jitterNanos >= mFrameIntervalNanos) { |
| 52 | nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalNanos; |
| 53 | mFrameTimeNanos = now - lastFrameOffset; |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 54 | // mFrameVsyncId is not adjusted here as we still want to send |
| 55 | // the vsync id that started this frame to the Surface Composer |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 56 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 57 | return mFrameTimeNanos; |
| 58 | } |
| 59 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 60 | } /* namespace renderthread */ |
| 61 | } /* namespace uirenderer */ |
| 62 | } /* namespace android */ |