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
29// ----------------------------------------------------------------------------
30
31namespace android {
32
33// ----------------------------------------------------------------------------
34
35class BitTube;
36class IDisplayEventConnection;
37
38// ----------------------------------------------------------------------------
39
40class DisplayEventReceiver {
41public:
42    enum {
43        DISPLAY_EVENT_VSYNC = 'vsyn',
44        DISPLAY_EVENT_HOTPLUG = 'plug'
45    };
46
47    struct Event {
48
49        struct Header {
50            uint32_t type;
51            uint32_t id;
52            nsecs_t timestamp;
53        };
54
55        struct VSync {
56            uint32_t count;
57        };
58
59        struct Hotplug {
60            bool connected;
61        };
62
63        Header header;
64        union {
65            VSync vsync;
66            Hotplug hotplug;
67        };
68    };
69
70public:
71    /*
72     * DisplayEventReceiver creates and registers an event connection with
73     * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
74     * or requestNextVsync to receive them.
75     * Other events start being delivered immediately.
76     */
77    DisplayEventReceiver();
78
79    /*
80     * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
81     * stop being delivered immediately. Note that the queue could have
82     * some events pending. These will be delivered.
83     */
84    ~DisplayEventReceiver();
85
86    /*
87     * initCheck returns the state of DisplayEventReceiver after construction.
88     */
89    status_t initCheck() const;
90
91    /*
92     * getFd returns the file descriptor to use to receive events.
93     * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
94     * file-descriptor.
95     */
96    int getFd() const;
97
98    /*
99     * getEvents reads events from the queue and returns how many events were
100     * read. Returns 0 if there are no more events or a negative error code.
101     * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
102     * should be destroyed and getEvents() shouldn't be called again.
103     */
104    ssize_t getEvents(Event* events, size_t count);
105    static ssize_t getEvents(const sp<BitTube>& dataChannel,
106            Event* events, size_t count);
107
108    /*
109     * sendEvents write events to the queue and returns how many events were
110     * written.
111     */
112    static ssize_t sendEvents(const sp<BitTube>& dataChannel,
113            Event const* events, size_t count);
114
115    /*
116     * setVsyncRate() sets the Event::VSync delivery rate. A value of
117     * 1 returns every Event::VSync. A value of 2 returns every other event,
118     * etc... a value of 0 returns no event unless  requestNextVsync() has
119     * been called.
120     */
121    status_t setVsyncRate(uint32_t count);
122
123    /*
124     * requestNextVsync() schedules the next Event::VSync. It has no effect
125     * if the vsync rate is > 0.
126     */
127    status_t requestNextVsync();
128
129private:
130    sp<IDisplayEventConnection> mEventConnection;
131    sp<BitTube> mDataChannel;
132};
133
134// ----------------------------------------------------------------------------
135}; // namespace android
136
137#endif // ANDROID_GUI_DISPLAY_EVENT_H
138