blob: cb9b3733929f30ecc427e2d50045a57a164d90a5 [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
35class BitTube;
36class IDisplayEventConnection;
37
Colin Crossc2b90172016-09-26 18:11:51 -070038static inline constexpr uint32_t fourcc(char c1, char c2, char c3, char c4) {
39 return static_cast<uint32_t>(c1) << 24 |
40 static_cast<uint32_t>(c2) << 16 |
41 static_cast<uint32_t>(c3) << 8 |
42 static_cast<uint32_t>(c4);
43}
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
Colin Crossc2b90172016-09-26 18:11:51 -070045// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080046class DisplayEventReceiver {
47public:
48 enum {
Colin Crossc2b90172016-09-26 18:11:51 -070049 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
50 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051 };
52
53 struct Event {
54
55 struct Header {
56 uint32_t type;
Mathias Agopianff28e202012-09-20 23:24:19 -070057 uint32_t id;
Fengwei Yin0c7c81f2014-03-20 16:45:05 +080058 nsecs_t timestamp __attribute__((aligned(8)));
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059 };
60
61 struct VSync {
62 uint32_t count;
63 };
64
Mathias Agopian148994e2012-09-19 17:31:36 -070065 struct Hotplug {
Mathias Agopian148994e2012-09-19 17:31:36 -070066 bool connected;
67 };
68
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069 Header header;
70 union {
71 VSync vsync;
Mathias Agopian148994e2012-09-19 17:31:36 -070072 Hotplug hotplug;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080073 };
74 };
75
76public:
77 /*
78 * DisplayEventReceiver creates and registers an event connection with
Mathias Agopian3cf199a2012-01-31 16:42:54 -080079 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
80 * or requestNextVsync to receive them.
81 * Other events start being delivered immediately.
Mathias Agopiand0566bc2011-11-17 17:49:17 -080082 */
83 DisplayEventReceiver();
84
85 /*
86 * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
87 * stop being delivered immediately. Note that the queue could have
88 * some events pending. These will be delivered.
89 */
90 ~DisplayEventReceiver();
91
92 /*
93 * initCheck returns the state of DisplayEventReceiver after construction.
94 */
95 status_t initCheck() const;
96
97 /*
98 * getFd returns the file descriptor to use to receive events.
99 * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
100 * file-descriptor.
101 */
102 int getFd() const;
103
104 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700105 * getEvents reads events from the queue and returns how many events were
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800106 * read. Returns 0 if there are no more events or a negative error code.
107 * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
108 * should be destroyed and getEvents() shouldn't be called again.
109 */
110 ssize_t getEvents(Event* events, size_t count);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800111 static ssize_t getEvents(const sp<BitTube>& dataChannel,
112 Event* events, size_t count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800113
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800114 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700115 * sendEvents write events to the queue and returns how many events were
116 * written.
117 */
118 static ssize_t sendEvents(const sp<BitTube>& dataChannel,
119 Event const* events, size_t count);
120
121 /*
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800122 * setVsyncRate() sets the Event::VSync delivery rate. A value of
123 * 1 returns every Event::VSync. A value of 2 returns every other event,
124 * etc... a value of 0 returns no event unless requestNextVsync() has
125 * been called.
126 */
127 status_t setVsyncRate(uint32_t count);
128
129 /*
130 * requestNextVsync() schedules the next Event::VSync. It has no effect
131 * if the vsync rate is > 0.
132 */
133 status_t requestNextVsync();
134
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800135private:
136 sp<IDisplayEventConnection> mEventConnection;
137 sp<BitTube> mDataChannel;
138};
139
140// ----------------------------------------------------------------------------
141}; // namespace android
142
143#endif // ANDROID_GUI_DISPLAY_EVENT_H