Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #define LOG_TAG "DisplayEventDispatcher" |
| 18 | |
| 19 | #include <cinttypes> |
| 20 | #include <cstdint> |
| 21 | |
| 22 | #include <gui/DisplayEventDispatcher.h> |
| 23 | #include <gui/DisplayEventReceiver.h> |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/Looper.h> |
| 26 | |
| 27 | #include <utils/Timers.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // Number of events to read at a time from the DisplayEventDispatcher pipe. |
| 32 | // The value should be large enough that we can quickly drain the pipe |
| 33 | // using just a few large reads. |
| 34 | static const size_t EVENT_BUFFER_SIZE = 100; |
| 35 | |
Cheng Shi | fc690e2 | 2021-10-22 08:53:11 +0000 | [diff] [blame] | 36 | static constexpr nsecs_t WAITING_FOR_VSYNC_TIMEOUT = ms2ns(300); |
| 37 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame^] | 38 | DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper, |
| 39 | gui::ISurfaceComposer::VsyncSource vsyncSource, |
| 40 | EventRegistrationFlags eventRegistration) |
| 41 | : mLooper(looper), |
| 42 | mReceiver(vsyncSource, eventRegistration), |
| 43 | mWaitingForVsync(false), |
| 44 | mLastVsyncCount(0), |
| 45 | mLastScheduleVsyncTime(0) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 46 | ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this); |
| 47 | } |
| 48 | |
| 49 | status_t DisplayEventDispatcher::initialize() { |
| 50 | status_t result = mReceiver.initCheck(); |
| 51 | if (result) { |
| 52 | ALOGW("Failed to initialize display event receiver, status=%d", result); |
| 53 | return result; |
| 54 | } |
| 55 | |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 56 | if (mLooper != nullptr) { |
| 57 | int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT, this, NULL); |
| 58 | if (rc < 0) { |
| 59 | return UNKNOWN_ERROR; |
| 60 | } |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 61 | } |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 62 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 63 | return OK; |
| 64 | } |
| 65 | |
| 66 | void DisplayEventDispatcher::dispose() { |
| 67 | ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this); |
| 68 | |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 69 | if (!mReceiver.initCheck() && mLooper != nullptr) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 70 | mLooper->removeFd(mReceiver.getFd()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | status_t DisplayEventDispatcher::scheduleVsync() { |
Ady Abraham | 1ff99d8 | 2021-02-09 02:27:08 +0000 | [diff] [blame] | 75 | if (!mWaitingForVsync) { |
| 76 | ALOGV("dispatcher %p ~ Scheduling vsync.", this); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 77 | |
Ady Abraham | 1ff99d8 | 2021-02-09 02:27:08 +0000 | [diff] [blame] | 78 | // Drain all pending events. |
| 79 | nsecs_t vsyncTimestamp; |
| 80 | PhysicalDisplayId vsyncDisplayId; |
| 81 | uint32_t vsyncCount; |
| 82 | VsyncEventData vsyncEventData; |
| 83 | if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) { |
| 84 | ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this, |
| 85 | ns2ms(static_cast<nsecs_t>(vsyncTimestamp))); |
| 86 | } |
Ady Abraham | 9817875 | 2020-12-21 19:14:30 -0800 | [diff] [blame] | 87 | |
Ady Abraham | 1ff99d8 | 2021-02-09 02:27:08 +0000 | [diff] [blame] | 88 | status_t status = mReceiver.requestNextVsync(); |
| 89 | if (status) { |
| 90 | ALOGW("Failed to request next vsync, status=%d", status); |
| 91 | return status; |
| 92 | } |
Ady Abraham | 9817875 | 2020-12-21 19:14:30 -0800 | [diff] [blame] | 93 | |
Ady Abraham | 1ff99d8 | 2021-02-09 02:27:08 +0000 | [diff] [blame] | 94 | mWaitingForVsync = true; |
Cheng Shi | fc690e2 | 2021-10-22 08:53:11 +0000 | [diff] [blame] | 95 | mLastScheduleVsyncTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 96 | } |
| 97 | return OK; |
| 98 | } |
| 99 | |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 100 | void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) { |
| 101 | mReceiver.sendEvents(&event, 1); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Alec Mouri | 921b277 | 2020-02-05 19:03:28 -0800 | [diff] [blame] | 104 | int DisplayEventDispatcher::getFd() const { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 105 | return mReceiver.getFd(); |
| 106 | } |
| 107 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 108 | int DisplayEventDispatcher::handleEvent(int, int events, void*) { |
| 109 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 110 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
| 111 | "events=0x%x", |
| 112 | events); |
| 113 | return 0; // remove the callback |
| 114 | } |
| 115 | |
| 116 | if (!(events & Looper::EVENT_INPUT)) { |
| 117 | ALOGW("Received spurious callback for unhandled poll event. " |
| 118 | "events=0x%x", |
| 119 | events); |
| 120 | return 1; // keep the callback |
| 121 | } |
| 122 | |
| 123 | // Drain all pending events, keep the last vsync. |
| 124 | nsecs_t vsyncTimestamp; |
| 125 | PhysicalDisplayId vsyncDisplayId; |
| 126 | uint32_t vsyncCount; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 127 | VsyncEventData vsyncEventData; |
| 128 | if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) { |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 129 | ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64 |
| 130 | ", displayId=%s, count=%d, vsyncId=%" PRId64, |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 131 | this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 132 | vsyncEventData.preferredVsyncId()); |
Ady Abraham | 1ff99d8 | 2021-02-09 02:27:08 +0000 | [diff] [blame] | 133 | mWaitingForVsync = false; |
Cheng Shi | fc690e2 | 2021-10-22 08:53:11 +0000 | [diff] [blame] | 134 | mLastVsyncCount = vsyncCount; |
Ady Abraham | 1ff99d8 | 2021-02-09 02:27:08 +0000 | [diff] [blame] | 135 | dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Cheng Shi | fc690e2 | 2021-10-22 08:53:11 +0000 | [diff] [blame] | 138 | if (mWaitingForVsync) { |
| 139 | const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 140 | const nsecs_t vsyncScheduleDelay = currentTime - mLastScheduleVsyncTime; |
| 141 | if (vsyncScheduleDelay > WAITING_FOR_VSYNC_TIMEOUT) { |
| 142 | ALOGW("Vsync time out! vsyncScheduleDelay=%" PRId64 "ms", ns2ms(vsyncScheduleDelay)); |
| 143 | mWaitingForVsync = false; |
| 144 | dispatchVsync(currentTime, vsyncDisplayId /* displayId is not used */, |
| 145 | ++mLastVsyncCount, vsyncEventData /* empty data */); |
| 146 | } |
| 147 | } |
| 148 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 149 | return 1; // keep the callback |
| 150 | } |
| 151 | |
| 152 | bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp, |
| 153 | PhysicalDisplayId* outDisplayId, |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 154 | uint32_t* outCount, |
| 155 | VsyncEventData* outVsyncEventData) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 156 | bool gotVsync = false; |
| 157 | DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; |
| 158 | ssize_t n; |
| 159 | while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { |
| 160 | ALOGV("dispatcher %p ~ Read %d events.", this, int(n)); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 161 | mFrameRateOverrides.reserve(n); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 162 | for (ssize_t i = 0; i < n; i++) { |
| 163 | const DisplayEventReceiver::Event& ev = buf[i]; |
| 164 | switch (ev.header.type) { |
| 165 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 166 | // Later vsync events will just overwrite the info from earlier |
| 167 | // ones. That's fine, we only care about the most recent. |
| 168 | gotVsync = true; |
| 169 | *outTimestamp = ev.header.timestamp; |
| 170 | *outDisplayId = ev.header.displayId; |
| 171 | *outCount = ev.vsync.count; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 172 | *outVsyncEventData = ev.vsync.vsyncData; |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 173 | break; |
| 174 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
| 175 | dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected); |
| 176 | break; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 177 | case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE: |
| 178 | dispatchModeChanged(ev.header.timestamp, ev.header.displayId, |
| 179 | ev.modeChange.modeId, ev.modeChange.vsyncPeriod); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 180 | break; |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 181 | case DisplayEventReceiver::DISPLAY_EVENT_NULL: |
| 182 | dispatchNullEvent(ev.header.timestamp, ev.header.displayId); |
| 183 | break; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 184 | case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE: |
| 185 | mFrameRateOverrides.emplace_back(ev.frameRateOverride); |
| 186 | break; |
| 187 | case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH: |
| 188 | dispatchFrameRateOverrides(ev.header.timestamp, ev.header.displayId, |
| 189 | std::move(mFrameRateOverrides)); |
| 190 | break; |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 191 | default: |
| 192 | ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | if (n < 0) { |
| 198 | ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n)); |
| 199 | } |
| 200 | return gotVsync; |
| 201 | } |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 202 | |
Rachel Lee | 4cf8bab | 2022-03-14 14:38:45 -0700 | [diff] [blame] | 203 | status_t DisplayEventDispatcher::getLatestVsyncEventData( |
| 204 | ParcelableVsyncEventData* outVsyncEventData) const { |
| 205 | return mReceiver.getLatestVsyncEventData(outVsyncEventData); |
| 206 | } |
| 207 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 208 | } // namespace android |