xref: /aosp_15_r20/frameworks/native/libs/gui/CompositorTiming.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "CompositorTiming"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <cutils/compiler.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <gui/CompositorTiming.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker namespace android::gui {
24*38e8c45fSAndroid Build Coastguard Worker 
CompositorTiming(nsecs_t vsyncDeadline,nsecs_t vsyncPeriod,nsecs_t vsyncPhase,nsecs_t presentLatency)25*38e8c45fSAndroid Build Coastguard Worker CompositorTiming::CompositorTiming(nsecs_t vsyncDeadline, nsecs_t vsyncPeriod, nsecs_t vsyncPhase,
26*38e8c45fSAndroid Build Coastguard Worker                                    nsecs_t presentLatency) {
27*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(vsyncPeriod <= 0)) {
28*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Invalid VSYNC period");
29*38e8c45fSAndroid Build Coastguard Worker         return;
30*38e8c45fSAndroid Build Coastguard Worker     }
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t idealLatency = [=] {
33*38e8c45fSAndroid Build Coastguard Worker         // Modulo rounds toward 0 not INT64_MIN, so treat signs separately.
34*38e8c45fSAndroid Build Coastguard Worker         if (vsyncPhase < 0) return -vsyncPhase % vsyncPeriod;
35*38e8c45fSAndroid Build Coastguard Worker 
36*38e8c45fSAndroid Build Coastguard Worker         const nsecs_t latency = (vsyncPeriod - vsyncPhase) % vsyncPeriod;
37*38e8c45fSAndroid Build Coastguard Worker         return latency > 0 ? latency : vsyncPeriod;
38*38e8c45fSAndroid Build Coastguard Worker     }();
39*38e8c45fSAndroid Build Coastguard Worker 
40*38e8c45fSAndroid Build Coastguard Worker     // Snap the latency to a value that removes scheduling jitter from the composite and present
41*38e8c45fSAndroid Build Coastguard Worker     // times, which often have >1ms of jitter. Reducing jitter is important if an app attempts to
42*38e8c45fSAndroid Build Coastguard Worker     // extrapolate something like user input to an accurate present time. Snapping also allows an
43*38e8c45fSAndroid Build Coastguard Worker     // app to precisely calculate vsyncPhase with (presentLatency % interval).
44*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t bias = vsyncPeriod / 2;
45*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t extraVsyncs = (presentLatency - idealLatency + bias) / vsyncPeriod;
46*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t snappedLatency =
47*38e8c45fSAndroid Build Coastguard Worker             extraVsyncs > 0 ? idealLatency + extraVsyncs * vsyncPeriod : idealLatency;
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     this->deadline = vsyncDeadline - idealLatency;
50*38e8c45fSAndroid Build Coastguard Worker     this->interval = vsyncPeriod;
51*38e8c45fSAndroid Build Coastguard Worker     this->presentLatency = snappedLatency;
52*38e8c45fSAndroid Build Coastguard Worker }
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker } // namespace android::gui
55