blob: 5dd058cf9f9049ecd6c59ac2cd7b0817e2974451 [file] [log] [blame]
Alec Mouri77a53872019-10-28 16:44:36 -07001/*
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
29namespace 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.
34static const size_t EVENT_BUFFER_SIZE = 100;
35
Cheng Shifc690e22021-10-22 08:53:11 +000036static constexpr nsecs_t WAITING_FOR_VSYNC_TIMEOUT = ms2ns(300);
37
Huihong Luo1b0c49f2022-03-15 19:18:21 -070038DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper,
39 gui::ISurfaceComposer::VsyncSource vsyncSource,
Rachel Lee2248f522023-01-27 16:45:23 -080040 EventRegistrationFlags eventRegistration,
41 const sp<IBinder>& layerHandle)
Huihong Luo1b0c49f2022-03-15 19:18:21 -070042 : mLooper(looper),
Rachel Lee2248f522023-01-27 16:45:23 -080043 mReceiver(vsyncSource, eventRegistration, layerHandle),
Huihong Luo1b0c49f2022-03-15 19:18:21 -070044 mWaitingForVsync(false),
45 mLastVsyncCount(0),
46 mLastScheduleVsyncTime(0) {
Alec Mouri77a53872019-10-28 16:44:36 -070047 ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
48}
49
50status_t DisplayEventDispatcher::initialize() {
51 status_t result = mReceiver.initCheck();
52 if (result) {
53 ALOGW("Failed to initialize display event receiver, status=%d", result);
54 return result;
55 }
56
Alec Mouri50a931d2019-11-19 16:23:59 -080057 if (mLooper != nullptr) {
58 int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT, this, NULL);
59 if (rc < 0) {
60 return UNKNOWN_ERROR;
61 }
Alec Mouri77a53872019-10-28 16:44:36 -070062 }
Alec Mouri50a931d2019-11-19 16:23:59 -080063
Alec Mouri77a53872019-10-28 16:44:36 -070064 return OK;
65}
66
67void DisplayEventDispatcher::dispose() {
68 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
69
Alec Mouri50a931d2019-11-19 16:23:59 -080070 if (!mReceiver.initCheck() && mLooper != nullptr) {
Alec Mouri77a53872019-10-28 16:44:36 -070071 mLooper->removeFd(mReceiver.getFd());
72 }
73}
74
75status_t DisplayEventDispatcher::scheduleVsync() {
Ady Abraham1ff99d82021-02-09 02:27:08 +000076 if (!mWaitingForVsync) {
77 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
Alec Mouri77a53872019-10-28 16:44:36 -070078
Ady Abraham1ff99d82021-02-09 02:27:08 +000079 // Drain all pending events.
80 nsecs_t vsyncTimestamp;
81 PhysicalDisplayId vsyncDisplayId;
82 uint32_t vsyncCount;
83 VsyncEventData vsyncEventData;
84 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
85 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this,
86 ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
87 }
Ady Abraham98178752020-12-21 19:14:30 -080088
Ady Abraham1ff99d82021-02-09 02:27:08 +000089 status_t status = mReceiver.requestNextVsync();
90 if (status) {
91 ALOGW("Failed to request next vsync, status=%d", status);
92 return status;
93 }
Ady Abraham98178752020-12-21 19:14:30 -080094
Ady Abraham1ff99d82021-02-09 02:27:08 +000095 mWaitingForVsync = true;
Cheng Shifc690e22021-10-22 08:53:11 +000096 mLastScheduleVsyncTime = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri77a53872019-10-28 16:44:36 -070097 }
98 return OK;
99}
100
Alec Mouri967d5d72020-08-05 12:50:03 -0700101void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
102 mReceiver.sendEvents(&event, 1);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700103}
104
Alec Mouri921b2772020-02-05 19:03:28 -0800105int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -0800106 return mReceiver.getFd();
107}
108
Alec Mouri77a53872019-10-28 16:44:36 -0700109int DisplayEventDispatcher::handleEvent(int, int events, void*) {
110 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
111 ALOGE("Display event receiver pipe was closed or an error occurred. "
112 "events=0x%x",
113 events);
114 return 0; // remove the callback
115 }
116
117 if (!(events & Looper::EVENT_INPUT)) {
118 ALOGW("Received spurious callback for unhandled poll event. "
119 "events=0x%x",
120 events);
121 return 1; // keep the callback
122 }
123
124 // Drain all pending events, keep the last vsync.
125 nsecs_t vsyncTimestamp;
126 PhysicalDisplayId vsyncDisplayId;
127 uint32_t vsyncCount;
Ady Abraham0d28d762020-10-05 17:50:41 -0700128 VsyncEventData vsyncEventData;
129 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
Ady Abraham74e17562020-08-24 18:18:19 -0700130 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
131 ", displayId=%s, count=%d, vsyncId=%" PRId64,
Ady Abraham0d28d762020-10-05 17:50:41 -0700132 this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800133 vsyncEventData.preferredVsyncId());
Ady Abraham1ff99d82021-02-09 02:27:08 +0000134 mWaitingForVsync = false;
Cheng Shifc690e22021-10-22 08:53:11 +0000135 mLastVsyncCount = vsyncCount;
Ady Abraham1ff99d82021-02-09 02:27:08 +0000136 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
Alec Mouri77a53872019-10-28 16:44:36 -0700137 }
138
Cheng Shifc690e22021-10-22 08:53:11 +0000139 if (mWaitingForVsync) {
140 const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
141 const nsecs_t vsyncScheduleDelay = currentTime - mLastScheduleVsyncTime;
142 if (vsyncScheduleDelay > WAITING_FOR_VSYNC_TIMEOUT) {
143 ALOGW("Vsync time out! vsyncScheduleDelay=%" PRId64 "ms", ns2ms(vsyncScheduleDelay));
144 mWaitingForVsync = false;
145 dispatchVsync(currentTime, vsyncDisplayId /* displayId is not used */,
146 ++mLastVsyncCount, vsyncEventData /* empty data */);
147 }
148 }
149
Alec Mouri77a53872019-10-28 16:44:36 -0700150 return 1; // keep the callback
151}
152
153bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
154 PhysicalDisplayId* outDisplayId,
Ady Abraham0d28d762020-10-05 17:50:41 -0700155 uint32_t* outCount,
156 VsyncEventData* outVsyncEventData) {
Alec Mouri77a53872019-10-28 16:44:36 -0700157 bool gotVsync = false;
158 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
159 ssize_t n;
160 while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
161 ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
Ady Abraham62f216c2020-10-13 19:07:23 -0700162 mFrameRateOverrides.reserve(n);
Alec Mouri77a53872019-10-28 16:44:36 -0700163 for (ssize_t i = 0; i < n; i++) {
164 const DisplayEventReceiver::Event& ev = buf[i];
165 switch (ev.header.type) {
166 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
167 // Later vsync events will just overwrite the info from earlier
168 // ones. That's fine, we only care about the most recent.
169 gotVsync = true;
170 *outTimestamp = ev.header.timestamp;
171 *outDisplayId = ev.header.displayId;
172 *outCount = ev.vsync.count;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800173 *outVsyncEventData = ev.vsync.vsyncData;
Alec Mouri77a53872019-10-28 16:44:36 -0700174 break;
175 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700176 if (ev.hotplug.connectionError == 0) {
177 dispatchHotplug(ev.header.timestamp, ev.header.displayId,
178 ev.hotplug.connected);
179 } else {
180 dispatchHotplugConnectionError(ev.header.timestamp,
181 ev.hotplug.connectionError);
182 }
Alec Mouri77a53872019-10-28 16:44:36 -0700183 break;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100184 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
185 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
186 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700187 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700188 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
189 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
190 break;
Ady Abraham62f216c2020-10-13 19:07:23 -0700191 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
192 mFrameRateOverrides.emplace_back(ev.frameRateOverride);
193 break;
194 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
195 dispatchFrameRateOverrides(ev.header.timestamp, ev.header.displayId,
196 std::move(mFrameRateOverrides));
197 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700198 default:
199 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
200 break;
201 }
202 }
203 }
204 if (n < 0) {
205 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
206 }
207 return gotVsync;
208}
Alec Mouri967d5d72020-08-05 12:50:03 -0700209
Rachel Lee4cf8bab2022-03-14 14:38:45 -0700210status_t DisplayEventDispatcher::getLatestVsyncEventData(
211 ParcelableVsyncEventData* outVsyncEventData) const {
212 return mReceiver.getLatestVsyncEventData(outVsyncEventData);
213}
214
Alec Mouri77a53872019-10-28 16:44:36 -0700215} // namespace android