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 <ftl/flags.h>
24
25 #include <utils/Errors.h>
26 #include <utils/RefBase.h>
27 #include <utils/Timers.h>
28
29 #include <android/gui/ISurfaceComposer.h>
30 #include <binder/IInterface.h>
31 #include <gui/VsyncEventData.h>
32
33 #include <ui/DisplayId.h>
34
35 // ----------------------------------------------------------------------------
36
37 namespace android {
38
39 // ----------------------------------------------------------------------------
40
41 using EventRegistrationFlags = ftl::Flags<gui::ISurfaceComposer::EventRegistration>;
42
43 using gui::IDisplayEventConnection;
44 using gui::ParcelableVsyncEventData;
45 using gui::VsyncEventData;
46
47 namespace gui {
48 class BitTube;
49 } // namespace gui
50
fourcc(char c1,char c2,char c3,char c4)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 }
57
58 // ----------------------------------------------------------------------------
59 class DisplayEventReceiver {
60 public:
61
62 enum {
63 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
64 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
65 DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'),
66 DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'),
67 DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
68 DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
69 };
70
71 struct Event {
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
77
78 struct Header {
79 uint32_t type;
80 PhysicalDisplayId displayId __attribute__((aligned(8)));
81 nsecs_t timestamp __attribute__((aligned(8)));
82 };
83
84 struct VSync {
85 uint32_t count;
86 VsyncEventData vsyncData;
87 };
88
89 struct Hotplug {
90 bool connected;
91 };
92
93 struct ModeChange {
94 int32_t modeId;
95 nsecs_t vsyncPeriod __attribute__((aligned(8)));
96 };
97
98 struct FrameRateOverride {
99 uid_t uid __attribute__((aligned(8)));
100 float frameRateHz __attribute__((aligned(8)));
101 };
102
103 Header header;
104 union {
105 VSync vsync;
106 Hotplug hotplug;
107 ModeChange modeChange;
108 FrameRateOverride frameRateOverride;
109 };
110 };
111
112 public:
113 /*
114 * DisplayEventReceiver creates and registers an event connection with
115 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
116 * or requestNextVsync to receive them.
117 * To receive ModeChanged and/or FrameRateOverrides events specify this in
118 * the constructor. Other events start being delivered immediately.
119 */
120 explicit DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource =
121 gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp,
122 EventRegistrationFlags eventRegistration = {},
123 const sp<IBinder>& layerHandle = nullptr);
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 /*
145 * getEvents reads events from the queue and returns how many events were
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);
151 static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
152
153 /*
154 * sendEvents write events to the queue and returns how many events were
155 * written.
156 */
157 ssize_t sendEvents(Event const* events, size_t count);
158 static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
159
160 /*
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
174 /**
175 * getLatestVsyncEventData() gets the latest vsync event data.
176 */
177 status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const;
178
179 private:
180 sp<IDisplayEventConnection> mEventConnection;
181 std::unique_ptr<gui::BitTube> mDataChannel;
182 std::optional<status_t> mInitError;
183 };
184
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
190 // ----------------------------------------------------------------------------
191 }; // namespace android
192
193 #endif // ANDROID_GUI_DISPLAY_EVENT_H
194