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> |
Manasi Navare | 5ef1fa1 | 2024-11-07 23:49:46 +0000 | [diff] [blame] | 24 | #include <ui/DisplayId.h> |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 25 | #include <utils/Errors.h> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 26 | |
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 | |
Manasi Navare | 5ef1fa1 | 2024-11-07 23:49:46 +0000 | [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 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 58 | using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride; |
Melody Hsu | e524dd9 | 2024-08-27 22:27:29 +0000 | [diff] [blame] | 59 | using BufferStuffingMap = ftl::SmallMap<uid_t, uint32_t, 10>; |
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 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 71 | class EventThreadConnection : public gui::BnDisplayEventConnection { |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 72 | public: |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 73 | EventThreadConnection(EventThread*, uid_t callingUid, |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 74 | EventRegistrationFlags eventRegistration = {}); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 75 | virtual ~EventThreadConnection(); |
| 76 | |
| 77 | virtual status_t postEvent(const DisplayEventReceiver::Event& event); |
| 78 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 79 | binder::Status stealReceiveChannel(gui::BitTube* outChannel) override; |
| 80 | binder::Status setVsyncRate(int rate) override; |
| 81 | binder::Status requestNextVsync() override; // asynchronous |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 82 | binder::Status getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) override; |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 83 | binder::Status getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) override; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 84 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 85 | VSyncRequest vsyncRequest = VSyncRequest::None; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 86 | const uid_t mOwnerUid; |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 87 | const EventRegistrationFlags mEventRegistration; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 88 | |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 89 | /** The frame rate set to the attached choreographer. */ |
| 90 | Fps frameRate; |
| 91 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 92 | private: |
| 93 | virtual void onFirstRef(); |
| 94 | EventThread* const mEventThread; |
Ady Abraham | 899e8cd | 2022-06-06 15:16:06 -0700 | [diff] [blame] | 95 | std::mutex mLock; |
| 96 | gui::BitTube mChannel GUARDED_BY(mLock); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 97 | |
| 98 | std::vector<DisplayEventReceiver::Event> mPendingEvents; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 101 | class EventThread { |
| 102 | public: |
| 103 | virtual ~EventThread(); |
| 104 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 105 | virtual sp<EventThreadConnection> createEventConnection( |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 106 | EventRegistrationFlags eventRegistration = {}) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 107 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 108 | // Feed clients with fake VSYNC, e.g. while the display is off. |
| 109 | virtual void enableSyntheticVsync(bool) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 110 | |
Ady Abraham | 8e3e2ea | 2024-10-31 15:52:31 -0700 | [diff] [blame] | 111 | virtual void omitVsyncDispatching(bool) = 0; |
| 112 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 113 | virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 114 | |
Brian Johnson | 5dcd75d | 2023-08-15 09:36:37 -0700 | [diff] [blame] | 115 | virtual void onHotplugConnectionError(int32_t connectionError) = 0; |
| 116 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 117 | // 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] | 118 | virtual void onModeChanged(const scheduler::FrameRateMode&) = 0; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 119 | |
Manasi Navare | 5ef1fa1 | 2024-11-07 23:49:46 +0000 | [diff] [blame] | 120 | // called when SF rejects the mode change request |
| 121 | virtual void onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) = 0; |
| 122 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 123 | // called when SF updates the Frame Rate Override list |
| 124 | virtual void onFrameRateOverridesChanged(PhysicalDisplayId displayId, |
| 125 | std::vector<FrameRateOverride> overrides) = 0; |
| 126 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 127 | virtual void dump(std::string& result) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 128 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 129 | virtual void setDuration(std::chrono::nanoseconds workDuration, |
| 130 | std::chrono::nanoseconds readyDuration) = 0; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 131 | |
| 132 | virtual status_t registerDisplayEventConnection( |
| 133 | const sp<EventThreadConnection>& connection) = 0; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 134 | virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 135 | // 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] | 136 | virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0; |
Ady Abraham | 40ec584 | 2024-04-04 13:22:38 -0700 | [diff] [blame] | 137 | virtual VsyncEventData getLatestVsyncEventData(const sp<EventThreadConnection>& connection, |
| 138 | nsecs_t now) const = 0; |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 139 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 140 | virtual void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) = 0; |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 141 | |
| 142 | virtual void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel, |
| 143 | int32_t maxLevel) = 0; |
Melody Hsu | e524dd9 | 2024-08-27 22:27:29 +0000 | [diff] [blame] | 144 | |
| 145 | // An elevated number of queued buffers in the server is detected. This propagates a |
| 146 | // flag to Choreographer indicating that buffer stuffing recovery should begin. |
| 147 | virtual void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids); |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 150 | struct IEventThreadCallback { |
| 151 | virtual ~IEventThreadCallback() = default; |
| 152 | |
| 153 | virtual bool throttleVsync(TimePoint, uid_t) = 0; |
| 154 | virtual Period getVsyncPeriod(uid_t) = 0; |
| 155 | virtual void resync() = 0; |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 156 | virtual void onExpectedPresentTimePosted(TimePoint) = 0; |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 159 | namespace impl { |
| 160 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 161 | class EventThread : public android::EventThread { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 162 | public: |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 163 | EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule>, |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 164 | frametimeline::TokenManager*, IEventThreadCallback& callback, |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 165 | std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 166 | ~EventThread(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 167 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 168 | sp<EventThreadConnection> createEventConnection( |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 169 | EventRegistrationFlags eventRegistration = {}) const override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 170 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 171 | status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 172 | void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 173 | void requestNextVsync(const sp<EventThreadConnection>& connection) override; |
Ady Abraham | 40ec584 | 2024-04-04 13:22:38 -0700 | [diff] [blame] | 174 | VsyncEventData getLatestVsyncEventData(const sp<EventThreadConnection>& connection, |
| 175 | nsecs_t now) const override; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 176 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 177 | void enableSyntheticVsync(bool) override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 178 | |
Ady Abraham | 8e3e2ea | 2024-10-31 15:52:31 -0700 | [diff] [blame] | 179 | void omitVsyncDispatching(bool) override; |
| 180 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 181 | void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 182 | |
Brian Johnson | 5dcd75d | 2023-08-15 09:36:37 -0700 | [diff] [blame] | 183 | void onHotplugConnectionError(int32_t connectionError) override; |
| 184 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 185 | void onModeChanged(const scheduler::FrameRateMode&) override; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 186 | |
Manasi Navare | 5ef1fa1 | 2024-11-07 23:49:46 +0000 | [diff] [blame] | 187 | void onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) override; |
| 188 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 189 | void onFrameRateOverridesChanged(PhysicalDisplayId displayId, |
| 190 | std::vector<FrameRateOverride> overrides) override; |
| 191 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 192 | void dump(std::string& result) const override; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 193 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 194 | void setDuration(std::chrono::nanoseconds workDuration, |
| 195 | std::chrono::nanoseconds readyDuration) override; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 196 | |
Leon Scroggins III | b225360 | 2023-04-19 17:04:04 -0400 | [diff] [blame] | 197 | void onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule>) override EXCLUDES(mMutex); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 198 | |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 199 | void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel, |
| 200 | int32_t maxLevel) override; |
| 201 | |
Melody Hsu | e524dd9 | 2024-08-27 22:27:29 +0000 | [diff] [blame] | 202 | void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) override; |
| 203 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 204 | private: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 205 | friend EventThreadTest; |
| 206 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 207 | using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>; |
| 208 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 209 | void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 210 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 211 | bool shouldConsumeEvent(const DisplayEventReceiver::Event& event, |
| 212 | const sp<EventThreadConnection>& connection) const REQUIRES(mMutex); |
| 213 | void dispatchEvent(const DisplayEventReceiver::Event& event, |
| 214 | const DisplayEventConsumers& consumers) REQUIRES(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 215 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 216 | void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) |
| 217 | REQUIRES(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 218 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 219 | void onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime); |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 220 | |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame] | 221 | int64_t generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 222 | nsecs_t expectedPresentationTime) const; |
| 223 | void generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval, |
| 224 | nsecs_t timestamp, nsecs_t preferredExpectedPresentationTime, |
| 225 | nsecs_t preferredDeadlineTimestamp) const; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 226 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 227 | scheduler::VSyncDispatch::Callback createDispatchCallback(); |
| 228 | |
Leon Scroggins III | b225360 | 2023-04-19 17:04:04 -0400 | [diff] [blame] | 229 | // Returns the old registration so it can be destructed outside the lock to |
| 230 | // avoid deadlock. |
| 231 | scheduler::VSyncCallbackRegistration onNewVsyncScheduleInternal( |
| 232 | std::shared_ptr<scheduler::VsyncSchedule>) EXCLUDES(mMutex); |
| 233 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 234 | const char* const mThreadName; |
| 235 | TracedOrdinal<int> mVsyncTracer; |
| 236 | TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mMutex); |
| 237 | std::chrono::nanoseconds mReadyDuration GUARDED_BY(mMutex); |
Leon Scroggins III | ed66a35 | 2023-03-23 17:55:43 -0400 | [diff] [blame] | 238 | std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule GUARDED_BY(mMutex); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 239 | TimePoint mLastVsyncCallbackTime GUARDED_BY(mMutex) = TimePoint::now(); |
ramindani | 92ab987 | 2024-07-26 15:09:37 -0700 | [diff] [blame] | 240 | TimePoint mLastCommittedVsyncTime GUARDED_BY(mMutex) = TimePoint::now(); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 241 | scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex); |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 242 | frametimeline::TokenManager* const mTokenManager; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 243 | |
Melody Hsu | e524dd9 | 2024-08-27 22:27:29 +0000 | [diff] [blame] | 244 | // All consumers that need to recover from buffer stuffing and the number |
| 245 | // of their queued buffers. |
| 246 | BufferStuffingMap mBufferStuffedUids GUARDED_BY(mMutex); |
| 247 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 248 | IEventThreadCallback& mCallback; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 249 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 250 | std::thread mThread; |
| 251 | mutable std::mutex mMutex; |
| 252 | mutable std::condition_variable mCondition; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 253 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 254 | std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 255 | std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 256 | |
| 257 | // VSYNC state of connected display. |
| 258 | struct VSyncState { |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 259 | // Number of VSYNC events since display was connected. |
| 260 | uint32_t count = 0; |
| 261 | |
| 262 | // True if VSYNC should be faked, e.g. when display is off. |
| 263 | bool synthetic = false; |
Ady Abraham | 8e3e2ea | 2024-10-31 15:52:31 -0700 | [diff] [blame] | 264 | |
| 265 | // True if VSYNC should not be delivered to apps. Used when the display is off. |
| 266 | bool omitted = false; |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 267 | }; |
| 268 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 269 | // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals, |
| 270 | // and support headless mode by injecting a fake display with synthetic VSYNC. |
| 271 | std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 272 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 273 | // State machine for event loop. |
| 274 | enum class State { |
| 275 | Idle, |
| 276 | Quit, |
| 277 | SyntheticVSync, |
| 278 | VSync, |
| 279 | }; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 280 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 281 | State mState GUARDED_BY(mMutex) = State::Idle; |
| 282 | |
| 283 | static const char* toCString(State); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | // --------------------------------------------------------------------------- |
| 287 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 288 | } // namespace impl |
| 289 | } // namespace android |