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