blob: aa27091908778bd94983fceb05a6a3f040449e53 [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
Leon Scroggins III31d41412022-11-18 16:42:53 -050026#include <scheduler/Fps.h>
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
Marin Shalamanov23c44202020-12-22 19:09:20 +010036#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
Dominik Laskowskif654d572018-12-20 11:03:06 -080058using ResyncCallback = std::function<void()>;
Ady Abraham62f216c2020-10-13 19:07:23 -070059using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
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
Leon Scroggins III31d41412022-11-18 16:42:53 -050071class IEventThreadCallback {
72public:
73 virtual ~IEventThreadCallback() = default;
74
75 virtual bool isVsyncTargetForUid(TimePoint expectedVsyncTime, uid_t uid) const = 0;
76
77 virtual Fps getLeaderRenderFrameRate(uid_t uid) const = 0;
78};
79
Huihong Luo6fac5232021-11-22 16:05:23 -080080class EventThreadConnection : public gui::BnDisplayEventConnection {
Ana Krulec85c39af2018-12-26 17:29:57 -080081public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070082 EventThreadConnection(EventThread*, uid_t callingUid, ResyncCallback,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070083 EventRegistrationFlags eventRegistration = {});
Ana Krulec85c39af2018-12-26 17:29:57 -080084 virtual ~EventThreadConnection();
85
86 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
87
Huihong Luo6fac5232021-11-22 16:05:23 -080088 binder::Status stealReceiveChannel(gui::BitTube* outChannel) override;
89 binder::Status setVsyncRate(int rate) override;
90 binder::Status requestNextVsync() override; // asynchronous
Rachel Leeb9c5a772022-02-04 21:17:37 -080091 binder::Status getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080092
Dominik Laskowskif654d572018-12-20 11:03:06 -080093 // Called in response to requestNextVsync.
94 const ResyncCallback resyncCallback;
95
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080096 VSyncRequest vsyncRequest = VSyncRequest::None;
Ady Abraham0bb6a472020-10-12 10:22:13 -070097 const uid_t mOwnerUid;
Huihong Luo1b0c49f2022-03-15 19:18:21 -070098 const EventRegistrationFlags mEventRegistration;
Ady Abraham0bb6a472020-10-12 10:22:13 -070099
Rachel Lee2248f522023-01-27 16:45:23 -0800100 /** The frame rate set to the attached choreographer. */
101 Fps frameRate;
102
Ana Krulec85c39af2018-12-26 17:29:57 -0800103private:
104 virtual void onFirstRef();
105 EventThread* const mEventThread;
Ady Abraham899e8cd2022-06-06 15:16:06 -0700106 std::mutex mLock;
107 gui::BitTube mChannel GUARDED_BY(mLock);
Ady Abraham62f216c2020-10-13 19:07:23 -0700108
109 std::vector<DisplayEventReceiver::Event> mPendingEvents;
Ana Krulec85c39af2018-12-26 17:29:57 -0800110};
111
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800112class EventThread {
113public:
114 virtual ~EventThread();
115
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700116 virtual sp<EventThreadConnection> createEventConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700117 ResyncCallback, EventRegistrationFlags eventRegistration = {}) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800118
119 // called before the screen is turned off from main thread
120 virtual void onScreenReleased() = 0;
121
122 // called after the screen is turned on from main thread
123 virtual void onScreenAcquired() = 0;
124
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800125 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800126
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100127 // called when SF changes the active mode and apps needs to be notified about the change
Ady Abraham67434eb2022-12-01 17:48:12 -0800128 virtual void onModeChanged(const scheduler::FrameRateMode&) = 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;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800144 virtual VsyncEventData getLatestVsyncEventData(
145 const sp<EventThreadConnection>& connection) const = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800146
147 // Retrieves the number of event connections tracked by this EventThread.
148 virtual size_t getEventThreadConnectionCount() = 0;
Leon Scroggins III31d41412022-11-18 16:42:53 -0500149
150 virtual void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800151};
152
153namespace impl {
154
Ady Abraham011f8ba2022-11-22 15:09:07 -0800155class EventThread : public android::EventThread {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800156public:
Leon Scroggins III31d41412022-11-18 16:42:53 -0500157 EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule>, IEventThreadCallback&,
158 frametimeline::TokenManager*, std::chrono::nanoseconds workDuration,
159 std::chrono::nanoseconds readyDuration);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800160 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800161
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700162 sp<EventThreadConnection> createEventConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700163 ResyncCallback, EventRegistrationFlags eventRegistration = {}) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800164
Ana Krulec85c39af2018-12-26 17:29:57 -0800165 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800166 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700167 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800168 VsyncEventData getLatestVsyncEventData(
169 const sp<EventThreadConnection>& connection) const override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800170
Mathias Agopian22ffb112012-04-10 21:04:02 -0700171 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800172 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700173
174 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800175 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800176
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800177 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700178
Ady Abraham67434eb2022-12-01 17:48:12 -0800179 void onModeChanged(const scheduler::FrameRateMode&) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800180
Ady Abraham62f216c2020-10-13 19:07:23 -0700181 void onFrameRateOverridesChanged(PhysicalDisplayId displayId,
182 std::vector<FrameRateOverride> overrides) override;
183
Yiwei Zhang5434a782018-12-05 18:06:32 -0800184 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800185
Ady Abraham9c53ee72020-07-22 21:16:18 -0700186 void setDuration(std::chrono::nanoseconds workDuration,
187 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700188
Alec Mouri717bcb62020-02-10 17:07:19 -0800189 size_t getEventThreadConnectionCount() override;
190
Leon Scroggins III31d41412022-11-18 16:42:53 -0500191 void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) override;
192
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800193private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800194 friend EventThreadTest;
195
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800196 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
197
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800198 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700199
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800200 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
201 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
202 void dispatchEvent(const DisplayEventReceiver::Event& event,
203 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700204
Ana Krulec85c39af2018-12-26 17:29:57 -0800205 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
206 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800207
Ady Abraham011f8ba2022-11-22 15:09:07 -0800208 void onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime);
Mathias Agopian23748662011-12-05 14:33:34 -0800209
Rachel Lee8d0c6102021-11-03 22:00:25 +0000210 int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800211 nsecs_t expectedPresentationTime) const;
212 void generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval,
213 nsecs_t timestamp, nsecs_t preferredExpectedPresentationTime,
214 nsecs_t preferredDeadlineTimestamp) const;
Rachel Lee3f028662021-11-04 19:32:24 +0000215
Leon Scroggins III31d41412022-11-18 16:42:53 -0500216 scheduler::VSyncDispatch::Callback createDispatchCallback();
217
Ady Abraham011f8ba2022-11-22 15:09:07 -0800218 const char* const mThreadName;
219 TracedOrdinal<int> mVsyncTracer;
220 TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mMutex);
221 std::chrono::nanoseconds mReadyDuration GUARDED_BY(mMutex);
Leon Scroggins III31d41412022-11-18 16:42:53 -0500222 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800223 TimePoint mLastVsyncCallbackTime GUARDED_BY(mMutex) = TimePoint::now();
224 scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700225 frametimeline::TokenManager* const mTokenManager;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800226
Leon Scroggins III31d41412022-11-18 16:42:53 -0500227 // mEventThreadCallback will outlive the EventThread.
228 IEventThreadCallback& mEventThreadCallback;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800229
Lloyd Pique46a46b32018-01-31 19:01:18 -0800230 std::thread mThread;
231 mutable std::mutex mMutex;
232 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800233
Ana Krulec85c39af2018-12-26 17:29:57 -0800234 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800235 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800236
237 // VSYNC state of connected display.
238 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800239 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800240
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800241 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800242
243 // Number of VSYNC events since display was connected.
244 uint32_t count = 0;
245
246 // True if VSYNC should be faked, e.g. when display is off.
247 bool synthetic = false;
248 };
249
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800250 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
251 // and support headless mode by injecting a fake display with synthetic VSYNC.
252 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800253
Dominik Laskowski029cc122019-01-23 19:52:06 -0800254 // State machine for event loop.
255 enum class State {
256 Idle,
257 Quit,
258 SyntheticVSync,
259 VSync,
260 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700261
Dominik Laskowski029cc122019-01-23 19:52:06 -0800262 State mState GUARDED_BY(mMutex) = State::Idle;
263
264 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800265};
266
267// ---------------------------------------------------------------------------
268
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800269} // namespace impl
270} // namespace android