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 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 19 | #include <android-base/thread_annotations.h> |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 20 | #include <android/gui/BnDisplayEventConnection.h> |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 21 | #include <gui/DisplayEventReceiver.h> |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 22 | #include <private/gui/BitTube.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 23 | #include <sys/types.h> |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 24 | #include <utils/Errors.h> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 25 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 26 | #include <scheduler/Fps.h> |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 27 | #include <scheduler/FrameRateMode.h> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 28 | #include <condition_variable> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 29 | #include <cstdint> |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 30 | #include <deque> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 31 | #include <mutex> |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 32 | #include <optional> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 33 | #include <thread> |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 34 | #include <vector> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 35 | |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 36 | #include "DisplayHardware/DisplayMode.h" |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 37 | #include "TracedOrdinal.h" |
| 38 | #include "VSyncDispatch.h" |
| 39 | #include "VsyncSchedule.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 40 | |
| 41 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 42 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 43 | // --------------------------------------------------------------------------- |
| 44 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 45 | class EventThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 46 | class EventThreadTest; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 47 | class SurfaceFlinger; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 48 | |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 49 | namespace frametimeline { |
| 50 | class TokenManager; |
| 51 | } // namespace frametimeline |
| 52 | |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 53 | using gui::ParcelableVsyncEventData; |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 54 | using gui::VsyncEventData; |
| 55 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 56 | // --------------------------------------------------------------------------- |
| 57 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 58 | using ResyncCallback = std::function<void()>; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 59 | using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 60 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 61 | enum class VSyncRequest { |
Tim Murray | b5daa91 | 2020-09-09 21:29:13 +0000 | [diff] [blame] | 62 | None = -2, |
| 63 | // Single wakes up for the next two frames to avoid scheduler overhead |
| 64 | Single = -1, |
| 65 | // SingleSuppressCallback only wakes up for the next frame |
| 66 | SingleSuppressCallback = 0, |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 67 | Periodic = 1, |
| 68 | // Subsequent values are periods. |
| 69 | }; |
| 70 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 71 | class IEventThreadCallback { |
| 72 | public: |
| 73 | virtual ~IEventThreadCallback() = default; |
| 74 | |
| 75 | virtual bool isVsyncTargetForUid(TimePoint expectedVsyncTime, uid_t uid) const = 0; |
| 76 | |
| 77 | virtual Fps getLeaderRenderFrameRate(uid_t uid) const = 0; |
| 78 | }; |
| 79 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 80 | class EventThreadConnection : public gui::BnDisplayEventConnection { |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 81 | public: |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 82 | EventThreadConnection(EventThread*, uid_t callingUid, ResyncCallback, |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 83 | EventRegistrationFlags eventRegistration = {}); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 84 | virtual ~EventThreadConnection(); |
| 85 | |
| 86 | virtual status_t postEvent(const DisplayEventReceiver::Event& event); |
| 87 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 88 | binder::Status stealReceiveChannel(gui::BitTube* outChannel) override; |
| 89 | binder::Status setVsyncRate(int rate) override; |
| 90 | binder::Status requestNextVsync() override; // asynchronous |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 91 | binder::Status getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) override; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 92 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 93 | // Called in response to requestNextVsync. |
| 94 | const ResyncCallback resyncCallback; |
| 95 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 96 | VSyncRequest vsyncRequest = VSyncRequest::None; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 97 | const uid_t mOwnerUid; |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 98 | const EventRegistrationFlags mEventRegistration; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 99 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 100 | private: |
| 101 | virtual void onFirstRef(); |
| 102 | EventThread* const mEventThread; |
Ady Abraham | 899e8cd | 2022-06-06 15:16:06 -0700 | [diff] [blame] | 103 | std::mutex mLock; |
| 104 | gui::BitTube mChannel GUARDED_BY(mLock); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 105 | |
| 106 | std::vector<DisplayEventReceiver::Event> mPendingEvents; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 109 | class EventThread { |
| 110 | public: |
| 111 | virtual ~EventThread(); |
| 112 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 113 | virtual sp<EventThreadConnection> createEventConnection( |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 114 | ResyncCallback, EventRegistrationFlags eventRegistration = {}) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 115 | |
| 116 | // called before the screen is turned off from main thread |
| 117 | virtual void onScreenReleased() = 0; |
| 118 | |
| 119 | // called after the screen is turned on from main thread |
| 120 | virtual void onScreenAcquired() = 0; |
| 121 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 122 | virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 123 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 124 | // called when SF changes the active mode and apps needs to be notified about the change |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 125 | virtual void onModeChanged(const scheduler::FrameRateMode&) = 0; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 126 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 127 | // called when SF updates the Frame Rate Override list |
| 128 | virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId, |
| 129 | std::vector<FrameRateOverride> overrides) = 0; |
| 130 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 131 | virtual void dump(std::string& result) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 132 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 133 | virtual void setDuration(std::chrono::nanoseconds workDuration, |
| 134 | std::chrono::nanoseconds readyDuration) = 0; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 135 | |
| 136 | virtual status_t registerDisplayEventConnection( |
| 137 | const sp<EventThreadConnection>& connection) = 0; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 138 | virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 139 | // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer. |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 140 | virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0; |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 141 | virtual VsyncEventData getLatestVsyncEventData( |
| 142 | const sp<EventThreadConnection>& connection) const = 0; |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 143 | |
| 144 | // Retrieves the number of event connections tracked by this EventThread. |
| 145 | virtual size_t getEventThreadConnectionCount() = 0; |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 146 | |
| 147 | virtual void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | namespace impl { |
| 151 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 152 | class EventThread : public android::EventThread { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 153 | public: |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 154 | EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule>, IEventThreadCallback&, |
| 155 | frametimeline::TokenManager*, std::chrono::nanoseconds workDuration, |
| 156 | std::chrono::nanoseconds readyDuration); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 157 | ~EventThread(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 158 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 159 | sp<EventThreadConnection> createEventConnection( |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 160 | ResyncCallback, EventRegistrationFlags eventRegistration = {}) const override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 161 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 162 | status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 163 | void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 164 | void requestNextVsync(const sp<EventThreadConnection>& connection) override; |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 165 | VsyncEventData getLatestVsyncEventData( |
| 166 | const sp<EventThreadConnection>& connection) const override; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 167 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 168 | // called before the screen is turned off from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 169 | void onScreenReleased() override; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 170 | |
| 171 | // called after the screen is turned on from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 172 | void onScreenAcquired() override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 173 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 174 | void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 175 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 176 | void onModeChanged(const scheduler::FrameRateMode&) override; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 177 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 178 | void onFrameRateOverridesChanged(PhysicalDisplayId displayId, |
| 179 | std::vector<FrameRateOverride> overrides) override; |
| 180 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 181 | void dump(std::string& result) const override; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 182 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 183 | void setDuration(std::chrono::nanoseconds workDuration, |
| 184 | std::chrono::nanoseconds readyDuration) override; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 185 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 186 | size_t getEventThreadConnectionCount() override; |
| 187 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 188 | void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) override; |
| 189 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 190 | private: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 191 | friend EventThreadTest; |
| 192 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 193 | using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>; |
| 194 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 195 | void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 196 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 197 | bool shouldConsumeEvent(const DisplayEventReceiver::Event& event, |
| 198 | const sp<EventThreadConnection>& connection) const REQUIRES(mMutex); |
| 199 | void dispatchEvent(const DisplayEventReceiver::Event& event, |
| 200 | const DisplayEventConsumers& consumers) REQUIRES(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 201 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 202 | void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) |
| 203 | REQUIRES(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 204 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 205 | void onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime); |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 206 | |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame] | 207 | int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 208 | nsecs_t expectedPresentationTime) const; |
| 209 | void generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval, |
| 210 | nsecs_t timestamp, nsecs_t preferredExpectedPresentationTime, |
| 211 | nsecs_t preferredDeadlineTimestamp) const; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 212 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 213 | scheduler::VSyncDispatch::Callback createDispatchCallback(); |
| 214 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 215 | const char* const mThreadName; |
| 216 | TracedOrdinal<int> mVsyncTracer; |
| 217 | TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mMutex); |
| 218 | std::chrono::nanoseconds mReadyDuration GUARDED_BY(mMutex); |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 219 | std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 220 | TimePoint mLastVsyncCallbackTime GUARDED_BY(mMutex) = TimePoint::now(); |
| 221 | scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex); |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 222 | frametimeline::TokenManager* const mTokenManager; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 223 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 224 | // mEventThreadCallback will outlive the EventThread. |
| 225 | IEventThreadCallback& mEventThreadCallback; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 226 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 227 | std::thread mThread; |
| 228 | mutable std::mutex mMutex; |
| 229 | mutable std::condition_variable mCondition; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 230 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 231 | std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 232 | std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 233 | |
| 234 | // VSYNC state of connected display. |
| 235 | struct VSyncState { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 236 | explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {} |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 237 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 238 | const PhysicalDisplayId displayId; |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 239 | |
| 240 | // Number of VSYNC events since display was connected. |
| 241 | uint32_t count = 0; |
| 242 | |
| 243 | // True if VSYNC should be faked, e.g. when display is off. |
| 244 | bool synthetic = false; |
| 245 | }; |
| 246 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 247 | // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals, |
| 248 | // and support headless mode by injecting a fake display with synthetic VSYNC. |
| 249 | std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 250 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 251 | // State machine for event loop. |
| 252 | enum class State { |
| 253 | Idle, |
| 254 | Quit, |
| 255 | SyntheticVSync, |
| 256 | VSync, |
| 257 | }; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 258 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 259 | State mState GUARDED_BY(mMutex) = State::Idle; |
| 260 | |
| 261 | static const char* toCString(State); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | // --------------------------------------------------------------------------- |
| 265 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 266 | } // namespace impl |
| 267 | } // namespace android |