blob: 26db59bcffeccc0b96def1f1942ef94e50803cd1 [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
Ady Abraham62f216c2020-10-13 19:07:23 -070038DisplayEventDispatcher::DisplayEventDispatcher(
39 const sp<Looper>& looper, ISurfaceComposer::VsyncSource vsyncSource,
40 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Cheng Shifc690e22021-10-22 08:53:11 +000041 : mLooper(looper), mReceiver(vsyncSource, eventRegistration), mWaitingForVsync(false),
42 mLastVsyncCount(0), mLastScheduleVsyncTime(0) {
Alec Mouri77a53872019-10-28 16:44:36 -070043 ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
44}
45
46status_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 Mouri50a931d2019-11-19 16:23:59 -080053 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 Mouri77a53872019-10-28 16:44:36 -070058 }
Alec Mouri50a931d2019-11-19 16:23:59 -080059
Alec Mouri77a53872019-10-28 16:44:36 -070060 return OK;
61}
62
63void DisplayEventDispatcher::dispose() {
64 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
65
Alec Mouri50a931d2019-11-19 16:23:59 -080066 if (!mReceiver.initCheck() && mLooper != nullptr) {
Alec Mouri77a53872019-10-28 16:44:36 -070067 mLooper->removeFd(mReceiver.getFd());
68 }
69}
70
71status_t DisplayEventDispatcher::scheduleVsync() {
Ady Abraham1ff99d82021-02-09 02:27:08 +000072 if (!mWaitingForVsync) {
73 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
Alec Mouri77a53872019-10-28 16:44:36 -070074
Ady Abraham1ff99d82021-02-09 02:27:08 +000075 // Drain all pending events.
76 nsecs_t vsyncTimestamp;
77 PhysicalDisplayId vsyncDisplayId;
78 uint32_t vsyncCount;
79 VsyncEventData vsyncEventData;
80 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
81 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this,
82 ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
83 }
Ady Abraham98178752020-12-21 19:14:30 -080084
Ady Abraham1ff99d82021-02-09 02:27:08 +000085 status_t status = mReceiver.requestNextVsync();
86 if (status) {
87 ALOGW("Failed to request next vsync, status=%d", status);
88 return status;
89 }
Ady Abraham98178752020-12-21 19:14:30 -080090
Ady Abraham1ff99d82021-02-09 02:27:08 +000091 mWaitingForVsync = true;
Cheng Shifc690e22021-10-22 08:53:11 +000092 mLastScheduleVsyncTime = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri77a53872019-10-28 16:44:36 -070093 }
94 return OK;
95}
96
Alec Mouri967d5d72020-08-05 12:50:03 -070097void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
98 mReceiver.sendEvents(&event, 1);
Alec Mouri60aee1c2019-10-28 16:18:59 -070099}
100
Alec Mouri921b2772020-02-05 19:03:28 -0800101int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -0800102 return mReceiver.getFd();
103}
104
Alec Mouri77a53872019-10-28 16:44:36 -0700105int DisplayEventDispatcher::handleEvent(int, int events, void*) {
106 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
107 ALOGE("Display event receiver pipe was closed or an error occurred. "
108 "events=0x%x",
109 events);
110 return 0; // remove the callback
111 }
112
113 if (!(events & Looper::EVENT_INPUT)) {
114 ALOGW("Received spurious callback for unhandled poll event. "
115 "events=0x%x",
116 events);
117 return 1; // keep the callback
118 }
119
120 // Drain all pending events, keep the last vsync.
121 nsecs_t vsyncTimestamp;
122 PhysicalDisplayId vsyncDisplayId;
123 uint32_t vsyncCount;
Ady Abraham0d28d762020-10-05 17:50:41 -0700124 VsyncEventData vsyncEventData;
125 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
Ady Abraham74e17562020-08-24 18:18:19 -0700126 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
127 ", displayId=%s, count=%d, vsyncId=%" PRId64,
Ady Abraham0d28d762020-10-05 17:50:41 -0700128 this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
129 vsyncEventData.id);
Ady Abraham1ff99d82021-02-09 02:27:08 +0000130 mWaitingForVsync = false;
Cheng Shifc690e22021-10-22 08:53:11 +0000131 mLastVsyncCount = vsyncCount;
Ady Abraham1ff99d82021-02-09 02:27:08 +0000132 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
Alec Mouri77a53872019-10-28 16:44:36 -0700133 }
134
Cheng Shifc690e22021-10-22 08:53:11 +0000135 if (mWaitingForVsync) {
136 const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
137 const nsecs_t vsyncScheduleDelay = currentTime - mLastScheduleVsyncTime;
138 if (vsyncScheduleDelay > WAITING_FOR_VSYNC_TIMEOUT) {
139 ALOGW("Vsync time out! vsyncScheduleDelay=%" PRId64 "ms", ns2ms(vsyncScheduleDelay));
140 mWaitingForVsync = false;
141 dispatchVsync(currentTime, vsyncDisplayId /* displayId is not used */,
142 ++mLastVsyncCount, vsyncEventData /* empty data */);
143 }
144 }
145
Alec Mouri77a53872019-10-28 16:44:36 -0700146 return 1; // keep the callback
147}
148
Rachel Lee3f028662021-11-04 19:32:24 +0000149void DisplayEventDispatcher::populateFrameTimelines(const DisplayEventReceiver::Event& event,
150 VsyncEventData* outVsyncEventData) const {
Rachel Lee18c34372022-01-20 13:57:18 -0800151 for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
Rachel Lee3f028662021-11-04 19:32:24 +0000152 DisplayEventReceiver::Event::VSync::FrameTimeline receiverTimeline =
153 event.vsync.frameTimelines[i];
Rachel Lee18c34372022-01-20 13:57:18 -0800154 outVsyncEventData->frameTimelines[i] =
155 VsyncEventData::FrameTimeline(receiverTimeline.vsyncId,
156 receiverTimeline.deadlineTimestamp,
157 receiverTimeline.expectedVSyncTimestamp);
Rachel Lee3f028662021-11-04 19:32:24 +0000158 }
159}
160
Alec Mouri77a53872019-10-28 16:44:36 -0700161bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
162 PhysicalDisplayId* outDisplayId,
Ady Abraham0d28d762020-10-05 17:50:41 -0700163 uint32_t* outCount,
164 VsyncEventData* outVsyncEventData) {
Alec Mouri77a53872019-10-28 16:44:36 -0700165 bool gotVsync = false;
166 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
167 ssize_t n;
168 while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
169 ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
Ady Abraham62f216c2020-10-13 19:07:23 -0700170 mFrameRateOverrides.reserve(n);
Alec Mouri77a53872019-10-28 16:44:36 -0700171 for (ssize_t i = 0; i < n; i++) {
172 const DisplayEventReceiver::Event& ev = buf[i];
173 switch (ev.header.type) {
174 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
175 // Later vsync events will just overwrite the info from earlier
176 // ones. That's fine, we only care about the most recent.
177 gotVsync = true;
178 *outTimestamp = ev.header.timestamp;
179 *outDisplayId = ev.header.displayId;
180 *outCount = ev.vsync.count;
Ady Abraham0d28d762020-10-05 17:50:41 -0700181 outVsyncEventData->id = ev.vsync.vsyncId;
182 outVsyncEventData->deadlineTimestamp = ev.vsync.deadlineTimestamp;
Jorim Jaggic0086af2021-02-12 18:18:11 +0100183 outVsyncEventData->frameInterval = ev.vsync.frameInterval;
Rachel Lee3f028662021-11-04 19:32:24 +0000184 outVsyncEventData->preferredFrameTimelineIndex =
185 ev.vsync.preferredFrameTimelineIndex;
186 populateFrameTimelines(ev, outVsyncEventData);
Alec Mouri77a53872019-10-28 16:44:36 -0700187 break;
188 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
189 dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
190 break;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100191 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
192 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
193 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700194 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700195 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
196 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
197 break;
Ady Abraham62f216c2020-10-13 19:07:23 -0700198 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
199 mFrameRateOverrides.emplace_back(ev.frameRateOverride);
200 break;
201 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
202 dispatchFrameRateOverrides(ev.header.timestamp, ev.header.displayId,
203 std::move(mFrameRateOverrides));
204 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700205 default:
206 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
207 break;
208 }
209 }
210 }
211 if (n < 0) {
212 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
213 }
214 return gotVsync;
215}
Alec Mouri967d5d72020-08-05 12:50:03 -0700216
Alec Mouri77a53872019-10-28 16:44:36 -0700217} // namespace android