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