blob: a41188569cebca3e88d29fa132ddb007b25f516f [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
Dominik Laskowskif654d572018-12-20 11:03:06 -080047using ResyncCallback = std::function<void()>;
48
Lloyd Piquee83f9312018-02-01 12:53:17 -080049class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070050public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080051 class Callback {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070052 public:
53 virtual ~Callback() {}
54 virtual void onVSyncEvent(nsecs_t when) = 0;
55 };
56
57 virtual ~VSyncSource() {}
58 virtual void setVSyncEnabled(bool enable) = 0;
Lloyd Piquee83f9312018-02-01 12:53:17 -080059 virtual void setCallback(Callback* callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070060 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070061};
62
Ana Krulec85c39af2018-12-26 17:29:57 -080063class EventThreadConnection : public BnDisplayEventConnection {
64public:
Dominik Laskowskif654d572018-12-20 11:03:06 -080065 EventThreadConnection(EventThread* eventThread, ResyncCallback resyncCallback);
Ana Krulec85c39af2018-12-26 17:29:57 -080066 virtual ~EventThreadConnection();
67
68 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
69
70 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
71 status_t setVsyncRate(uint32_t count) override;
72 void requestNextVsync() override; // asynchronous
Ana Krulec7d1d6832018-12-27 11:10:09 -080073 // Requesting Vsync for HWC does not reset the idle timer, since HWC requires a refresh
74 // in order to update the configs.
75 void requestNextVsyncForHWC();
Ana Krulec85c39af2018-12-26 17:29:57 -080076
Dominik Laskowskif654d572018-12-20 11:03:06 -080077 // Called in response to requestNextVsync.
78 const ResyncCallback resyncCallback;
79
Ana Krulec85c39af2018-12-26 17:29:57 -080080 // count >= 1 : continuous event. count is the vsync rate
81 // count == 0 : one-shot event that has not fired
82 // count ==-1 : one-shot event that fired this round / disabled
83 int32_t count;
84
85private:
86 virtual void onFirstRef();
87 EventThread* const mEventThread;
88 gui::BitTube mChannel;
89};
90
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080091class EventThread {
92public:
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070093 // TODO: Remove once stable display IDs are plumbed through SF/WM interface.
94 enum class DisplayType { Primary, External };
95
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080096 virtual ~EventThread();
97
Dominik Laskowskif654d572018-12-20 11:03:06 -080098 virtual sp<EventThreadConnection> createEventConnection(
99 ResyncCallback resyncCallback) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800100
101 // called before the screen is turned off from main thread
102 virtual void onScreenReleased() = 0;
103
104 // called after the screen is turned on from main thread
105 virtual void onScreenAcquired() = 0;
106
107 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700108 virtual void onHotplugReceived(DisplayType displayType, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800109
Yiwei Zhang5434a782018-12-05 18:06:32 -0800110 virtual void dump(std::string& result) const = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800111
112 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -0800113
114 virtual status_t registerDisplayEventConnection(
115 const sp<EventThreadConnection>& connection) = 0;
116 virtual void setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) = 0;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800117 // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer.
118 virtual void requestNextVsync(const sp<EventThreadConnection>& connection,
119 bool resetIdleTimer) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800120};
121
122namespace impl {
123
124class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800125public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800126 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
Ana Krulecfb772822018-11-30 10:44:07 +0100127 using ResetIdleTimerCallback = std::function<void()>;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800128
Ana Krulec98b5b242018-08-10 15:03:23 -0700129 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
Dominik Laskowskif654d572018-12-20 11:03:06 -0800130 EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
131 const char* threadName);
Ana Krulec98b5b242018-08-10 15:03:23 -0700132 EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +0100133 const InterceptVSyncsCallback& interceptVSyncsCallback,
134 const ResetIdleTimerCallback& resetIdleTimerCallback, const char* threadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800135 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800136
Dominik Laskowskif654d572018-12-20 11:03:06 -0800137 sp<EventThreadConnection> createEventConnection(ResyncCallback resyncCallback) const override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800138
Ana Krulec85c39af2018-12-26 17:29:57 -0800139 status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override;
140 void setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800141 void requestNextVsync(const sp<EventThreadConnection>& connection,
142 bool resetIdleTimer) override;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800143
Mathias Agopian22ffb112012-04-10 21:04:02 -0700144 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800145 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700146
147 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800148 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800149
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700150 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700151 void onHotplugReceived(DisplayType displayType, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700152
Yiwei Zhang5434a782018-12-05 18:06:32 -0800153 void dump(std::string& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800154
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800155 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700156
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800157private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800158 friend EventThreadTest;
159
Ana Krulec98b5b242018-08-10 15:03:23 -0700160 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
161 EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700162 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
163
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700164
Lloyd Pique46a46b32018-01-31 19:01:18 -0800165 void threadMain();
Ana Krulec85c39af2018-12-26 17:29:57 -0800166 std::vector<sp<EventThreadConnection>> waitForEventLocked(std::unique_lock<std::mutex>* lock,
167 DisplayEventReceiver::Event* event)
Lloyd Pique46a46b32018-01-31 19:01:18 -0800168 REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700169
Ana Krulec85c39af2018-12-26 17:29:57 -0800170 void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection)
171 REQUIRES(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800172 void enableVSyncLocked() REQUIRES(mMutex);
173 void disableVSyncLocked() REQUIRES(mMutex);
174
175 // Implements VSyncSource::Callback
176 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800177
Ana Krulec7d1d6832018-12-27 11:10:09 -0800178 // Acquires mutex and requests next vsync.
179 void requestNextVsyncInternal(const sp<EventThreadConnection>& connection) EXCLUDES(mMutex);
180
Ana Krulec98b5b242018-08-10 15:03:23 -0700181 // TODO(b/113612090): Once the Scheduler is complete this pointer will become obsolete.
182 VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr;
183 std::unique_ptr<VSyncSource> mVSyncSourceUnique GUARDED_BY(mMutex) = nullptr;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800184 // constants
Lloyd Pique24b0a482018-03-09 18:52:26 -0800185 const InterceptVSyncsCallback mInterceptVSyncsCallback;
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
191 // protected by mLock
Ana Krulec85c39af2018-12-26 17:29:57 -0800192 std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex);
Chia-I Wueac3db22018-09-14 11:28:03 -0700193 std::queue<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700194 std::array<DisplayEventReceiver::Event, 2> mVSyncEvent GUARDED_BY(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800195 bool mUseSoftwareVSync GUARDED_BY(mMutex) = false;
196 bool mVsyncEnabled GUARDED_BY(mMutex) = false;
197 bool mKeepRunning GUARDED_BY(mMutex) = true;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700198
199 // for debugging
Lloyd Pique46a46b32018-01-31 19:01:18 -0800200 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false;
Ana Krulecfb772822018-11-30 10:44:07 +0100201
202 // Callback that resets the idle timer when the next vsync is received.
203 ResetIdleTimerCallback mResetIdleTimer;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800204};
205
206// ---------------------------------------------------------------------------
207
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800208} // namespace impl
209} // namespace android