blob: d8a3f42690f4f0b76915290c0695da412f4b6ba1 [file] [log] [blame]
Michael Wright3d3fe502015-12-04 17:59:42 +00001/*
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 <androidfw/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
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -070036DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper,
Ady Abraham9c501aa2019-06-04 16:07:44 -070037 ISurfaceComposer::VsyncSource vsyncSource,
38 ISurfaceComposer::ConfigChanged configChanged) :
39 mLooper(looper), mReceiver(vsyncSource, configChanged), mWaitingForVsync(false) {
Michael Wright3d3fe502015-12-04 17:59:42 +000040 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
50 int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT,
51 this, NULL);
52 if (rc < 0) {
53 return UNKNOWN_ERROR;
54 }
55 return OK;
56}
57
58void DisplayEventDispatcher::dispose() {
59 ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
60
61 if (!mReceiver.initCheck()) {
62 mLooper->removeFd(mReceiver.getFd());
63 }
64}
65
66status_t DisplayEventDispatcher::scheduleVsync() {
67 if (!mWaitingForVsync) {
68 ALOGV("dispatcher %p ~ Scheduling vsync.", this);
69
70 // Drain all pending events.
71 nsecs_t vsyncTimestamp;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080072 PhysicalDisplayId vsyncDisplayId;
Michael Wright3d3fe502015-12-04 17:59:42 +000073 uint32_t vsyncCount;
74 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount)) {
75 ALOGE("dispatcher %p ~ last event processed while scheduling was for %" PRId64 "",
76 this, ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));
77 }
78
79 status_t status = mReceiver.requestNextVsync();
80 if (status) {
81 ALOGW("Failed to request next vsync, status=%d", status);
82 return status;
83 }
84
85 mWaitingForVsync = true;
86 }
87 return OK;
88}
89
90int DisplayEventDispatcher::handleEvent(int, int events, void*) {
91 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
92 ALOGE("Display event receiver pipe was closed or an error occurred. "
93 "events=0x%x", events);
94 return 0; // remove the callback
95 }
96
97 if (!(events & Looper::EVENT_INPUT)) {
98 ALOGW("Received spurious callback for unhandled poll event. "
99 "events=0x%x", events);
100 return 1; // keep the callback
101 }
102
103 // Drain all pending events, keep the last vsync.
104 nsecs_t vsyncTimestamp;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800105 PhysicalDisplayId vsyncDisplayId;
Michael Wright3d3fe502015-12-04 17:59:42 +0000106 uint32_t vsyncCount;
107 if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount)) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800108 ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64 ", displayId=%"
109 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%d",
Michael Wright3d3fe502015-12-04 17:59:42 +0000110 this, ns2ms(vsyncTimestamp), vsyncDisplayId, vsyncCount);
111 mWaitingForVsync = false;
112 dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount);
113 }
114
115 return 1; // keep the callback
116}
117
118bool DisplayEventDispatcher::processPendingEvents(
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800119 nsecs_t* outTimestamp, PhysicalDisplayId* outDisplayId, uint32_t* outCount) {
Michael Wright3d3fe502015-12-04 17:59:42 +0000120 bool gotVsync = false;
121 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
122 ssize_t n;
123 while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
124 ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
125 for (ssize_t i = 0; i < n; i++) {
126 const DisplayEventReceiver::Event& ev = buf[i];
127 switch (ev.header.type) {
128 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
129 // Later vsync events will just overwrite the info from earlier
130 // ones. That's fine, we only care about the most recent.
131 gotVsync = true;
132 *outTimestamp = ev.header.timestamp;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800133 *outDisplayId = ev.header.displayId;
Michael Wright3d3fe502015-12-04 17:59:42 +0000134 *outCount = ev.vsync.count;
135 break;
136 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800137 dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
Michael Wright3d3fe502015-12-04 17:59:42 +0000138 break;
Ady Abrahama5a21f72019-02-13 16:41:59 -0800139 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
140 dispatchConfigChanged(ev.header.timestamp, ev.header.displayId, ev.config.configId);
141 break;
Michael Wright3d3fe502015-12-04 17:59:42 +0000142 default:
143 ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
144 break;
145 }
146 }
147 }
148 if (n < 0) {
149 ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
150 }
151 return gotVsync;
152}
153}