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