blob: 9557b4f96709a84a68c0b3fd761a780aff9dd8f5 [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
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
29// ----------------------------------------------------------------------------
30
31namespace android {
32
33// ----------------------------------------------------------------------------
34
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035class IDisplayEventConnection;
36
Dan Stoza27c81152017-03-31 16:30:42 -070037namespace gui {
38class BitTube;
39} // namespace gui
40
Colin Crossc2b90172016-09-26 18:11:51 -070041static inline constexpr uint32_t fourcc(char c1, char c2, char c3, char c4) {
42 return static_cast<uint32_t>(c1) << 24 |
43 static_cast<uint32_t>(c2) << 16 |
44 static_cast<uint32_t>(c3) << 8 |
45 static_cast<uint32_t>(c4);
46}
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Colin Crossc2b90172016-09-26 18:11:51 -070048// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049class DisplayEventReceiver {
50public:
51 enum {
Colin Crossc2b90172016-09-26 18:11:51 -070052 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
53 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054 };
55
56 struct Event {
57
58 struct Header {
59 uint32_t type;
Mathias Agopianff28e202012-09-20 23:24:19 -070060 uint32_t id;
Fengwei Yin0c7c81f2014-03-20 16:45:05 +080061 nsecs_t timestamp __attribute__((aligned(8)));
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062 };
63
64 struct VSync {
65 uint32_t count;
66 };
67
Mathias Agopian148994e2012-09-19 17:31:36 -070068 struct Hotplug {
Mathias Agopian148994e2012-09-19 17:31:36 -070069 bool connected;
70 };
71
Mathias Agopiand0566bc2011-11-17 17:49:17 -080072 Header header;
73 union {
74 VSync vsync;
Mathias Agopian148994e2012-09-19 17:31:36 -070075 Hotplug hotplug;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080076 };
77 };
78
79public:
80 /*
81 * DisplayEventReceiver creates and registers an event connection with
Mathias Agopian3cf199a2012-01-31 16:42:54 -080082 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
83 * or requestNextVsync to receive them.
84 * Other events start being delivered immediately.
Mathias Agopiand0566bc2011-11-17 17:49:17 -080085 */
86 DisplayEventReceiver();
87
88 /*
89 * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
90 * stop being delivered immediately. Note that the queue could have
91 * some events pending. These will be delivered.
92 */
93 ~DisplayEventReceiver();
94
95 /*
96 * initCheck returns the state of DisplayEventReceiver after construction.
97 */
98 status_t initCheck() const;
99
100 /*
101 * getFd returns the file descriptor to use to receive events.
102 * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
103 * file-descriptor.
104 */
105 int getFd() const;
106
107 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700108 * getEvents reads events from the queue and returns how many events were
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800109 * read. Returns 0 if there are no more events or a negative error code.
110 * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
111 * should be destroyed and getEvents() shouldn't be called again.
112 */
113 ssize_t getEvents(Event* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700114 static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800115
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800116 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700117 * sendEvents write events to the queue and returns how many events were
118 * written.
119 */
Dan Stoza6b698e42017-04-03 13:09:08 -0700120 static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700121
122 /*
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800123 * setVsyncRate() sets the Event::VSync delivery rate. A value of
124 * 1 returns every Event::VSync. A value of 2 returns every other event,
125 * etc... a value of 0 returns no event unless requestNextVsync() has
126 * been called.
127 */
128 status_t setVsyncRate(uint32_t count);
129
130 /*
131 * requestNextVsync() schedules the next Event::VSync. It has no effect
132 * if the vsync rate is > 0.
133 */
134 status_t requestNextVsync();
135
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800136private:
137 sp<IDisplayEventConnection> mEventConnection;
Dan Stoza6b698e42017-04-03 13:09:08 -0700138 std::unique_ptr<gui::BitTube> mDataChannel;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800139};
140
141// ----------------------------------------------------------------------------
142}; // namespace android
143
144#endif // ANDROID_GUI_DISPLAY_EVENT_H