blob: a858169e650b51c21701e657c1de865ea3ba5d59 [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>
Huihong Luo6fac5232021-11-22 16:05:23 -080020#include <android/gui/BnDisplayEventConnection.h>
Alec Mouri717bcb62020-02-10 17:07:19 -080021#include <gui/DisplayEventReceiver.h>
Alec Mouri717bcb62020-02-10 17:07:19 -080022#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
Rachel Leeef2e21f2022-02-01 14:51:34 -080048using gui::VsyncEventData;
49
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050// ---------------------------------------------------------------------------
51
Dominik Laskowskif654d572018-12-20 11:03:06 -080052using ResyncCallback = std::function<void()>;
Ady Abraham62f216c2020-10-13 19:07:23 -070053using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
Dominik Laskowskif654d572018-12-20 11:03:06 -080054
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080055enum class VSyncRequest {
Tim Murrayb5daa912020-09-09 21:29:13 +000056 None = -2,
57 // Single wakes up for the next two frames to avoid scheduler overhead
58 Single = -1,
59 // SingleSuppressCallback only wakes up for the next frame
60 SingleSuppressCallback = 0,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080061 Periodic = 1,
62 // Subsequent values are periods.
63};
64
Lloyd Piquee83f9312018-02-01 12:53:17 -080065class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066public:
Rachel Leef16da3c2022-01-20 13:57:18 -080067 class VSyncData {
68 public:
69 nsecs_t expectedVSyncTimestamp;
70 nsecs_t deadlineTimestamp;
71 };
72
Lloyd Piquee83f9312018-02-01 12:53:17 -080073 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070074 public:
75 virtual ~Callback() {}
Rachel Leef16da3c2022-01-20 13:57:18 -080076 virtual void onVSyncEvent(nsecs_t when, VSyncData vsyncData) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070077 };
78
79 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070080
81 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070082 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080083 virtual void setCallback(Callback* callback) = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -070084 virtual void setDuration(std::chrono::nanoseconds workDuration,
85 std::chrono::nanoseconds readyDuration) = 0;
Rachel Leeef2e21f2022-02-01 14:51:34 -080086 virtual VSyncData getLatestVSyncData() const = 0;
Ady Abraham5e7371c2020-03-24 14:47:24 -070087
88 virtual void dump(std::string& result) const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089};
90
Huihong Luo6fac5232021-11-22 16:05:23 -080091class EventThreadConnection : public gui::BnDisplayEventConnection {
Ana Krulec85c39af2018-12-26 17:29:57 -080092public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070093 EventThreadConnection(EventThread*, uid_t callingUid, ResyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070094 ISurfaceComposer::EventRegistrationFlags eventRegistration = {});
Ana Krulec85c39af2018-12-26 17:29:57 -080095 virtual ~EventThreadConnection();
96
97 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
98
Huihong Luo6fac5232021-11-22 16:05:23 -080099 binder::Status stealReceiveChannel(gui::BitTube* outChannel) override;
100 binder::Status setVsyncRate(int rate) override;
101 binder::Status requestNextVsync() override; // asynchronous
Rachel Leeef2e21f2022-02-01 14:51:34 -0800102 binder::Status getLatestVsyncEventData(VsyncEventData* outVsyncEventData) override;
Ana Krulec85c39af2018-12-26 17:29:57 -0800103
Dominik Laskowskif654d572018-12-20 11:03:06 -0800104 // Called in response to requestNextVsync.
105 const ResyncCallback resyncCallback;
106
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800107 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700108 const uid_t mOwnerUid;
Ady Abraham62f216c2020-10-13 19:07:23 -0700109 const ISurfaceComposer::EventRegistrationFlags mEventRegistration;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700110
Ana Krulec85c39af2018-12-26 17:29:57 -0800111private:
112 virtual void onFirstRef();
113 EventThread* const mEventThread;
114 gui::BitTube mChannel;
Ady Abraham62f216c2020-10-13 19:07:23 -0700115
116 std::vector<DisplayEventReceiver::Event> mPendingEvents;
Ana Krulec85c39af2018-12-26 17:29:57 -0800117};
118
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800119class EventThread {
120public:
121 virtual ~EventThread();
122
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700123 virtual sp<EventThreadConnection> createEventConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700124 ResyncCallback,
125 ISurfaceComposer::EventRegistrationFlags eventRegistration = {}) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800126
127 // called before the screen is turned off from main thread
128 virtual void onScreenReleased() = 0;
129
130 // called after the screen is turned on from main thread
131 virtual void onScreenAcquired() = 0;
132
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800133 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800134
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100135 // called when SF changes the active mode and apps needs to be notified about the change
Ady Abraham690f4612021-07-01 23:24:03 -0700136 virtual void onModeChanged(DisplayModePtr) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800137
Ady Abraham62f216c2020-10-13 19:07:23 -0700138 // called when SF updates the Frame Rate Override list
139 virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
140 std::vector<FrameRateOverride> overrides) = 0;
141
Yiwei Zhang5434a782018-12-05 18:06:32 -0800142 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800143
Ady Abraham9c53ee72020-07-22 21:16:18 -0700144 virtual void setDuration(std::chrono::nanoseconds workDuration,
145 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800146
147 virtual status_t registerDisplayEventConnection(
148 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800149 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800150 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700151 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800152 virtual VsyncEventData getLatestVsyncEventData(
153 const sp<EventThreadConnection>& connection) const = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800154
155 // Retrieves the number of event connections tracked by this EventThread.
156 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800157};
158
159namespace impl {
160
161class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800162public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800163 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700164 using ThrottleVsyncCallback = std::function<bool(nsecs_t, uid_t)>;
Jorim Jaggic0086af2021-02-12 18:18:11 +0100165 using GetVsyncPeriodFunction = std::function<nsecs_t(uid_t)>;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800166
Ady Abraham0bb6a472020-10-12 10:22:13 -0700167 EventThread(std::unique_ptr<VSyncSource>, frametimeline::TokenManager*, InterceptVSyncsCallback,
Jorim Jaggic0086af2021-02-12 18:18:11 +0100168 ThrottleVsyncCallback, GetVsyncPeriodFunction);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800169 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800170
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700171 sp<EventThreadConnection> createEventConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700172 ResyncCallback,
173 ISurfaceComposer::EventRegistrationFlags eventRegistration = {}) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800174
Ana Krulec85c39af2018-12-26 17:29:57 -0800175 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800176 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700177 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800178 VsyncEventData getLatestVsyncEventData(
179 const sp<EventThreadConnection>& connection) const override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800180
Mathias Agopian22ffb112012-04-10 21:04:02 -0700181 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800182 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700183
184 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800185 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800186
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800187 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700188
Ady Abraham690f4612021-07-01 23:24:03 -0700189 void onModeChanged(DisplayModePtr) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800190
Ady Abraham62f216c2020-10-13 19:07:23 -0700191 void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
192 std::vector<FrameRateOverride> overrides) override;
193
Yiwei Zhang5434a782018-12-05 18:06:32 -0800194 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800195
Ady Abraham9c53ee72020-07-22 21:16:18 -0700196 void setDuration(std::chrono::nanoseconds workDuration,
197 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700198
Alec Mouri717bcb62020-02-10 17:07:19 -0800199 size_t getEventThreadConnectionCount() override;
200
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800201private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800202 friend EventThreadTest;
203
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800204 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
205
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800206 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700207
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800208 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
209 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
210 void dispatchEvent(const DisplayEventReceiver::Event& event,
211 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700212
Ana Krulec85c39af2018-12-26 17:29:57 -0800213 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
214 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800215
Lloyd Pique46a46b32018-01-31 19:01:18 -0800216 // Implements VSyncSource::Callback
Rachel Leef16da3c2022-01-20 13:57:18 -0800217 void onVSyncEvent(nsecs_t timestamp, VSyncSource::VSyncData vsyncData) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800218
Rachel Lee8d0c6102021-11-03 22:00:25 +0000219 int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
220 nsecs_t expectedVSyncTimestamp) const;
Rachel Lee3f028662021-11-04 19:32:24 +0000221 void generateFrameTimeline(DisplayEventReceiver::Event& event) const;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800222 void generateFrameTimeline(VsyncEventData& out, const nsecs_t frameInterval,
223 const nsecs_t timestamp) const;
224 void generateFrameTimeline(
225 nsecs_t frameInterval, nsecs_t timestamp, nsecs_t preferredExpectedVSyncTimestamp,
226 nsecs_t preferredDeadlineTimestamp,
227 std::function<void(int64_t index)> setPreferredFrameTimelineIndex,
228 std::function<void(int64_t index, int64_t vsyncId, nsecs_t expectedVSyncTimestamp,
229 nsecs_t deadlineTimestamp)>
230 setFrameTimeline) const;
Rachel Lee3f028662021-11-04 19:32:24 +0000231
Dominik Laskowski6505f792019-09-18 11:10:05 -0700232 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700233 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800234
Lloyd Pique24b0a482018-03-09 18:52:26 -0800235 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700236 const ThrottleVsyncCallback mThrottleVsyncCallback;
Jorim Jaggic0086af2021-02-12 18:18:11 +0100237 const GetVsyncPeriodFunction mGetVsyncPeriodFunction;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800238 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800239
Lloyd Pique46a46b32018-01-31 19:01:18 -0800240 std::thread mThread;
241 mutable std::mutex mMutex;
242 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800243
Ana Krulec85c39af2018-12-26 17:29:57 -0800244 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800245 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800246
247 // VSYNC state of connected display.
248 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800249 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800250
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800251 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800252
253 // Number of VSYNC events since display was connected.
254 uint32_t count = 0;
255
256 // True if VSYNC should be faked, e.g. when display is off.
257 bool synthetic = false;
258 };
259
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800260 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
261 // and support headless mode by injecting a fake display with synthetic VSYNC.
262 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800263
Dominik Laskowski029cc122019-01-23 19:52:06 -0800264 // State machine for event loop.
265 enum class State {
266 Idle,
267 Quit,
268 SyntheticVSync,
269 VSync,
270 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700271
Dominik Laskowski029cc122019-01-23 19:52:06 -0800272 State mState GUARDED_BY(mMutex) = State::Idle;
273
274 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800275};
276
277// ---------------------------------------------------------------------------
278
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800279} // namespace impl
280} // namespace android