EventThread.h revision ae2cfb5746e87f1bf17c446e20274c41ce0a57ce
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
69    void setVsyncRate(uint32_t count, const sp<Connection>& connection);
70    void requestNextVsync(const sp<Connection>& connection);
71
72    // called before the screen is turned off from main thread
73    void onScreenReleased();
74
75    // called after the screen is turned on from main thread
76    void onScreenAcquired();
77
78    // called when receiving a vsync event
79    void onVSyncReceived(const wp<IBinder>& display, nsecs_t timestamp);
80
81    Vector< sp<EventThread::Connection> > waitForEvent(
82            DisplayEventReceiver::Event* event);
83
84    void dump(String8& result, char* buffer, size_t SIZE) const;
85
86private:
87    virtual bool        threadLoop();
88    virtual void        onFirstRef();
89
90    void removeDisplayEventConnection(const wp<Connection>& connection);
91    void enableVSyncLocked();
92    void disableVSyncLocked();
93
94    // constants
95    sp<SurfaceFlinger> mFlinger;
96    PowerHAL mPowerHAL;
97
98    mutable Mutex mLock;
99    mutable Condition mCondition;
100
101    // protected by mLock
102    SortedVector< wp<Connection> > mDisplayEventConnections;
103    nsecs_t mVSyncTimestamp;
104    bool mUseSoftwareVSync;
105    size_t mVSyncCount;
106
107    // for debugging
108    bool mDebugVsyncEnabled;
109};
110
111// ---------------------------------------------------------------------------
112
113}; // namespace android
114
115// ---------------------------------------------------------------------------
116
117#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
118