blob: 2daf126d77895395d8dc0fc5633493d8c104ed6d [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
Ady Abraham67434eb2022-12-01 17:48:12 -080026#include <scheduler/FrameRateMode.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080027#include <condition_variable>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070028#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080029#include <deque>
Lloyd Pique46a46b32018-01-31 19:01:18 -080030#include <mutex>
Dominik Laskowski1eba0202019-01-24 09:14:40 -080031#include <optional>
Lloyd Pique46a46b32018-01-31 19:01:18 -080032#include <thread>
Chia-I Wu98cd38f2018-09-14 11:53:25 -070033#include <vector>
Lloyd Pique46a46b32018-01-31 19:01:18 -080034
Marin Shalamanov23c44202020-12-22 19:09:20 +010035#include "DisplayHardware/DisplayMode.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080036#include "TracedOrdinal.h"
37#include "VSyncDispatch.h"
38#include "VsyncSchedule.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039
40// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042// ---------------------------------------------------------------------------
43
Ana Krulec85c39af2018-12-26 17:29:57 -080044class EventThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080045class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080046class SurfaceFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070048namespace frametimeline {
49class TokenManager;
50} // namespace frametimeline
51
Rachel Leeb9c5a772022-02-04 21:17:37 -080052using gui::ParcelableVsyncEventData;
Rachel Leeef2e21f2022-02-01 14:51:34 -080053using gui::VsyncEventData;
54
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055// ---------------------------------------------------------------------------
56
Ady Abraham62f216c2020-10-13 19:07:23 -070057using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
Dominik Laskowskif654d572018-12-20 11:03:06 -080058
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080059enum class VSyncRequest {
Tim Murrayb5daa912020-09-09 21:29:13 +000060 None = -2,
61 // Single wakes up for the next two frames to avoid scheduler overhead
62 Single = -1,
63 // SingleSuppressCallback only wakes up for the next frame
64 SingleSuppressCallback = 0,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080065 Periodic = 1,
66 // Subsequent values are periods.
67};
68
Huihong Luo6fac5232021-11-22 16:05:23 -080069class EventThreadConnection : public gui::BnDisplayEventConnection {
Ana Krulec85c39af2018-12-26 17:29:57 -080070public:
Ady Abrahamf2851612023-09-25 17:19:00 -070071 EventThreadConnection(EventThread*, uid_t callingUid,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070072 EventRegistrationFlags eventRegistration = {});
Ana Krulec85c39af2018-12-26 17:29:57 -080073 virtual ~EventThreadConnection();
74
75 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
76
Huihong Luo6fac5232021-11-22 16:05:23 -080077 binder::Status stealReceiveChannel(gui::BitTube* outChannel) override;
78 binder::Status setVsyncRate(int rate) override;
79 binder::Status requestNextVsync() override; // asynchronous
Rachel Leeb9c5a772022-02-04 21:17:37 -080080 binder::Status getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) override;
Ady Abraham07d03c42023-09-27 19:15:08 -070081 binder::Status getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080082
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080083 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0bb6a472020-10-12 10:22:13 -070084 const uid_t mOwnerUid;
Huihong Luo1b0c49f2022-03-15 19:18:21 -070085 const EventRegistrationFlags mEventRegistration;
Ady Abraham0bb6a472020-10-12 10:22:13 -070086
Rachel Lee2248f522023-01-27 16:45:23 -080087 /** The frame rate set to the attached choreographer. */
88 Fps frameRate;
89
Ana Krulec85c39af2018-12-26 17:29:57 -080090private:
91 virtual void onFirstRef();
92 EventThread* const mEventThread;
Ady Abraham899e8cd2022-06-06 15:16:06 -070093 std::mutex mLock;
94 gui::BitTube mChannel GUARDED_BY(mLock);
Ady Abraham62f216c2020-10-13 19:07:23 -070095
96 std::vector<DisplayEventReceiver::Event> mPendingEvents;
Ana Krulec85c39af2018-12-26 17:29:57 -080097};
98
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080099class EventThread {
100public:
101 virtual ~EventThread();
102
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700103 virtual sp<EventThreadConnection> createEventConnection(
Ady Abrahamf2851612023-09-25 17:19:00 -0700104 EventRegistrationFlags eventRegistration = {}) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800105
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500106 // Feed clients with fake VSYNC, e.g. while the display is off.
107 virtual void enableSyntheticVsync(bool) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800108
Ady Abraham8e3e2ea2024-10-31 15:52:31 -0700109 virtual void omitVsyncDispatching(bool) = 0;
110
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800111 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800112
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700113 virtual void onHotplugConnectionError(int32_t connectionError) = 0;
114
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100115 // called when SF changes the active mode and apps needs to be notified about the change
Ady Abraham67434eb2022-12-01 17:48:12 -0800116 virtual void onModeChanged(const scheduler::FrameRateMode&) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800117
Ady Abraham62f216c2020-10-13 19:07:23 -0700118 // called when SF updates the Frame Rate Override list
119 virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
120 std::vector<FrameRateOverride> overrides) = 0;
121
Yiwei Zhang5434a782018-12-05 18:06:32 -0800122 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800123
Ady Abraham9c53ee72020-07-22 21:16:18 -0700124 virtual void setDuration(std::chrono::nanoseconds workDuration,
125 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800126
127 virtual status_t registerDisplayEventConnection(
128 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800129 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800130 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700131 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Ady Abraham40ec5842024-04-04 13:22:38 -0700132 virtual VsyncEventData getLatestVsyncEventData(const sp<EventThreadConnection>& connection,
133 nsecs_t now) const = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800134
Leon Scroggins III67388622023-02-06 20:36:20 -0500135 virtual void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) = 0;
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700136
137 virtual void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
138 int32_t maxLevel) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800139};
140
Ady Abrahamf2851612023-09-25 17:19:00 -0700141struct IEventThreadCallback {
142 virtual ~IEventThreadCallback() = default;
143
144 virtual bool throttleVsync(TimePoint, uid_t) = 0;
145 virtual Period getVsyncPeriod(uid_t) = 0;
146 virtual void resync() = 0;
ramindaniae645822024-01-11 10:57:29 -0800147 virtual void onExpectedPresentTimePosted(TimePoint) = 0;
Ady Abrahamf2851612023-09-25 17:19:00 -0700148};
149
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800150namespace impl {
151
Ady Abraham011f8ba2022-11-22 15:09:07 -0800152class EventThread : public android::EventThread {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800153public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500154 EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule>,
Ady Abrahamf2851612023-09-25 17:19:00 -0700155 frametimeline::TokenManager*, IEventThreadCallback& callback,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500156 std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800157 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800158
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700159 sp<EventThreadConnection> createEventConnection(
Ady Abrahamf2851612023-09-25 17:19:00 -0700160 EventRegistrationFlags eventRegistration = {}) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800161
Ana Krulec85c39af2018-12-26 17:29:57 -0800162 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800163 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700164 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Ady Abraham40ec5842024-04-04 13:22:38 -0700165 VsyncEventData getLatestVsyncEventData(const sp<EventThreadConnection>& connection,
166 nsecs_t now) const override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800167
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500168 void enableSyntheticVsync(bool) override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800169
Ady Abraham8e3e2ea2024-10-31 15:52:31 -0700170 void omitVsyncDispatching(bool) override;
171
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800172 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700173
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700174 void onHotplugConnectionError(int32_t connectionError) override;
175
Ady Abraham67434eb2022-12-01 17:48:12 -0800176 void onModeChanged(const scheduler::FrameRateMode&) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800177
Ady Abraham62f216c2020-10-13 19:07:23 -0700178 void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
179 std::vector<FrameRateOverride> overrides) override;
180
Yiwei Zhang5434a782018-12-05 18:06:32 -0800181 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800182
Ady Abraham9c53ee72020-07-22 21:16:18 -0700183 void setDuration(std::chrono::nanoseconds workDuration,
184 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700185
Leon Scroggins IIIb2253602023-04-19 17:04:04 -0400186 void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) override EXCLUDES(mMutex);
Leon Scroggins III67388622023-02-06 20:36:20 -0500187
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700188 void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
189 int32_t maxLevel) override;
190
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800191private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800192 friend EventThreadTest;
193
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800194 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
195
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800196 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700197
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800198 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
199 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
200 void dispatchEvent(const DisplayEventReceiver::Event& event,
201 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700202
Ana Krulec85c39af2018-12-26 17:29:57 -0800203 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
204 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800205
Ady Abraham011f8ba2022-11-22 15:09:07 -0800206 void onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime);
Mathias Agopian23748662011-12-05 14:33:34 -0800207
Rachel Lee8d0c6102021-11-03 22:00:25 +0000208 int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800209 nsecs_t expectedPresentationTime) const;
210 void generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval,
211 nsecs_t timestamp, nsecs_t preferredExpectedPresentationTime,
212 nsecs_t preferredDeadlineTimestamp) const;
Rachel Lee3f028662021-11-04 19:32:24 +0000213
Leon Scroggins III67388622023-02-06 20:36:20 -0500214 scheduler::VSyncDispatch::Callback createDispatchCallback();
215
Leon Scroggins IIIb2253602023-04-19 17:04:04 -0400216 // Returns the old registration so it can be destructed outside the lock to
217 // avoid deadlock.
218 scheduler::VSyncCallbackRegistration onNewVsyncScheduleInternal(
219 std::shared_ptr<scheduler::VsyncSchedule>) EXCLUDES(mMutex);
220
Ady Abraham011f8ba2022-11-22 15:09:07 -0800221 const char* const mThreadName;
222 TracedOrdinal<int> mVsyncTracer;
223 TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mMutex);
224 std::chrono::nanoseconds mReadyDuration GUARDED_BY(mMutex);
Leon Scroggins IIIed66a352023-03-23 17:55:43 -0400225 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule GUARDED_BY(mMutex);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800226 TimePoint mLastVsyncCallbackTime GUARDED_BY(mMutex) = TimePoint::now();
ramindani92ab9872024-07-26 15:09:37 -0700227 TimePoint mLastCommittedVsyncTime GUARDED_BY(mMutex) = TimePoint::now();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800228 scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700229 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800230
Ady Abrahamf2851612023-09-25 17:19:00 -0700231 IEventThreadCallback& mCallback;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800232
Lloyd Pique46a46b32018-01-31 19:01:18 -0800233 std::thread mThread;
234 mutable std::mutex mMutex;
235 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800236
Ana Krulec85c39af2018-12-26 17:29:57 -0800237 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800238 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800239
240 // VSYNC state of connected display.
241 struct VSyncState {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800242 // Number of VSYNC events since display was connected.
243 uint32_t count = 0;
244
245 // True if VSYNC should be faked, e.g. when display is off.
246 bool synthetic = false;
Ady Abraham8e3e2ea2024-10-31 15:52:31 -0700247
248 // True if VSYNC should not be delivered to apps. Used when the display is off.
249 bool omitted = false;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800250 };
251
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800252 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
253 // and support headless mode by injecting a fake display with synthetic VSYNC.
254 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800255
Dominik Laskowski029cc122019-01-23 19:52:06 -0800256 // State machine for event loop.
257 enum class State {
258 Idle,
259 Quit,
260 SyntheticVSync,
261 VSync,
262 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700263
Dominik Laskowski029cc122019-01-23 19:52:06 -0800264 State mState GUARDED_BY(mMutex) = State::Idle;
265
266 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800267};
268
269// ---------------------------------------------------------------------------
270
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800271} // namespace impl
272} // namespace android