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