blob: 49f624c35e606de60b5982ce4428240ecdee0c13 [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>
20#include <gui/DisplayEventReceiver.h>
21#include <gui/IDisplayEventConnection.h>
22#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
Ady Abraham2139f732019-11-13 18:56:40 -080034#include "HwcStrongTypes.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
44// ---------------------------------------------------------------------------
45
Dominik Laskowskif654d572018-12-20 11:03:06 -080046using ResyncCallback = std::function<void()>;
47
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080048enum class VSyncRequest {
49 None = -1,
50 Single = 0,
51 Periodic = 1,
52 // Subsequent values are periods.
53};
54
Lloyd Piquee83f9312018-02-01 12:53:17 -080055class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070056public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080057 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070058 public:
59 virtual ~Callback() {}
Ady Abraham5facfb12020-04-22 15:18:31 -070060 virtual void onVSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070061 };
62
63 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070064
65 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080067 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070068 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ady Abraham5e7371c2020-03-24 14:47:24 -070069
70 virtual void dump(std::string& result) const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070071};
72
Ana Krulec85c39af2018-12-26 17:29:57 -080073class EventThreadConnection : public BnDisplayEventConnection {
74public:
Ady Abraham0f4a1b12019-06-04 16:04:04 -070075 EventThreadConnection(EventThread*, ResyncCallback,
76 ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec85c39af2018-12-26 17:29:57 -080077 virtual ~EventThreadConnection();
78
79 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
80
81 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080082 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080083 void requestNextVsync() override; // asynchronous
84
Dominik Laskowskif654d572018-12-20 11:03:06 -080085 // Called in response to requestNextVsync.
86 const ResyncCallback resyncCallback;
87
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080088 VSyncRequest vsyncRequest = VSyncRequest::None;
Alec Mouri967d5d72020-08-05 12:50:03 -070089 const ISurfaceComposer::ConfigChanged mConfigChanged =
Alec Mouri60aee1c2019-10-28 16:18:59 -070090 ISurfaceComposer::ConfigChanged::eConfigChangedSuppress;
Ana Krulec85c39af2018-12-26 17:29:57 -080091
92private:
93 virtual void onFirstRef();
94 EventThread* const mEventThread;
95 gui::BitTube mChannel;
96};
97
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080098class EventThread {
99public:
100 virtual ~EventThread();
101
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700102 virtual sp<EventThreadConnection> createEventConnection(
103 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800104
105 // called before the screen is turned off from main thread
106 virtual void onScreenReleased() = 0;
107
108 // called after the screen is turned on from main thread
109 virtual void onScreenAcquired() = 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
Ady Abraham447052e2019-02-13 16:07:27 -0800113 // called when SF changes the active config and apps needs to be notified about the change
Alec Mouri60aee1c2019-10-28 16:18:59 -0700114 virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
115 nsecs_t vsyncPeriod) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800116
Yiwei Zhang5434a782018-12-05 18:06:32 -0800117 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800118
119 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800120
121 virtual status_t registerDisplayEventConnection(
122 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800123 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800124 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700125 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800126
127 // Retrieves the number of event connections tracked by this EventThread.
128 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800129};
130
131namespace impl {
132
133class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800134public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800135 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
136
Dominik Laskowski6505f792019-09-18 11:10:05 -0700137 EventThread(std::unique_ptr<VSyncSource>, InterceptVSyncsCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800138 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800139
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700140 sp<EventThreadConnection> createEventConnection(
141 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800142
Ana Krulec85c39af2018-12-26 17:29:57 -0800143 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800144 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700145 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800146
Mathias Agopian22ffb112012-04-10 21:04:02 -0700147 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800148 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700149
150 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800151 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800152
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800153 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700154
Alec Mouri60aee1c2019-10-28 16:18:59 -0700155 void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
156 nsecs_t vsyncPeriod) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800157
Yiwei Zhang5434a782018-12-05 18:06:32 -0800158 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800159
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800160 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700161
Alec Mouri717bcb62020-02-10 17:07:19 -0800162 size_t getEventThreadConnectionCount() override;
163
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800164private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800165 friend EventThreadTest;
166
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800167 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
168
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800169 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700170
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800171 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
172 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
173 void dispatchEvent(const DisplayEventReceiver::Event& event,
174 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700175
Ana Krulec85c39af2018-12-26 17:29:57 -0800176 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
177 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800178
Lloyd Pique46a46b32018-01-31 19:01:18 -0800179 // Implements VSyncSource::Callback
Ady Abraham5facfb12020-04-22 15:18:31 -0700180 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800181
Dominik Laskowski6505f792019-09-18 11:10:05 -0700182 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800183
Lloyd Pique24b0a482018-03-09 18:52:26 -0800184 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800185 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800186
Lloyd Pique46a46b32018-01-31 19:01:18 -0800187 std::thread mThread;
188 mutable std::mutex mMutex;
189 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800190
Ana Krulec85c39af2018-12-26 17:29:57 -0800191 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800192 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800193
194 // VSYNC state of connected display.
195 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800196 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800197
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800198 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800199
200 // Number of VSYNC events since display was connected.
201 uint32_t count = 0;
202
203 // True if VSYNC should be faked, e.g. when display is off.
204 bool synthetic = false;
205 };
206
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800207 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
208 // and support headless mode by injecting a fake display with synthetic VSYNC.
209 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800210
Dominik Laskowski029cc122019-01-23 19:52:06 -0800211 // State machine for event loop.
212 enum class State {
213 Idle,
214 Quit,
215 SyntheticVSync,
216 VSync,
217 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700218
Dominik Laskowski029cc122019-01-23 19:52:06 -0800219 State mState GUARDED_BY(mMutex) = State::Idle;
220
221 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800222};
223
224// ---------------------------------------------------------------------------
225
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800226} // namespace impl
227} // namespace android