blob: 00948aedb44994134d070567480053da86b41dfb [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
30DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
31 const char* name)
32 : mName(name),
Ana Krulec241cf832018-08-10 15:03:23 -070033 mTraceVsync(traceVsync),
Ana Krulec072233f2018-08-10 15:03:23 -070034 mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
35 mVsyncEventLabel(base::StringPrintf("VSYNC-%s", name)),
Ana Krulec241cf832018-08-10 15:03:23 -070036 mDispSync(dispSync),
Ana Krulec072233f2018-08-10 15:03:23 -070037 mPhaseOffset(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);
Ana Krulec241cf832018-08-10 15:03:23 -070067
68 // Normalize phaseOffset to [0, period)
69 auto period = mDispSync->getPeriod();
70 phaseOffset %= period;
71 if (phaseOffset < 0) {
72 // If we're here, then phaseOffset is in (-period, 0). After this
73 // operation, it will be in (0, period)
74 phaseOffset += period;
75 }
76 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;
95
96 if (mTraceVsync) {
97 mValue = (mValue + 1) % 2;
Ana Krulec072233f2018-08-10 15:03:23 -070098 ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
Ana Krulec241cf832018-08-10 15:03:23 -070099 }
100 }
101
102 if (callback != nullptr) {
103 callback->onVSyncEvent(when);
104 }
105}
106
107} // namespace android