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 | |
| 17 | #ifndef ANDROID_GUI_DISPLAY_EVENT_H |
| 18 | #define ANDROID_GUI_DISPLAY_EVENT_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 23 | #include <ftl/flags.h> |
| 24 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 25 | #include <utils/Errors.h> |
| 26 | #include <utils/RefBase.h> |
| 27 | #include <utils/Timers.h> |
| 28 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 29 | #include <android/gui/ISurfaceComposer.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | #include <binder/IInterface.h> |
Rachel Lee | 18c3437 | 2022-01-20 13:57:18 -0800 | [diff] [blame] | 31 | #include <gui/VsyncEventData.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 32 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 33 | #include <ui/DisplayId.h> |
| 34 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 41 | using EventRegistrationFlags = ftl::Flags<gui::ISurfaceComposer::EventRegistration>; |
| 42 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 43 | using gui::IDisplayEventConnection; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 44 | using gui::ParcelableVsyncEventData; |
Rachel Lee | 18c3437 | 2022-01-20 13:57:18 -0800 | [diff] [blame] | 45 | using gui::VsyncEventData; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 46 | |
Dan Stoza | 27c8115 | 2017-03-31 16:30:42 -0700 | [diff] [blame] | 47 | namespace gui { |
| 48 | class BitTube; |
| 49 | } // namespace gui |
| 50 | |
Colin Cross | c2b9017 | 2016-09-26 18:11:51 -0700 | [diff] [blame] | 51 | static inline constexpr uint32_t fourcc(char c1, char c2, char c3, char c4) { |
| 52 | return static_cast<uint32_t>(c1) << 24 | |
| 53 | static_cast<uint32_t>(c2) << 16 | |
| 54 | static_cast<uint32_t>(c3) << 8 | |
| 55 | static_cast<uint32_t>(c4); |
| 56 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 57 | |
Colin Cross | c2b9017 | 2016-09-26 18:11:51 -0700 | [diff] [blame] | 58 | // ---------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 59 | class DisplayEventReceiver { |
| 60 | public: |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 61 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 62 | enum { |
Colin Cross | c2b9017 | 2016-09-26 18:11:51 -0700 | [diff] [blame] | 63 | DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'), |
| 64 | DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'), |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 65 | DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'), |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 66 | DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'), |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 67 | DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'), |
| 68 | DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'), |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | struct Event { |
Yahan Zhou | 3cbda59 | 2020-08-03 15:53:27 -0700 | [diff] [blame] | 72 | // We add __attribute__((aligned(8))) for nsecs_t fields because |
| 73 | // we need to make sure all fields are aligned the same with x86 |
| 74 | // and x64 (long long has different default alignment): |
| 75 | // |
| 76 | // https://en.wikipedia.org/wiki/Data_structure_alignment |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 77 | |
| 78 | struct Header { |
| 79 | uint32_t type; |
Yahan Zhou | 3cbda59 | 2020-08-03 15:53:27 -0700 | [diff] [blame] | 80 | PhysicalDisplayId displayId __attribute__((aligned(8))); |
Fengwei Yin | 0c7c81f | 2014-03-20 16:45:05 +0800 | [diff] [blame] | 81 | nsecs_t timestamp __attribute__((aligned(8))); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct VSync { |
| 85 | uint32_t count; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 86 | VsyncEventData vsyncData; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 89 | struct Hotplug { |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 90 | bool connected; |
| 91 | }; |
| 92 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 93 | struct ModeChange { |
| 94 | int32_t modeId; |
Yahan Zhou | 3cbda59 | 2020-08-03 15:53:27 -0700 | [diff] [blame] | 95 | nsecs_t vsyncPeriod __attribute__((aligned(8))); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 98 | struct FrameRateOverride { |
| 99 | uid_t uid __attribute__((aligned(8))); |
| 100 | float frameRateHz __attribute__((aligned(8))); |
| 101 | }; |
| 102 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 103 | Header header; |
| 104 | union { |
| 105 | VSync vsync; |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 106 | Hotplug hotplug; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 107 | ModeChange modeChange; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 108 | FrameRateOverride frameRateOverride; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 109 | }; |
| 110 | }; |
| 111 | |
| 112 | public: |
| 113 | /* |
| 114 | * DisplayEventReceiver creates and registers an event connection with |
Mathias Agopian | 3cf199a | 2012-01-31 16:42:54 -0800 | [diff] [blame] | 115 | * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate |
| 116 | * or requestNextVsync to receive them. |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 117 | * To receive ModeChanged and/or FrameRateOverrides events specify this in |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 118 | * the constructor. Other events start being delivered immediately. |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 119 | */ |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 120 | explicit DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource = |
| 121 | gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp, |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 122 | EventRegistrationFlags eventRegistration = {}, |
| 123 | const sp<IBinder>& layerHandle = nullptr); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events |
| 127 | * stop being delivered immediately. Note that the queue could have |
| 128 | * some events pending. These will be delivered. |
| 129 | */ |
| 130 | ~DisplayEventReceiver(); |
| 131 | |
| 132 | /* |
| 133 | * initCheck returns the state of DisplayEventReceiver after construction. |
| 134 | */ |
| 135 | status_t initCheck() const; |
| 136 | |
| 137 | /* |
| 138 | * getFd returns the file descriptor to use to receive events. |
| 139 | * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this |
| 140 | * file-descriptor. |
| 141 | */ |
| 142 | int getFd() const; |
| 143 | |
| 144 | /* |
Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 145 | * getEvents reads events from the queue and returns how many events were |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 146 | * read. Returns 0 if there are no more events or a negative error code. |
| 147 | * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it |
| 148 | * should be destroyed and getEvents() shouldn't be called again. |
| 149 | */ |
| 150 | ssize_t getEvents(Event* events, size_t count); |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 151 | static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 152 | |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 153 | /* |
Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 154 | * sendEvents write events to the queue and returns how many events were |
| 155 | * written. |
| 156 | */ |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 157 | ssize_t sendEvents(Event const* events, size_t count); |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 158 | static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count); |
Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 161 | * setVsyncRate() sets the Event::VSync delivery rate. A value of |
| 162 | * 1 returns every Event::VSync. A value of 2 returns every other event, |
| 163 | * etc... a value of 0 returns no event unless requestNextVsync() has |
| 164 | * been called. |
| 165 | */ |
| 166 | status_t setVsyncRate(uint32_t count); |
| 167 | |
| 168 | /* |
| 169 | * requestNextVsync() schedules the next Event::VSync. It has no effect |
| 170 | * if the vsync rate is > 0. |
| 171 | */ |
| 172 | status_t requestNextVsync(); |
| 173 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 174 | /** |
| 175 | * getLatestVsyncEventData() gets the latest vsync event data. |
| 176 | */ |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 177 | status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const; |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 178 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 179 | private: |
| 180 | sp<IDisplayEventConnection> mEventConnection; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 181 | std::unique_ptr<gui::BitTube> mDataChannel; |
Ady Abraham | 745f7b5 | 2022-02-08 19:36:37 -0800 | [diff] [blame] | 182 | std::optional<status_t> mInitError; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 185 | inline bool operator==(DisplayEventReceiver::Event::FrameRateOverride lhs, |
| 186 | DisplayEventReceiver::Event::FrameRateOverride rhs) { |
| 187 | return (lhs.uid == rhs.uid) && std::abs(lhs.frameRateHz - rhs.frameRateHz) < 0.001f; |
| 188 | } |
| 189 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 190 | // ---------------------------------------------------------------------------- |
| 191 | }; // namespace android |
| 192 | |
| 193 | #endif // ANDROID_GUI_DISPLAY_EVENT_H |