Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | #include <utils/Mutex.h> |
| 20 | #include <utils/Trace.h> |
| 21 | |
| 22 | #include "DispSync.h" |
| 23 | #include "EventThread.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync, |
| 28 | const char* name) |
| 29 | : mName(name), |
| 30 | mValue(0), |
| 31 | mTraceVsync(traceVsync), |
| 32 | mVsyncOnLabel(String8::format("VsyncOn-%s", name)), |
| 33 | mVsyncEventLabel(String8::format("VSYNC-%s", name)), |
| 34 | mDispSync(dispSync), |
| 35 | mCallbackMutex(), |
| 36 | mVsyncMutex(), |
| 37 | mPhaseOffset(phaseOffset), |
| 38 | mEnabled(false) {} |
| 39 | |
| 40 | void DispSyncSource::setVSyncEnabled(bool enable) { |
| 41 | Mutex::Autolock lock(mVsyncMutex); |
| 42 | if (enable) { |
| 43 | status_t err = mDispSync->addEventListener(mName, mPhaseOffset, |
| 44 | static_cast<DispSync::Callback*>(this)); |
| 45 | if (err != NO_ERROR) { |
| 46 | ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err); |
| 47 | } |
| 48 | // ATRACE_INT(mVsyncOnLabel.string(), 1); |
| 49 | } else { |
| 50 | status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this)); |
| 51 | if (err != NO_ERROR) { |
| 52 | ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err); |
| 53 | } |
| 54 | // ATRACE_INT(mVsyncOnLabel.string(), 0); |
| 55 | } |
| 56 | mEnabled = enable; |
| 57 | } |
| 58 | |
| 59 | void DispSyncSource::setCallback(VSyncSource::Callback* callback) { |
| 60 | Mutex::Autolock lock(mCallbackMutex); |
| 61 | mCallback = callback; |
| 62 | } |
| 63 | |
| 64 | void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) { |
| 65 | Mutex::Autolock lock(mVsyncMutex); |
| 66 | |
| 67 | // Normalize phaseOffset to [0, period) |
| 68 | auto period = mDispSync->getPeriod(); |
| 69 | phaseOffset %= period; |
| 70 | if (phaseOffset < 0) { |
| 71 | // If we're here, then phaseOffset is in (-period, 0). After this |
| 72 | // operation, it will be in (0, period) |
| 73 | phaseOffset += period; |
| 74 | } |
| 75 | mPhaseOffset = phaseOffset; |
| 76 | |
| 77 | // If we're not enabled, we don't need to mess with the listeners |
| 78 | if (!mEnabled) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | status_t err = |
| 83 | mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset); |
| 84 | if (err != NO_ERROR) { |
| 85 | ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void DispSyncSource::onDispSyncEvent(nsecs_t when) { |
| 90 | VSyncSource::Callback* callback; |
| 91 | { |
| 92 | Mutex::Autolock lock(mCallbackMutex); |
| 93 | callback = mCallback; |
| 94 | |
| 95 | if (mTraceVsync) { |
| 96 | mValue = (mValue + 1) % 2; |
| 97 | ATRACE_INT(mVsyncEventLabel.string(), mValue); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (callback != nullptr) { |
| 102 | callback->onVSyncEvent(when); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace android |