| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2009 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 | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 18 |  | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 19 | #include <binder/IPCThreadState.h> | 
|  | 20 |  | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 22 | #include <utils/Timers.h> | 
|  | 23 | #include <utils/threads.h> | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 24 |  | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 25 | #include <gui/DisplayEventReceiver.h> | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 26 |  | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 27 | #include "EventThread.h" | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 28 | #include "FrameTimeline.h" | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 29 | #include "MessageQueue.h" | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 30 | #include "SurfaceFlinger.h" | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 31 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 32 | namespace android::impl { | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 33 |  | 
| Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 34 | void MessageQueue::Handler::dispatchRefresh() { | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 35 | if ((mEventMask.fetch_or(eventMaskRefresh) & eventMaskRefresh) == 0) { | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 36 | mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH)); | 
|  | 37 | } | 
|  | 38 | } | 
|  | 39 |  | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 40 | void MessageQueue::Handler::dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp) { | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 41 | if ((mEventMask.fetch_or(eventMaskInvalidate) & eventMaskInvalidate) == 0) { | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 42 | mVsyncId = vsyncId; | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 43 | mExpectedVSyncTime = expectedVSyncTimestamp; | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 44 | mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE)); | 
|  | 45 | } | 
|  | 46 | } | 
|  | 47 |  | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 48 | bool MessageQueue::Handler::invalidatePending() { | 
|  | 49 | constexpr auto pendingMask = eventMaskInvalidate | eventMaskRefresh; | 
|  | 50 | return (mEventMask.load() & pendingMask) != 0; | 
|  | 51 | } | 
|  | 52 |  | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 53 | void MessageQueue::Handler::handleMessage(const Message& message) { | 
|  | 54 | switch (message.what) { | 
|  | 55 | case INVALIDATE: | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 56 | mEventMask.fetch_and(~eventMaskInvalidate); | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 57 | mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime); | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 58 | break; | 
|  | 59 | case REFRESH: | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 60 | mEventMask.fetch_and(~eventMaskRefresh); | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 61 | mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime); | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 62 | break; | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | // --------------------------------------------------------------------------- | 
|  | 67 |  | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 68 | void MessageQueue::init(const sp<SurfaceFlinger>& flinger) { | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 69 | mFlinger = flinger; | 
|  | 70 | mLooper = new Looper(true); | 
|  | 71 | mHandler = new Handler(*this); | 
|  | 72 | } | 
|  | 73 |  | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 74 | // TODO(b/169865816): refactor VSyncInjections to use MessageQueue directly | 
|  | 75 | // and remove the EventThread from MessageQueue | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 76 | void MessageQueue::setInjector(sp<EventThreadConnection> connection) { | 
|  | 77 | auto& tube = mInjector.tube; | 
|  | 78 |  | 
|  | 79 | if (const int fd = tube.getFd(); fd >= 0) { | 
|  | 80 | mLooper->removeFd(fd); | 
| Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 83 | if (connection) { | 
|  | 84 | // The EventThreadConnection is retained when disabling injection, so avoid subsequently | 
|  | 85 | // stealing invalid FDs. Note that the stolen FDs are kept open. | 
|  | 86 | if (tube.getFd() < 0) { | 
|  | 87 | connection->stealReceiveChannel(&tube); | 
|  | 88 | } else { | 
|  | 89 | ALOGW("Recycling channel for VSYNC injection."); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | mLooper->addFd( | 
|  | 93 | tube.getFd(), 0, Looper::EVENT_INPUT, | 
|  | 94 | [](int, int, void* data) { | 
|  | 95 | reinterpret_cast<MessageQueue*>(data)->injectorCallback(); | 
|  | 96 | return 1; // Keep registration. | 
|  | 97 | }, | 
|  | 98 | this); | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 99 | } | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 100 |  | 
|  | 101 | std::lock_guard lock(mInjector.mutex); | 
|  | 102 | mInjector.connection = std::move(connection); | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) { | 
|  | 106 | ATRACE_CALL(); | 
|  | 107 | // Trace VSYNC-sf | 
|  | 108 | mVsync.value = (mVsync.value + 1) % 2; | 
|  | 109 |  | 
|  | 110 | { | 
|  | 111 | std::lock_guard lock(mVsync.mutex); | 
|  | 112 | mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime); | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 113 | mVsync.scheduled = false; | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 114 | } | 
|  | 115 | mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions( | 
|  | 116 | {targetWakeupTime, readyTime, vsyncTime}), | 
|  | 117 | vsyncTime); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch, | 
|  | 121 | frametimeline::TokenManager& tokenManager, | 
|  | 122 | std::chrono::nanoseconds workDuration) { | 
|  | 123 | setDuration(workDuration); | 
|  | 124 | mVsync.tokenManager = &tokenManager; | 
|  | 125 | mVsync.registration = std::make_unique< | 
|  | 126 | scheduler::VSyncCallbackRegistration>(dispatch, | 
|  | 127 | std::bind(&MessageQueue::vsyncCallback, this, | 
|  | 128 | std::placeholders::_1, | 
|  | 129 | std::placeholders::_2, | 
|  | 130 | std::placeholders::_3), | 
|  | 131 | "sf"); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) { | 
|  | 135 | ATRACE_CALL(); | 
|  | 136 | std::lock_guard lock(mVsync.mutex); | 
|  | 137 | mVsync.workDuration = workDuration; | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 138 | if (mVsync.scheduled) { | 
|  | 139 | mVsync.expectedWakeupTime = mVsync.registration->schedule( | 
|  | 140 | {mVsync.workDuration.get().count(), | 
|  | 141 | /*readyDuration=*/0, mVsync.lastCallbackTime.count()}); | 
| Ady Abraham | 326ecde | 2020-11-06 15:05:53 -0800 | [diff] [blame] | 142 | } | 
| Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 145 | void MessageQueue::waitMessage() { | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 146 | do { | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 147 | IPCThreadState::self()->flushCommands(); | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 148 | int32_t ret = mLooper->pollOnce(-1); | 
|  | 149 | switch (ret) { | 
| Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 150 | case Looper::POLL_WAKE: | 
|  | 151 | case Looper::POLL_CALLBACK: | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 152 | continue; | 
| Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 153 | case Looper::POLL_ERROR: | 
|  | 154 | ALOGE("Looper::POLL_ERROR"); | 
| Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 155 | continue; | 
| Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 156 | case Looper::POLL_TIMEOUT: | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 157 | // timeout (should not happen) | 
|  | 158 | continue; | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 159 | default: | 
|  | 160 | // should not happen | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 161 | ALOGE("Looper::pollOnce() returned unknown status %d", ret); | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 162 | continue; | 
|  | 163 | } | 
|  | 164 | } while (true); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 167 | void MessageQueue::postMessage(sp<MessageHandler>&& handler) { | 
|  | 168 | mLooper->sendMessage(handler, Message()); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 171 | void MessageQueue::invalidate() { | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 172 | ATRACE_CALL(); | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 173 |  | 
|  | 174 | { | 
|  | 175 | std::lock_guard lock(mInjector.mutex); | 
|  | 176 | if (CC_UNLIKELY(mInjector.connection)) { | 
|  | 177 | ALOGD("%s while injecting VSYNC", __FUNCTION__); | 
|  | 178 | mInjector.connection->requestNextVsync(); | 
|  | 179 | return; | 
|  | 180 | } | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 181 | } | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 182 |  | 
|  | 183 | std::lock_guard lock(mVsync.mutex); | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 184 | mVsync.scheduled = true; | 
|  | 185 | mVsync.expectedWakeupTime = | 
|  | 186 | mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(), | 
|  | 187 | .readyDuration = 0, | 
|  | 188 | .earliestVsync = mVsync.lastCallbackTime.count()}); | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 191 | void MessageQueue::refresh() { | 
| Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 192 | mHandler->dispatchRefresh(); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 195 | void MessageQueue::injectorCallback() { | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 196 | ssize_t n; | 
|  | 197 | DisplayEventReceiver::Event buffer[8]; | 
| Dominik Laskowski | 208235c | 2020-12-03 15:38:51 -0800 | [diff] [blame] | 198 | while ((n = DisplayEventReceiver::getEvents(&mInjector.tube, buffer, 8)) > 0) { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 199 | for (int i = 0; i < n; i++) { | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 200 | if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) { | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 201 | mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId, | 
|  | 202 | buffer[i].vsync.expectedVSyncTimestamp); | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 203 | break; | 
|  | 204 | } | 
|  | 205 | } | 
|  | 206 | } | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 209 | std::optional<std::chrono::steady_clock::time_point> MessageQueue::nextExpectedInvalidate() { | 
|  | 210 | if (mHandler->invalidatePending()) { | 
|  | 211 | return std::chrono::steady_clock::now(); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | std::lock_guard lock(mVsync.mutex); | 
|  | 215 | if (mVsync.scheduled) { | 
|  | 216 | LOG_ALWAYS_FATAL_IF(!mVsync.expectedWakeupTime.has_value(), "callback was never scheduled"); | 
|  | 217 | const auto expectedWakeupTime = std::chrono::nanoseconds(*mVsync.expectedWakeupTime); | 
|  | 218 | return std::optional<std::chrono::steady_clock::time_point>(expectedWakeupTime); | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | return std::nullopt; | 
|  | 222 | } | 
|  | 223 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 224 | } // namespace android::impl |