blob: 68f10f4d80bea5f29ce8c1e85de293402f5bf7d6 [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"
Ady Abraham1d0cae92024-06-14 13:41:12 -070018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Alec Mouri77a53872019-10-28 16:44:36 -070019
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 Mouri77a53872019-10-28 16:44:36 -070027#include <utils/Timers.h>
Ady Abraham1d0cae92024-06-14 13:41:12 -070028#include <utils/Trace.h>
29
30#include <com_android_graphics_libgui_flags.h>
Alec Mouri77a53872019-10-28 16:44:36 -070031
32namespace android {
Ady Abraham1d0cae92024-06-14 13:41:12 -070033using namespace com::android::graphics::libgui;
Alec Mouri77a53872019-10-28 16:44:36 -070034
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.
38static const size_t EVENT_BUFFER_SIZE = 100;
39
Cheng Shifc690e22021-10-22 08:53:11 +000040static constexpr nsecs_t WAITING_FOR_VSYNC_TIMEOUT = ms2ns(300);
41
Huihong Luo1b0c49f2022-03-15 19:18:21 -070042DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper,
43 gui::ISurfaceComposer::VsyncSource vsyncSource,
Rachel Lee2248f522023-01-27 16:45:23 -080044 EventRegistrationFlags eventRegistration,
45 const sp<IBinder>& layerHandle)
Huihong Luo1b0c49f2022-03-15 19:18:21 -070046 : mLooper(looper),
Rachel Lee2248f522023-01-27 16:45:23 -080047 mReceiver(vsyncSource, eventRegistration, layerHandle),
Huihong Luo1b0c49f2022-03-15 19:18:21 -070048 mWaitingForVsync(false),
49 mLastVsyncCount(0),
50 mLastScheduleVsyncTime(0) {
Alec Mouri77a53872019-10-28 16:44:36 -070051 ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
52}
53
54status_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 Mouri50a931d2019-11-19 16:23:59 -080061 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 Mouri77a53872019-10-28 16:44:36 -070066 }
Alec Mouri50a931d2019-11-19 16:23:59 -080067
Alec Mouri77a53872019-10-28 16:44:36 -070068 return OK;
69}
70
71void DisplayEventDispatcher::dispose() {
72 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
73
Alec Mouri50a931d2019-11-19 16:23:59 -080074 if (!mReceiver.initCheck() && mLooper != nullptr) {
Alec Mouri77a53872019-10-28 16:44:36 -070075 mLooper->removeFd(mReceiver.getFd());
76 }
77}
78
79status_t DisplayEventDispatcher::scheduleVsync() {
Ady Abraham1ff99d82021-02-09 02:27:08 +000080 if (!mWaitingForVsync) {
81 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
Alec Mouri77a53872019-10-28 16:44:36 -070082
Ady Abraham1ff99d82021-02-09 02:27:08 +000083 // 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 Abraham98178752020-12-21 19:14:30 -080092
Ady Abraham1ff99d82021-02-09 02:27:08 +000093 status_t status = mReceiver.requestNextVsync();
94 if (status) {
95 ALOGW("Failed to request next vsync, status=%d", status);
96 return status;
97 }
Ady Abraham98178752020-12-21 19:14:30 -080098
Ady Abraham1ff99d82021-02-09 02:27:08 +000099 mWaitingForVsync = true;
Cheng Shifc690e22021-10-22 08:53:11 +0000100 mLastScheduleVsyncTime = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri77a53872019-10-28 16:44:36 -0700101 }
102 return OK;
103}
104
Alec Mouri967d5d72020-08-05 12:50:03 -0700105void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
106 mReceiver.sendEvents(&event, 1);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700107}
108
Alec Mouri921b2772020-02-05 19:03:28 -0800109int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -0800110 return mReceiver.getFd();
111}
112
Alec Mouri77a53872019-10-28 16:44:36 -0700113int 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 Abraham0d28d762020-10-05 17:50:41 -0700132 VsyncEventData vsyncEventData;
133 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
Ady Abraham74e17562020-08-24 18:18:19 -0700134 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
135 ", displayId=%s, count=%d, vsyncId=%" PRId64,
Ady Abraham0d28d762020-10-05 17:50:41 -0700136 this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800137 vsyncEventData.preferredVsyncId());
Ady Abraham1ff99d82021-02-09 02:27:08 +0000138 mWaitingForVsync = false;
Cheng Shifc690e22021-10-22 08:53:11 +0000139 mLastVsyncCount = vsyncCount;
Ady Abraham1ff99d82021-02-09 02:27:08 +0000140 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
Alec Mouri77a53872019-10-28 16:44:36 -0700141 }
142
Cheng Shifc690e22021-10-22 08:53:11 +0000143 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 Mouri77a53872019-10-28 16:44:36 -0700154 return 1; // keep the callback
155}
156
157bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
158 PhysicalDisplayId* outDisplayId,
Ady Abraham0d28d762020-10-05 17:50:41 -0700159 uint32_t* outCount,
160 VsyncEventData* outVsyncEventData) {
Alec Mouri77a53872019-10-28 16:44:36 -0700161 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 Abraham62f216c2020-10-13 19:07:23 -0700166 mFrameRateOverrides.reserve(n);
Alec Mouri77a53872019-10-28 16:44:36 -0700167 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 Leeb9c5a772022-02-04 21:17:37 -0800177 *outVsyncEventData = ev.vsync.vsyncData;
Ady Abraham1d0cae92024-06-14 13:41:12 -0700178
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 Mouri77a53872019-10-28 16:44:36 -0700185 break;
186 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700187 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 Mouri77a53872019-10-28 16:44:36 -0700194 break;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100195 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
196 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
197 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700198 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700199 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
200 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
201 break;
Ady Abraham62f216c2020-10-13 19:07:23 -0700202 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 Luo9ebb7a72023-06-27 17:01:50 -0700209 case DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE:
210 dispatchHdcpLevelsChanged(ev.header.displayId,
211 ev.hdcpLevelsChange.connectedLevel,
212 ev.hdcpLevelsChange.maxLevel);
213 break;
Manasi Navare5ef1fa12024-11-07 23:49:46 +0000214 case DisplayEventReceiver::DISPLAY_EVENT_MODE_REJECTION:
215 dispatchModeRejected(ev.header.displayId, ev.modeRejection.modeId);
216 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700217 default:
218 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
219 break;
220 }
221 }
222 }
223 if (n < 0) {
224 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
225 }
226 return gotVsync;
227}
Alec Mouri967d5d72020-08-05 12:50:03 -0700228
Rachel Lee4cf8bab2022-03-14 14:38:45 -0700229status_t DisplayEventDispatcher::getLatestVsyncEventData(
230 ParcelableVsyncEventData* outVsyncEventData) const {
231 return mReceiver.getLatestVsyncEventData(outVsyncEventData);
232}
233
Alec Mouri77a53872019-10-28 16:44:36 -0700234} // namespace android