blob: 8752b6600e08db07e70f9dd40542dd0f5a180a77 [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 {
Ady Abraham5e7371c2020-03-24 14:47:24 -070033using base::StringAppendF;
Ana Krulec241cf832018-08-10 15:03:23 -070034
Ady Abraham9e16a482019-12-03 17:19:41 -080035DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Ana Krulec241cf832018-08-10 15:03:23 -070036 const char* name)
37 : mName(name),
Ady Abraham50204dd2019-07-19 15:47:11 -070038 mValue(base::StringPrintf("VSYNC-%s", name), 0),
Ana Krulec241cf832018-08-10 15:03:23 -070039 mTraceVsync(traceVsync),
Ana Krulec072233f2018-08-10 15:03:23 -070040 mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
Ana Krulec241cf832018-08-10 15:03:23 -070041 mDispSync(dispSync),
Ady Abraham9e16a482019-12-03 17:19:41 -080042 mPhaseOffset(base::StringPrintf("VsyncOffset-%s", name), phaseOffset) {}
Ana Krulec241cf832018-08-10 15:03:23 -070043
44void DispSyncSource::setVSyncEnabled(bool enable) {
Ana Krulec072233f2018-08-10 15:03:23 -070045 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070046 if (enable) {
47 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();
Ady Abraham45e4e362019-06-07 18:20:51 -070073
74 // Normalize phaseOffset to [-period, period)
75 const int numPeriods = phaseOffset / period;
76 phaseOffset -= numPeriods * period;
Ady Abraham11b6a702019-06-27 11:24:19 -070077 if (mPhaseOffset == phaseOffset) {
78 return;
79 }
80
Ana Krulec241cf832018-08-10 15:03:23 -070081 mPhaseOffset = phaseOffset;
82
83 // If we're not enabled, we don't need to mess with the listeners
84 if (!mEnabled) {
85 return;
86 }
87
88 status_t err =
89 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
90 if (err != NO_ERROR) {
91 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
92 }
93}
94
Ady Abraham5facfb12020-04-22 15:18:31 -070095void DispSyncSource::onDispSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp) {
Ana Krulec241cf832018-08-10 15:03:23 -070096 VSyncSource::Callback* callback;
97 {
Ana Krulec072233f2018-08-10 15:03:23 -070098 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070099 callback = mCallback;
Alec Mourid7599d82019-05-22 19:58:00 -0700100 }
Ana Krulec241cf832018-08-10 15:03:23 -0700101
Alec Mourid7599d82019-05-22 19:58:00 -0700102 if (mTraceVsync) {
103 mValue = (mValue + 1) % 2;
Ana Krulec241cf832018-08-10 15:03:23 -0700104 }
105
106 if (callback != nullptr) {
Ady Abraham5facfb12020-04-22 15:18:31 -0700107 callback->onVSyncEvent(when, expectedVSyncTimestamp);
Ana Krulec241cf832018-08-10 15:03:23 -0700108 }
109}
110
Ady Abraham5e7371c2020-03-24 14:47:24 -0700111void DispSyncSource::dump(std::string& result) const {
112 std::lock_guard lock(mVsyncMutex);
113 StringAppendF(&result, "DispSyncSource: %s(%s)\n", mName, mEnabled ? "enabled" : "disabled");
114 mDispSync->dump(result);
115}
116
Alec Mourid7599d82019-05-22 19:58:00 -0700117} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800118
119// TODO(b/129481165): remove the #pragma below and fix conversion issues
120#pragma clang diagnostic pop // ignored "-Wconversion"