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