blob: 2e2d98950ec230342b3a995e8e687f77aea2184e [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 {
Tim Murrayb5daa912020-09-09 21:29:13 +000053 None = -2,
54 // Single wakes up for the next two frames to avoid scheduler overhead
55 Single = -1,
56 // SingleSuppressCallback only wakes up for the next frame
57 SingleSuppressCallback = 0,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080058 Periodic = 1,
59 // Subsequent values are periods.
60};
61
Lloyd Piquee83f9312018-02-01 12:53:17 -080062class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080064 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065 public:
66 virtual ~Callback() {}
Ady Abraham9c53ee72020-07-22 21:16:18 -070067 virtual void onVSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp,
68 nsecs_t deadlineTimestamp) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070069 };
70
71 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070072
73 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070074 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080075 virtual void setCallback(Callback* callback) = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -070076 virtual void setDuration(std::chrono::nanoseconds workDuration,
77 std::chrono::nanoseconds readyDuration) = 0;
Ady Abraham5e7371c2020-03-24 14:47:24 -070078
79 virtual void dump(std::string& result) const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070080};
81
Ana Krulec85c39af2018-12-26 17:29:57 -080082class EventThreadConnection : public BnDisplayEventConnection {
83public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070084 EventThreadConnection(EventThread*, uid_t callingUid, ResyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070085 ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec85c39af2018-12-26 17:29:57 -080086 virtual ~EventThreadConnection();
87
88 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
89
90 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080091 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080092 void requestNextVsync() override; // asynchronous
93
Dominik Laskowskif654d572018-12-20 11:03:06 -080094 // Called in response to requestNextVsync.
95 const ResyncCallback resyncCallback;
96
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080097 VSyncRequest vsyncRequest = VSyncRequest::None;
Alec Mouri967d5d72020-08-05 12:50:03 -070098 const ISurfaceComposer::ConfigChanged mConfigChanged =
Alec Mouri60aee1c2019-10-28 16:18:59 -070099 ISurfaceComposer::ConfigChanged::eConfigChangedSuppress;
Ana Krulec85c39af2018-12-26 17:29:57 -0800100
Ady Abraham0bb6a472020-10-12 10:22:13 -0700101 const uid_t mOwnerUid;
102
Ana Krulec85c39af2018-12-26 17:29:57 -0800103private:
104 virtual void onFirstRef();
105 EventThread* const mEventThread;
106 gui::BitTube mChannel;
107};
108
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800109class EventThread {
110public:
111 virtual ~EventThread();
112
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700113 virtual sp<EventThreadConnection> createEventConnection(
114 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800115
116 // called before the screen is turned off from main thread
117 virtual void onScreenReleased() = 0;
118
119 // called after the screen is turned on from main thread
120 virtual void onScreenAcquired() = 0;
121
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800122 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800123
Ady Abraham447052e2019-02-13 16:07:27 -0800124 // called when SF changes the active config and apps needs to be notified about the change
Alec Mouri60aee1c2019-10-28 16:18:59 -0700125 virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
126 nsecs_t vsyncPeriod) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800127
Yiwei Zhang5434a782018-12-05 18:06:32 -0800128 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800129
Ady Abraham9c53ee72020-07-22 21:16:18 -0700130 virtual void setDuration(std::chrono::nanoseconds workDuration,
131 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800132
133 virtual status_t registerDisplayEventConnection(
134 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800135 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800136 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700137 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800138
139 // Retrieves the number of event connections tracked by this EventThread.
140 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800141};
142
143namespace impl {
144
145class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800146public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800147 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700148 using ThrottleVsyncCallback = std::function<bool(nsecs_t, uid_t)>;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800149
Ady Abraham0bb6a472020-10-12 10:22:13 -0700150 EventThread(std::unique_ptr<VSyncSource>, frametimeline::TokenManager*, InterceptVSyncsCallback,
151 ThrottleVsyncCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800152 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800153
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700154 sp<EventThreadConnection> createEventConnection(
155 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800156
Ana Krulec85c39af2018-12-26 17:29:57 -0800157 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800158 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700159 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800160
Mathias Agopian22ffb112012-04-10 21:04:02 -0700161 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800162 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700163
164 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800165 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800166
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800167 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700168
Alec Mouri60aee1c2019-10-28 16:18:59 -0700169 void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
170 nsecs_t vsyncPeriod) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800171
Yiwei Zhang5434a782018-12-05 18:06:32 -0800172 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800173
Ady Abraham9c53ee72020-07-22 21:16:18 -0700174 void setDuration(std::chrono::nanoseconds workDuration,
175 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700176
Alec Mouri717bcb62020-02-10 17:07:19 -0800177 size_t getEventThreadConnectionCount() override;
178
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800179private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800180 friend EventThreadTest;
181
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800182 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
183
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800184 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700185
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800186 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
187 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
188 void dispatchEvent(const DisplayEventReceiver::Event& event,
189 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700190
Ana Krulec85c39af2018-12-26 17:29:57 -0800191 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
192 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800193
Lloyd Pique46a46b32018-01-31 19:01:18 -0800194 // Implements VSyncSource::Callback
Ady Abraham9c53ee72020-07-22 21:16:18 -0700195 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
196 nsecs_t deadlineTimestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800197
Dominik Laskowski6505f792019-09-18 11:10:05 -0700198 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700199 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800200
Lloyd Pique24b0a482018-03-09 18:52:26 -0800201 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700202 const ThrottleVsyncCallback mThrottleVsyncCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800203 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800204
Lloyd Pique46a46b32018-01-31 19:01:18 -0800205 std::thread mThread;
206 mutable std::mutex mMutex;
207 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800208
Ana Krulec85c39af2018-12-26 17:29:57 -0800209 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800210 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800211
212 // VSYNC state of connected display.
213 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800214 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800215
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800216 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800217
218 // Number of VSYNC events since display was connected.
219 uint32_t count = 0;
220
221 // True if VSYNC should be faked, e.g. when display is off.
222 bool synthetic = false;
223 };
224
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800225 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
226 // and support headless mode by injecting a fake display with synthetic VSYNC.
227 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800228
Dominik Laskowski029cc122019-01-23 19:52:06 -0800229 // State machine for event loop.
230 enum class State {
231 Idle,
232 Quit,
233 SyntheticVSync,
234 VSync,
235 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700236
Dominik Laskowski029cc122019-01-23 19:52:06 -0800237 State mState GUARDED_BY(mMutex) = State::Idle;
238
239 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800240};
241
242// ---------------------------------------------------------------------------
243
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800244} // namespace impl
245} // namespace android