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 | |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 30 | DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, |
| 31 | nsecs_t offsetThresholdForNextVsync, bool traceVsync, |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 32 | const char* name) |
| 33 | : mName(name), |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 34 | mValue(base::StringPrintf("VSYNC-%s", name), 0), |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 35 | mTraceVsync(traceVsync), |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 36 | mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)), |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 37 | mDispSync(dispSync), |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 38 | mPhaseOffset(base::StringPrintf("VsyncOffset-%s", name), phaseOffset), |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 39 | mOffsetThresholdForNextVsync(offsetThresholdForNextVsync) {} |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 40 | |
| 41 | void DispSyncSource::setVSyncEnabled(bool enable) { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 42 | std::lock_guard lock(mVsyncMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 43 | if (enable) { |
| 44 | status_t err = mDispSync->addEventListener(mName, mPhaseOffset, |
Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 45 | static_cast<DispSync::Callback*>(this), |
| 46 | mLastCallbackTime); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 47 | if (err != NO_ERROR) { |
| 48 | ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err); |
| 49 | } |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 50 | // ATRACE_INT(mVsyncOnLabel.c_str(), 1); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 51 | } else { |
Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 52 | status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this), |
| 53 | &mLastCallbackTime); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 54 | if (err != NO_ERROR) { |
| 55 | ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err); |
| 56 | } |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 57 | // ATRACE_INT(mVsyncOnLabel.c_str(), 0); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 58 | } |
| 59 | mEnabled = enable; |
| 60 | } |
| 61 | |
| 62 | void DispSyncSource::setCallback(VSyncSource::Callback* callback) { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 63 | std::lock_guard lock(mCallbackMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 64 | mCallback = callback; |
| 65 | } |
| 66 | |
| 67 | void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 68 | std::lock_guard lock(mVsyncMutex); |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 69 | const nsecs_t period = mDispSync->getPeriod(); |
| 70 | // Check if offset should be handled as negative |
| 71 | if (phaseOffset >= mOffsetThresholdForNextVsync) { |
| 72 | phaseOffset -= period; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 73 | } |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 74 | |
| 75 | // Normalize phaseOffset to [-period, period) |
| 76 | const int numPeriods = phaseOffset / period; |
| 77 | phaseOffset -= numPeriods * period; |
Ady Abraham | 11b6a70 | 2019-06-27 11:24:19 -0700 | [diff] [blame] | 78 | if (mPhaseOffset == phaseOffset) { |
| 79 | return; |
| 80 | } |
| 81 | |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 82 | mPhaseOffset = phaseOffset; |
| 83 | |
| 84 | // If we're not enabled, we don't need to mess with the listeners |
| 85 | if (!mEnabled) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | status_t err = |
| 90 | mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset); |
| 91 | if (err != NO_ERROR) { |
| 92 | ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void DispSyncSource::onDispSyncEvent(nsecs_t when) { |
| 97 | VSyncSource::Callback* callback; |
| 98 | { |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 99 | std::lock_guard lock(mCallbackMutex); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 100 | callback = mCallback; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 101 | } |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 102 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 103 | if (mTraceVsync) { |
| 104 | mValue = (mValue + 1) % 2; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (callback != nullptr) { |
| 108 | callback->onVSyncEvent(when); |
| 109 | } |
| 110 | } |
| 111 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 112 | } // namespace android |