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 | |
| 36 | DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper, |
| 37 | ISurfaceComposer::VsyncSource vsyncSource, |
| 38 | ISurfaceComposer::ConfigChanged configChanged) |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 39 | : mLooper(looper), mReceiver(vsyncSource, configChanged), mWaitingForVsync(false) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 40 | ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this); |
| 41 | } |
| 42 | |
| 43 | status_t DisplayEventDispatcher::initialize() { |
| 44 | status_t result = mReceiver.initCheck(); |
| 45 | if (result) { |
| 46 | ALOGW("Failed to initialize display event receiver, status=%d", result); |
| 47 | return result; |
| 48 | } |
| 49 | |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 50 | if (mLooper != nullptr) { |
| 51 | int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT, this, NULL); |
| 52 | if (rc < 0) { |
| 53 | return UNKNOWN_ERROR; |
| 54 | } |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 55 | } |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 56 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 57 | return OK; |
| 58 | } |
| 59 | |
| 60 | void DisplayEventDispatcher::dispose() { |
| 61 | ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this); |
| 62 | |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 63 | if (!mReceiver.initCheck() && mLooper != nullptr) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 64 | mLooper->removeFd(mReceiver.getFd()); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | status_t DisplayEventDispatcher::scheduleVsync() { |
| 69 | if (!mWaitingForVsync) { |
| 70 | ALOGV("dispatcher %p ~ Scheduling vsync.", this); |
| 71 | |
| 72 | // Drain all pending events. |
| 73 | nsecs_t vsyncTimestamp; |
| 74 | PhysicalDisplayId vsyncDisplayId; |
| 75 | uint32_t vsyncCount; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame^] | 76 | VsyncEventData vsyncEventData; |
| 77 | if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 78 | ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this, |
| 79 | ns2ms(static_cast<nsecs_t>(vsyncTimestamp))); |
| 80 | } |
| 81 | |
| 82 | status_t status = mReceiver.requestNextVsync(); |
| 83 | if (status) { |
| 84 | ALOGW("Failed to request next vsync, status=%d", status); |
| 85 | return status; |
| 86 | } |
| 87 | |
| 88 | mWaitingForVsync = true; |
| 89 | } |
| 90 | return OK; |
| 91 | } |
| 92 | |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 93 | void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) { |
| 94 | mReceiver.sendEvents(&event, 1); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alec Mouri | 921b277 | 2020-02-05 19:03:28 -0800 | [diff] [blame] | 97 | int DisplayEventDispatcher::getFd() const { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 98 | return mReceiver.getFd(); |
| 99 | } |
| 100 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 101 | int DisplayEventDispatcher::handleEvent(int, int events, void*) { |
| 102 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 103 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
| 104 | "events=0x%x", |
| 105 | events); |
| 106 | return 0; // remove the callback |
| 107 | } |
| 108 | |
| 109 | if (!(events & Looper::EVENT_INPUT)) { |
| 110 | ALOGW("Received spurious callback for unhandled poll event. " |
| 111 | "events=0x%x", |
| 112 | events); |
| 113 | return 1; // keep the callback |
| 114 | } |
| 115 | |
| 116 | // Drain all pending events, keep the last vsync. |
| 117 | nsecs_t vsyncTimestamp; |
| 118 | PhysicalDisplayId vsyncDisplayId; |
| 119 | uint32_t vsyncCount; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame^] | 120 | VsyncEventData vsyncEventData; |
| 121 | if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) { |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 122 | ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64 |
| 123 | ", displayId=%s, count=%d, vsyncId=%" PRId64, |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame^] | 124 | this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount, |
| 125 | vsyncEventData.id); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 126 | mWaitingForVsync = false; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame^] | 127 | dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return 1; // keep the callback |
| 131 | } |
| 132 | |
| 133 | bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp, |
| 134 | PhysicalDisplayId* outDisplayId, |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame^] | 135 | uint32_t* outCount, |
| 136 | VsyncEventData* outVsyncEventData) { |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 137 | bool gotVsync = false; |
| 138 | DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; |
| 139 | ssize_t n; |
| 140 | while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { |
| 141 | ALOGV("dispatcher %p ~ Read %d events.", this, int(n)); |
| 142 | for (ssize_t i = 0; i < n; i++) { |
| 143 | const DisplayEventReceiver::Event& ev = buf[i]; |
| 144 | switch (ev.header.type) { |
| 145 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 146 | // Later vsync events will just overwrite the info from earlier |
| 147 | // ones. That's fine, we only care about the most recent. |
| 148 | gotVsync = true; |
| 149 | *outTimestamp = ev.header.timestamp; |
| 150 | *outDisplayId = ev.header.displayId; |
| 151 | *outCount = ev.vsync.count; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame^] | 152 | outVsyncEventData->id = ev.vsync.vsyncId; |
| 153 | outVsyncEventData->deadlineTimestamp = ev.vsync.deadlineTimestamp; |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 154 | break; |
| 155 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
| 156 | dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected); |
| 157 | break; |
| 158 | case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: |
| 159 | dispatchConfigChanged(ev.header.timestamp, ev.header.displayId, |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 160 | ev.config.configId, ev.config.vsyncPeriod); |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 161 | break; |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 162 | case DisplayEventReceiver::DISPLAY_EVENT_NULL: |
| 163 | dispatchNullEvent(ev.header.timestamp, ev.header.displayId); |
| 164 | break; |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 165 | default: |
| 166 | ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if (n < 0) { |
| 172 | ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n)); |
| 173 | } |
| 174 | return gotVsync; |
| 175 | } |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 176 | |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 177 | } // namespace android |