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_CONFIG_CHANGED = fourcc('c', 'o', 'n', 'f'),
56     };
57 
58     struct Event {
59 
60         struct Header {
61             uint32_t type;
62             PhysicalDisplayId displayId;
63             nsecs_t timestamp __attribute__((aligned(8)));
64         };
65 
66         struct VSync {
67             uint32_t count;
68             nsecs_t expectedVSyncTimestamp;
69         };
70 
71         struct Hotplug {
72             bool connected;
73         };
74 
75         struct Config {
76             int32_t configId;
77             nsecs_t vsyncPeriod;
78         };
79 
80         Header header;
81         union {
82             VSync vsync;
83             Hotplug hotplug;
84             Config config;
85         };
86     };
87 
88 public:
89     /*
90      * DisplayEventReceiver creates and registers an event connection with
91      * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
92      * or requestNextVsync to receive them.
93      * To receive Config Changed events specify this in the constructor.
94      * Other events start being delivered immediately.
95      */
96     explicit DisplayEventReceiver(
97             ISurfaceComposer::VsyncSource vsyncSource = ISurfaceComposer::eVsyncSourceApp,
98             ISurfaceComposer::ConfigChanged configChanged =
99                     ISurfaceComposer::eConfigChangedSuppress);
100 
101     /*
102      * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
103      * stop being delivered immediately. Note that the queue could have
104      * some events pending. These will be delivered.
105      */
106     ~DisplayEventReceiver();
107 
108     /*
109      * initCheck returns the state of DisplayEventReceiver after construction.
110      */
111     status_t initCheck() const;
112 
113     /*
114      * getFd returns the file descriptor to use to receive events.
115      * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
116      * file-descriptor.
117      */
118     int getFd() const;
119 
120     /*
121      * getEvents reads events from the queue and returns how many events were
122      * read. Returns 0 if there are no more events or a negative error code.
123      * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
124      * should be destroyed and getEvents() shouldn't be called again.
125      */
126     ssize_t getEvents(Event* events, size_t count);
127     static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
128 
129     /*
130      * sendEvents write events to the queue and returns how many events were
131      * written.
132      */
133     static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
134 
135     /*
136      * setVsyncRate() sets the Event::VSync delivery rate. A value of
137      * 1 returns every Event::VSync. A value of 2 returns every other event,
138      * etc... a value of 0 returns no event unless  requestNextVsync() has
139      * been called.
140      */
141     status_t setVsyncRate(uint32_t count);
142 
143     /*
144      * requestNextVsync() schedules the next Event::VSync. It has no effect
145      * if the vsync rate is > 0.
146      */
147     status_t requestNextVsync();
148 
149     /*
150      * requestLatestConfig() force-requests the current config for the primary
151      * display.
152      */
153     status_t requestLatestConfig();
154 
155 private:
156     sp<IDisplayEventConnection> mEventConnection;
157     std::unique_ptr<gui::BitTube> mDataChannel;
158 };
159 
160 // ----------------------------------------------------------------------------
161 }; // namespace android
162 
163 #endif // ANDROID_GUI_DISPLAY_EVENT_H
164