blob: 9cd3f631c8d61db8ada991c311ff85b289246922 [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
Ady Abraham62f216c2020-10-13 19:07:23 -070036DisplayEventDispatcher::DisplayEventDispatcher(
37 const sp<Looper>& looper, ISurfaceComposer::VsyncSource vsyncSource,
38 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham1ff99d82021-02-09 02:27:08 +000039 : mLooper(looper), mReceiver(vsyncSource, eventRegistration), mWaitingForVsync(false) {
Alec Mouri77a53872019-10-28 16:44:36 -070040 ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
41}
42
43status_t DisplayEventDispatcher::initialize() {
44 status_t result = mReceiver.initCheck();
45 if (result) {
46 ALOGW("Failed to initialize display event receiver, status=%d", result);
47 return result;
48 }
49
Alec Mouri50a931d2019-11-19 16:23:59 -080050 if (mLooper != nullptr) {
51 int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT, this, NULL);
52 if (rc < 0) {
53 return UNKNOWN_ERROR;
54 }
Alec Mouri77a53872019-10-28 16:44:36 -070055 }
Alec Mouri50a931d2019-11-19 16:23:59 -080056
Alec Mouri77a53872019-10-28 16:44:36 -070057 return OK;
58}
59
60void DisplayEventDispatcher::dispose() {
61 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
62
Alec Mouri50a931d2019-11-19 16:23:59 -080063 if (!mReceiver.initCheck() && mLooper != nullptr) {
Alec Mouri77a53872019-10-28 16:44:36 -070064 mLooper->removeFd(mReceiver.getFd());
65 }
66}
67
68status_t DisplayEventDispatcher::scheduleVsync() {
Ady Abraham1ff99d82021-02-09 02:27:08 +000069 if (!mWaitingForVsync) {
70 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
Alec Mouri77a53872019-10-28 16:44:36 -070071
Ady Abraham1ff99d82021-02-09 02:27:08 +000072 // Drain all pending events.
73 nsecs_t vsyncTimestamp;
74 PhysicalDisplayId vsyncDisplayId;
75 uint32_t vsyncCount;
76 VsyncEventData vsyncEventData;
77 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
78 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this,
79 ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
80 }
Ady Abraham98178752020-12-21 19:14:30 -080081
Ady Abraham1ff99d82021-02-09 02:27:08 +000082 status_t status = mReceiver.requestNextVsync();
83 if (status) {
84 ALOGW("Failed to request next vsync, status=%d", status);
85 return status;
86 }
Ady Abraham98178752020-12-21 19:14:30 -080087
Ady Abraham1ff99d82021-02-09 02:27:08 +000088 mWaitingForVsync = true;
Alec Mouri77a53872019-10-28 16:44:36 -070089 }
90 return OK;
91}
92
Alec Mouri967d5d72020-08-05 12:50:03 -070093void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
94 mReceiver.sendEvents(&event, 1);
Alec Mouri60aee1c2019-10-28 16:18:59 -070095}
96
Alec Mouri921b2772020-02-05 19:03:28 -080097int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -080098 return mReceiver.getFd();
99}
100
Alec Mouri77a53872019-10-28 16:44:36 -0700101int DisplayEventDispatcher::handleEvent(int, int events, void*) {
102 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
103 ALOGE("Display event receiver pipe was closed or an error occurred. "
104 "events=0x%x",
105 events);
106 return 0; // remove the callback
107 }
108
109 if (!(events & Looper::EVENT_INPUT)) {
110 ALOGW("Received spurious callback for unhandled poll event. "
111 "events=0x%x",
112 events);
113 return 1; // keep the callback
114 }
115
116 // Drain all pending events, keep the last vsync.
117 nsecs_t vsyncTimestamp;
118 PhysicalDisplayId vsyncDisplayId;
119 uint32_t vsyncCount;
Ady Abraham0d28d762020-10-05 17:50:41 -0700120 VsyncEventData vsyncEventData;
121 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
Ady Abraham74e17562020-08-24 18:18:19 -0700122 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
123 ", displayId=%s, count=%d, vsyncId=%" PRId64,
Ady Abraham0d28d762020-10-05 17:50:41 -0700124 this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
125 vsyncEventData.id);
Ady Abraham1ff99d82021-02-09 02:27:08 +0000126 mWaitingForVsync = false;
127 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
Alec Mouri77a53872019-10-28 16:44:36 -0700128 }
129
130 return 1; // keep the callback
131}
132
133bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
134 PhysicalDisplayId* outDisplayId,
Ady Abraham0d28d762020-10-05 17:50:41 -0700135 uint32_t* outCount,
136 VsyncEventData* outVsyncEventData) {
Alec Mouri77a53872019-10-28 16:44:36 -0700137 bool gotVsync = false;
138 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
139 ssize_t n;
140 while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
141 ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
Ady Abraham62f216c2020-10-13 19:07:23 -0700142 mFrameRateOverrides.reserve(n);
Alec Mouri77a53872019-10-28 16:44:36 -0700143 for (ssize_t i = 0; i < n; i++) {
144 const DisplayEventReceiver::Event& ev = buf[i];
145 switch (ev.header.type) {
146 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
147 // Later vsync events will just overwrite the info from earlier
148 // ones. That's fine, we only care about the most recent.
149 gotVsync = true;
150 *outTimestamp = ev.header.timestamp;
151 *outDisplayId = ev.header.displayId;
152 *outCount = ev.vsync.count;
Ady Abraham0d28d762020-10-05 17:50:41 -0700153 outVsyncEventData->id = ev.vsync.vsyncId;
154 outVsyncEventData->deadlineTimestamp = ev.vsync.deadlineTimestamp;
Alec Mouri77a53872019-10-28 16:44:36 -0700155 break;
156 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
157 dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
158 break;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100159 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
160 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
161 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700162 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700163 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
164 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
165 break;
Ady Abraham62f216c2020-10-13 19:07:23 -0700166 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
167 mFrameRateOverrides.emplace_back(ev.frameRateOverride);
168 break;
169 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
170 dispatchFrameRateOverrides(ev.header.timestamp, ev.header.displayId,
171 std::move(mFrameRateOverrides));
172 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700173 default:
174 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
175 break;
176 }
177 }
178 }
179 if (n < 0) {
180 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
181 }
182 return gotVsync;
183}
Alec Mouri967d5d72020-08-05 12:50:03 -0700184
Alec Mouri77a53872019-10-28 16:44:36 -0700185} // namespace android