EventThread.h revision cb9732a951d20cacb7ebe2dab132b5738226b1b6
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_SURFACE_FLINGER_EVENT_THREAD_H
18#define ANDROID_SURFACE_FLINGER_EVENT_THREAD_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <gui/DisplayEventReceiver.h>
24#include <gui/IDisplayEventConnection.h>
25
26#include <utils/Errors.h>
27#include <utils/threads.h>
28#include <utils/SortedVector.h>
29
30// ---------------------------------------------------------------------------
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
37class DisplayHardware;
38
39// ---------------------------------------------------------------------------
40
41class EventThread : public Thread {
42    class Connection : public BnDisplayEventConnection {
43    public:
44        Connection(const sp<EventThread>& eventThread);
45        status_t postEvent(const DisplayEventReceiver::Event& event);
46
47        // count >= 1 : continuous event. count is the vsync rate
48        // count == 0 : one-shot event that has not fired
49        // count ==-1 : one-shot event that fired this round / disabled
50        // count ==-2 : one-shot event that fired the round before
51        int32_t count;
52
53    private:
54        virtual ~Connection();
55        virtual void onFirstRef();
56        virtual sp<BitTube> getDataChannel() const;
57        virtual void setVsyncRate(uint32_t count);
58        virtual void requestNextVsync();    // asynchronous
59        sp<EventThread> const mEventThread;
60        sp<BitTube> const mChannel;
61    };
62
63public:
64
65    EventThread(const sp<SurfaceFlinger>& flinger);
66
67    sp<Connection> createEventConnection() const;
68    status_t registerDisplayEventConnection(const sp<Connection>& connection);
69    status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
70
71    void setVsyncRate(uint32_t count, const sp<Connection>& connection);
72    void requestNextVsync(const sp<Connection>& connection);
73
74    nsecs_t getLastVSyncTimestamp() const;
75    nsecs_t getVSyncPeriod() const;
76
77    void dump(String8& result, char* buffer, size_t SIZE) const;
78
79private:
80    virtual bool        threadLoop();
81    virtual status_t    readyToRun();
82    virtual void        onFirstRef();
83
84    void removeDisplayEventConnection(const wp<Connection>& connection);
85
86    // constants
87    sp<SurfaceFlinger> mFlinger;
88    const DisplayHardware& mHw;
89
90    mutable Mutex mLock;
91    mutable Condition mCondition;
92
93    // protected by mLock
94    SortedVector< wp<Connection> > mDisplayEventConnections;
95    nsecs_t mLastVSyncTimestamp;
96
97    // main thread only
98    size_t mDeliveredEvents;
99};
100
101// ---------------------------------------------------------------------------
102
103}; // namespace android
104
105// ---------------------------------------------------------------------------
106
107#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
108