1d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian/*
2d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * Copyright (C) 2011 The Android Open Source Project
3d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *
4d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * you may not use this file except in compliance with the License.
6d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * You may obtain a copy of the License at
7d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *
8d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *
10d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * See the License for the specific language governing permissions and
14d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * limitations under the License.
15d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian */
16d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
17d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#ifndef ANDROID_GUI_DISPLAY_EVENT_H
18d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#define ANDROID_GUI_DISPLAY_EVENT_H
19d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
20d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <stdint.h>
21d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <sys/types.h>
22d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
23d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <utils/Errors.h>
24d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <utils/RefBase.h>
25d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <utils/Timers.h>
26d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
27d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <binder/IInterface.h>
28d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
29d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ----------------------------------------------------------------------------
30d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
31d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopiannamespace android {
32d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
33d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ----------------------------------------------------------------------------
34d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
35d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianclass BitTube;
36d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianclass IDisplayEventConnection;
37d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
38d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ----------------------------------------------------------------------------
39d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
40d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianclass DisplayEventReceiver {
41d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianpublic:
42d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    enum {
43148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        DISPLAY_EVENT_VSYNC = 'vsyn',
44148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        DISPLAY_EVENT_HOTPLUG = 'plug'
45d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    };
46d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
47d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    struct Event {
48d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
49d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        struct Header {
50d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian            uint32_t type;
51ff28e201ec0c6d620eaaa29814ab52958487dc31Mathias Agopian            uint32_t id;
520c7c81f5a62937f79b7e734668aa997fd8df1927Fengwei Yin            nsecs_t timestamp __attribute__((aligned(8)));
53d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        };
54d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
55d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        struct VSync {
56d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian            uint32_t count;
57d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        };
58d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
59148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        struct Hotplug {
60148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian            bool connected;
61148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        };
62148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian
63d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        Header header;
64d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        union {
65d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian            VSync vsync;
66148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian            Hotplug hotplug;
67d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian        };
68d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    };
69d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
70d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianpublic:
71d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    /*
72d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * DisplayEventReceiver creates and registers an event connection with
733cf199aef6250552555344944c3e3a6e4ab05f92Mathias Agopian     * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
743cf199aef6250552555344944c3e3a6e4ab05f92Mathias Agopian     * or requestNextVsync to receive them.
753cf199aef6250552555344944c3e3a6e4ab05f92Mathias Agopian     * Other events start being delivered immediately.
76d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     */
77d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    DisplayEventReceiver();
78d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
79d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    /*
80d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
81d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * stop being delivered immediately. Note that the queue could have
82d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * some events pending. These will be delivered.
83d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     */
84d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    ~DisplayEventReceiver();
85d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
86d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    /*
87d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * initCheck returns the state of DisplayEventReceiver after construction.
88d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     */
89d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    status_t initCheck() const;
90d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
91d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    /*
92d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * getFd returns the file descriptor to use to receive events.
93d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
94d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * file-descriptor.
95d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     */
96d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    int getFd() const;
97d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
98d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    /*
997b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian     * getEvents reads events from the queue and returns how many events were
100d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * read. Returns 0 if there are no more events or a negative error code.
101d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
102d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     * should be destroyed and getEvents() shouldn't be called again.
103d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian     */
104d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    ssize_t getEvents(Event* events, size_t count);
10599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    static ssize_t getEvents(const sp<BitTube>& dataChannel,
10699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian            Event* events, size_t count);
107d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
108478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian    /*
1097b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian     * sendEvents write events to the queue and returns how many events were
1107b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian     * written.
1117b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian     */
1127b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian    static ssize_t sendEvents(const sp<BitTube>& dataChannel,
1137b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian            Event const* events, size_t count);
1147b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian
1157b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian    /*
116478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     * setVsyncRate() sets the Event::VSync delivery rate. A value of
117478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     * 1 returns every Event::VSync. A value of 2 returns every other event,
118478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     * etc... a value of 0 returns no event unless  requestNextVsync() has
119478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     * been called.
120478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     */
121478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian    status_t setVsyncRate(uint32_t count);
122478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian
123478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian    /*
124478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     * requestNextVsync() schedules the next Event::VSync. It has no effect
125478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     * if the vsync rate is > 0.
126478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian     */
127478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian    status_t requestNextVsync();
128478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian
129d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianprivate:
130d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    sp<IDisplayEventConnection> mEventConnection;
131d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    sp<BitTube> mDataChannel;
132d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian};
133d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
134d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ----------------------------------------------------------------------------
135d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian}; // namespace android
136d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
137d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#endif // ANDROID_GUI_DISPLAY_EVENT_H
138