blob: 776e98463fb25a0642c4649960232d45c56614fa [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Midas Chienbc5f22f2018-08-16 15:51:19 +080021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Ana Krulec241cf832018-08-10 15:03:23 -070023#include "DispSyncSource.h"
24
Ana Krulec072233f2018-08-10 15:03:23 -070025#include <android-base/stringprintf.h>
Ana Krulec241cf832018-08-10 15:03:23 -070026#include <utils/Trace.h>
Ana Krulec072233f2018-08-10 15:03:23 -070027#include <mutex>
Ana Krulec241cf832018-08-10 15:03:23 -070028
29#include "DispSync.h"
30#include "EventThread.h"
31
32namespace android {
33
Ady Abraham9e16a482019-12-03 17:19:41 -080034DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Ana Krulec241cf832018-08-10 15:03:23 -070035 const char* name)
36 : mName(name),
Ady Abraham50204dd2019-07-19 15:47:11 -070037 mValue(base::StringPrintf("VSYNC-%s", name), 0),
Ana Krulec241cf832018-08-10 15:03:23 -070038 mTraceVsync(traceVsync),
Ana Krulec072233f2018-08-10 15:03:23 -070039 mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
Ana Krulec241cf832018-08-10 15:03:23 -070040 mDispSync(dispSync),
Ady Abraham9e16a482019-12-03 17:19:41 -080041 mPhaseOffset(base::StringPrintf("VsyncOffset-%s", name), phaseOffset) {}
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) {
46 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Alec Mouri7355eb22019-03-05 14:19:10 -080047 static_cast<DispSync::Callback*>(this),
48 mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070049 if (err != NO_ERROR) {
50 ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
51 }
Ana Krulec072233f2018-08-10 15:03:23 -070052 // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
Ana Krulec241cf832018-08-10 15:03:23 -070053 } else {
Alec Mouri7355eb22019-03-05 14:19:10 -080054 status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this),
55 &mLastCallbackTime);
Ana Krulec241cf832018-08-10 15:03:23 -070056 if (err != NO_ERROR) {
57 ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
58 }
Ana Krulec072233f2018-08-10 15:03:23 -070059 // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
Ana Krulec241cf832018-08-10 15:03:23 -070060 }
61 mEnabled = enable;
62}
63
64void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
Ana Krulec072233f2018-08-10 15:03:23 -070065 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070066 mCallback = callback;
67}
68
69void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
Ana Krulec072233f2018-08-10 15:03:23 -070070 std::lock_guard lock(mVsyncMutex);
Ady Abraham45e4e362019-06-07 18:20:51 -070071 const nsecs_t period = mDispSync->getPeriod();
Ady Abraham45e4e362019-06-07 18:20:51 -070072
73 // Normalize phaseOffset to [-period, period)
74 const int numPeriods = phaseOffset / period;
75 phaseOffset -= numPeriods * period;
Ady Abraham11b6a702019-06-27 11:24:19 -070076 if (mPhaseOffset == phaseOffset) {
77 return;
78 }
79
Ana Krulec241cf832018-08-10 15:03:23 -070080 mPhaseOffset = phaseOffset;
81
82 // If we're not enabled, we don't need to mess with the listeners
83 if (!mEnabled) {
84 return;
85 }
86
87 status_t err =
88 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
89 if (err != NO_ERROR) {
90 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
91 }
92}
93
94void DispSyncSource::onDispSyncEvent(nsecs_t when) {
95 VSyncSource::Callback* callback;
96 {
Ana Krulec072233f2018-08-10 15:03:23 -070097 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070098 callback = mCallback;
Alec Mourid7599d82019-05-22 19:58:00 -070099 }
Ana Krulec241cf832018-08-10 15:03:23 -0700100
Alec Mourid7599d82019-05-22 19:58:00 -0700101 if (mTraceVsync) {
102 mValue = (mValue + 1) % 2;
Ana Krulec241cf832018-08-10 15:03:23 -0700103 }
104
105 if (callback != nullptr) {
106 callback->onVSyncEvent(when);
107 }
108}
109
Alec Mourid7599d82019-05-22 19:58:00 -0700110} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800111
112// TODO(b/129481165): remove the #pragma below and fix conversion issues
113#pragma clang diagnostic pop // ignored "-Wconversion"