blob: 18bf41643c405c5560b37ca119fff51ae6fcdc77 [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>
Manasi Navare5ef1fa12024-11-07 23:49:46 +000024#include <ui/DisplayId.h>
Alec Mouri717bcb62020-02-10 17:07:19 -080025#include <utils/Errors.h>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070026
Ady Abraham67434eb2022-12-01 17:48:12 -080027#include <scheduler/FrameRateMode.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080028#include <condition_variable>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070029#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080030#include <deque>
Lloyd Pique46a46b32018-01-31 19:01:18 -080031#include <mutex>
Dominik Laskowski1eba0202019-01-24 09:14:40 -080032#include <optional>
Lloyd Pique46a46b32018-01-31 19:01:18 -080033#include <thread>
Chia-I Wu98cd38f2018-09-14 11:53:25 -070034#include <vector>
Lloyd Pique46a46b32018-01-31 19:01:18 -080035
Manasi Navare5ef1fa12024-11-07 23:49:46 +000036#include "DisplayHardware/DisplayMode.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080037#include "TracedOrdinal.h"
38#include "VSyncDispatch.h"
39#include "VsyncSchedule.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040
41// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043// ---------------------------------------------------------------------------
44
Ana Krulec85c39af2018-12-26 17:29:57 -080045class EventThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080046class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047class SurfaceFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070049namespace frametimeline {
50class TokenManager;
51} // namespace frametimeline
52
Rachel Leeb9c5a772022-02-04 21:17:37 -080053using gui::ParcelableVsyncEventData;
Rachel Leeef2e21f2022-02-01 14:51:34 -080054using gui::VsyncEventData;
55
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056// ---------------------------------------------------------------------------
57
Ady Abraham62f216c2020-10-13 19:07:23 -070058using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
Melody Hsue524dd92024-08-27 22:27:29 +000059using BufferStuffingMap = ftl::SmallMap<uid_t, uint32_t, 10>;
Dominik Laskowskif654d572018-12-20 11:03:06 -080060
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080061enum class VSyncRequest {
Tim Murrayb5daa912020-09-09 21:29:13 +000062 None = -2,
63 // Single wakes up for the next two frames to avoid scheduler overhead
64 Single = -1,
65 // SingleSuppressCallback only wakes up for the next frame
66 SingleSuppressCallback = 0,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080067 Periodic = 1,
68 // Subsequent values are periods.
69};
70
Huihong Luo6fac5232021-11-22 16:05:23 -080071class EventThreadConnection : public gui::BnDisplayEventConnection {
Ana Krulec85c39af2018-12-26 17:29:57 -080072public:
Ady Abrahamf2851612023-09-25 17:19:00 -070073 EventThreadConnection(EventThread*, uid_t callingUid,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070074 EventRegistrationFlags eventRegistration = {});
Ana Krulec85c39af2018-12-26 17:29:57 -080075 virtual ~EventThreadConnection();
76
77 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
78
Huihong Luo6fac5232021-11-22 16:05:23 -080079 binder::Status stealReceiveChannel(gui::BitTube* outChannel) override;
80 binder::Status setVsyncRate(int rate) override;
81 binder::Status requestNextVsync() override; // asynchronous
Rachel Leeb9c5a772022-02-04 21:17:37 -080082 binder::Status getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) override;
Ady Abraham07d03c42023-09-27 19:15:08 -070083 binder::Status getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080084
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080085 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0bb6a472020-10-12 10:22:13 -070086 const uid_t mOwnerUid;
Huihong Luo1b0c49f2022-03-15 19:18:21 -070087 const EventRegistrationFlags mEventRegistration;
Ady Abraham0bb6a472020-10-12 10:22:13 -070088
Rachel Lee2248f522023-01-27 16:45:23 -080089 /** The frame rate set to the attached choreographer. */
90 Fps frameRate;
91
Ana Krulec85c39af2018-12-26 17:29:57 -080092private:
93 virtual void onFirstRef();
94 EventThread* const mEventThread;
Ady Abraham899e8cd2022-06-06 15:16:06 -070095 std::mutex mLock;
96 gui::BitTube mChannel GUARDED_BY(mLock);
Ady Abraham62f216c2020-10-13 19:07:23 -070097
98 std::vector<DisplayEventReceiver::Event> mPendingEvents;
Ana Krulec85c39af2018-12-26 17:29:57 -080099};
100
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800101class EventThread {
102public:
103 virtual ~EventThread();
104
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700105 virtual sp<EventThreadConnection> createEventConnection(
Ady Abrahamf2851612023-09-25 17:19:00 -0700106 EventRegistrationFlags eventRegistration = {}) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800107
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500108 // Feed clients with fake VSYNC, e.g. while the display is off.
109 virtual void enableSyntheticVsync(bool) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800110
Ady Abraham8e3e2ea2024-10-31 15:52:31 -0700111 virtual void omitVsyncDispatching(bool) = 0;
112
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800113 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800114
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700115 virtual void onHotplugConnectionError(int32_t connectionError) = 0;
116
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100117 // called when SF changes the active mode and apps needs to be notified about the change
Ady Abraham67434eb2022-12-01 17:48:12 -0800118 virtual void onModeChanged(const scheduler::FrameRateMode&) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800119
Manasi Navare5ef1fa12024-11-07 23:49:46 +0000120 // called when SF rejects the mode change request
121 virtual void onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) = 0;
122
Ady Abraham62f216c2020-10-13 19:07:23 -0700123 // called when SF updates the Frame Rate Override list
124 virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
125 std::vector<FrameRateOverride> overrides) = 0;
126
Yiwei Zhang5434a782018-12-05 18:06:32 -0800127 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800128
Ady Abraham9c53ee72020-07-22 21:16:18 -0700129 virtual void setDuration(std::chrono::nanoseconds workDuration,
130 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800131
132 virtual status_t registerDisplayEventConnection(
133 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800134 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800135 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700136 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Ady Abraham40ec5842024-04-04 13:22:38 -0700137 virtual VsyncEventData getLatestVsyncEventData(const sp<EventThreadConnection>& connection,
138 nsecs_t now) const = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800139
Leon Scroggins III67388622023-02-06 20:36:20 -0500140 virtual void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) = 0;
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700141
142 virtual void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
143 int32_t maxLevel) = 0;
Melody Hsue524dd92024-08-27 22:27:29 +0000144
145 // An elevated number of queued buffers in the server is detected. This propagates a
146 // flag to Choreographer indicating that buffer stuffing recovery should begin.
147 virtual void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids);
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800148};
149
Ady Abrahamf2851612023-09-25 17:19:00 -0700150struct IEventThreadCallback {
151 virtual ~IEventThreadCallback() = default;
152
153 virtual bool throttleVsync(TimePoint, uid_t) = 0;
154 virtual Period getVsyncPeriod(uid_t) = 0;
155 virtual void resync() = 0;
ramindaniae645822024-01-11 10:57:29 -0800156 virtual void onExpectedPresentTimePosted(TimePoint) = 0;
Ady Abrahamf2851612023-09-25 17:19:00 -0700157};
158
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800159namespace impl {
160
Ady Abraham011f8ba2022-11-22 15:09:07 -0800161class EventThread : public android::EventThread {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800162public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500163 EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule>,
Ady Abrahamf2851612023-09-25 17:19:00 -0700164 frametimeline::TokenManager*, IEventThreadCallback& callback,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500165 std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800166 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800167
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700168 sp<EventThreadConnection> createEventConnection(
Ady Abrahamf2851612023-09-25 17:19:00 -0700169 EventRegistrationFlags eventRegistration = {}) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800170
Ana Krulec85c39af2018-12-26 17:29:57 -0800171 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800172 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700173 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Ady Abraham40ec5842024-04-04 13:22:38 -0700174 VsyncEventData getLatestVsyncEventData(const sp<EventThreadConnection>& connection,
175 nsecs_t now) const override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800176
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500177 void enableSyntheticVsync(bool) override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800178
Ady Abraham8e3e2ea2024-10-31 15:52:31 -0700179 void omitVsyncDispatching(bool) override;
180
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800181 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700182
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700183 void onHotplugConnectionError(int32_t connectionError) override;
184
Ady Abraham67434eb2022-12-01 17:48:12 -0800185 void onModeChanged(const scheduler::FrameRateMode&) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800186
Manasi Navare5ef1fa12024-11-07 23:49:46 +0000187 void onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) override;
188
Ady Abraham62f216c2020-10-13 19:07:23 -0700189 void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
190 std::vector<FrameRateOverride> overrides) override;
191
Yiwei Zhang5434a782018-12-05 18:06:32 -0800192 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800193
Ady Abraham9c53ee72020-07-22 21:16:18 -0700194 void setDuration(std::chrono::nanoseconds workDuration,
195 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700196
Leon Scroggins IIIb2253602023-04-19 17:04:04 -0400197 void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) override EXCLUDES(mMutex);
Leon Scroggins III67388622023-02-06 20:36:20 -0500198
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700199 void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
200 int32_t maxLevel) override;
201
Melody Hsue524dd92024-08-27 22:27:29 +0000202 void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) override;
203
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800204private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800205 friend EventThreadTest;
206
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800207 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
208
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800209 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700210
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800211 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
212 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
213 void dispatchEvent(const DisplayEventReceiver::Event& event,
214 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700215
Ana Krulec85c39af2018-12-26 17:29:57 -0800216 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
217 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800218
Ady Abraham011f8ba2022-11-22 15:09:07 -0800219 void onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime);
Mathias Agopian23748662011-12-05 14:33:34 -0800220
Rachel Lee8d0c6102021-11-03 22:00:25 +0000221 int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800222 nsecs_t expectedPresentationTime) const;
223 void generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval,
224 nsecs_t timestamp, nsecs_t preferredExpectedPresentationTime,
225 nsecs_t preferredDeadlineTimestamp) const;
Rachel Lee3f028662021-11-04 19:32:24 +0000226
Leon Scroggins III67388622023-02-06 20:36:20 -0500227 scheduler::VSyncDispatch::Callback createDispatchCallback();
228
Leon Scroggins IIIb2253602023-04-19 17:04:04 -0400229 // Returns the old registration so it can be destructed outside the lock to
230 // avoid deadlock.
231 scheduler::VSyncCallbackRegistration onNewVsyncScheduleInternal(
232 std::shared_ptr<scheduler::VsyncSchedule>) EXCLUDES(mMutex);
233
Ady Abraham011f8ba2022-11-22 15:09:07 -0800234 const char* const mThreadName;
235 TracedOrdinal<int> mVsyncTracer;
236 TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mMutex);
237 std::chrono::nanoseconds mReadyDuration GUARDED_BY(mMutex);
Leon Scroggins IIIed66a352023-03-23 17:55:43 -0400238 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule GUARDED_BY(mMutex);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800239 TimePoint mLastVsyncCallbackTime GUARDED_BY(mMutex) = TimePoint::now();
ramindani92ab9872024-07-26 15:09:37 -0700240 TimePoint mLastCommittedVsyncTime GUARDED_BY(mMutex) = TimePoint::now();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800241 scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700242 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800243
Melody Hsue524dd92024-08-27 22:27:29 +0000244 // All consumers that need to recover from buffer stuffing and the number
245 // of their queued buffers.
246 BufferStuffingMap mBufferStuffedUids GUARDED_BY(mMutex);
247
Ady Abrahamf2851612023-09-25 17:19:00 -0700248 IEventThreadCallback& mCallback;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800249
Lloyd Pique46a46b32018-01-31 19:01:18 -0800250 std::thread mThread;
251 mutable std::mutex mMutex;
252 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800253
Ana Krulec85c39af2018-12-26 17:29:57 -0800254 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800255 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800256
257 // VSYNC state of connected display.
258 struct VSyncState {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800259 // Number of VSYNC events since display was connected.
260 uint32_t count = 0;
261
262 // True if VSYNC should be faked, e.g. when display is off.
263 bool synthetic = false;
Ady Abraham8e3e2ea2024-10-31 15:52:31 -0700264
265 // True if VSYNC should not be delivered to apps. Used when the display is off.
266 bool omitted = false;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800267 };
268
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800269 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
270 // and support headless mode by injecting a fake display with synthetic VSYNC.
271 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800272
Dominik Laskowski029cc122019-01-23 19:52:06 -0800273 // State machine for event loop.
274 enum class State {
275 Idle,
276 Quit,
277 SyntheticVSync,
278 VSync,
279 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700280
Dominik Laskowski029cc122019-01-23 19:52:06 -0800281 State mState GUARDED_BY(mMutex) = State::Idle;
282
283 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800284};
285
286// ---------------------------------------------------------------------------
287
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800288} // namespace impl
289} // namespace android