blob: 61f2ac8b0165da512110ed75c21bf4ee99d634d0 [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
17#include "DispSyncSource.h"
18
Ana Krulec072233f2018-08-10 15:03:23 -070019#include <android-base/stringprintf.h>
Ana Krulec241cf832018-08-10 15:03:23 -070020#include <utils/Trace.h>
Ana Krulec072233f2018-08-10 15:03:23 -070021#include <mutex>
Ana Krulec241cf832018-08-10 15:03:23 -070022
23#include "DispSync.h"
24#include "EventThread.h"
25
26namespace android {
27
28DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
29 const char* name)
30 : mName(name),
Ana Krulec241cf832018-08-10 15:03:23 -070031 mTraceVsync(traceVsync),
Ana Krulec072233f2018-08-10 15:03:23 -070032 mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
33 mVsyncEventLabel(base::StringPrintf("VSYNC-%s", name)),
Ana Krulec241cf832018-08-10 15:03:23 -070034 mDispSync(dispSync),
Ana Krulec072233f2018-08-10 15:03:23 -070035 mPhaseOffset(phaseOffset) {}
Ana Krulec241cf832018-08-10 15:03:23 -070036
37void DispSyncSource::setVSyncEnabled(bool enable) {
Ana Krulec072233f2018-08-10 15:03:23 -070038 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070039 if (enable) {
40 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
41 static_cast<DispSync::Callback*>(this));
42 if (err != NO_ERROR) {
43 ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
44 }
Ana Krulec072233f2018-08-10 15:03:23 -070045 // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
Ana Krulec241cf832018-08-10 15:03:23 -070046 } else {
47 status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this));
48 if (err != NO_ERROR) {
49 ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
50 }
Ana Krulec072233f2018-08-10 15:03:23 -070051 // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
Ana Krulec241cf832018-08-10 15:03:23 -070052 }
53 mEnabled = enable;
54}
55
56void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
Ana Krulec072233f2018-08-10 15:03:23 -070057 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070058 mCallback = callback;
59}
60
61void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
Ana Krulec072233f2018-08-10 15:03:23 -070062 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070063
64 // Normalize phaseOffset to [0, period)
65 auto period = mDispSync->getPeriod();
66 phaseOffset %= period;
67 if (phaseOffset < 0) {
68 // If we're here, then phaseOffset is in (-period, 0). After this
69 // operation, it will be in (0, period)
70 phaseOffset += period;
71 }
72 mPhaseOffset = phaseOffset;
73
74 // If we're not enabled, we don't need to mess with the listeners
75 if (!mEnabled) {
76 return;
77 }
78
79 status_t err =
80 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
81 if (err != NO_ERROR) {
82 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
83 }
84}
85
86void DispSyncSource::onDispSyncEvent(nsecs_t when) {
87 VSyncSource::Callback* callback;
88 {
Ana Krulec072233f2018-08-10 15:03:23 -070089 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070090 callback = mCallback;
91
92 if (mTraceVsync) {
93 mValue = (mValue + 1) % 2;
Ana Krulec072233f2018-08-10 15:03:23 -070094 ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
Ana Krulec241cf832018-08-10 15:03:23 -070095 }
96 }
97
98 if (callback != nullptr) {
99 callback->onVSyncEvent(when);
100 }
101}
102
103} // namespace android