blob: 67ca39c99e1703ee68edbf91f477b6f25975c9d5 [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) {
Anton Ivanov81793802025-02-13 22:57:24 -080062 int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT,
63 sp<LooperCallback>::fromExisting(this), NULL);
Alec Mouri50a931d2019-11-19 16:23:59 -080064 if (rc < 0) {
65 return UNKNOWN_ERROR;
66 }
Alec Mouri77a53872019-10-28 16:44:36 -070067 }
Alec Mouri50a931d2019-11-19 16:23:59 -080068
Alec Mouri77a53872019-10-28 16:44:36 -070069 return OK;
70}
71
72void DisplayEventDispatcher::dispose() {
73 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
74
Alec Mouri50a931d2019-11-19 16:23:59 -080075 if (!mReceiver.initCheck() && mLooper != nullptr) {
Alec Mouri77a53872019-10-28 16:44:36 -070076 mLooper->removeFd(mReceiver.getFd());
77 }
78}
79
80status_t DisplayEventDispatcher::scheduleVsync() {
Ady Abraham1ff99d82021-02-09 02:27:08 +000081 if (!mWaitingForVsync) {
82 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
Alec Mouri77a53872019-10-28 16:44:36 -070083
Ady Abraham1ff99d82021-02-09 02:27:08 +000084 // Drain all pending events.
85 nsecs_t vsyncTimestamp;
86 PhysicalDisplayId vsyncDisplayId;
87 uint32_t vsyncCount;
88 VsyncEventData vsyncEventData;
89 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
90 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this,
91 ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
92 }
Ady Abraham98178752020-12-21 19:14:30 -080093
Ady Abraham1ff99d82021-02-09 02:27:08 +000094 status_t status = mReceiver.requestNextVsync();
95 if (status) {
96 ALOGW("Failed to request next vsync, status=%d", status);
97 return status;
98 }
Ady Abraham98178752020-12-21 19:14:30 -080099
Ady Abraham1ff99d82021-02-09 02:27:08 +0000100 mWaitingForVsync = true;
Cheng Shifc690e22021-10-22 08:53:11 +0000101 mLastScheduleVsyncTime = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri77a53872019-10-28 16:44:36 -0700102 }
103 return OK;
104}
105
Alec Mouri967d5d72020-08-05 12:50:03 -0700106void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
107 mReceiver.sendEvents(&event, 1);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700108}
109
Alec Mouri921b2772020-02-05 19:03:28 -0800110int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -0800111 return mReceiver.getFd();
112}
113
Alec Mouri77a53872019-10-28 16:44:36 -0700114int DisplayEventDispatcher::handleEvent(int, int events, void*) {
115 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
116 ALOGE("Display event receiver pipe was closed or an error occurred. "
117 "events=0x%x",
118 events);
119 return 0; // remove the callback
120 }
121
122 if (!(events & Looper::EVENT_INPUT)) {
123 ALOGW("Received spurious callback for unhandled poll event. "
124 "events=0x%x",
125 events);
126 return 1; // keep the callback
127 }
128
129 // Drain all pending events, keep the last vsync.
130 nsecs_t vsyncTimestamp;
131 PhysicalDisplayId vsyncDisplayId;
132 uint32_t vsyncCount;
Ady Abraham0d28d762020-10-05 17:50:41 -0700133 VsyncEventData vsyncEventData;
134 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
Ady Abraham74e17562020-08-24 18:18:19 -0700135 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
136 ", displayId=%s, count=%d, vsyncId=%" PRId64,
Ady Abraham0d28d762020-10-05 17:50:41 -0700137 this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800138 vsyncEventData.preferredVsyncId());
Ady Abraham1ff99d82021-02-09 02:27:08 +0000139 mWaitingForVsync = false;
Cheng Shifc690e22021-10-22 08:53:11 +0000140 mLastVsyncCount = vsyncCount;
Ady Abraham1ff99d82021-02-09 02:27:08 +0000141 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
Alec Mouri77a53872019-10-28 16:44:36 -0700142 }
143
Cheng Shifc690e22021-10-22 08:53:11 +0000144 if (mWaitingForVsync) {
145 const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
146 const nsecs_t vsyncScheduleDelay = currentTime - mLastScheduleVsyncTime;
147 if (vsyncScheduleDelay > WAITING_FOR_VSYNC_TIMEOUT) {
148 ALOGW("Vsync time out! vsyncScheduleDelay=%" PRId64 "ms", ns2ms(vsyncScheduleDelay));
149 mWaitingForVsync = false;
150 dispatchVsync(currentTime, vsyncDisplayId /* displayId is not used */,
151 ++mLastVsyncCount, vsyncEventData /* empty data */);
152 }
153 }
154
Alec Mouri77a53872019-10-28 16:44:36 -0700155 return 1; // keep the callback
156}
157
158bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
159 PhysicalDisplayId* outDisplayId,
Ady Abraham0d28d762020-10-05 17:50:41 -0700160 uint32_t* outCount,
161 VsyncEventData* outVsyncEventData) {
Alec Mouri77a53872019-10-28 16:44:36 -0700162 bool gotVsync = false;
163 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
164 ssize_t n;
165 while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
166 ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
Ady Abraham62f216c2020-10-13 19:07:23 -0700167 mFrameRateOverrides.reserve(n);
Alec Mouri77a53872019-10-28 16:44:36 -0700168 for (ssize_t i = 0; i < n; i++) {
169 const DisplayEventReceiver::Event& ev = buf[i];
170 switch (ev.header.type) {
171 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
172 // Later vsync events will just overwrite the info from earlier
173 // ones. That's fine, we only care about the most recent.
174 gotVsync = true;
175 *outTimestamp = ev.header.timestamp;
176 *outDisplayId = ev.header.displayId;
177 *outCount = ev.vsync.count;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800178 *outVsyncEventData = ev.vsync.vsyncData;
Ady Abraham1d0cae92024-06-14 13:41:12 -0700179
180 // Trace the RenderRate for this app
181 if (ATRACE_ENABLED() && flags::trace_frame_rate_override()) {
182 const auto frameInterval = ev.vsync.vsyncData.frameInterval;
183 int fps = frameInterval > 0 ? 1e9f / frameInterval : 0;
184 ATRACE_INT("RenderRate", fps);
185 }
Alec Mouri77a53872019-10-28 16:44:36 -0700186 break;
187 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700188 if (ev.hotplug.connectionError == 0) {
189 dispatchHotplug(ev.header.timestamp, ev.header.displayId,
190 ev.hotplug.connected);
191 } else {
192 dispatchHotplugConnectionError(ev.header.timestamp,
193 ev.hotplug.connectionError);
194 }
Alec Mouri77a53872019-10-28 16:44:36 -0700195 break;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100196 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
197 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
198 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700199 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700200 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
201 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
202 break;
Ady Abraham62f216c2020-10-13 19:07:23 -0700203 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
204 mFrameRateOverrides.emplace_back(ev.frameRateOverride);
205 break;
206 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
207 dispatchFrameRateOverrides(ev.header.timestamp, ev.header.displayId,
208 std::move(mFrameRateOverrides));
209 break;
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700210 case DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE:
211 dispatchHdcpLevelsChanged(ev.header.displayId,
212 ev.hdcpLevelsChange.connectedLevel,
213 ev.hdcpLevelsChange.maxLevel);
214 break;
Manasi Navare5ef1fa12024-11-07 23:49:46 +0000215 case DisplayEventReceiver::DISPLAY_EVENT_MODE_REJECTION:
216 dispatchModeRejected(ev.header.displayId, ev.modeRejection.modeId);
217 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700218 default:
219 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
220 break;
221 }
222 }
223 }
224 if (n < 0) {
225 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
226 }
227 return gotVsync;
228}
Alec Mouri967d5d72020-08-05 12:50:03 -0700229
Rachel Lee4cf8bab2022-03-14 14:38:45 -0700230status_t DisplayEventDispatcher::getLatestVsyncEventData(
231 ParcelableVsyncEventData* outVsyncEventData) const {
232 return mReceiver.getLatestVsyncEventData(outVsyncEventData);
233}
234
Alec Mouri77a53872019-10-28 16:44:36 -0700235} // namespace android