blob: 3879ae48f64dfb51e558b2f5a04ae55abcffc62d [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>
27
28#include <android-base/thread_annotations.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029
Mathias Agopiancb9732a2012-04-03 17:48:03 -070030#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031#include <gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080032#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033
34#include <utils/Errors.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070035#include <utils/SortedVector.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
Lloyd Pique24b0a482018-03-09 18:52:26 -080041class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043class String8;
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
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080061class EventThread {
62public:
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070063 // TODO: Remove once stable display IDs are plumbed through SF/WM interface.
64 enum class DisplayType { Primary, External };
65
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080066 virtual ~EventThread();
67
68 virtual sp<BnDisplayEventConnection> createEventConnection() const = 0;
69
70 // called before the screen is turned off from main thread
71 virtual void onScreenReleased() = 0;
72
73 // called after the screen is turned on from main thread
74 virtual void onScreenAcquired() = 0;
75
76 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070077 virtual void onHotplugReceived(DisplayType displayType, bool connected) = 0;
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080078
79 virtual void dump(String8& result) const = 0;
80
81 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
82};
83
84namespace impl {
85
86class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070087 class Connection : public BnDisplayEventConnection {
88 public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080089 explicit Connection(EventThread* eventThread);
Lloyd Pique24b0a482018-03-09 18:52:26 -080090 virtual ~Connection();
91
92 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070093
94 // count >= 1 : continuous event. count is the vsync rate
95 // count == 0 : one-shot event that has not fired
96 // count ==-1 : one-shot event that fired this round / disabled
Mathias Agopiancb9732a2012-04-03 17:48:03 -070097 int32_t count;
98
99 private:
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700100 virtual void onFirstRef();
Dan Stoza6b698e42017-04-03 13:09:08 -0700101 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dan Stozae1c599b2017-03-30 16:37:19 -0700102 status_t setVsyncRate(uint32_t count) override;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800103 void requestNextVsync() override; // asynchronous
Lloyd Piquee83f9312018-02-01 12:53:17 -0800104 EventThread* const mEventThread;
Dan Stoza6b698e42017-04-03 13:09:08 -0700105 gui::BitTube mChannel;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700106 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800107
108public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800109 using ResyncWithRateLimitCallback = std::function<void()>;
110 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
111
Ana Krulec98b5b242018-08-10 15:03:23 -0700112 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800113 EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback,
114 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
Ana Krulec98b5b242018-08-10 15:03:23 -0700115 EventThread(std::unique_ptr<VSyncSource> src,
116 ResyncWithRateLimitCallback resyncWithRateLimitCallback,
117 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800118 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800119
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800120 sp<BnDisplayEventConnection> createEventConnection() const override;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700121 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800122
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700123 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
124 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800125
Mathias Agopian22ffb112012-04-10 21:04:02 -0700126 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800127 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700128
129 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800130 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800131
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700132 // called when receiving a hotplug event
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700133 void onHotplugReceived(DisplayType displayType, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700134
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800135 void dump(String8& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800136
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800137 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700138
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800139private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800140 friend EventThreadTest;
141
Ana Krulec98b5b242018-08-10 15:03:23 -0700142 // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete.
143 EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
144 ResyncWithRateLimitCallback resyncWithRateLimitCallback,
145 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
146
Lloyd Pique46a46b32018-01-31 19:01:18 -0800147 void threadMain();
148 Vector<sp<EventThread::Connection>> waitForEventLocked(std::unique_lock<std::mutex>* lock,
149 DisplayEventReceiver::Event* event)
150 REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700151
Lloyd Pique9cbe4da2018-02-13 18:51:55 -0800152 void removeDisplayEventConnectionLocked(const wp<Connection>& connection) REQUIRES(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800153 void enableVSyncLocked() REQUIRES(mMutex);
154 void disableVSyncLocked() REQUIRES(mMutex);
155
156 // Implements VSyncSource::Callback
157 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800158
Ana Krulec98b5b242018-08-10 15:03:23 -0700159 // TODO(b/113612090): Once the Scheduler is complete this pointer will become obsolete.
160 VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr;
161 std::unique_ptr<VSyncSource> mVSyncSourceUnique GUARDED_BY(mMutex) = nullptr;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800162 // constants
Lloyd Pique24b0a482018-03-09 18:52:26 -0800163 const ResyncWithRateLimitCallback mResyncWithRateLimitCallback;
164 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800165
Lloyd Pique46a46b32018-01-31 19:01:18 -0800166 std::thread mThread;
167 mutable std::mutex mMutex;
168 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800169
170 // protected by mLock
Lloyd Pique46a46b32018-01-31 19:01:18 -0800171 SortedVector<wp<Connection>> mDisplayEventConnections GUARDED_BY(mMutex);
Chia-I Wueac3db22018-09-14 11:28:03 -0700172 std::queue<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700173 std::array<DisplayEventReceiver::Event, 2> mVSyncEvent GUARDED_BY(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800174 bool mUseSoftwareVSync GUARDED_BY(mMutex) = false;
175 bool mVsyncEnabled GUARDED_BY(mMutex) = false;
176 bool mKeepRunning GUARDED_BY(mMutex) = true;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700177
178 // for debugging
Lloyd Pique46a46b32018-01-31 19:01:18 -0800179 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800180};
181
182// ---------------------------------------------------------------------------
183
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800184} // namespace impl
185} // namespace android