blob: 026b55707c7aaea10cc80f2d3a168d0ad86b511b [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 Abraham45e4e362019-06-07 18:20:51 -070030DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset,
31 nsecs_t offsetThresholdForNextVsync, bool traceVsync,
Ana Krulec241cf832018-08-10 15:03:23 -070032 const char* name)
33 : mName(name),
Ana Krulec241cf832018-08-10 15:03:23 -070034 mTraceVsync(traceVsync),
Ana Krulec072233f2018-08-10 15:03:23 -070035 mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
36 mVsyncEventLabel(base::StringPrintf("VSYNC-%s", name)),
Alec Mourid7599d82019-05-22 19:58:00 -070037 mVsyncOffsetLabel(base::StringPrintf("VsyncOffset-%s", name)),
38 mVsyncNegativeOffsetLabel(base::StringPrintf("VsyncNegativeOffset-%s", name)),
Ana Krulec241cf832018-08-10 15:03:23 -070039 mDispSync(dispSync),
Ady Abraham45e4e362019-06-07 18:20:51 -070040 mPhaseOffset(phaseOffset),
41 mOffsetThresholdForNextVsync(offsetThresholdForNextVsync) {}
Ana Krulec241cf832018-08-10 15:03:23 -070042
43void DispSyncSource::setVSyncEnabled(bool enable) {
Ana Krulec072233f2018-08-10 15:03:23 -070044 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070045 if (enable) {
Alec Mourid7599d82019-05-22 19:58:00 -070046 tracePhaseOffset();
Ana Krulec241cf832018-08-10 15:03:23 -070047 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Alec Mouri7355eb22019-03-05 14:19:10 -080048 static_cast<DispSync::Callback*>(this),
49 mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070050 if (err != NO_ERROR) {
51 ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
52 }
Ana Krulec072233f2018-08-10 15:03:23 -070053 // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
Ana Krulec241cf832018-08-10 15:03:23 -070054 } else {
Alec Mouri7355eb22019-03-05 14:19:10 -080055 status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this),
56 &mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070057 if (err != NO_ERROR) {
58 ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
59 }
Ana Krulec072233f2018-08-10 15:03:23 -070060 // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
Ana Krulec241cf832018-08-10 15:03:23 -070061 }
62 mEnabled = enable;
63}
64
65void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
Ana Krulec072233f2018-08-10 15:03:23 -070066 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070067 mCallback = callback;
68}
69
70void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
Ana Krulec072233f2018-08-10 15:03:23 -070071 std::lock_guard lock(mVsyncMutex);
Ady Abraham45e4e362019-06-07 18:20:51 -070072 const nsecs_t period = mDispSync->getPeriod();
73 // Check if offset should be handled as negative
74 if (phaseOffset >= mOffsetThresholdForNextVsync) {
75 phaseOffset -= period;
Ana Krulec241cf832018-08-10 15:03:23 -070076 }
Ady Abraham45e4e362019-06-07 18:20:51 -070077
78 // Normalize phaseOffset to [-period, period)
79 const int numPeriods = phaseOffset / period;
80 phaseOffset -= numPeriods * period;
Ana Krulec241cf832018-08-10 15:03:23 -070081 mPhaseOffset = phaseOffset;
Alec Mourid7599d82019-05-22 19:58:00 -070082 tracePhaseOffset();
Ana Krulec241cf832018-08-10 15:03:23 -070083
84 // If we're not enabled, we don't need to mess with the listeners
85 if (!mEnabled) {
86 return;
87 }
88
89 status_t err =
90 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
91 if (err != NO_ERROR) {
92 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
93 }
94}
95
96void DispSyncSource::onDispSyncEvent(nsecs_t when) {
97 VSyncSource::Callback* callback;
98 {
Ana Krulec072233f2018-08-10 15:03:23 -070099 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -0700100 callback = mCallback;
Alec Mourid7599d82019-05-22 19:58:00 -0700101 }
Ana Krulec241cf832018-08-10 15:03:23 -0700102
Alec Mourid7599d82019-05-22 19:58:00 -0700103 if (mTraceVsync) {
104 mValue = (mValue + 1) % 2;
105 ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
Ana Krulec241cf832018-08-10 15:03:23 -0700106 }
107
108 if (callback != nullptr) {
109 callback->onVSyncEvent(when);
110 }
111}
112
Alec Mourid7599d82019-05-22 19:58:00 -0700113void DispSyncSource::tracePhaseOffset() {
114 if (mPhaseOffset > 0) {
115 ATRACE_INT(mVsyncOffsetLabel.c_str(), mPhaseOffset);
116 ATRACE_INT(mVsyncNegativeOffsetLabel.c_str(), 0);
117 } else {
118 ATRACE_INT(mVsyncOffsetLabel.c_str(), 0);
119 ATRACE_INT(mVsyncNegativeOffsetLabel.c_str(), -mPhaseOffset);
120 }
121}
122
123} // namespace android