blob: b33bc9e55679af18d6327d552ee481f722d5fb8f [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 Mouri271de042020-04-27 22:38:19 -070092void DisplayEventDispatcher::requestLatestConfig() {
93 status_t status = mReceiver.requestLatestConfig();
Alec Mouri60aee1c2019-10-28 16:18:59 -070094 if (status) {
95 ALOGW("Failed enable config events, status=%d", status);
96 return;
97 }
Alec Mouri60aee1c2019-10-28 16:18:59 -070098}
99
Alec Mouri921b2772020-02-05 19:03:28 -0800100int DisplayEventDispatcher::getFd() const {
Alec Mouri50a931d2019-11-19 16:23:59 -0800101 return mReceiver.getFd();
102}
103
Alec Mouri77a53872019-10-28 16:44:36 -0700104int DisplayEventDispatcher::handleEvent(int, int events, void*) {
105 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
106 ALOGE("Display event receiver pipe was closed or an error occurred. "
107 "events=0x%x",
108 events);
109 return 0; // remove the callback
110 }
111
112 if (!(events & Looper::EVENT_INPUT)) {
113 ALOGW("Received spurious callback for unhandled poll event. "
114 "events=0x%x",
115 events);
116 return 1; // keep the callback
117 }
118
119 // Drain all pending events, keep the last vsync.
120 nsecs_t vsyncTimestamp;
121 PhysicalDisplayId vsyncDisplayId;
122 uint32_t vsyncCount;
123 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount)) {
124 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
125 ", displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%d",
126 this, ns2ms(vsyncTimestamp), vsyncDisplayId, vsyncCount);
127 mWaitingForVsync = false;
128 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount);
129 }
130
131 return 1; // keep the callback
132}
133
134bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
135 PhysicalDisplayId* outDisplayId,
136 uint32_t* outCount) {
137 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));
142 for (ssize_t i = 0; i < n; i++) {
143 const DisplayEventReceiver::Event& ev = buf[i];
144 switch (ev.header.type) {
145 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
146 // Later vsync events will just overwrite the info from earlier
147 // ones. That's fine, we only care about the most recent.
148 gotVsync = true;
149 *outTimestamp = ev.header.timestamp;
150 *outDisplayId = ev.header.displayId;
151 *outCount = ev.vsync.count;
152 break;
153 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
154 dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
155 break;
156 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
157 dispatchConfigChanged(ev.header.timestamp, ev.header.displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700158 ev.config.configId, ev.config.vsyncPeriod);
Alec Mouri77a53872019-10-28 16:44:36 -0700159 break;
160 default:
161 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
162 break;
163 }
164 }
165 }
166 if (n < 0) {
167 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
168 }
169 return gotVsync;
170}
171} // namespace android