blob: a42546c7dfea25cede386cb75615ad173e9dc262 [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
Mathias Agopiand0566bc2011-11-17 17:49:17 -080019#include <sys/types.h>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070020
Lloyd Pique46a46b32018-01-31 19:01:18 -080021#include <condition_variable>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070022#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080023#include <deque>
Lloyd Pique46a46b32018-01-31 19:01:18 -080024#include <mutex>
Dominik Laskowski1eba0202019-01-24 09:14:40 -080025#include <optional>
Lloyd Pique46a46b32018-01-31 19:01:18 -080026#include <thread>
Chia-I Wu98cd38f2018-09-14 11:53:25 -070027#include <vector>
Lloyd Pique46a46b32018-01-31 19:01:18 -080028
29#include <android-base/thread_annotations.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030
Mathias Agopiancb9732a2012-04-03 17:48:03 -070031#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032#include <gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080033#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034
35#include <utils/Errors.h>
Ady Abraham2139f732019-11-13 18:56:40 -080036#include "HwcStrongTypes.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037
38// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040// ---------------------------------------------------------------------------
41
Ana Krulec85c39af2018-12-26 17:29:57 -080042class EventThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080043class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044class SurfaceFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045
46// ---------------------------------------------------------------------------
47
Dominik Laskowskif654d572018-12-20 11:03:06 -080048using ResyncCallback = std::function<void()>;
49
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080050enum class VSyncRequest {
51 None = -1,
52 Single = 0,
53 Periodic = 1,
54 // Subsequent values are periods.
55};
56
Lloyd Piquee83f9312018-02-01 12:53:17 -080057class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070058public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080059 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070060 public:
61 virtual ~Callback() {}
62 virtual void onVSyncEvent(nsecs_t when) = 0;
63 };
64
65 virtual ~VSyncSource() {}
Dominik Laskowski6505f792019-09-18 11:10:05 -070066
67 virtual const char* getName() const = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070068 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080069 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070070 virtual void setPhaseOffset(nsecs_t phaseOffset) = 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;
Ady Abraham0f4a1b12019-06-04 16:04:04 -070089 const ISurfaceComposer::ConfigChanged configChanged;
Ana Krulec85c39af2018-12-26 17:29:57 -080090
91private:
92 virtual void onFirstRef();
93 EventThread* const mEventThread;
94 gui::BitTube mChannel;
95};
96
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080097class EventThread {
98public:
99 virtual ~EventThread();
100
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700101 virtual sp<EventThreadConnection> createEventConnection(
102 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800103
104 // called before the screen is turned off from main thread
105 virtual void onScreenReleased() = 0;
106
107 // called after the screen is turned on from main thread
108 virtual void onScreenAcquired() = 0;
109
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800110 virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800111
Ady Abraham447052e2019-02-13 16:07:27 -0800112 // called when SF changes the active config and apps needs to be notified about the change
Ady Abraham2139f732019-11-13 18:56:40 -0800113 virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId) = 0;
Ady Abraham447052e2019-02-13 16:07:27 -0800114
Yiwei Zhang5434a782018-12-05 18:06:32 -0800115 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800116
117 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800118
119 virtual status_t registerDisplayEventConnection(
120 const sp<EventThreadConnection>& connection) = 0;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800121 virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800122 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
Ady Abraham8532d012019-05-08 14:50:56 -0700123 virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800124};
125
126namespace impl {
127
128class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800129public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800130 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
131
Dominik Laskowski6505f792019-09-18 11:10:05 -0700132 EventThread(std::unique_ptr<VSyncSource>, InterceptVSyncsCallback);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800133 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800134
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700135 sp<EventThreadConnection> createEventConnection(
136 ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800137
Ana Krulec85c39af2018-12-26 17:29:57 -0800138 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800139 void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override;
Ady Abraham8532d012019-05-08 14:50:56 -0700140 void requestNextVsync(const sp<EventThreadConnection>& connection) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800141
Mathias Agopian22ffb112012-04-10 21:04:02 -0700142 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800143 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700144
145 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800146 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800147
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800148 void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700149
Ady Abraham2139f732019-11-13 18:56:40 -0800150 void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId) override;
Ady Abraham447052e2019-02-13 16:07:27 -0800151
Yiwei Zhang5434a782018-12-05 18:06:32 -0800152 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800153
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800154 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700155
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800156private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800157 friend EventThreadTest;
158
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800159 using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>;
160
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800161 void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700162
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800163 bool shouldConsumeEvent(const DisplayEventReceiver::Event& event,
164 const sp<EventThreadConnection>& connection) const REQUIRES(mMutex);
165 void dispatchEvent(const DisplayEventReceiver::Event& event,
166 const DisplayEventConsumers& consumers) REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700167
Ana Krulec85c39af2018-12-26 17:29:57 -0800168 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
169 REQUIRES(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800170
Lloyd Pique46a46b32018-01-31 19:01:18 -0800171 // Implements VSyncSource::Callback
172 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800173
Dominik Laskowski6505f792019-09-18 11:10:05 -0700174 const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800175
Lloyd Pique24b0a482018-03-09 18:52:26 -0800176 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800177 const char* const mThreadName;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800178
Lloyd Pique46a46b32018-01-31 19:01:18 -0800179 std::thread mThread;
180 mutable std::mutex mMutex;
181 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800182
Ana Krulec85c39af2018-12-26 17:29:57 -0800183 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800184 std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800185
186 // VSYNC state of connected display.
187 struct VSyncState {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800188 explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {}
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800189
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800190 const PhysicalDisplayId displayId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800191
192 // Number of VSYNC events since display was connected.
193 uint32_t count = 0;
194
195 // True if VSYNC should be faked, e.g. when display is off.
196 bool synthetic = false;
197 };
198
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800199 // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals,
200 // and support headless mode by injecting a fake display with synthetic VSYNC.
201 std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800202
Dominik Laskowski029cc122019-01-23 19:52:06 -0800203 // State machine for event loop.
204 enum class State {
205 Idle,
206 Quit,
207 SyntheticVSync,
208 VSync,
209 };
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700210
Dominik Laskowski029cc122019-01-23 19:52:06 -0800211 State mState GUARDED_BY(mMutex) = State::Idle;
212
213 static const char* toCString(State);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800214};
215
216// ---------------------------------------------------------------------------
217
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800218} // namespace impl
219} // namespace android