blob: 80bd6066366b8313f35138d35e54c3c38c7c921d [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
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070044namespace frametimeline {
45class TokenManager;
46} // namespace frametimeline
47
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048// ---------------------------------------------------------------------------
49
Dominik Laskowskif654d572018-12-20 11:03:06 -080050using ResyncCallback = std::function<void()>;
51
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080052enum class VSyncRequest {
53 None = -1,
54 Single = 0,
55 Periodic = 1,
56 // Subsequent values are periods.
57};
58
Lloyd Piquee83f9312018-02-01 12:53:17 -080059class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070060public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080061 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062 public:
63 virtual ~Callback() {}
Ady Abraham9c53ee72020-07-22 21:16:18 -070064 virtual void onVSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp,
65 nsecs_t deadlineTimestamp) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066 };
67
68 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070069
70 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070071 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080072 virtual void setCallback(Callback* callback) = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -070073 virtual void setDuration(std::chrono::nanoseconds workDuration,
74 std::chrono::nanoseconds readyDuration) = 0;
Ady Abraham5e7371c2020-03-24 14:47:24 -070075
76 virtual void dump(std::string& result) const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070077};
78
Ana Krulec85c39af2018-12-26 17:29:57 -080079class EventThreadConnection : public BnDisplayEventConnection {
80public:
Ady Abraham0f4a1b12019-06-04 16:04:04 -070081 EventThreadConnection(EventThread*, ResyncCallback,
82 ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec85c39af2018-12-26 17:29:57 -080083 virtual ~EventThreadConnection();
84
85 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
86
87 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080088 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080089 void requestNextVsync() override; // asynchronous
90
Dominik Laskowskif654d572018-12-20 11:03:06 -080091 // Called in response to requestNextVsync.
92 const ResyncCallback resyncCallback;
93
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080094 VSyncRequest vsyncRequest = VSyncRequest::None;
Alec Mouri967d5d72020-08-05 12:50:03 -070095 const ISurfaceComposer::ConfigChanged mConfigChanged =
Alec Mouri60aee1c2019-10-28 16:18:59 -070096 ISurfaceComposer::ConfigChanged::eConfigChangedSuppress;
Ana Krulec85c39af2018-12-26 17:29:57 -080097
98private:
99 virtual void onFirstRef();
100 EventThread* const mEventThread;
101 gui::BitTube mChannel;
102};
103
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800104class EventThread {
105public:
106 virtual ~EventThread();
107
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700108 virtual sp<EventThreadConnection> createEventConnection(
109 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800110
111 // called before the screen is turned off from main thread
112 virtual void onScreenReleased() = 0;
113
114 // called after the screen is turned on from main thread
115 virtual void onScreenAcquired() = 0;
116
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800117 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800118
Ady Abraham447052e2019-02-13 16:07:27 -0800119 // called when SF changes the active config and apps needs to be notified about the change
Alec Mouri60aee1c2019-10-28 16:18:59 -0700120 virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
121 nsecs_t vsyncPeriod) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800122
Yiwei Zhang5434a782018-12-05 18:06:32 -0800123 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800124
Ady Abraham9c53ee72020-07-22 21:16:18 -0700125 virtual void setDuration(std::chrono::nanoseconds workDuration,
126 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800127
128 virtual status_t registerDisplayEventConnection(
129 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800130 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800131 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700132 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800133
134 // Retrieves the number of event connections tracked by this EventThread.
135 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800136};
137
138namespace impl {
139
140class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800141public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800142 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
143
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700144 EventThread(std::unique_ptr<VSyncSource>, frametimeline::TokenManager*,
145 InterceptVSyncsCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800146 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800147
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700148 sp<EventThreadConnection> createEventConnection(
149 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800150
Ana Krulec85c39af2018-12-26 17:29:57 -0800151 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800152 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700153 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800154
Mathias Agopian22ffb112012-04-10 21:04:02 -0700155 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800156 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700157
158 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800159 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800160
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800161 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700162
Alec Mouri60aee1c2019-10-28 16:18:59 -0700163 void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
164 nsecs_t vsyncPeriod) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800165
Yiwei Zhang5434a782018-12-05 18:06:32 -0800166 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800167
Ady Abraham9c53ee72020-07-22 21:16:18 -0700168 void setDuration(std::chrono::nanoseconds workDuration,
169 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700170
Alec Mouri717bcb62020-02-10 17:07:19 -0800171 size_t getEventThreadConnectionCount() override;
172
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800173private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800174 friend EventThreadTest;
175
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800176 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
177
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800178 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700179
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800180 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
181 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
182 void dispatchEvent(const DisplayEventReceiver::Event& event,
183 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700184
Ana Krulec85c39af2018-12-26 17:29:57 -0800185 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
186 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800187
Lloyd Pique46a46b32018-01-31 19:01:18 -0800188 // Implements VSyncSource::Callback
Ady Abraham9c53ee72020-07-22 21:16:18 -0700189 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
190 nsecs_t deadlineTimestamp) 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);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700193 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800194
Lloyd Pique24b0a482018-03-09 18:52:26 -0800195 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800196 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800197
Lloyd Pique46a46b32018-01-31 19:01:18 -0800198 std::thread mThread;
199 mutable std::mutex mMutex;
200 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800201
Ana Krulec85c39af2018-12-26 17:29:57 -0800202 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800203 std::deque<DisplayEventReceiver::Event> mPendingEvents 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