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 | |
| 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 21 | #include <condition_variable> |
| 22 | #include <mutex> |
| 23 | #include <thread> |
| 24 | |
| 25 | #include <android-base/thread_annotations.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 27 | #include <gui/DisplayEventReceiver.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 28 | #include <gui/IDisplayEventConnection.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 29 | #include <private/gui/BitTube.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | |
| 31 | #include <utils/Errors.h> |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 32 | #include <utils/SortedVector.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 33 | |
Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 34 | #include "DisplayDevice.h" |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 36 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 37 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- |
| 39 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 40 | class EventThreadTest; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 41 | class SurfaceFlinger; |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 42 | class String8; |
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: |
| 62 | virtual ~EventThread(); |
| 63 | |
| 64 | virtual sp<BnDisplayEventConnection> createEventConnection() const = 0; |
| 65 | |
| 66 | // called before the screen is turned off from main thread |
| 67 | virtual void onScreenReleased() = 0; |
| 68 | |
| 69 | // called after the screen is turned on from main thread |
| 70 | virtual void onScreenAcquired() = 0; |
| 71 | |
| 72 | // called when receiving a hotplug event |
| 73 | virtual void onHotplugReceived(int type, bool connected) = 0; |
| 74 | |
| 75 | virtual void dump(String8& result) const = 0; |
| 76 | |
| 77 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
| 78 | }; |
| 79 | |
| 80 | namespace impl { |
| 81 | |
| 82 | class EventThread : public android::EventThread, private VSyncSource::Callback { |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 83 | class Connection : public BnDisplayEventConnection { |
| 84 | public: |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 85 | explicit Connection(EventThread* eventThread); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 86 | virtual ~Connection(); |
| 87 | |
| 88 | virtual status_t postEvent(const DisplayEventReceiver::Event& event); |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 89 | |
| 90 | // count >= 1 : continuous event. count is the vsync rate |
| 91 | // count == 0 : one-shot event that has not fired |
| 92 | // count ==-1 : one-shot event that fired this round / disabled |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 93 | int32_t count; |
| 94 | |
| 95 | private: |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 96 | virtual void onFirstRef(); |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 97 | status_t stealReceiveChannel(gui::BitTube* outChannel) override; |
Dan Stoza | e1c599b | 2017-03-30 16:37:19 -0700 | [diff] [blame] | 98 | status_t setVsyncRate(uint32_t count) override; |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 99 | void requestNextVsync() override; // asynchronous |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 100 | EventThread* const mEventThread; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 101 | gui::BitTube mChannel; |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 102 | }; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 103 | |
| 104 | public: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 105 | using ResyncWithRateLimitCallback = std::function<void()>; |
| 106 | using InterceptVSyncsCallback = std::function<void(nsecs_t)>; |
| 107 | |
| 108 | EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback, |
| 109 | InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 110 | ~EventThread(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 111 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 112 | sp<BnDisplayEventConnection> createEventConnection() const override; |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 113 | status_t registerDisplayEventConnection(const sp<Connection>& connection); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 114 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 115 | void setVsyncRate(uint32_t count, const sp<Connection>& connection); |
| 116 | void requestNextVsync(const sp<Connection>& connection); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 117 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 118 | // called before the screen is turned off from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 119 | void onScreenReleased() override; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 120 | |
| 121 | // called after the screen is turned on from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 122 | void onScreenAcquired() override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 123 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 124 | // called when receiving a hotplug event |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 125 | void onHotplugReceived(int type, bool connected) override; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 126 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 127 | void dump(String8& result) const override; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 128 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 129 | void setPhaseOffset(nsecs_t phaseOffset) override; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 130 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 131 | private: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 132 | friend EventThreadTest; |
| 133 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 134 | void threadMain(); |
| 135 | Vector<sp<EventThread::Connection>> waitForEventLocked(std::unique_lock<std::mutex>* lock, |
| 136 | DisplayEventReceiver::Event* event) |
| 137 | REQUIRES(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 138 | |
Lloyd Pique | 9cbe4da | 2018-02-13 18:51:55 -0800 | [diff] [blame] | 139 | void removeDisplayEventConnectionLocked(const wp<Connection>& connection) REQUIRES(mMutex); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 140 | void enableVSyncLocked() REQUIRES(mMutex); |
| 141 | void disableVSyncLocked() REQUIRES(mMutex); |
| 142 | |
| 143 | // Implements VSyncSource::Callback |
| 144 | void onVSyncEvent(nsecs_t timestamp) override; |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 145 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 146 | // constants |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 147 | VSyncSource* const mVSyncSource GUARDED_BY(mMutex) = nullptr; |
| 148 | const ResyncWithRateLimitCallback mResyncWithRateLimitCallback; |
| 149 | const InterceptVSyncsCallback mInterceptVSyncsCallback; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 150 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 151 | std::thread mThread; |
| 152 | mutable std::mutex mMutex; |
| 153 | mutable std::condition_variable mCondition; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 154 | |
| 155 | // protected by mLock |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 156 | SortedVector<wp<Connection>> mDisplayEventConnections GUARDED_BY(mMutex); |
| 157 | Vector<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); |
| 158 | DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES] GUARDED_BY( |
| 159 | mMutex); |
| 160 | bool mUseSoftwareVSync GUARDED_BY(mMutex) = false; |
| 161 | bool mVsyncEnabled GUARDED_BY(mMutex) = false; |
| 162 | bool mKeepRunning GUARDED_BY(mMutex) = true; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 163 | |
| 164 | // for debugging |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 165 | bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | // --------------------------------------------------------------------------- |
| 169 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 170 | } // namespace impl |
| 171 | } // namespace android |