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