blob: 5c9949b5499e4c815b485fda4bb08ee6b28df41a [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 Abraham98178752020-12-21 19:14:30 -080039 : mLooper(looper),
40 mReceiver(vsyncSource, eventRegistration),
41 mVsyncState(VsyncState::Unregistered) {
Alec Mouri77a53872019-10-28 16:44:36 -070042 ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
43}
44
45status_t DisplayEventDispatcher::initialize() {
46 status_t result = mReceiver.initCheck();
47 if (result) {
48 ALOGW("Failed to initialize display event receiver, status=%d", result);
49 return result;
50 }
51
Alec Mouri50a931d2019-11-19 16:23:59 -080052 if (mLooper != nullptr) {
53 int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT, this, NULL);
54 if (rc < 0) {
55 return UNKNOWN_ERROR;
56 }
Alec Mouri77a53872019-10-28 16:44:36 -070057 }
Alec Mouri50a931d2019-11-19 16:23:59 -080058
Alec Mouri77a53872019-10-28 16:44:36 -070059 return OK;
60}
61
62void DisplayEventDispatcher::dispose() {
63 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
64
Alec Mouri50a931d2019-11-19 16:23:59 -080065 if (!mReceiver.initCheck() && mLooper != nullptr) {
Alec Mouri77a53872019-10-28 16:44:36 -070066 mLooper->removeFd(mReceiver.getFd());
67 }
68}
69
70status_t DisplayEventDispatcher::scheduleVsync() {
Ady Abraham98178752020-12-21 19:14:30 -080071 switch (mVsyncState) {
72 case VsyncState::Unregistered: {
73 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
Alec Mouri77a53872019-10-28 16:44:36 -070074
Ady Abraham98178752020-12-21 19:14:30 -080075 // Drain all pending events.
76 nsecs_t vsyncTimestamp;
77 PhysicalDisplayId vsyncDisplayId;
78 uint32_t vsyncCount;
79 VsyncEventData vsyncEventData;
80 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount,
81 &vsyncEventData)) {
82 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "",
83 this, ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
84 }
85
86 status_t status = mReceiver.setVsyncRate(1);
87 if (status) {
88 ALOGW("Failed to set vsync rate, status=%d", status);
89 return status;
90 }
91
92 mVsyncState = VsyncState::RegisteredAndWaitingForVsync;
93 break;
Alec Mouri77a53872019-10-28 16:44:36 -070094 }
Ady Abraham98178752020-12-21 19:14:30 -080095 case VsyncState::Registered: {
96 mVsyncState = VsyncState::RegisteredAndWaitingForVsync;
97 break;
Alec Mouri77a53872019-10-28 16:44:36 -070098 }
Ady Abraham98178752020-12-21 19:14:30 -080099 case VsyncState::RegisteredAndWaitingForVsync: {
100 break;
101 }
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,
138 vsyncEventData.id);
Ady Abraham98178752020-12-21 19:14:30 -0800139 switch (mVsyncState) {
140 case VsyncState::Unregistered:
141 ALOGW("Received unexpected VSYNC event");
142 break;
143 case VsyncState::RegisteredAndWaitingForVsync:
144 mVsyncState = VsyncState::Registered;
145 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
146 break;
147 case VsyncState::Registered:
148 status_t status = mReceiver.setVsyncRate(0);
149 if (status) {
150 ALOGW("Failed to reset vsync rate, status=%d", status);
151 return status;
152 }
153 mVsyncState = VsyncState::Unregistered;
154 break;
155 }
Alec Mouri77a53872019-10-28 16:44:36 -0700156 }
157
158 return 1; // keep the callback
159}
160
161bool 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;
Alec Mouri77a53872019-10-28 16:44:36 -0700183 break;
184 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
185 dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
186 break;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100187 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
188 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
189 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700190 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700191 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
192 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
193 break;
Ady Abraham62f216c2020-10-13 19:07:23 -0700194 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
195 mFrameRateOverrides.emplace_back(ev.frameRateOverride);
196 break;
197 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
198 dispatchFrameRateOverrides(ev.header.timestamp, ev.header.displayId,
199 std::move(mFrameRateOverrides));
200 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700201 default:
202 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
203 break;
204 }
205 }
206 }
207 if (n < 0) {
208 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
209 }
210 return gotVsync;
211}
Alec Mouri967d5d72020-08-05 12:50:03 -0700212
Alec Mouri77a53872019-10-28 16:44:36 -0700213} // namespace android