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