blob: 1a8ebb79030020a72081b5763e20e0d42ffb13fa [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
Lloyd Pique46a46b32018-01-31 19:01:18 -080017#pragma once
Mathias Agopiand0566bc2011-11-17 17:49:17 -080018
Mathias Agopiand0566bc2011-11-17 17:49:17 -080019#include <sys/types.h>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070020
Lloyd Pique46a46b32018-01-31 19:01:18 -080021#include <condition_variable>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070022#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080023#include <deque>
Lloyd Pique46a46b32018-01-31 19:01:18 -080024#include <mutex>
25#include <thread>
Chia-I Wu98cd38f2018-09-14 11:53:25 -070026#include <vector>
Lloyd Pique46a46b32018-01-31 19:01:18 -080027
28#include <android-base/thread_annotations.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029
Mathias Agopiancb9732a2012-04-03 17:48:03 -070030#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031#include <gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080032#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033
34#include <utils/Errors.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035
36// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038// ---------------------------------------------------------------------------
39
Ana Krulec85c39af2018-12-26 17:29:57 -080040class EventThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080041class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042class SurfaceFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
44// ---------------------------------------------------------------------------
45
Dominik Laskowskif654d572018-12-20 11:03:06 -080046using ResyncCallback = std::function<void()>;
47
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080048enum class VSyncRequest {
49 None = -1,
50 Single = 0,
51 Periodic = 1,
52 // Subsequent values are periods.
53};
54
Lloyd Piquee83f9312018-02-01 12:53:17 -080055class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070056public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080057 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070058 public:
59 virtual ~Callback() {}
60 virtual void onVSyncEvent(nsecs_t when) = 0;
61 };
62
63 virtual ~VSyncSource() {}
64 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080065 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070066 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070067};
68
Ana Krulec85c39af2018-12-26 17:29:57 -080069class EventThreadConnection : public BnDisplayEventConnection {
70public:
Dominik Laskowskif654d572018-12-20 11:03:06 -080071 EventThreadConnection(EventThread* eventThread, ResyncCallback resyncCallback);
Ana Krulec85c39af2018-12-26 17:29:57 -080072 virtual ~EventThreadConnection();
73
74 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
75
76 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080077 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080078 void requestNextVsync() override; // asynchronous
Ana Krulec7d1d6832018-12-27 11:10:09 -080079 // Requesting Vsync for HWC does not reset the idle timer, since HWC requires a refresh
80 // in order to update the configs.
81 void requestNextVsyncForHWC();
Ana Krulec85c39af2018-12-26 17:29:57 -080082
Dominik Laskowskif654d572018-12-20 11:03:06 -080083 // Called in response to requestNextVsync.
84 const ResyncCallback resyncCallback;
85
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080086 VSyncRequest vsyncRequest = VSyncRequest::None;
Ana Krulec85c39af2018-12-26 17:29:57 -080087
88private:
89 virtual void onFirstRef();
90 EventThread* const mEventThread;
91 gui::BitTube mChannel;
92};
93
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080094class EventThread {
95public:
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070096 // TODO: Remove once stable display IDs are plumbed through SF/WM interface.
97 enum class DisplayType { Primary, External };
98
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080099 virtual ~EventThread();
100
Dominik Laskowskif654d572018-12-20 11:03:06 -0800101 virtual sp<EventThreadConnection> createEventConnection(
102 ResyncCallback resyncCallback) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800103
104 // called before the screen is turned off from main thread
105 virtual void onScreenReleased() = 0;
106
107 // called after the screen is turned on from main thread
108 virtual void onScreenAcquired() = 0;
109
110 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700111 virtual void onHotplugReceived(DisplayType displayType, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800112
Yiwei Zhang5434a782018-12-05 18:06:32 -0800113 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800114
115 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800116
117 virtual status_t registerDisplayEventConnection(
118 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800119 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800120 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
121 virtual void requestNextVsync(const sp<EventThreadConnection>& connection,
122 bool resetIdleTimer) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800123};
124
125namespace impl {
126
127class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800128public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800129 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
Ana Krulecfb772822018-11-30 10:44:07 +0100130 using ResetIdleTimerCallback = std::function<void()>;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800131
Ana Krulec98b5b242018-08-10 15:03:23 -0700132 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
Dominik Laskowskif654d572018-12-20 11:03:06 -0800133 EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
134 const char* threadName);
Ana Krulec98b5b242018-08-10 15:03:23 -0700135 EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +0100136 const InterceptVSyncsCallback& interceptVSyncsCallback,
137 const ResetIdleTimerCallback& resetIdleTimerCallback, const char* threadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800138 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800139
Dominik Laskowskif654d572018-12-20 11:03:06 -0800140 sp<EventThreadConnection> createEventConnection(ResyncCallback resyncCallback) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800141
Ana Krulec85c39af2018-12-26 17:29:57 -0800142 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800143 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800144 void requestNextVsync(const sp<EventThreadConnection>& connection,
145 bool resetIdleTimer) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800146
Mathias Agopian22ffb112012-04-10 21:04:02 -0700147 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800148 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700149
150 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800151 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800152
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700154 void onHotplugReceived(DisplayType displayType, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700155
Yiwei Zhang5434a782018-12-05 18:06:32 -0800156 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800157
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800158 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700159
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800160private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800161 friend EventThreadTest;
162
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800163 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
164
Ana Krulec98b5b242018-08-10 15:03:23 -0700165 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
166 EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700167 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
168
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800169 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700170
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800171 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
172 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
173 void dispatchEvent(const DisplayEventReceiver::Event& event,
174 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700175
Ana Krulec85c39af2018-12-26 17:29:57 -0800176 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
177 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800178
Lloyd Pique46a46b32018-01-31 19:01:18 -0800179 void enableVSyncLocked() REQUIRES(mMutex);
180 void disableVSyncLocked() REQUIRES(mMutex);
181
182 // Implements VSyncSource::Callback
183 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800184
Ana Krulec7d1d6832018-12-27 11:10:09 -0800185 // Acquires mutex and requests next vsync.
186 void requestNextVsyncInternal(const sp<EventThreadConnection>& connection) EXCLUDES(mMutex);
187
Ana Krulec98b5b242018-08-10 15:03:23 -0700188 // TODO(b/113612090): Once the Scheduler is complete this pointer will become obsolete.
189 VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr;
190 std::unique_ptr<VSyncSource> mVSyncSourceUnique GUARDED_BY(mMutex) = nullptr;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800191
Lloyd Pique24b0a482018-03-09 18:52:26 -0800192 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800193 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800194
Lloyd Pique46a46b32018-01-31 19:01:18 -0800195 std::thread mThread;
196 mutable std::mutex mMutex;
197 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800198
Ana Krulec85c39af2018-12-26 17:29:57 -0800199 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800200 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800201
202 // VSYNC state of connected display.
203 struct VSyncState {
204 uint32_t displayId = 0;
205
206 // Number of VSYNC events since display was connected.
207 uint32_t count = 0;
208
209 // True if VSYNC should be faked, e.g. when display is off.
210 bool synthetic = false;
211 };
212
213 VSyncState mVSyncState GUARDED_BY(mMutex);
214
Lloyd Pique46a46b32018-01-31 19:01:18 -0800215 bool mVsyncEnabled GUARDED_BY(mMutex) = false;
216 bool mKeepRunning GUARDED_BY(mMutex) = true;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700217
218 // for debugging
Lloyd Pique46a46b32018-01-31 19:01:18 -0800219 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false;
Ana Krulecfb772822018-11-30 10:44:07 +0100220
221 // Callback that resets the idle timer when the next vsync is received.
222 ResetIdleTimerCallback mResetIdleTimer;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800223};
224
225// ---------------------------------------------------------------------------
226
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800227} // namespace impl
228} // namespace android