blob: 2cc7c34c4007d73467ff288a8491895e6f111145 [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
36DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper,
37 ISurfaceComposer::VsyncSource vsyncSource,
38 ISurfaceComposer::ConfigChanged configChanged)
Alec Mouri271de042020-04-27 22:38:19 -070039 : mLooper(looper), mReceiver(vsyncSource, configChanged), 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() {
69 if (!mWaitingForVsync) {
70 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
71
72 // Drain all pending events.
73 nsecs_t vsyncTimestamp;
74 PhysicalDisplayId vsyncDisplayId;
75 uint32_t vsyncCount;
76 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount)) {
77 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "", this,
78 ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
79 }
80
81 status_t status = mReceiver.requestNextVsync();
82 if (status) {
83 ALOGW("Failed to request next vsync, status=%d", status);
84 return status;
85 }
86
87 mWaitingForVsync = true;
88 }
89 return OK;
90}
91
Alec Mouri967d5d72020-08-05 12:50:03 -070092void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
93 mReceiver.sendEvents(&event, 1);
Alec Mouri60aee1c2019-10-28 16:18:59 -070094}
95
Alec Mouri921b2772020-02-05 19:03:28 -080096int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -080097 return mReceiver.getFd();
98}
99
Alec Mouri77a53872019-10-28 16:44:36 -0700100int DisplayEventDispatcher::handleEvent(int, int events, void*) {
101 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
102 ALOGE("Display event receiver pipe was closed or an error occurred. "
103 "events=0x%x",
104 events);
105 return 0; // remove the callback
106 }
107
108 if (!(events & Looper::EVENT_INPUT)) {
109 ALOGW("Received spurious callback for unhandled poll event. "
110 "events=0x%x",
111 events);
112 return 1; // keep the callback
113 }
114
115 // Drain all pending events, keep the last vsync.
116 nsecs_t vsyncTimestamp;
117 PhysicalDisplayId vsyncDisplayId;
118 uint32_t vsyncCount;
119 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount)) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200120 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64 ", displayId=%s, count=%d", this,
121 ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount);
Alec Mouri77a53872019-10-28 16:44:36 -0700122 mWaitingForVsync = false;
123 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount);
124 }
125
126 return 1; // keep the callback
127}
128
129bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
130 PhysicalDisplayId* outDisplayId,
131 uint32_t* outCount) {
132 bool gotVsync = false;
133 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
134 ssize_t n;
135 while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
136 ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
137 for (ssize_t i = 0; i < n; i++) {
138 const DisplayEventReceiver::Event& ev = buf[i];
139 switch (ev.header.type) {
140 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
141 // Later vsync events will just overwrite the info from earlier
142 // ones. That's fine, we only care about the most recent.
143 gotVsync = true;
144 *outTimestamp = ev.header.timestamp;
145 *outDisplayId = ev.header.displayId;
146 *outCount = ev.vsync.count;
147 break;
148 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
149 dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
150 break;
151 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
152 dispatchConfigChanged(ev.header.timestamp, ev.header.displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700153 ev.config.configId, ev.config.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700154 break;
Alec Mouri967d5d72020-08-05 12:50:03 -0700155 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
156 dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
157 break;
Alec Mouri77a53872019-10-28 16:44:36 -0700158 default:
159 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
160 break;
161 }
162 }
163 }
164 if (n < 0) {
165 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
166 }
167 return gotVsync;
168}
Alec Mouri967d5d72020-08-05 12:50:03 -0700169
Alec Mouri77a53872019-10-28 16:44:36 -0700170} // namespace android