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