1 /*
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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
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>
27 #include <utils/Timers.h>
28 #include <utils/Trace.h>
29
30 #include <com_android_graphics_libgui_flags.h>
31
32 namespace android {
33 using namespace com::android::graphics::libgui;
34
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.
38 static const size_t EVENT_BUFFER_SIZE = 100;
39
40 static constexpr nsecs_t WAITING_FOR_VSYNC_TIMEOUT = ms2ns(300);
41
DisplayEventDispatcher(const sp<Looper> & looper,gui::ISurfaceComposer::VsyncSource vsyncSource,EventRegistrationFlags eventRegistration,const sp<IBinder> & layerHandle)42 DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper,
43 gui::ISurfaceComposer::VsyncSource vsyncSource,
44 EventRegistrationFlags eventRegistration,
45 const sp<IBinder>& layerHandle)
46 : mLooper(looper),
47 mReceiver(vsyncSource, eventRegistration, layerHandle),
48 mWaitingForVsync(false),
49 mLastVsyncCount(0),
50 mLastScheduleVsyncTime(0) {
51 ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
52 }
53
initialize()54 status_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
61 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 }
66 }
67
68 return OK;
69 }
70
dispose()71 void DisplayEventDispatcher::dispose() {
72 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
73
74 if (!mReceiver.initCheck() && mLooper != nullptr) {
75 mLooper->removeFd(mReceiver.getFd());
76 }
77 }
78
scheduleVsync()79 status_t DisplayEventDispatcher::scheduleVsync() {
80 if (!mWaitingForVsync) {
81 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
82
83 // 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 }
92
93 status_t status = mReceiver.requestNextVsync();
94 if (status) {
95 ALOGW("Failed to request next vsync, status=%d", status);
96 return status;
97 }
98
99 mWaitingForVsync = true;
100 mLastScheduleVsyncTime = systemTime(SYSTEM_TIME_MONOTONIC);
101 }
102 return OK;
103 }
104
injectEvent(const DisplayEventReceiver::Event & event)105 void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
106 mReceiver.sendEvents(&event, 1);
107 }
108
getFd() const109 int DisplayEventDispatcher::getFd() const {
110 return mReceiver.getFd();
111 }
112
handleEvent(int,int events,void *)113 int 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;
132 VsyncEventData vsyncEventData;
133 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
134 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
135 ", displayId=%s, count=%d, vsyncId=%" PRId64,
136 this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
137 vsyncEventData.preferredVsyncId());
138 mWaitingForVsync = false;
139 mLastVsyncCount = vsyncCount;
140 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
141 }
142
143 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
154 return 1; // keep the callback
155 }
156
processPendingEvents(nsecs_t * outTimestamp,PhysicalDisplayId * outDisplayId,uint32_t * outCount,VsyncEventData * outVsyncEventData)157 bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
158 PhysicalDisplayId* outDisplayId,
159 uint32_t* outCount,
160 VsyncEventData* outVsyncEventData) {
161 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));
166 mFrameRateOverrides.reserve(n);
167 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;
177 *outVsyncEventData = ev.vsync.vsyncData;
178
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 }
185 break;
186 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
187 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 }
194 break;
195 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
196 dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
197 ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
198 break;
199 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
200 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
201 break;
202 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;
209 case DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE:
210 dispatchHdcpLevelsChanged(ev.header.displayId,
211 ev.hdcpLevelsChange.connectedLevel,
212 ev.hdcpLevelsChange.maxLevel);
213 break;
214 default:
215 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
216 break;
217 }
218 }
219 }
220 if (n < 0) {
221 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
222 }
223 return gotVsync;
224 }
225
getLatestVsyncEventData(ParcelableVsyncEventData * outVsyncEventData) const226 status_t DisplayEventDispatcher::getLatestVsyncEventData(
227 ParcelableVsyncEventData* outVsyncEventData) const {
228 return mReceiver.getLatestVsyncEventData(outVsyncEventData);
229 }
230
231 } // namespace android
232