Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 17 | #pragma once |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 18 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 20 | |
| 21 | #include <array> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 22 | #include <condition_variable> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 23 | #include <cstdint> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 24 | #include <mutex> |
Chia-I Wu | eac3db2 | 2018-09-14 11:28:03 -0700 | [diff] [blame] | 25 | #include <queue> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 26 | #include <thread> |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 27 | #include <vector> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 28 | |
| 29 | #include <android-base/thread_annotations.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 31 | #include <gui/DisplayEventReceiver.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 32 | #include <gui/IDisplayEventConnection.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 33 | #include <private/gui/BitTube.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 34 | |
| 35 | #include <utils/Errors.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 36 | |
| 37 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 38 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 39 | // --------------------------------------------------------------------------- |
| 40 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 41 | class EventThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 42 | class EventThreadTest; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 43 | class SurfaceFlinger; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 47 | class VSyncSource { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 48 | public: |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 49 | class Callback { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 50 | 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 Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 57 | virtual void setCallback(Callback* callback) = 0; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 58 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 61 | class EventThreadConnection : public BnDisplayEventConnection { |
| 62 | public: |
| 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 |
| 71 | |
| 72 | // count >= 1 : continuous event. count is the vsync rate |
| 73 | // count == 0 : one-shot event that has not fired |
| 74 | // count ==-1 : one-shot event that fired this round / disabled |
| 75 | int32_t count; |
| 76 | |
| 77 | private: |
| 78 | virtual void onFirstRef(); |
| 79 | EventThread* const mEventThread; |
| 80 | gui::BitTube mChannel; |
| 81 | }; |
| 82 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 83 | class EventThread { |
| 84 | public: |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 85 | // TODO: Remove once stable display IDs are plumbed through SF/WM interface. |
| 86 | enum class DisplayType { Primary, External }; |
| 87 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 88 | virtual ~EventThread(); |
| 89 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 90 | virtual sp<EventThreadConnection> createEventConnection() const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 91 | |
| 92 | // called before the screen is turned off from main thread |
| 93 | virtual void onScreenReleased() = 0; |
| 94 | |
| 95 | // called after the screen is turned on from main thread |
| 96 | virtual void onScreenAcquired() = 0; |
| 97 | |
| 98 | // called when receiving a hotplug event |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 99 | virtual void onHotplugReceived(DisplayType displayType, bool connected) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 100 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 101 | virtual void dump(std::string& result) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 102 | |
| 103 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 104 | |
| 105 | virtual status_t registerDisplayEventConnection( |
| 106 | const sp<EventThreadConnection>& connection) = 0; |
| 107 | virtual void setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) = 0; |
| 108 | virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | namespace impl { |
| 112 | |
| 113 | class EventThread : public android::EventThread, private VSyncSource::Callback { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 114 | public: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 115 | using ResyncWithRateLimitCallback = std::function<void()>; |
| 116 | using InterceptVSyncsCallback = std::function<void(nsecs_t)>; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 117 | using ResetIdleTimerCallback = std::function<void()>; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 118 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 119 | // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 120 | EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback, |
| 121 | InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 122 | EventThread(std::unique_ptr<VSyncSource> src, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 123 | const ResyncWithRateLimitCallback& resyncWithRateLimitCallback, |
| 124 | const InterceptVSyncsCallback& interceptVSyncsCallback, |
| 125 | const ResetIdleTimerCallback& resetIdleTimerCallback, const char* threadName); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 126 | ~EventThread(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 127 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 128 | sp<EventThreadConnection> createEventConnection() const override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 129 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 130 | status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override; |
| 131 | void setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) override; |
| 132 | void requestNextVsync(const sp<EventThreadConnection>& connection) override; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 133 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 134 | // called before the screen is turned off from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 135 | void onScreenReleased() override; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 136 | |
| 137 | // called after the screen is turned on from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 138 | void onScreenAcquired() override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 139 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 140 | // called when receiving a hotplug event |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 141 | void onHotplugReceived(DisplayType displayType, bool connected) override; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 142 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 143 | void dump(std::string& result) const override; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 144 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 145 | void setPhaseOffset(nsecs_t phaseOffset) override; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 146 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 147 | private: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 148 | friend EventThreadTest; |
| 149 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 150 | // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete. |
| 151 | EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc, |
| 152 | ResyncWithRateLimitCallback resyncWithRateLimitCallback, |
| 153 | InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName); |
| 154 | |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 155 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 156 | void threadMain(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 157 | std::vector<sp<EventThreadConnection>> waitForEventLocked(std::unique_lock<std::mutex>* lock, |
| 158 | DisplayEventReceiver::Event* event) |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 159 | REQUIRES(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 160 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 161 | void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) |
| 162 | REQUIRES(mMutex); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 163 | void enableVSyncLocked() REQUIRES(mMutex); |
| 164 | void disableVSyncLocked() REQUIRES(mMutex); |
| 165 | |
| 166 | // Implements VSyncSource::Callback |
| 167 | void onVSyncEvent(nsecs_t timestamp) override; |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 168 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 169 | // TODO(b/113612090): Once the Scheduler is complete this pointer will become obsolete. |
| 170 | VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr; |
| 171 | std::unique_ptr<VSyncSource> mVSyncSourceUnique GUARDED_BY(mMutex) = nullptr; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 172 | // constants |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 173 | const ResyncWithRateLimitCallback mResyncWithRateLimitCallback; |
| 174 | const InterceptVSyncsCallback mInterceptVSyncsCallback; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 175 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 176 | std::thread mThread; |
| 177 | mutable std::mutex mMutex; |
| 178 | mutable std::condition_variable mCondition; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 179 | |
| 180 | // protected by mLock |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 181 | std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex); |
Chia-I Wu | eac3db2 | 2018-09-14 11:28:03 -0700 | [diff] [blame] | 182 | std::queue<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 183 | std::array<DisplayEventReceiver::Event, 2> mVSyncEvent GUARDED_BY(mMutex); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 184 | bool mUseSoftwareVSync GUARDED_BY(mMutex) = false; |
| 185 | bool mVsyncEnabled GUARDED_BY(mMutex) = false; |
| 186 | bool mKeepRunning GUARDED_BY(mMutex) = true; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 187 | |
| 188 | // for debugging |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 189 | bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 190 | |
| 191 | // Callback that resets the idle timer when the next vsync is received. |
| 192 | ResetIdleTimerCallback mResetIdleTimer; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | // --------------------------------------------------------------------------- |
| 196 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 197 | } // namespace impl |
| 198 | } // namespace android |