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 |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 72 | void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) { |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 73 | if (mEventTube.getFd() >= 0) { |
| 74 | mLooper->removeFd(mEventTube.getFd()); |
| 75 | } |
| 76 | |
| 77 | mEvents = connection; |
Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame^] | 78 | if (mEvents) { |
| 79 | mEvents->stealReceiveChannel(&mEventTube); |
| 80 | mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver, |
| 81 | this); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) { |
| 86 | ATRACE_CALL(); |
| 87 | // Trace VSYNC-sf |
| 88 | mVsync.value = (mVsync.value + 1) % 2; |
| 89 | |
| 90 | { |
| 91 | std::lock_guard lock(mVsync.mutex); |
| 92 | mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime); |
| 93 | } |
| 94 | mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions( |
| 95 | {targetWakeupTime, readyTime, vsyncTime}), |
| 96 | vsyncTime); |
| 97 | } |
| 98 | |
| 99 | void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch, |
| 100 | frametimeline::TokenManager& tokenManager, |
| 101 | std::chrono::nanoseconds workDuration) { |
| 102 | setDuration(workDuration); |
| 103 | mVsync.tokenManager = &tokenManager; |
| 104 | mVsync.registration = std::make_unique< |
| 105 | scheduler::VSyncCallbackRegistration>(dispatch, |
| 106 | std::bind(&MessageQueue::vsyncCallback, this, |
| 107 | std::placeholders::_1, |
| 108 | std::placeholders::_2, |
| 109 | std::placeholders::_3), |
| 110 | "sf"); |
| 111 | } |
| 112 | |
| 113 | void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) { |
| 114 | ATRACE_CALL(); |
| 115 | std::lock_guard lock(mVsync.mutex); |
| 116 | mVsync.workDuration = workDuration; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 119 | void MessageQueue::waitMessage() { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 120 | do { |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 121 | IPCThreadState::self()->flushCommands(); |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 122 | int32_t ret = mLooper->pollOnce(-1); |
| 123 | switch (ret) { |
Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 124 | case Looper::POLL_WAKE: |
| 125 | case Looper::POLL_CALLBACK: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 126 | continue; |
Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 127 | case Looper::POLL_ERROR: |
| 128 | ALOGE("Looper::POLL_ERROR"); |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 129 | continue; |
Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 130 | case Looper::POLL_TIMEOUT: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 131 | // timeout (should not happen) |
| 132 | continue; |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 133 | default: |
| 134 | // should not happen |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 135 | ALOGE("Looper::pollOnce() returned unknown status %d", ret); |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 136 | continue; |
| 137 | } |
| 138 | } while (true); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 141 | void MessageQueue::postMessage(sp<MessageHandler>&& handler) { |
| 142 | mLooper->sendMessage(handler, Message()); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 145 | void MessageQueue::invalidate() { |
Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame^] | 146 | ATRACE_CALL(); |
| 147 | if (mEvents) { |
| 148 | mEvents->requestNextVsync(); |
| 149 | } else { |
| 150 | const auto [workDuration, lastVsyncCallback] = [&] { |
| 151 | std::lock_guard lock(mVsync.mutex); |
| 152 | std::chrono::nanoseconds mWorkDurationNanos = mVsync.workDuration; |
| 153 | return std::make_pair(mWorkDurationNanos.count(), mVsync.lastCallbackTime.count()); |
| 154 | }(); |
| 155 | |
| 156 | mVsync.registration->schedule({workDuration, /*readyDuration=*/0, lastVsyncCallback}); |
| 157 | } |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 160 | void MessageQueue::refresh() { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 161 | mHandler->dispatchRefresh(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 164 | int MessageQueue::cb_eventReceiver(int fd, int events, void* data) { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 165 | MessageQueue* queue = reinterpret_cast<MessageQueue*>(data); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 166 | return queue->eventReceiver(fd, events); |
| 167 | } |
| 168 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 169 | int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) { |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 170 | ssize_t n; |
| 171 | DisplayEventReceiver::Event buffer[8]; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 172 | while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 173 | for (int i = 0; i < n; i++) { |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 174 | if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) { |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 175 | mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId, |
| 176 | buffer[i].vsync.expectedVSyncTimestamp); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return 1; |
| 182 | } |
| 183 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 184 | } // namespace android::impl |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 185 | |