| 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 |  | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
| Midas Chien | bc5f22f | 2018-08-16 15:51:19 +0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 22 |  | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 23 | #include "DispSyncSource.h" | 
|  | 24 |  | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 26 | #include <utils/Trace.h> | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 27 | #include <mutex> | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | #include "DispSync.h" | 
|  | 30 | #include "EventThread.h" | 
|  | 31 |  | 
|  | 32 | namespace android { | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 33 | using base::StringAppendF; | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 34 |  | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 35 | DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync, | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 36 | const char* name) | 
|  | 37 | : mName(name), | 
| Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 38 | mValue(base::StringPrintf("VSYNC-%s", name), 0), | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 39 | mTraceVsync(traceVsync), | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 40 | mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)), | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 41 | mDispSync(dispSync), | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 42 | mPhaseOffset(base::StringPrintf("VsyncOffset-%s", name), phaseOffset) {} | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | void DispSyncSource::setVSyncEnabled(bool enable) { | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 45 | std::lock_guard lock(mVsyncMutex); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 46 | if (enable) { | 
|  | 47 | status_t err = mDispSync->addEventListener(mName, mPhaseOffset, | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 48 | static_cast<DispSync::Callback*>(this), | 
|  | 49 | mLastCallbackTime); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 50 | if (err != NO_ERROR) { | 
|  | 51 | ALOGE("error registering 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(), 1); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 54 | } else { | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 55 | status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this), | 
|  | 56 | &mLastCallbackTime); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 57 | if (err != NO_ERROR) { | 
|  | 58 | ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err); | 
|  | 59 | } | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 60 | // ATRACE_INT(mVsyncOnLabel.c_str(), 0); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 61 | } | 
|  | 62 | mEnabled = enable; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | void DispSyncSource::setCallback(VSyncSource::Callback* callback) { | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 66 | std::lock_guard lock(mCallbackMutex); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 67 | mCallback = callback; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) { | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 71 | std::lock_guard lock(mVsyncMutex); | 
| Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 72 | const nsecs_t period = mDispSync->getPeriod(); | 
| Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 73 |  | 
|  | 74 | // Normalize phaseOffset to [-period, period) | 
|  | 75 | const int numPeriods = phaseOffset / period; | 
|  | 76 | phaseOffset -= numPeriods * period; | 
| Ady Abraham | 11b6a70 | 2019-06-27 11:24:19 -0700 | [diff] [blame] | 77 | if (mPhaseOffset == phaseOffset) { | 
|  | 78 | return; | 
|  | 79 | } | 
|  | 80 |  | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 81 | mPhaseOffset = phaseOffset; | 
|  | 82 |  | 
|  | 83 | // If we're not enabled, we don't need to mess with the listeners | 
|  | 84 | if (!mEnabled) { | 
|  | 85 | return; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | status_t err = | 
|  | 89 | mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), mPhaseOffset); | 
|  | 90 | if (err != NO_ERROR) { | 
|  | 91 | ALOGE("error changing vsync offset: %s (%d)", strerror(-err), err); | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 |  | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 95 | void DispSyncSource::onDispSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp) { | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 96 | VSyncSource::Callback* callback; | 
|  | 97 | { | 
| Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 98 | std::lock_guard lock(mCallbackMutex); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 99 | callback = mCallback; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 100 | } | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 101 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 102 | if (mTraceVsync) { | 
|  | 103 | mValue = (mValue + 1) % 2; | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
|  | 106 | if (callback != nullptr) { | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 107 | callback->onVSyncEvent(when, expectedVSyncTimestamp); | 
| Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 108 | } | 
|  | 109 | } | 
|  | 110 |  | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 111 | void DispSyncSource::dump(std::string& result) const { | 
|  | 112 | std::lock_guard lock(mVsyncMutex); | 
|  | 113 | StringAppendF(&result, "DispSyncSource: %s(%s)\n", mName, mEnabled ? "enabled" : "disabled"); | 
|  | 114 | mDispSync->dump(result); | 
|  | 115 | } | 
|  | 116 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 117 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 118 |  | 
|  | 119 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 120 | #pragma clang diagnostic pop // ignored "-Wconversion" |