blob: 697d6344fa58a8d386758101ec2d44d0671f6f0a [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
88void DispSyncSource::onDispSyncEvent(nsecs_t when) {
89 VSyncSource::Callback* callback;
90 {
Ana Krulec072233f2018-08-10 15:03:23 -070091 std::lock_guard lock(mCallbackMutex);
Ana Krulec241cf832018-08-10 15:03:23 -070092 callback = mCallback;
93
94 if (mTraceVsync) {
95 mValue = (mValue + 1) % 2;
Ana Krulec072233f2018-08-10 15:03:23 -070096 ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
Ana Krulec241cf832018-08-10 15:03:23 -070097 }
98 }
99
100 if (callback != nullptr) {
101 callback->onVSyncEvent(when);
102 }
103}
104
105} // namespace android