1 /*
2  * Copyright 2018 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 
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 
19 #include "DispSyncSource.h"
20 
21 #include <android-base/stringprintf.h>
22 #include <utils/Trace.h>
23 #include <mutex>
24 
25 #include "DispSync.h"
26 #include "EventThread.h"
27 
28 namespace android {
29 
DispSyncSource(DispSync * dispSync,nsecs_t phaseOffset,nsecs_t offsetThresholdForNextVsync,bool traceVsync,const char * name)30 DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset,
31                                nsecs_t offsetThresholdForNextVsync, bool traceVsync,
32                                const char* name)
33       : mName(name),
34         mTraceVsync(traceVsync),
35         mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
36         mVsyncEventLabel(base::StringPrintf("VSYNC-%s", name)),
37         mVsyncOffsetLabel(base::StringPrintf("VsyncOffset-%s", name)),
38         mVsyncNegativeOffsetLabel(base::StringPrintf("VsyncNegativeOffset-%s", name)),
39         mDispSync(dispSync),
40         mPhaseOffset(phaseOffset),
41         mOffsetThresholdForNextVsync(offsetThresholdForNextVsync) {}
42 
setVSyncEnabled(bool enable)43 void DispSyncSource::setVSyncEnabled(bool enable) {
44     std::lock_guard lock(mVsyncMutex);
45     if (enable) {
46         tracePhaseOffset();
47         status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
48                                                    static_cast<DispSync::Callback*>(this),
49                                                    mLastCallbackTime);
50         if (err != NO_ERROR) {
51             ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
52         }
53         // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
54     } else {
55         status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this),
56                                                       &mLastCallbackTime);
57         if (err != NO_ERROR) {
58             ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
59         }
60         // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
61     }
62     mEnabled = enable;
63 }
64 
setCallback(VSyncSource::Callback * callback)65 void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
66     std::lock_guard lock(mCallbackMutex);
67     mCallback = callback;
68 }
69 
setPhaseOffset(nsecs_t phaseOffset)70 void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
71     std::lock_guard lock(mVsyncMutex);
72     const nsecs_t period = mDispSync->getPeriod();
73     // Check if offset should be handled as negative
74     if (phaseOffset >= mOffsetThresholdForNextVsync) {
75         phaseOffset -= period;
76     }
77 
78     // Normalize phaseOffset to [-period, period)
79     const int numPeriods = phaseOffset / period;
80     phaseOffset -= numPeriods * period;
81     if (mPhaseOffset == phaseOffset) {
82         return;
83     }
84 
85     mPhaseOffset = phaseOffset;
86     tracePhaseOffset();
87 
88     // If we're not enabled, we don't need to mess with the listeners
89     if (!mEnabled) {
90         return;
91     }
92 
93     status_t err =
94             mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
95     if (err != NO_ERROR) {
96         ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
97     }
98 }
99 
onDispSyncEvent(nsecs_t when)100 void DispSyncSource::onDispSyncEvent(nsecs_t when) {
101     VSyncSource::Callback* callback;
102     {
103         std::lock_guard lock(mCallbackMutex);
104         callback = mCallback;
105     }
106 
107     if (mTraceVsync) {
108         mValue = (mValue + 1) % 2;
109         ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
110     }
111 
112     if (callback != nullptr) {
113         callback->onVSyncEvent(when);
114     }
115 }
116 
tracePhaseOffset()117 void DispSyncSource::tracePhaseOffset() {
118     if (mPhaseOffset > 0) {
119         ATRACE_INT(mVsyncOffsetLabel.c_str(), mPhaseOffset);
120         ATRACE_INT(mVsyncNegativeOffsetLabel.c_str(), 0);
121     } else {
122         ATRACE_INT(mVsyncOffsetLabel.c_str(), 0);
123         ATRACE_INT(mVsyncNegativeOffsetLabel.c_str(), -mPhaseOffset);
124     }
125 }
126 
127 } // namespace android
128