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"
17 #include <limits>
18 #include "FrameInfo.h"
19
20 namespace android {
21 namespace uirenderer {
22 namespace renderthread {
23
TimeLord()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()) {}
30
vsyncReceived(nsecs_t vsync,nsecs_t intendedVsync,int64_t vsyncId,int64_t frameDeadline,nsecs_t frameInterval)31 bool TimeLord::vsyncReceived(nsecs_t vsync, nsecs_t intendedVsync, int64_t vsyncId,
32 int64_t frameDeadline, nsecs_t frameInterval) {
33 if (intendedVsync > mFrameIntendedTimeNanos) {
34 mFrameIntendedTimeNanos = intendedVsync;
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;
40 mFrameDeadline = frameDeadline;
41 if (frameInterval > 0) {
42 mFrameIntervalNanos = frameInterval;
43 }
44 }
45
46 if (vsync > mFrameTimeNanos) {
47 mFrameTimeNanos = vsync;
48 return true;
49 }
50 return false;
51 }
52
computeFrameTimeNanos()53 nsecs_t TimeLord::computeFrameTimeNanos() {
54 // Logic copied from Choreographer.java
55 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
56 nsecs_t jitterNanos = now - mFrameTimeNanos;
57 if (jitterNanos >= mFrameIntervalNanos) {
58 nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalNanos;
59 mFrameTimeNanos = now - lastFrameOffset;
60 // mFrameVsyncId is not adjusted here as we still want to send
61 // the vsync id that started this frame to the Surface Composer
62 }
63 return mFrameTimeNanos;
64 }
65
66 } /* namespace renderthread */
67 } /* namespace uirenderer */
68 } /* namespace android */
69