EventThread.h revision a4cb35a2864d58e9a764a17623e15ab25a9964a0
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#include "DisplayHardware/PowerHAL.h"
31
32// ---------------------------------------------------------------------------
33namespace android {
34// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
37class String8;
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        int32_t count;
51
52    private:
53        virtual ~Connection();
54        virtual void onFirstRef();
55        virtual sp<BitTube> getDataChannel() const;
56        virtual void setVsyncRate(uint32_t count);
57        virtual void requestNextVsync();    // asynchronous
58        sp<EventThread> const mEventThread;
59        sp<BitTube> const mChannel;
60    };
61
62public:
63
64    EventThread(const sp<SurfaceFlinger>& flinger);
65
66    sp<Connection> createEventConnection() const;
67    status_t registerDisplayEventConnection(const sp<Connection>& connection);
68    status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
69
70    void setVsyncRate(uint32_t count, const sp<Connection>& connection);
71    void requestNextVsync(const sp<Connection>& connection);
72
73    // called before the screen is turned off from main thread
74    void onScreenReleased();
75
76    // called after the screen is turned on from main thread
77    void onScreenAcquired();
78
79    // called when receiving a vsync event
80    void onVSyncReceived(int display, nsecs_t timestamp);
81
82    void dump(String8& result, char* buffer, size_t SIZE) const;
83
84private:
85    virtual bool        threadLoop();
86    virtual status_t    readyToRun();
87    virtual void        onFirstRef();
88
89    void removeDisplayEventConnection(const wp<Connection>& connection);
90    void enableVSyncLocked();
91    void disableVSyncLocked();
92
93    // constants
94    sp<SurfaceFlinger> mFlinger;
95    PowerHAL mPowerHAL;
96
97    mutable Mutex mLock;
98    mutable Condition mCondition;
99
100    // protected by mLock
101    SortedVector< wp<Connection> > mDisplayEventConnections;
102    nsecs_t mVSyncTimestamp;
103    bool mUseSoftwareVSync;
104    size_t mVSyncCount;
105
106    // for debugging
107    bool mDebugVsyncEnabled;
108};
109
110// ---------------------------------------------------------------------------
111
112}; // namespace android
113
114// ---------------------------------------------------------------------------
115
116#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
117