• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
29 namespace 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.
34 static const size_t EVENT_BUFFER_SIZE = 100;
35 
DisplayEventDispatcher(const sp<Looper> & looper,ISurfaceComposer::VsyncSource vsyncSource,ISurfaceComposer::ConfigChanged configChanged)36 DisplayEventDispatcher::DisplayEventDispatcher(const sp<Looper>& looper,
37                                                ISurfaceComposer::VsyncSource vsyncSource,
38                                                ISurfaceComposer::ConfigChanged configChanged)
39       : mLooper(looper), mReceiver(vsyncSource, configChanged), mWaitingForVsync(false) {
40     ALOGV("dispatcher %p ~ Initializing display event dispatcher.", this);
41 }
42 
initialize()43 status_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     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         }
55     }
56 
57     return OK;
58 }
59 
dispose()60 void DisplayEventDispatcher::dispose() {
61     ALOGV("dispatcher %p ~ Disposing display event dispatcher.", this);
62 
63     if (!mReceiver.initCheck() && mLooper != nullptr) {
64         mLooper->removeFd(mReceiver.getFd());
65     }
66 }
67 
scheduleVsync()68 status_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 
injectEvent(const DisplayEventReceiver::Event & event)92 void DisplayEventDispatcher::injectEvent(const DisplayEventReceiver::Event& event) {
93     mReceiver.sendEvents(&event, 1);
94 }
95 
getFd() const96 int DisplayEventDispatcher::getFd() const {
97     return mReceiver.getFd();
98 }
99 
handleEvent(int,int events,void *)100 int 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)) {
120         ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
121               ", displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%d",
122               this, ns2ms(vsyncTimestamp), vsyncDisplayId, vsyncCount);
123         mWaitingForVsync = false;
124         dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount);
125     }
126 
127     return 1; // keep the callback
128 }
129 
processPendingEvents(nsecs_t * outTimestamp,PhysicalDisplayId * outDisplayId,uint32_t * outCount)130 bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
131                                                   PhysicalDisplayId* outDisplayId,
132                                                   uint32_t* outCount) {
133     bool gotVsync = false;
134     DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
135     ssize_t n;
136     while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
137         ALOGV("dispatcher %p ~ Read %d events.", this, int(n));
138         for (ssize_t i = 0; i < n; i++) {
139             const DisplayEventReceiver::Event& ev = buf[i];
140             switch (ev.header.type) {
141                 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
142                     // Later vsync events will just overwrite the info from earlier
143                     // ones. That's fine, we only care about the most recent.
144                     gotVsync = true;
145                     *outTimestamp = ev.header.timestamp;
146                     *outDisplayId = ev.header.displayId;
147                     *outCount = ev.vsync.count;
148                     break;
149                 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
150                     dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
151                     break;
152                 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
153                     dispatchConfigChanged(ev.header.timestamp, ev.header.displayId,
154                                           ev.config.configId, ev.config.vsyncPeriod);
155                     break;
156                 case DisplayEventReceiver::DISPLAY_EVENT_NULL:
157                     dispatchNullEvent(ev.header.timestamp, ev.header.displayId);
158                     break;
159                 default:
160                     ALOGW("dispatcher %p ~ ignoring unknown event type %#x", this, ev.header.type);
161                     break;
162             }
163         }
164     }
165     if (n < 0) {
166         ALOGW("Failed to get events from display event dispatcher, status=%d", status_t(n));
167     }
168     return gotVsync;
169 }
170 
171 } // namespace android
172