blob: a02958608861ded206fef0382c5461b6041922ce [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>
Dominik Laskowski1eba0202019-01-24 09:14:40 -080025#include <optional>
Lloyd Pique46a46b32018-01-31 19:01:18 -080026#include <thread>
Chia-I Wu98cd38f2018-09-14 11:53:25 -070027#include <vector>
Lloyd Pique46a46b32018-01-31 19:01:18 -080028
29#include <android-base/thread_annotations.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030
Mathias Agopiancb9732a2012-04-03 17:48:03 -070031#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032#include <gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080033#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034
35#include <utils/Errors.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036
37// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039// ---------------------------------------------------------------------------
40
Ana Krulec85c39af2018-12-26 17:29:57 -080041class EventThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080042class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043class SurfaceFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
45// ---------------------------------------------------------------------------
46
Dominik Laskowskif654d572018-12-20 11:03:06 -080047using ResyncCallback = std::function<void()>;
48
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080049enum class VSyncRequest {
50 None = -1,
51 Single = 0,
52 Periodic = 1,
53 // Subsequent values are periods.
54};
55
Lloyd Piquee83f9312018-02-01 12:53:17 -080056class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070057public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080058 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070059 public:
60 virtual ~Callback() {}
61 virtual void onVSyncEvent(nsecs_t when) = 0;
62 };
63
64 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070065
66 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070067 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080068 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070069 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070070};
71
Ana Krulec85c39af2018-12-26 17:29:57 -080072class EventThreadConnection : public BnDisplayEventConnection {
73public:
Ady Abraham0f4a1b12019-06-04 16:04:04 -070074 EventThreadConnection(EventThread*, ResyncCallback,
75 ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec85c39af2018-12-26 17:29:57 -080076 virtual ~EventThreadConnection();
77
78 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
79
80 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080081 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080082 void requestNextVsync() override; // asynchronous
83
Dominik Laskowskif654d572018-12-20 11:03:06 -080084 // Called in response to requestNextVsync.
85 const ResyncCallback resyncCallback;
86
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080087 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0f4a1b12019-06-04 16:04:04 -070088 const ISurfaceComposer::ConfigChanged configChanged;
Ana Krulec85c39af2018-12-26 17:29:57 -080089
90private:
91 virtual void onFirstRef();
92 EventThread* const mEventThread;
93 gui::BitTube mChannel;
94};
95
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080096class EventThread {
97public:
98 virtual ~EventThread();
99
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700100 virtual sp<EventThreadConnection> createEventConnection(
101 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800102
103 // called before the screen is turned off from main thread
104 virtual void onScreenReleased() = 0;
105
106 // called after the screen is turned on from main thread
107 virtual void onScreenAcquired() = 0;
108
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800109 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800110
Ady Abraham447052e2019-02-13 16:07:27 -0800111 // called when SF changes the active config and apps needs to be notified about the change
112 virtual void onConfigChanged(PhysicalDisplayId displayId, int32_t configId) = 0;
113
Yiwei Zhang5434a782018-12-05 18:06:32 -0800114 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800115
116 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800117
118 virtual status_t registerDisplayEventConnection(
119 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800120 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800121 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700122 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 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)>;
130
Dominik Laskowski6505f792019-09-18 11:10:05 -0700131 EventThread(std::unique_ptr<VSyncSource>, InterceptVSyncsCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800132 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800133
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700134 sp<EventThreadConnection> createEventConnection(
135 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800136
Ana Krulec85c39af2018-12-26 17:29:57 -0800137 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800138 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700139 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800140
Mathias Agopian22ffb112012-04-10 21:04:02 -0700141 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800142 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700143
144 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800145 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800146
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800147 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700148
Ady Abraham447052e2019-02-13 16:07:27 -0800149 void onConfigChanged(PhysicalDisplayId displayId, int32_t configId) override;
150
Yiwei Zhang5434a782018-12-05 18:06:32 -0800151 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800152
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800153 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700154
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800155private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800156 friend EventThreadTest;
157
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800158 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
159
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800160 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700161
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800162 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
163 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
164 void dispatchEvent(const DisplayEventReceiver::Event& event,
165 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700166
Ana Krulec85c39af2018-12-26 17:29:57 -0800167 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
168 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800169
Lloyd Pique46a46b32018-01-31 19:01:18 -0800170 // Implements VSyncSource::Callback
171 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800172
Dominik Laskowski6505f792019-09-18 11:10:05 -0700173 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800174
Lloyd Pique24b0a482018-03-09 18:52:26 -0800175 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800176 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800177
Lloyd Pique46a46b32018-01-31 19:01:18 -0800178 std::thread mThread;
179 mutable std::mutex mMutex;
180 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800181
Ana Krulec85c39af2018-12-26 17:29:57 -0800182 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800183 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800184
185 // VSYNC state of connected display.
186 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800187 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800188
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800189 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800190
191 // Number of VSYNC events since display was connected.
192 uint32_t count = 0;
193
194 // True if VSYNC should be faked, e.g. when display is off.
195 bool synthetic = false;
196 };
197
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800198 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
199 // and support headless mode by injecting a fake display with synthetic VSYNC.
200 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800201
Dominik Laskowski029cc122019-01-23 19:52:06 -0800202 // State machine for event loop.
203 enum class State {
204 Idle,
205 Quit,
206 SyntheticVSync,
207 VSync,
208 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700209
Dominik Laskowski029cc122019-01-23 19:52:06 -0800210 State mState GUARDED_BY(mMutex) = State::Idle;
211
212 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800213};
214
215// ---------------------------------------------------------------------------
216
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800217} // namespace impl
218} // namespace android