blob: 35406041a8a9d119dd45c5cfbe54572359ac8137 [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
Marin Shalamanov23c44202020-12-22 19:09:20 +010034#include "DisplayHardware/DisplayMode.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()>;
Ady Abraham62f216c2020-10-13 19:07:23 -070051using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
Dominik Laskowskif654d572018-12-20 11:03:06 -080052
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080053enum class VSyncRequest {
Tim Murrayb5daa912020-09-09 21:29:13 +000054 None = -2,
55 // Single wakes up for the next two frames to avoid scheduler overhead
56 Single = -1,
57 // SingleSuppressCallback only wakes up for the next frame
58 SingleSuppressCallback = 0,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080059 Periodic = 1,
60 // Subsequent values are periods.
61};
62
Lloyd Piquee83f9312018-02-01 12:53:17 -080063class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070064public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080065 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066 public:
67 virtual ~Callback() {}
Ady Abraham9c53ee72020-07-22 21:16:18 -070068 virtual void onVSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp,
69 nsecs_t deadlineTimestamp) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070070 };
71
72 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070073
74 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070075 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080076 virtual void setCallback(Callback* callback) = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -070077 virtual void setDuration(std::chrono::nanoseconds workDuration,
78 std::chrono::nanoseconds readyDuration) = 0;
Ady Abraham5e7371c2020-03-24 14:47:24 -070079
80 virtual void dump(std::string& result) const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070081};
82
Ana Krulec85c39af2018-12-26 17:29:57 -080083class EventThreadConnection : public BnDisplayEventConnection {
84public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070085 EventThreadConnection(EventThread*, uid_t callingUid, ResyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070086 ISurfaceComposer::EventRegistrationFlags eventRegistration = {});
Ana Krulec85c39af2018-12-26 17:29:57 -080087 virtual ~EventThreadConnection();
88
89 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
90
91 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080092 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080093 void requestNextVsync() override; // asynchronous
94
Dominik Laskowskif654d572018-12-20 11:03:06 -080095 // Called in response to requestNextVsync.
96 const ResyncCallback resyncCallback;
97
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080098 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0bb6a472020-10-12 10:22:13 -070099 const uid_t mOwnerUid;
Ady Abraham62f216c2020-10-13 19:07:23 -0700100 const ISurfaceComposer::EventRegistrationFlags mEventRegistration;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700101
Ana Krulec85c39af2018-12-26 17:29:57 -0800102private:
103 virtual void onFirstRef();
104 EventThread* const mEventThread;
105 gui::BitTube mChannel;
Ady Abraham62f216c2020-10-13 19:07:23 -0700106
107 std::vector<DisplayEventReceiver::Event> mPendingEvents;
Ana Krulec85c39af2018-12-26 17:29:57 -0800108};
109
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800110class EventThread {
111public:
112 virtual ~EventThread();
113
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700114 virtual sp<EventThreadConnection> createEventConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700115 ResyncCallback,
116 ISurfaceComposer::EventRegistrationFlags eventRegistration = {}) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800117
118 // called before the screen is turned off from main thread
119 virtual void onScreenReleased() = 0;
120
121 // called after the screen is turned on from main thread
122 virtual void onScreenAcquired() = 0;
123
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800124 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800125
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100126 // called when SF changes the active mode and apps needs to be notified about the change
127 virtual void onModeChanged(PhysicalDisplayId displayId, DisplayModeId modeId,
128 nsecs_t vsyncPeriod) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800129
Ady Abraham62f216c2020-10-13 19:07:23 -0700130 // called when SF updates the Frame Rate Override list
131 virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
132 std::vector<FrameRateOverride> overrides) = 0;
133
Yiwei Zhang5434a782018-12-05 18:06:32 -0800134 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800135
Ady Abraham9c53ee72020-07-22 21:16:18 -0700136 virtual void setDuration(std::chrono::nanoseconds workDuration,
137 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800138
139 virtual status_t registerDisplayEventConnection(
140 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800141 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800142 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700143 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800144
145 // Retrieves the number of event connections tracked by this EventThread.
146 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800147};
148
149namespace impl {
150
151class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800152public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800153 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700154 using ThrottleVsyncCallback = std::function<bool(nsecs_t, uid_t)>;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800155
Ady Abraham0bb6a472020-10-12 10:22:13 -0700156 EventThread(std::unique_ptr<VSyncSource>, frametimeline::TokenManager*, InterceptVSyncsCallback,
157 ThrottleVsyncCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800158 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800159
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700160 sp<EventThreadConnection> createEventConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700161 ResyncCallback,
162 ISurfaceComposer::EventRegistrationFlags eventRegistration = {}) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800163
Ana Krulec85c39af2018-12-26 17:29:57 -0800164 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800165 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700166 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800167
Mathias Agopian22ffb112012-04-10 21:04:02 -0700168 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800169 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700170
171 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800172 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800173
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800174 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700175
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100176 void onModeChanged(PhysicalDisplayId displayId, DisplayModeId modeId,
177 nsecs_t vsyncPeriod) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800178
Ady Abraham62f216c2020-10-13 19:07:23 -0700179 void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
180 std::vector<FrameRateOverride> overrides) override;
181
Yiwei Zhang5434a782018-12-05 18:06:32 -0800182 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800183
Ady Abraham9c53ee72020-07-22 21:16:18 -0700184 void setDuration(std::chrono::nanoseconds workDuration,
185 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700186
Alec Mouri717bcb62020-02-10 17:07:19 -0800187 size_t getEventThreadConnectionCount() override;
188
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800189private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800190 friend EventThreadTest;
191
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800192 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
193
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800194 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700195
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800196 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
197 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
198 void dispatchEvent(const DisplayEventReceiver::Event& event,
199 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700200
Ana Krulec85c39af2018-12-26 17:29:57 -0800201 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
202 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800203
Lloyd Pique46a46b32018-01-31 19:01:18 -0800204 // Implements VSyncSource::Callback
Ady Abraham9c53ee72020-07-22 21:16:18 -0700205 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
206 nsecs_t deadlineTimestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800207
Dominik Laskowski6505f792019-09-18 11:10:05 -0700208 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700209 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800210
Lloyd Pique24b0a482018-03-09 18:52:26 -0800211 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700212 const ThrottleVsyncCallback mThrottleVsyncCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800213 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800214
Lloyd Pique46a46b32018-01-31 19:01:18 -0800215 std::thread mThread;
216 mutable std::mutex mMutex;
217 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800218
Ana Krulec85c39af2018-12-26 17:29:57 -0800219 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800220 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800221
222 // VSYNC state of connected display.
223 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800224 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800225
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800226 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800227
228 // Number of VSYNC events since display was connected.
229 uint32_t count = 0;
230
231 // True if VSYNC should be faked, e.g. when display is off.
232 bool synthetic = false;
233 };
234
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800235 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
236 // and support headless mode by injecting a fake display with synthetic VSYNC.
237 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800238
Dominik Laskowski029cc122019-01-23 19:52:06 -0800239 // State machine for event loop.
240 enum class State {
241 Idle,
242 Quit,
243 SyntheticVSync,
244 VSync,
245 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700246
Dominik Laskowski029cc122019-01-23 19:52:06 -0800247 State mState GUARDED_BY(mMutex) = State::Idle;
248
249 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800250};
251
252// ---------------------------------------------------------------------------
253
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800254} // namespace impl
255} // namespace android