blob: 466234d3d731b6ad594abfb2c1ff07a13cfc050d [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
Alec Mouri717bcb62020-02-10 17:07:19 -080019#include <android-base/thread_annotations.h>
20#include <gui/DisplayEventReceiver.h>
21#include <gui/IDisplayEventConnection.h>
22#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080023#include <sys/types.h>
Alec Mouri717bcb62020-02-10 17:07:19 -080024#include <utils/Errors.h>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070025
Lloyd Pique46a46b32018-01-31 19:01:18 -080026#include <condition_variable>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070027#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080028#include <deque>
Lloyd Pique46a46b32018-01-31 19:01:18 -080029#include <mutex>
Dominik Laskowski1eba0202019-01-24 09:14:40 -080030#include <optional>
Lloyd Pique46a46b32018-01-31 19:01:18 -080031#include <thread>
Chia-I Wu98cd38f2018-09-14 11:53:25 -070032#include <vector>
Lloyd Pique46a46b32018-01-31 19:01:18 -080033
Ady Abraham2139f732019-11-13 18:56:40 -080034#include "HwcStrongTypes.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() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070064
65 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080067 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070068 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070069};
70
Ana Krulec85c39af2018-12-26 17:29:57 -080071class EventThreadConnection : public BnDisplayEventConnection {
72public:
Ady Abraham0f4a1b12019-06-04 16:04:04 -070073 EventThreadConnection(EventThread*, ResyncCallback,
74 ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec85c39af2018-12-26 17:29:57 -080075 virtual ~EventThreadConnection();
76
77 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
78
79 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080080 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080081 void requestNextVsync() override; // asynchronous
Alec Mouri60aee1c2019-10-28 16:18:59 -070082 void toggleConfigEvents(ISurfaceComposer::ConfigChanged configChangeFlag) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080083
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;
Alec Mouri60aee1c2019-10-28 16:18:59 -070088 std::atomic<ISurfaceComposer::ConfigChanged> mConfigChanged =
89 ISurfaceComposer::ConfigChanged::eConfigChangedSuppress;
90 // Store whether we need to force dispatching a config change separately -
91 // if mConfigChanged ever changes before the config change is dispatched
92 // then we still need to propagate an initial config to the app if we
93 // haven't already.
94 std::atomic<bool> mForcedConfigChangeDispatch = false;
Ana Krulec85c39af2018-12-26 17:29:57 -080095
96private:
97 virtual void onFirstRef();
98 EventThread* const mEventThread;
99 gui::BitTube mChannel;
100};
101
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800102class EventThread {
103public:
104 virtual ~EventThread();
105
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700106 virtual sp<EventThreadConnection> createEventConnection(
107 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800108
109 // called before the screen is turned off from main thread
110 virtual void onScreenReleased() = 0;
111
112 // called after the screen is turned on from main thread
113 virtual void onScreenAcquired() = 0;
114
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800115 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800116
Ady Abraham447052e2019-02-13 16:07:27 -0800117 // called when SF changes the active config and apps needs to be notified about the change
Alec Mouri60aee1c2019-10-28 16:18:59 -0700118 virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
119 nsecs_t vsyncPeriod) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800120
Yiwei Zhang5434a782018-12-05 18:06:32 -0800121 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800122
123 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800124
125 virtual status_t registerDisplayEventConnection(
126 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800127 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800128 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700129 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700130
131 // Dispatches the most recent configuration
132 // Usage of this method assumes that only the primary internal display
133 // supports multiple display configurations.
134 virtual void requestLatestConfig() = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800135
136 // Retrieves the number of event connections tracked by this EventThread.
137 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800138};
139
140namespace impl {
141
142class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800143public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
145
Dominik Laskowski6505f792019-09-18 11:10:05 -0700146 EventThread(std::unique_ptr<VSyncSource>, InterceptVSyncsCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800147 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800148
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700149 sp<EventThreadConnection> createEventConnection(
150 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800151
Ana Krulec85c39af2018-12-26 17:29:57 -0800152 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800153 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700154 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700155 void requestLatestConfig() override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800156
Mathias Agopian22ffb112012-04-10 21:04:02 -0700157 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800158 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700159
160 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800161 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800162
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800163 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700164
Alec Mouri60aee1c2019-10-28 16:18:59 -0700165 void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
166 nsecs_t vsyncPeriod) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800167
Yiwei Zhang5434a782018-12-05 18:06:32 -0800168 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800169
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800170 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700171
Alec Mouri717bcb62020-02-10 17:07:19 -0800172 size_t getEventThreadConnectionCount() override;
173
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800174private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800175 friend EventThreadTest;
176
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800177 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
178
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800179 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700180
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800181 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
182 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
183 void dispatchEvent(const DisplayEventReceiver::Event& event,
184 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700185
Ana Krulec85c39af2018-12-26 17:29:57 -0800186 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
187 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800188
Lloyd Pique46a46b32018-01-31 19:01:18 -0800189 // Implements VSyncSource::Callback
190 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800191
Dominik Laskowski6505f792019-09-18 11:10:05 -0700192 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800193
Lloyd Pique24b0a482018-03-09 18:52:26 -0800194 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800195 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800196
Lloyd Pique46a46b32018-01-31 19:01:18 -0800197 std::thread mThread;
198 mutable std::mutex mMutex;
199 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800200
Ana Krulec85c39af2018-12-26 17:29:57 -0800201 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800202 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700203 DisplayEventReceiver::Event mLastConfigChangeEvent GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800204
205 // VSYNC state of connected display.
206 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800207 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800208
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800209 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800210
211 // Number of VSYNC events since display was connected.
212 uint32_t count = 0;
213
214 // True if VSYNC should be faked, e.g. when display is off.
215 bool synthetic = false;
216 };
217
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800218 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
219 // and support headless mode by injecting a fake display with synthetic VSYNC.
220 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800221
Dominik Laskowski029cc122019-01-23 19:52:06 -0800222 // State machine for event loop.
223 enum class State {
224 Idle,
225 Quit,
226 SyntheticVSync,
227 VSync,
228 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700229
Dominik Laskowski029cc122019-01-23 19:52:06 -0800230 State mState GUARDED_BY(mMutex) = State::Idle;
231
232 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800233};
234
235// ---------------------------------------------------------------------------
236
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800237} // namespace impl
238} // namespace android