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 | |
Midas Chien | bc5f22f | 2018-08-16 15:51:19 +0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 19 | #include "DispSyncSource.h" |
| 20 | |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 22 | #include <utils/Trace.h> |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 23 | #include <mutex> |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 24 | |
| 25 | #include "DispSync.h" |
| 26 | #include "EventThread.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync, |
| 31 | const char* name) |
| 32 | : mName(name), |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 33 | mTraceVsync(traceVsync), |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 34 | mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)), |
| 35 | mVsyncEventLabel(base::StringPrintf("VSYNC-%s", name)), |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 36 | mDispSync(dispSync), |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 37 | mPhaseOffset(phaseOffset) {} |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 38 | |
| 39 | void DispSyncSource::setVSyncEnabled(bool enable) { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 40 | std::lock_guard lock(mVsyncMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 41 | 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 Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 47 | // ATRACE_INT(mVsyncOnLabel.c_str(), 1); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 48 | } 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 Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 53 | // ATRACE_INT(mVsyncOnLabel.c_str(), 0); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 54 | } |
| 55 | mEnabled = enable; |
| 56 | } |
| 57 | |
| 58 | void DispSyncSource::setCallback(VSyncSource::Callback* callback) { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 59 | std::lock_guard lock(mCallbackMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 60 | mCallback = callback; |
| 61 | } |
| 62 | |
| 63 | void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 64 | std::lock_guard lock(mVsyncMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 65 | |
| 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 | |
| 88 | void DispSyncSource::onDispSyncEvent(nsecs_t when) { |
| 89 | VSyncSource::Callback* callback; |
| 90 | { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 91 | std::lock_guard lock(mCallbackMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 92 | callback = mCallback; |
| 93 | |
| 94 | if (mTraceVsync) { |
| 95 | mValue = (mValue + 1) % 2; |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 96 | ATRACE_INT(mVsyncEventLabel.c_str(), mValue); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | if (callback != nullptr) { |
| 101 | callback->onVSyncEvent(when); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } // namespace android |