blob: bd4b0ec0d12d1b067d4b179d22b1140851168592 [file] [log] [blame]
Ana Krulec241cf832018-08-10 15:03:23 -07001/*
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
Midas Chienbc5f22f2018-08-16 15:51:19 +080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Ana Krulec241cf832018-08-10 15:03:23 -070019#include "DispSyncSource.h"
20
Ana Krulec072233f2018-08-10 15:03:23 -070021#include <android-base/stringprintf.h>
Ana Krulec241cf832018-08-10 15:03:23 -070022#include <utils/Trace.h>
Ana Krulec072233f2018-08-10 15:03:23 -070023#include <mutex>
Ana Krulec241cf832018-08-10 15:03:23 -070024
25#include "DispSync.h"
26#include "EventThread.h"
27
28namespace android {
29
Ady Abraham9e16a482019-12-03 17:19:41 -080030DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Ana Krulec241cf832018-08-10 15:03:23 -070031 const char* name)
32 : mName(name),
Ady Abraham50204dd2019-07-19 15:47:11 -070033 mValue(base::StringPrintf("VSYNC-%s", name), 0),
Ana Krulec241cf832018-08-10 15:03:23 -070034 mTraceVsync(traceVsync),
Ana Krulec072233f2018-08-10 15:03:23 -070035 mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
Ana Krulec241cf832018-08-10 15:03:23 -070036 mDispSync(dispSync),
Ady Abraham9e16a482019-12-03 17:19:41 -080037 mPhaseOffset(base::StringPrintf("VsyncOffset-%s", name), phaseOffset) {}
Ana Krulec241cf832018-08-10 15:03:23 -070038
39void DispSyncSource::setVSyncEnabled(bool enable) {
Ana Krulec072233f2018-08-10 15:03:23 -070040 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070041 if (enable) {
42 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Alec Mouri7355eb22019-03-05 14:19:10 -080043 static_cast<DispSync::Callback*>(this),
44 mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070045 if (err != NO_ERROR) {
46 ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
47 }
Ana Krulec072233f2018-08-10 15:03:23 -070048 // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
Ana Krulec241cf832018-08-10 15:03:23 -070049 } else {
Alec Mouri7355eb22019-03-05 14:19:10 -080050 status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this),
51 &mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070052 if (err != NO_ERROR) {
53 ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
54 }
Ana Krulec072233f2018-08-10 15:03:23 -070055 // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
Ana Krulec241cf832018-08-10 15:03:23 -070056 }
57 mEnabled = enable;
58}
59
60void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
Ana Krulec072233f2018-08-10 15:03:23 -070061 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070062 mCallback = callback;
63}
64
65void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
Ana Krulec072233f2018-08-10 15:03:23 -070066 std::lock_guard lock(mVsyncMutex);
Ady Abraham45e4e362019-06-07 18:20:51 -070067 const nsecs_t period = mDispSync->getPeriod();
Ady Abraham45e4e362019-06-07 18:20:51 -070068
69 // Normalize phaseOffset to [-period, period)
70 const int numPeriods = phaseOffset / period;
71 phaseOffset -= numPeriods * period;
Ady Abraham11b6a702019-06-27 11:24:19 -070072 if (mPhaseOffset == phaseOffset) {
73 return;
74 }
75
Ana Krulec241cf832018-08-10 15:03:23 -070076 mPhaseOffset = phaseOffset;
77
78 // If we're not enabled, we don't need to mess with the listeners
79 if (!mEnabled) {
80 return;
81 }
82
83 status_t err =
84 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
85 if (err != NO_ERROR) {
86 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
87 }
88}
89
90void DispSyncSource::onDispSyncEvent(nsecs_t when) {
91 VSyncSource::Callback* callback;
92 {
Ana Krulec072233f2018-08-10 15:03:23 -070093 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070094 callback = mCallback;
Alec Mourid7599d82019-05-22 19:58:00 -070095 }
Ana Krulec241cf832018-08-10 15:03:23 -070096
Alec Mourid7599d82019-05-22 19:58:00 -070097 if (mTraceVsync) {
98 mValue = (mValue + 1) % 2;
Ana Krulec241cf832018-08-10 15:03:23 -070099 }
100
101 if (callback != nullptr) {
102 callback->onVSyncEvent(when);
103 }
104}
105
Alec Mourid7599d82019-05-22 19:58:00 -0700106} // namespace android