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