blob: fa1ca64c863cffce964d531c5dd7af3198ad8908 [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 Abraham9c53ee72020-07-22 21:16:18 -070060 virtual void onVSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp,
61 nsecs_t deadlineTimestamp) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062 };
63
64 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070065
66 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070067 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080068 virtual void setCallback(Callback* callback) = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -070069 virtual void setDuration(std::chrono::nanoseconds workDuration,
70 std::chrono::nanoseconds readyDuration) = 0;
Ady Abraham5e7371c2020-03-24 14:47:24 -070071
72 virtual void dump(std::string& result) const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070073};
74
Ana Krulec85c39af2018-12-26 17:29:57 -080075class EventThreadConnection : public BnDisplayEventConnection {
76public:
Ady Abraham0f4a1b12019-06-04 16:04:04 -070077 EventThreadConnection(EventThread*, ResyncCallback,
78 ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec85c39af2018-12-26 17:29:57 -080079 virtual ~EventThreadConnection();
80
81 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
82
83 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080084 status_t setVsyncRate(uint32_t rate) override;
Ana Krulec85c39af2018-12-26 17:29:57 -080085 void requestNextVsync() override; // asynchronous
86
Dominik Laskowskif654d572018-12-20 11:03:06 -080087 // Called in response to requestNextVsync.
88 const ResyncCallback resyncCallback;
89
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080090 VSyncRequest vsyncRequest = VSyncRequest::None;
Alec Mouri967d5d72020-08-05 12:50:03 -070091 const ISurfaceComposer::ConfigChanged mConfigChanged =
Alec Mouri60aee1c2019-10-28 16:18:59 -070092 ISurfaceComposer::ConfigChanged::eConfigChangedSuppress;
Ana Krulec85c39af2018-12-26 17:29:57 -080093
94private:
95 virtual void onFirstRef();
96 EventThread* const mEventThread;
97 gui::BitTube mChannel;
98};
99
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800100class EventThread {
101public:
102 virtual ~EventThread();
103
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700104 virtual sp<EventThreadConnection> createEventConnection(
105 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800106
107 // called before the screen is turned off from main thread
108 virtual void onScreenReleased() = 0;
109
110 // called after the screen is turned on from main thread
111 virtual void onScreenAcquired() = 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
Ady Abraham447052e2019-02-13 16:07:27 -0800115 // called when SF changes the active config and apps needs to be notified about the change
Alec Mouri60aee1c2019-10-28 16:18:59 -0700116 virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
117 nsecs_t vsyncPeriod) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800118
Yiwei Zhang5434a782018-12-05 18:06:32 -0800119 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800120
Ady Abraham9c53ee72020-07-22 21:16:18 -0700121 virtual void setDuration(std::chrono::nanoseconds workDuration,
122 std::chrono::nanoseconds readyDuration) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800123
124 virtual status_t registerDisplayEventConnection(
125 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800126 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800127 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700128 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800129
130 // Retrieves the number of event connections tracked by this EventThread.
131 virtual size_t getEventThreadConnectionCount() = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800132};
133
134namespace impl {
135
136class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800137public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800138 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
139
Dominik Laskowski6505f792019-09-18 11:10:05 -0700140 EventThread(std::unique_ptr<VSyncSource>, InterceptVSyncsCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800141 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800142
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700143 sp<EventThreadConnection> createEventConnection(
144 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800145
Ana Krulec85c39af2018-12-26 17:29:57 -0800146 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800147 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700148 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800149
Mathias Agopian22ffb112012-04-10 21:04:02 -0700150 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800151 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700152
153 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800154 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800155
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800156 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700157
Alec Mouri60aee1c2019-10-28 16:18:59 -0700158 void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
159 nsecs_t vsyncPeriod) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800160
Yiwei Zhang5434a782018-12-05 18:06:32 -0800161 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800162
Ady Abraham9c53ee72020-07-22 21:16:18 -0700163 void setDuration(std::chrono::nanoseconds workDuration,
164 std::chrono::nanoseconds readyDuration) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700165
Alec Mouri717bcb62020-02-10 17:07:19 -0800166 size_t getEventThreadConnectionCount() override;
167
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800168private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800169 friend EventThreadTest;
170
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800171 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
172
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800173 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700174
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800175 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
176 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
177 void dispatchEvent(const DisplayEventReceiver::Event& event,
178 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700179
Ana Krulec85c39af2018-12-26 17:29:57 -0800180 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
181 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800182
Lloyd Pique46a46b32018-01-31 19:01:18 -0800183 // Implements VSyncSource::Callback
Ady Abraham9c53ee72020-07-22 21:16:18 -0700184 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
185 nsecs_t deadlineTimestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800186
Dominik Laskowski6505f792019-09-18 11:10:05 -0700187 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800188
Lloyd Pique24b0a482018-03-09 18:52:26 -0800189 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800190 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800191
Lloyd Pique46a46b32018-01-31 19:01:18 -0800192 std::thread mThread;
193 mutable std::mutex mMutex;
194 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800195
Ana Krulec85c39af2018-12-26 17:29:57 -0800196 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800197 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800198
199 // VSYNC state of connected display.
200 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800201 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800202
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800203 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800204
205 // Number of VSYNC events since display was connected.
206 uint32_t count = 0;
207
208 // True if VSYNC should be faked, e.g. when display is off.
209 bool synthetic = false;
210 };
211
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800212 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
213 // and support headless mode by injecting a fake display with synthetic VSYNC.
214 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800215
Dominik Laskowski029cc122019-01-23 19:52:06 -0800216 // State machine for event loop.
217 enum class State {
218 Idle,
219 Quit,
220 SyntheticVSync,
221 VSync,
222 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700223
Dominik Laskowski029cc122019-01-23 19:52:06 -0800224 State mState GUARDED_BY(mMutex) = State::Idle;
225
226 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800227};
228
229// ---------------------------------------------------------------------------
230
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800231} // namespace impl
232} // namespace android