blob: e11048845e44cdc1166cdeed39958b00d6c94ff8 [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
21#include <array>
Lloyd Pique46a46b32018-01-31 19:01:18 -080022#include <condition_variable>
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070023#include <cstdint>
Lloyd Pique46a46b32018-01-31 19:01:18 -080024#include <mutex>
Chia-I Wueac3db22018-09-14 11:28:03 -070025#include <queue>
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>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036
37// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039// ---------------------------------------------------------------------------
40
Ana Krulec85c39af2018-12-26 17:29:57 -080041class EventThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080042class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043class SurfaceFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
45// ---------------------------------------------------------------------------
46
Lloyd Piquee83f9312018-02-01 12:53:17 -080047class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070048public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080049 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070050 public:
51 virtual ~Callback() {}
52 virtual void onVSyncEvent(nsecs_t when) = 0;
53 };
54
55 virtual ~VSyncSource() {}
56 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080057 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070058 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070059};
60
Ana Krulec85c39af2018-12-26 17:29:57 -080061class EventThreadConnection : public BnDisplayEventConnection {
62public:
63 explicit EventThreadConnection(EventThread* eventThread);
64 virtual ~EventThreadConnection();
65
66 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
67
68 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
69 status_t setVsyncRate(uint32_t count) override;
70 void requestNextVsync() override; // asynchronous
Ana Krulec7d1d6832018-12-27 11:10:09 -080071 // Requesting Vsync for HWC does not reset the idle timer, since HWC requires a refresh
72 // in order to update the configs.
73 void requestNextVsyncForHWC();
Ana Krulec85c39af2018-12-26 17:29:57 -080074
75 // count >= 1 : continuous event. count is the vsync rate
76 // count == 0 : one-shot event that has not fired
77 // count ==-1 : one-shot event that fired this round / disabled
78 int32_t count;
79
80private:
81 virtual void onFirstRef();
82 EventThread* const mEventThread;
83 gui::BitTube mChannel;
84};
85
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080086class EventThread {
87public:
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070088 // TODO: Remove once stable display IDs are plumbed through SF/WM interface.
89 enum class DisplayType { Primary, External };
90
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080091 virtual ~EventThread();
92
Ana Krulec85c39af2018-12-26 17:29:57 -080093 virtual sp<EventThreadConnection> createEventConnection() const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080094
95 // called before the screen is turned off from main thread
96 virtual void onScreenReleased() = 0;
97
98 // called after the screen is turned on from main thread
99 virtual void onScreenAcquired() = 0;
100
101 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700102 virtual void onHotplugReceived(DisplayType displayType, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800103
Yiwei Zhang5434a782018-12-05 18:06:32 -0800104 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800105
106 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800107
108 virtual status_t registerDisplayEventConnection(
109 const sp<EventThreadConnection>& connection) = 0;
110 virtual void setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800111 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
112 virtual void requestNextVsync(const sp<EventThreadConnection>& connection,
113 bool resetIdleTimer) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800114};
115
116namespace impl {
117
118class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800119public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800120 using ResyncWithRateLimitCallback = std::function<void()>;
121 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
Ana Krulecfb772822018-11-30 10:44:07 +0100122 using ResetIdleTimerCallback = std::function<void()>;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123
Ana Krulec98b5b242018-08-10 15:03:23 -0700124 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125 EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback,
126 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
Ana Krulec98b5b242018-08-10 15:03:23 -0700127 EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +0100128 const ResyncWithRateLimitCallback& resyncWithRateLimitCallback,
129 const InterceptVSyncsCallback& interceptVSyncsCallback,
130 const ResetIdleTimerCallback& resetIdleTimerCallback, const char* threadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800131 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800132
Ana Krulec85c39af2018-12-26 17:29:57 -0800133 sp<EventThreadConnection> createEventConnection() const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800134
Ana Krulec85c39af2018-12-26 17:29:57 -0800135 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
136 void setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800137 void requestNextVsync(const sp<EventThreadConnection>& connection,
138 bool resetIdleTimer) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800139
Mathias Agopian22ffb112012-04-10 21:04:02 -0700140 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800141 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700142
143 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800144 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800145
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700146 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700147 void onHotplugReceived(DisplayType displayType, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700148
Yiwei Zhang5434a782018-12-05 18:06:32 -0800149 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800150
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800151 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700152
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800153private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800154 friend EventThreadTest;
155
Ana Krulec98b5b242018-08-10 15:03:23 -0700156 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
157 EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
158 ResyncWithRateLimitCallback resyncWithRateLimitCallback,
159 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
160
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700161
Lloyd Pique46a46b32018-01-31 19:01:18 -0800162 void threadMain();
Ana Krulec85c39af2018-12-26 17:29:57 -0800163 std::vector<sp<EventThreadConnection>> waitForEventLocked(std::unique_lock<std::mutex>* lock,
164 DisplayEventReceiver::Event* event)
Lloyd Pique46a46b32018-01-31 19:01:18 -0800165 REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700166
Ana Krulec85c39af2018-12-26 17:29:57 -0800167 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
168 REQUIRES(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800169 void enableVSyncLocked() REQUIRES(mMutex);
170 void disableVSyncLocked() REQUIRES(mMutex);
171
172 // Implements VSyncSource::Callback
173 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800174
Ana Krulec7d1d6832018-12-27 11:10:09 -0800175 // Acquires mutex and requests next vsync.
176 void requestNextVsyncInternal(const sp<EventThreadConnection>& connection) EXCLUDES(mMutex);
177
Ana Krulec98b5b242018-08-10 15:03:23 -0700178 // TODO(b/113612090): Once the Scheduler is complete this pointer will become obsolete.
179 VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr;
180 std::unique_ptr<VSyncSource> mVSyncSourceUnique GUARDED_BY(mMutex) = nullptr;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800181 // constants
Lloyd Pique24b0a482018-03-09 18:52:26 -0800182 const ResyncWithRateLimitCallback mResyncWithRateLimitCallback;
183 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800184
Lloyd Pique46a46b32018-01-31 19:01:18 -0800185 std::thread mThread;
186 mutable std::mutex mMutex;
187 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800188
189 // protected by mLock
Ana Krulec85c39af2018-12-26 17:29:57 -0800190 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Chia-I Wueac3db22018-09-14 11:28:03 -0700191 std::queue<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700192 std::array<DisplayEventReceiver::Event, 2> mVSyncEvent GUARDED_BY(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800193 bool mUseSoftwareVSync GUARDED_BY(mMutex) = false;
194 bool mVsyncEnabled GUARDED_BY(mMutex) = false;
195 bool mKeepRunning GUARDED_BY(mMutex) = true;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700196
197 // for debugging
Lloyd Pique46a46b32018-01-31 19:01:18 -0800198 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false;
Ana Krulecfb772822018-11-30 10:44:07 +0100199
200 // Callback that resets the idle timer when the next vsync is received.
201 ResetIdleTimerCallback mResetIdleTimer;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800202};
203
204// ---------------------------------------------------------------------------
205
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800206} // namespace impl
207} // namespace android