blob: 1f08f4e1fc3fe59b6fbe0aa940edcf3f4147e193 [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
2 * Copyright (C) 2011 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
Mathias Agopian841cde52012-03-01 15:44:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Lloyd Pique46a46b32018-01-31 19:01:18 -080019#include <pthread.h>
20#include <sched.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080021#include <sys/types.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080022#include <chrono>
23#include <cstdint>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080024
Yiwei Zhang5434a782018-12-05 18:06:32 -080025#include <android-base/stringprintf.h>
26
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070027#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080028#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070029
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030#include <gui/DisplayEventReceiver.h>
31
32#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080033#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035#include "EventThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036
Lloyd Pique46a46b32018-01-31 19:01:18 -080037using namespace std::chrono_literals;
Yiwei Zhang5434a782018-12-05 18:06:32 -080038using android::base::StringAppendF;
Lloyd Pique46a46b32018-01-31 19:01:18 -080039
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040// ---------------------------------------------------------------------------
41
Lloyd Pique46a46b32018-01-31 19:01:18 -080042namespace android {
43
44// ---------------------------------------------------------------------------
45
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080046EventThread::~EventThread() = default;
47
48namespace impl {
49
Ana Krulec98b5b242018-08-10 15:03:23 -070050EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +010051 const ResyncWithRateLimitCallback& resyncWithRateLimitCallback,
52 const InterceptVSyncsCallback& interceptVSyncsCallback,
53 const ResetIdleTimerCallback& resetIdleTimerCallback,
54 const char* threadName)
Ana Krulec98b5b242018-08-10 15:03:23 -070055 : EventThread(nullptr, std::move(src), resyncWithRateLimitCallback, interceptVSyncsCallback,
Ana Krulecfb772822018-11-30 10:44:07 +010056 threadName) {
57 mResetIdleTimer = resetIdleTimerCallback;
58}
Ana Krulec98b5b242018-08-10 15:03:23 -070059
Lloyd Pique24b0a482018-03-09 18:52:26 -080060EventThread::EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback,
61 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Ana Krulec98b5b242018-08-10 15:03:23 -070062 : EventThread(src, nullptr, resyncWithRateLimitCallback, interceptVSyncsCallback,
63 threadName) {}
64
65EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
66 ResyncWithRateLimitCallback resyncWithRateLimitCallback,
67 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -080068 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -070069 mVSyncSourceUnique(std::move(uniqueSrc)),
Lloyd Pique24b0a482018-03-09 18:52:26 -080070 mResyncWithRateLimitCallback(resyncWithRateLimitCallback),
71 mInterceptVSyncsCallback(interceptVSyncsCallback) {
Ana Krulec98b5b242018-08-10 15:03:23 -070072 if (src == nullptr) {
73 mVSyncSource = mVSyncSourceUnique.get();
74 }
Lloyd Pique46a46b32018-01-31 19:01:18 -080075 for (auto& event : mVSyncEvent) {
76 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
77 event.header.id = 0;
78 event.header.timestamp = 0;
79 event.vsync.count = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -070080 }
Lloyd Pique46a46b32018-01-31 19:01:18 -080081
82 mThread = std::thread(&EventThread::threadMain, this);
83
84 pthread_setname_np(mThread.native_handle(), threadName);
85
86 pid_t tid = pthread_gettid_np(mThread.native_handle());
87
88 // Use SCHED_FIFO to minimize jitter
89 constexpr int EVENT_THREAD_PRIORITY = 2;
90 struct sched_param param = {0};
91 param.sched_priority = EVENT_THREAD_PRIORITY;
92 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
93 ALOGE("Couldn't set SCHED_FIFO for EventThread");
94 }
95
96 set_sched_policy(tid, SP_FOREGROUND);
97}
98
99EventThread::~EventThread() {
100 {
101 std::lock_guard<std::mutex> lock(mMutex);
102 mKeepRunning = false;
103 mCondition.notify_all();
104 }
105 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700106}
107
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700108void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800109 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700110 mVSyncSource->setPhaseOffset(phaseOffset);
111}
112
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800113sp<BnDisplayEventConnection> EventThread::createEventConnection() const {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700114 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800115}
116
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700118 const sp<EventThread::Connection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800119 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700120
121 // this should never happen
122 auto it = std::find(mDisplayEventConnections.cbegin(),
123 mDisplayEventConnections.cend(), connection);
124 if (it != mDisplayEventConnections.cend()) {
125 ALOGW("DisplayEventConnection %p already exists", connection.get());
126 mCondition.notify_all();
127 return ALREADY_EXISTS;
128 }
129
130 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800131 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800132 return NO_ERROR;
133}
134
Ana Krulecfefcb582018-08-07 14:22:37 -0700135void EventThread::removeDisplayEventConnectionLocked(
136 const wp<EventThread::Connection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700137 auto it = std::find(mDisplayEventConnections.cbegin(),
138 mDisplayEventConnections.cend(), connection);
139 if (it != mDisplayEventConnections.cend()) {
140 mDisplayEventConnections.erase(it);
141 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800142}
143
Lloyd Pique78ce4182018-01-31 16:39:51 -0800144void EventThread::setVsyncRate(uint32_t count, const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800145 if (int32_t(count) >= 0) { // server must protect against bad params
Lloyd Pique46a46b32018-01-31 19:01:18 -0800146 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700147 const int32_t new_count = (count == 0) ? -1 : count;
148 if (connection->count != new_count) {
149 connection->count = new_count;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800150 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800151 }
152 }
153}
154
Lloyd Pique78ce4182018-01-31 16:39:51 -0800155void EventThread::requestNextVsync(const sp<EventThread::Connection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800156 std::lock_guard<std::mutex> lock(mMutex);
Ana Krulecfb772822018-11-30 10:44:07 +0100157 if (mResetIdleTimer) {
158 mResetIdleTimer();
159 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000160
Lloyd Pique24b0a482018-03-09 18:52:26 -0800161 if (mResyncWithRateLimitCallback) {
162 mResyncWithRateLimitCallback();
163 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000164
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700165 if (connection->count < 0) {
166 connection->count = 0;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800167 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800168 }
Mathias Agopian23748662011-12-05 14:33:34 -0800169}
170
Mathias Agopian22ffb112012-04-10 21:04:02 -0700171void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800172 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700173 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700174 // disable reliance on h/w vsync
175 mUseSoftwareVSync = true;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800176 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700177 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700178}
179
180void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800181 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian7d886472012-06-14 23:39:35 -0700182 if (mUseSoftwareVSync) {
183 // resume use of h/w vsync
184 mUseSoftwareVSync = false;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800185 mCondition.notify_all();
Mathias Agopian7d886472012-06-14 23:39:35 -0700186 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700187}
188
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700189void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800190 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700191 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
192 mVSyncEvent[0].header.id = 0;
193 mVSyncEvent[0].header.timestamp = timestamp;
194 mVSyncEvent[0].vsync.count++;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800195 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700196}
197
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700198void EventThread::onHotplugReceived(DisplayType displayType, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800199 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700200
201 DisplayEventReceiver::Event event;
202 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
203 event.header.id = displayType == DisplayType::Primary ? 0 : 1;
204 event.header.timestamp = systemTime();
205 event.hotplug.connected = connected;
206
Chia-I Wueac3db22018-09-14 11:28:03 -0700207 mPendingEvents.push(event);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700208 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700209}
210
Lloyd Pique46a46b32018-01-31 19:01:18 -0800211void EventThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
212 std::unique_lock<std::mutex> lock(mMutex);
213 while (mKeepRunning) {
214 DisplayEventReceiver::Event event;
Chia-I Wub02d51c2018-09-14 11:20:48 -0700215 std::vector<sp<EventThread::Connection>> signalConnections;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800216 signalConnections = waitForEventLocked(&lock, &event);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700217
Lloyd Pique46a46b32018-01-31 19:01:18 -0800218 // dispatch events to listeners...
Chia-I Wub02d51c2018-09-14 11:20:48 -0700219 for (const sp<Connection>& conn : signalConnections) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800220 // now see if we still need to report this event
221 status_t err = conn->postEvent(event);
222 if (err == -EAGAIN || err == -EWOULDBLOCK) {
223 // The destination doesn't accept events anymore, it's probably
224 // full. For now, we just drop the events on the floor.
225 // FIXME: Note that some events cannot be dropped and would have
226 // to be re-sent later.
227 // Right-now we don't have the ability to do this.
228 ALOGW("EventThread: dropping event (%08x) for connection %p", event.header.type,
229 conn.get());
230 } else if (err < 0) {
231 // handle any other error on the pipe as fatal. the only
232 // reasonable thing to do is to clean-up this connection.
233 // The most common error we'll get here is -EPIPE.
Chia-I Wub02d51c2018-09-14 11:20:48 -0700234 removeDisplayEventConnectionLocked(conn);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800235 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800236 }
237 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800238}
239
Andy McFadden6bf552e2012-08-30 16:34:41 -0700240// This will return when (1) a vsync event has been received, and (2) there was
241// at least one connection interested in receiving it when we started waiting.
Chia-I Wub02d51c2018-09-14 11:20:48 -0700242std::vector<sp<EventThread::Connection>> EventThread::waitForEventLocked(
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700243 std::unique_lock<std::mutex>* lock, DisplayEventReceiver::Event* outEvent) {
Chia-I Wub02d51c2018-09-14 11:20:48 -0700244 std::vector<sp<EventThread::Connection>> signalConnections;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700245
Chia-I Wub02d51c2018-09-14 11:20:48 -0700246 while (signalConnections.empty() && mKeepRunning) {
Mathias Agopian148994e2012-09-19 17:31:36 -0700247 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700248 bool waitForVSync = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700249
Mathias Agopianff28e202012-09-20 23:24:19 -0700250 size_t vsyncCount = 0;
251 nsecs_t timestamp = 0;
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700252 for (auto& event : mVSyncEvent) {
253 timestamp = event.header.timestamp;
Mathias Agopianff28e202012-09-20 23:24:19 -0700254 if (timestamp) {
255 // we have a vsync event to dispatch
Lloyd Pique24b0a482018-03-09 18:52:26 -0800256 if (mInterceptVSyncsCallback) {
257 mInterceptVSyncsCallback(timestamp);
Irvelab046852016-07-28 11:23:08 -0700258 }
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700259 *outEvent = event;
260 event.header.timestamp = 0;
261 vsyncCount = event.vsync.count;
Mathias Agopianff28e202012-09-20 23:24:19 -0700262 break;
263 }
264 }
265
266 if (!timestamp) {
267 // no vsync event, see if there are some other event
Chia-I Wueac3db22018-09-14 11:28:03 -0700268 eventPending = !mPendingEvents.empty();
Mathias Agopian148994e2012-09-19 17:31:36 -0700269 if (eventPending) {
270 // we have some other event to dispatch
Chia-I Wueac3db22018-09-14 11:28:03 -0700271 *outEvent = mPendingEvents.front();
272 mPendingEvents.pop();
Mathias Agopian148994e2012-09-19 17:31:36 -0700273 }
274 }
275
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700276 // find out connections waiting for events
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700277 auto it = mDisplayEventConnections.begin();
278 while (it != mDisplayEventConnections.end()) {
279 sp<Connection> connection(it->promote());
Peiyong Lin566a3b42018-01-09 18:22:43 -0800280 if (connection != nullptr) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700281 bool added = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700282 if (connection->count >= 0) {
283 // we need vsync events because at least
284 // one connection is waiting for it
285 waitForVSync = true;
286 if (timestamp) {
287 // we consume the event only if it's time
288 // (ie: we received a vsync event)
289 if (connection->count == 0) {
290 // fired this time around
291 connection->count = -1;
Chia-I Wub02d51c2018-09-14 11:20:48 -0700292 signalConnections.push_back(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700293 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700294 } else if (connection->count == 1 ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800295 (vsyncCount % connection->count) == 0) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700296 // continuous event, and time to report it
Chia-I Wub02d51c2018-09-14 11:20:48 -0700297 signalConnections.push_back(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700298 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700299 }
300 }
301 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700302
303 if (eventPending && !timestamp && !added) {
304 // we don't have a vsync event to process
305 // (timestamp==0), but we have some pending
306 // messages.
Chia-I Wub02d51c2018-09-14 11:20:48 -0700307 signalConnections.push_back(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700308 }
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700309 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700310 } else {
311 // we couldn't promote this reference, the connection has
312 // died, so clean-up!
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700313 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700314 }
315 }
316
317 // Here we figure out if we need to enable or disable vsyncs
318 if (timestamp && !waitForVSync) {
319 // we received a VSYNC but we have no clients
320 // don't report it, and disable VSYNC events
321 disableVSyncLocked();
322 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700323 // we have at least one client, so we want vsync enabled
324 // (TODO: this function is called right after we finish
325 // notifying clients of a vsync, so this call will be made
326 // at the vsync rate, e.g. 60fps. If we can accurately
327 // track the current state we could avoid making this call
328 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700329 enableVSyncLocked();
330 }
331
Andy McFadden6bf552e2012-08-30 16:34:41 -0700332 // note: !timestamp implies signalConnections.isEmpty(), because we
333 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700334 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700335 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700336 if (waitForVSync) {
337 // This is where we spend most of our time, waiting
338 // for vsync events and new client registrations.
339 //
340 // If the screen is off, we can't use h/w vsync, so we
341 // use a 16ms timeout instead. It doesn't need to be
342 // precise, we just need to keep feeding our clients.
343 //
344 // We don't want to stall if there's a driver bug, so we
345 // use a (long) timeout when waiting for h/w vsync, and
346 // generate fake events when necessary.
347 bool softwareSync = mUseSoftwareVSync;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800348 auto timeout = softwareSync ? 16ms : 1000ms;
349 if (mCondition.wait_for(*lock, timeout) == std::cv_status::timeout) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700350 if (!softwareSync) {
351 ALOGW("Timed out waiting for hw vsync; faking it");
352 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700353 // FIXME: how do we decide which display id the fake
354 // vsync came from ?
355 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700356 mVSyncEvent[0].header.id = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -0700357 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
358 mVSyncEvent[0].vsync.count++;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700359 }
360 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700361 // Nobody is interested in vsync, so we just want to sleep.
362 // h/w vsync should be disabled, so this will wait until we
363 // get a new connection, or an existing connection becomes
364 // interested in receiving vsync again.
Lloyd Pique46a46b32018-01-31 19:01:18 -0800365 mCondition.wait(*lock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700366 }
367 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800368 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700369
370 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700371 // (The connections might have dropped out of mDisplayEventConnections
372 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700373 return signalConnections;
374}
375
Mathias Agopian22ffb112012-04-10 21:04:02 -0700376void EventThread::enableVSyncLocked() {
377 if (!mUseSoftwareVSync) {
378 // never enable h/w VSYNC when screen is off
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700379 if (!mVsyncEnabled) {
380 mVsyncEnabled = true;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800381 mVSyncSource->setCallback(this);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 mVSyncSource->setVSyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700383 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700384 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700385 mDebugVsyncEnabled = true;
386}
387
Mathias Agopian22ffb112012-04-10 21:04:02 -0700388void EventThread::disableVSyncLocked() {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700389 if (mVsyncEnabled) {
390 mVsyncEnabled = false;
391 mVSyncSource->setVSyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700392 mDebugVsyncEnabled = false;
393 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700394}
395
Yiwei Zhang5434a782018-12-05 18:06:32 -0800396void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800397 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800398 StringAppendF(&result, "VSYNC state: %s\n", mDebugVsyncEnabled ? "enabled" : "disabled");
399 StringAppendF(&result, " soft-vsync: %s\n", mUseSoftwareVSync ? "enabled" : "disabled");
400 StringAppendF(&result, " numListeners=%zu,\n events-delivered: %u\n",
401 mDisplayEventConnections.size(), mVSyncEvent[0].vsync.count);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700402 for (const wp<Connection>& weak : mDisplayEventConnections) {
403 sp<Connection> connection = weak.promote();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800404 StringAppendF(&result, " %p: count=%d\n", connection.get(),
405 connection != nullptr ? connection->count : 0);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700406 }
Yiwei Zhang5434a782018-12-05 18:06:32 -0800407 StringAppendF(&result, " other-events-pending: %zu\n", mPendingEvents.size());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800408}
409
410// ---------------------------------------------------------------------------
411
Lloyd Piquee83f9312018-02-01 12:53:17 -0800412EventThread::Connection::Connection(EventThread* eventThread)
Lloyd Pique78ce4182018-01-31 16:39:51 -0800413 : count(-1), mEventThread(eventThread), mChannel(gui::BitTube::DefaultSize) {}
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700414
415EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700416 // do nothing here -- clean-up will happen automatically
417 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700418}
419
420void EventThread::Connection::onFirstRef() {
421 // NOTE: mEventThread doesn't hold a strong reference on us
422 mEventThread->registerDisplayEventConnection(this);
423}
424
Dan Stoza6b698e42017-04-03 13:09:08 -0700425status_t EventThread::Connection::stealReceiveChannel(gui::BitTube* outChannel) {
426 outChannel->setReceiveFd(mChannel.moveReceiveFd());
Dan Stozae1c599b2017-03-30 16:37:19 -0700427 return NO_ERROR;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700428}
429
Dan Stozae1c599b2017-03-30 16:37:19 -0700430status_t EventThread::Connection::setVsyncRate(uint32_t count) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700431 mEventThread->setVsyncRate(count, this);
Dan Stozae1c599b2017-03-30 16:37:19 -0700432 return NO_ERROR;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700433}
434
435void EventThread::Connection::requestNextVsync() {
436 mEventThread->requestNextVsync(this);
437}
438
Lloyd Pique78ce4182018-01-31 16:39:51 -0800439status_t EventThread::Connection::postEvent(const DisplayEventReceiver::Event& event) {
Dan Stoza6b698e42017-04-03 13:09:08 -0700440 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700441 return size < 0 ? status_t(size) : status_t(NO_ERROR);
442}
443
444// ---------------------------------------------------------------------------
445
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800446} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800447} // namespace android