blob: 265b8aa47feadc904db209abe796295f1f2d2857 [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)),
Ana Krulec241cf832018-08-10 15:03:23 -070037 mDispSync(dispSync),
Ady Abraham45e4e362019-06-07 18:20:51 -070038 mPhaseOffset(phaseOffset),
39 mOffsetThresholdForNextVsync(offsetThresholdForNextVsync) {}
Ana Krulec241cf832018-08-10 15:03:23 -070040
41void DispSyncSource::setVSyncEnabled(bool enable) {
Ana Krulec072233f2018-08-10 15:03:23 -070042 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070043 if (enable) {
44 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Alec Mouri7355eb22019-03-05 14:19:10 -080045 static_cast<DispSync::Callback*>(this),
46 mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070047 if (err != NO_ERROR) {
48 ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
49 }
Ana Krulec072233f2018-08-10 15:03:23 -070050 // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
Ana Krulec241cf832018-08-10 15:03:23 -070051 } else {
Alec Mouri7355eb22019-03-05 14:19:10 -080052 status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this),
53 &mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070054 if (err != NO_ERROR) {
55 ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
56 }
Ana Krulec072233f2018-08-10 15:03:23 -070057 // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
Ana Krulec241cf832018-08-10 15:03:23 -070058 }
59 mEnabled = enable;
60}
61
62void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
Ana Krulec072233f2018-08-10 15:03:23 -070063 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070064 mCallback = callback;
65}
66
67void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
Ana Krulec072233f2018-08-10 15:03:23 -070068 std::lock_guard lock(mVsyncMutex);
Ady Abraham45e4e362019-06-07 18:20:51 -070069 const nsecs_t period = mDispSync->getPeriod();
70 // Check if offset should be handled as negative
71 if (phaseOffset >= mOffsetThresholdForNextVsync) {
72 phaseOffset -= period;
Ana Krulec241cf832018-08-10 15:03:23 -070073 }
Ady Abraham45e4e362019-06-07 18:20:51 -070074
75 // Normalize phaseOffset to [-period, period)
76 const int numPeriods = phaseOffset / period;
77 phaseOffset -= numPeriods * period;
Ana Krulec241cf832018-08-10 15:03:23 -070078 mPhaseOffset = phaseOffset;
79
80 // If we're not enabled, we don't need to mess with the listeners
81 if (!mEnabled) {
82 return;
83 }
84
85 status_t err =
86 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
87 if (err != NO_ERROR) {
88 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
89 }
90}
91
92void DispSyncSource::onDispSyncEvent(nsecs_t when) {
93 VSyncSource::Callback* callback;
94 {
Ana Krulec072233f2018-08-10 15:03:23 -070095 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070096 callback = mCallback;
97
98 if (mTraceVsync) {
99 mValue = (mValue + 1) % 2;
Ana Krulec072233f2018-08-10 15:03:23 -0700100 ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
Ana Krulec241cf832018-08-10 15:03:23 -0700101 }
102 }
103
104 if (callback != nullptr) {
105 callback->onVSyncEvent(when);
106 }
107}
108
109} // namespace android