blob: 50378604821c4658f0cb4fb416118131b60e8848 [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
Dominik Laskowskif654d572018-12-20 11:03:06 -080057using ResyncCallback = std::function<void()>;
Ady Abraham62f216c2020-10-13 19:07:23 -070058using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
Dominik Laskowskif654d572018-12-20 11:03:06 -080059
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080060enum class VSyncRequest {
Tim Murrayb5daa912020-09-09 21:29:13 +000061 None = -2,
62 // Single wakes up for the next two frames to avoid scheduler overhead
63 Single = -1,
64 // SingleSuppressCallback only wakes up for the next frame
65 SingleSuppressCallback = 0,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080066 Periodic = 1,
67 // Subsequent values are periods.
68};
69
Huihong Luo6fac5232021-11-22 16:05:23 -080070class EventThreadConnection : public gui::BnDisplayEventConnection {
Ana Krulec85c39af2018-12-26 17:29:57 -080071public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070072 EventThreadConnection(EventThread*, uid_t callingUid, ResyncCallback,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070073 EventRegistrationFlags eventRegistration = {});
Ana Krulec85c39af2018-12-26 17:29:57 -080074 virtual ~EventThreadConnection();
75
76 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
77
Huihong Luo6fac5232021-11-22 16:05:23 -080078 binder::Status stealReceiveChannel(gui::BitTube* outChannel) override;
79 binder::Status setVsyncRate(int rate) override;
80 binder::Status requestNextVsync() override; // asynchronous
Rachel Leeb9c5a772022-02-04 21:17:37 -080081 binder::Status getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080082
Dominik Laskowskif654d572018-12-20 11:03:06 -080083 // Called in response to requestNextVsync.
84 const ResyncCallback resyncCallback;
85
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080086 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0bb6a472020-10-12 10:22:13 -070087 const uid_t mOwnerUid;
Huihong Luo1b0c49f2022-03-15 19:18:21 -070088 const EventRegistrationFlags mEventRegistration;
Ady Abraham0bb6a472020-10-12 10:22:13 -070089
Rachel Lee2248f522023-01-27 16:45:23 -080090 /** The frame rate set to the attached choreographer. */
91 Fps frameRate;
92
Ana Krulec85c39af2018-12-26 17:29:57 -080093private:
94 virtual void onFirstRef();
95 EventThread* const mEventThread;
Ady Abraham899e8cd2022-06-06 15:16:06 -070096 std::mutex mLock;
97 gui::BitTube mChannel GUARDED_BY(mLock);
Ady Abraham62f216c2020-10-13 19:07:23 -070098
99 std::vector<DisplayEventReceiver::Event> mPendingEvents;
Ana Krulec85c39af2018-12-26 17:29:57 -0800100};
101
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800102class EventThread {
103public:
104 virtual ~EventThread();
105
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700106 virtual sp<EventThreadConnection> createEventConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700107 ResyncCallback, EventRegistrationFlags eventRegistration = {}) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800108
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500109 // Feed clients with fake VSYNC, e.g. while the display is off.
110 virtual void enableSyntheticVsync(bool) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800111
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800112 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800113
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100114 // called when SF changes the active mode and apps needs to be notified about the change
Ady Abraham67434eb2022-12-01 17:48:12 -0800115 virtual void onModeChanged(const scheduler::FrameRateMode&) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800116
Ady Abraham62f216c2020-10-13 19:07:23 -0700117 // called when SF updates the Frame Rate Override list
118 virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
119 std::vector<FrameRateOverride> overrides) = 0;
120
Yiwei Zhang5434a782018-12-05 18:06:32 -0800121 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800122
Ady Abraham9c53ee72020-07-22 21:16:18 -0700123 virtual void setDuration(std::chrono::nanoseconds workDuration,
124 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800125
126 virtual status_t registerDisplayEventConnection(
127 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800128 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800129 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700130 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800131 virtual VsyncEventData getLatestVsyncEventData(
132 const sp<EventThreadConnection>& connection) const = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800133
134 // Retrieves the number of event connections tracked by this EventThread.
135 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800136};
137
138namespace impl {
139
Ady Abraham011f8ba2022-11-22 15:09:07 -0800140class EventThread : public android::EventThread {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800141public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500142 using ThrottleVsyncCallback = std::function<bool(nsecs_t, uid_t)>;
143 using GetVsyncPeriodFunction = std::function<nsecs_t(uid_t)>;
144
145 EventThread(const char* name, scheduler::VsyncSchedule&, frametimeline::TokenManager*,
146 ThrottleVsyncCallback, GetVsyncPeriodFunction,
147 std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800148 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800149
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700150 sp<EventThreadConnection> createEventConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700151 ResyncCallback, EventRegistrationFlags eventRegistration = {}) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800152
Ana Krulec85c39af2018-12-26 17:29:57 -0800153 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800154 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700155 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800156 VsyncEventData getLatestVsyncEventData(
157 const sp<EventThreadConnection>& connection) const override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800158
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500159 void enableSyntheticVsync(bool) override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800160
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800161 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700162
Ady Abraham67434eb2022-12-01 17:48:12 -0800163 void onModeChanged(const scheduler::FrameRateMode&) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800164
Ady Abraham62f216c2020-10-13 19:07:23 -0700165 void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
166 std::vector<FrameRateOverride> overrides) override;
167
Yiwei Zhang5434a782018-12-05 18:06:32 -0800168 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800169
Ady Abraham9c53ee72020-07-22 21:16:18 -0700170 void setDuration(std::chrono::nanoseconds workDuration,
171 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700172
Alec Mouri717bcb62020-02-10 17:07:19 -0800173 size_t getEventThreadConnectionCount() override;
174
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800175private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800176 friend EventThreadTest;
177
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800178 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
179
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800180 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700181
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800182 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
183 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
184 void dispatchEvent(const DisplayEventReceiver::Event& event,
185 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700186
Ana Krulec85c39af2018-12-26 17:29:57 -0800187 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
188 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800189
Ady Abraham011f8ba2022-11-22 15:09:07 -0800190 void onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime);
Mathias Agopian23748662011-12-05 14:33:34 -0800191
Rachel Lee8d0c6102021-11-03 22:00:25 +0000192 int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800193 nsecs_t expectedPresentationTime) const;
194 void generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval,
195 nsecs_t timestamp, nsecs_t preferredExpectedPresentationTime,
196 nsecs_t preferredDeadlineTimestamp) const;
Rachel Lee3f028662021-11-04 19:32:24 +0000197
Ady Abraham011f8ba2022-11-22 15:09:07 -0800198 const char* const mThreadName;
199 TracedOrdinal<int> mVsyncTracer;
200 TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mMutex);
201 std::chrono::nanoseconds mReadyDuration GUARDED_BY(mMutex);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500202 scheduler::VsyncSchedule& mVsyncSchedule;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800203 TimePoint mLastVsyncCallbackTime GUARDED_BY(mMutex) = TimePoint::now();
204 scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700205 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800206
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500207 const ThrottleVsyncCallback mThrottleVsyncCallback;
208 const GetVsyncPeriodFunction mGetVsyncPeriodFunction;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800209
Lloyd Pique46a46b32018-01-31 19:01:18 -0800210 std::thread mThread;
211 mutable std::mutex mMutex;
212 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800213
Ana Krulec85c39af2018-12-26 17:29:57 -0800214 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800215 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800216
217 // VSYNC state of connected display.
218 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800219 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800220
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800221 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800222
223 // Number of VSYNC events since display was connected.
224 uint32_t count = 0;
225
226 // True if VSYNC should be faked, e.g. when display is off.
227 bool synthetic = false;
228 };
229
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800230 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
231 // and support headless mode by injecting a fake display with synthetic VSYNC.
232 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800233
Dominik Laskowski029cc122019-01-23 19:52:06 -0800234 // State machine for event loop.
235 enum class State {
236 Idle,
237 Quit,
238 SyntheticVSync,
239 VSync,
240 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700241
Dominik Laskowski029cc122019-01-23 19:52:06 -0800242 State mState GUARDED_BY(mMutex) = State::Idle;
243
244 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800245};
246
247// ---------------------------------------------------------------------------
248
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800249} // namespace impl
250} // namespace android