blob: d848c976ddff19a87fa5a883ce0440ab220ac2fb [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,
43 static_cast<DispSync::Callback*>(this));
44 if (err != NO_ERROR) {
45 ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
46 }
Ana Krulec072233f2018-08-10 15:03:23 -070047 // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
Ana Krulec241cf832018-08-10 15:03:23 -070048 } else {
49 status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this));
50 if (err != NO_ERROR) {
51 ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
52 }
Ana Krulec072233f2018-08-10 15:03:23 -070053 // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
Ana Krulec241cf832018-08-10 15:03:23 -070054 }
55 mEnabled = enable;
56}
57
58void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
Ana Krulec072233f2018-08-10 15:03:23 -070059 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070060 mCallback = callback;
61}
62
63void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
Ana Krulec072233f2018-08-10 15:03:23 -070064 std::lock_guard lock(mVsyncMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070065
66 // Normalize phaseOffset to [0, period)
67 auto period = mDispSync->getPeriod();
68 phaseOffset %= period;
69 if (phaseOffset < 0) {
70 // If we're here, then phaseOffset is in (-period, 0). After this
71 // operation, it will be in (0, period)
72 phaseOffset += period;
73 }
74 mPhaseOffset = phaseOffset;
75
76 // If we're not enabled, we don't need to mess with the listeners
77 if (!mEnabled) {
78 return;
79 }
80
81 status_t err =
82 mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset);
83 if (err != NO_ERROR) {
84 ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err);
85 }
86}
87
Ady Abrahamb838aed2019-02-12 15:30:16 -080088void DispSyncSource::pauseVsyncCallback(bool pause) {
89 std::lock_guard lock(mVsyncMutex);
90 mCallbackPaused = pause;
91}
92
Ana Krulec241cf832018-08-10 15:03:23 -070093void DispSyncSource::onDispSyncEvent(nsecs_t when) {
Ady Abrahamb838aed2019-02-12 15:30:16 -080094 {
95 std::lock_guard lock(mVsyncMutex);
96 if (mCallbackPaused) {
97 return;
98 }
99 }
100
Ana Krulec241cf832018-08-10 15:03:23 -0700101 VSyncSource::Callback* callback;
102 {
Ana Krulec072233f2018-08-10 15:03:23 -0700103 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -0700104 callback = mCallback;
105
106 if (mTraceVsync) {
107 mValue = (mValue + 1) % 2;
Ana Krulec072233f2018-08-10 15:03:23 -0700108 ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
Ana Krulec241cf832018-08-10 15:03:23 -0700109 }
110 }
111
112 if (callback != nullptr) {
113 callback->onVSyncEvent(when);
114 }
115}
116
117} // namespace android