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>
28 #include <gui/ISurfaceComposer.h>
29
30 // ----------------------------------------------------------------------------
31
32 namespace android {
33
34 // ----------------------------------------------------------------------------
35
36 class IDisplayEventConnection;
37
38 namespace gui {
39 class BitTube;
40 } // namespace gui
41
fourcc(char c1,char c2,char c3,char c4)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 }
48
49 // ----------------------------------------------------------------------------
50 class DisplayEventReceiver {
51 public:
52 enum {
53 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
54 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
55 DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'),
56 DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'),
57 DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
58 DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
59 };
60
61 struct Event {
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
67
68 struct Header {
69 uint32_t type;
70 PhysicalDisplayId displayId __attribute__((aligned(8)));
71 nsecs_t timestamp __attribute__((aligned(8)));
72 };
73
74 struct VSync {
75 uint32_t count;
76 nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
77 nsecs_t deadlineTimestamp __attribute__((aligned(8)));
78 nsecs_t frameInterval __attribute__((aligned(8)));
79 int64_t vsyncId;
80 };
81
82 struct Hotplug {
83 bool connected;
84 };
85
86 struct ModeChange {
87 int32_t modeId;
88 nsecs_t vsyncPeriod __attribute__((aligned(8)));
89 };
90
91 struct FrameRateOverride {
92 uid_t uid __attribute__((aligned(8)));
93 float frameRateHz __attribute__((aligned(8)));
94 };
95
96 Header header;
97 union {
98 VSync vsync;
99 Hotplug hotplug;
100 ModeChange modeChange;
101 FrameRateOverride frameRateOverride;
102 };
103 };
104
105 public:
106 /*
107 * DisplayEventReceiver creates and registers an event connection with
108 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
109 * or requestNextVsync to receive them.
110 * To receive ModeChanged and/or FrameRateOverrides events specify this in
111 * the constructor. Other events start being delivered immediately.
112 */
113 explicit DisplayEventReceiver(
114 ISurfaceComposer::VsyncSource vsyncSource = ISurfaceComposer::eVsyncSourceApp,
115 ISurfaceComposer::EventRegistrationFlags eventRegistration = {});
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 /*
137 * getEvents reads events from the queue and returns how many events were
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);
143 static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
144
145 /*
146 * sendEvents write events to the queue and returns how many events were
147 * written.
148 */
149 ssize_t sendEvents(Event const* events, size_t count);
150 static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
151
152 /*
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
166 private:
167 sp<IDisplayEventConnection> mEventConnection;
168 std::unique_ptr<gui::BitTube> mDataChannel;
169 };
170
171 // ----------------------------------------------------------------------------
172 }; // namespace android
173
174 #endif // ANDROID_GUI_DISPLAY_EVENT_H
175