| 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'), | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 55 | }; | 
|  | 56 |  | 
|  | 57 | struct Event { | 
|  | 58 |  | 
|  | 59 | struct Header { | 
|  | 60 | uint32_t type; | 
| Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 61 | uint32_t id; | 
| Fengwei Yin | 0c7c81f | 2014-03-20 16:45:05 +0800 | [diff] [blame] | 62 | nsecs_t timestamp __attribute__((aligned(8))); | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 63 | }; | 
|  | 64 |  | 
|  | 65 | struct VSync { | 
|  | 66 | uint32_t count; | 
|  | 67 | }; | 
|  | 68 |  | 
| Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 69 | struct Hotplug { | 
| Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 70 | bool connected; | 
|  | 71 | }; | 
|  | 72 |  | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 73 | Header header; | 
|  | 74 | union { | 
|  | 75 | VSync vsync; | 
| Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 76 | Hotplug hotplug; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 77 | }; | 
|  | 78 | }; | 
|  | 79 |  | 
|  | 80 | public: | 
|  | 81 | /* | 
|  | 82 | * DisplayEventReceiver creates and registers an event connection with | 
| Mathias Agopian | 3cf199a | 2012-01-31 16:42:54 -0800 | [diff] [blame] | 83 | * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate | 
|  | 84 | * or requestNextVsync to receive them. | 
|  | 85 | * Other events start being delivered immediately. | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 86 | */ | 
| Jorim Jaggi | b1e2f8d | 2017-06-08 15:43:59 -0700 | [diff] [blame] | 87 | DisplayEventReceiver( | 
|  | 88 | ISurfaceComposer::VsyncSource vsyncSource = ISurfaceComposer::eVsyncSourceApp); | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 89 |  | 
|  | 90 | /* | 
|  | 91 | * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events | 
|  | 92 | * stop being delivered immediately. Note that the queue could have | 
|  | 93 | * some events pending. These will be delivered. | 
|  | 94 | */ | 
|  | 95 | ~DisplayEventReceiver(); | 
|  | 96 |  | 
|  | 97 | /* | 
|  | 98 | * initCheck returns the state of DisplayEventReceiver after construction. | 
|  | 99 | */ | 
|  | 100 | status_t initCheck() const; | 
|  | 101 |  | 
|  | 102 | /* | 
|  | 103 | * getFd returns the file descriptor to use to receive events. | 
|  | 104 | * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this | 
|  | 105 | * file-descriptor. | 
|  | 106 | */ | 
|  | 107 | int getFd() const; | 
|  | 108 |  | 
|  | 109 | /* | 
| Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 110 | * getEvents reads events from the queue and returns how many events were | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 111 | * read. Returns 0 if there are no more events or a negative error code. | 
|  | 112 | * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it | 
|  | 113 | * should be destroyed and getEvents() shouldn't be called again. | 
|  | 114 | */ | 
|  | 115 | ssize_t getEvents(Event* events, size_t count); | 
| Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 116 | static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count); | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 117 |  | 
| Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 118 | /* | 
| Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 119 | * sendEvents write events to the queue and returns how many events were | 
|  | 120 | * written. | 
|  | 121 | */ | 
| Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 122 | 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] | 123 |  | 
|  | 124 | /* | 
| Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 125 | * setVsyncRate() sets the Event::VSync delivery rate. A value of | 
|  | 126 | * 1 returns every Event::VSync. A value of 2 returns every other event, | 
|  | 127 | * etc... a value of 0 returns no event unless  requestNextVsync() has | 
|  | 128 | * been called. | 
|  | 129 | */ | 
|  | 130 | status_t setVsyncRate(uint32_t count); | 
|  | 131 |  | 
|  | 132 | /* | 
|  | 133 | * requestNextVsync() schedules the next Event::VSync. It has no effect | 
|  | 134 | * if the vsync rate is > 0. | 
|  | 135 | */ | 
|  | 136 | status_t requestNextVsync(); | 
|  | 137 |  | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 138 | private: | 
|  | 139 | sp<IDisplayEventConnection> mEventConnection; | 
| Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 140 | std::unique_ptr<gui::BitTube> mDataChannel; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 141 | }; | 
|  | 142 |  | 
|  | 143 | // ---------------------------------------------------------------------------- | 
|  | 144 | }; // namespace android | 
|  | 145 |  | 
|  | 146 | #endif // ANDROID_GUI_DISPLAY_EVENT_H |