EventThread.h revision 9e663de4fe1dcc872373ee530c60a375624671c3
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 "DisplayDevice.h"
31#include "DisplayHardware/PowerHAL.h"
32
33// ---------------------------------------------------------------------------
34namespace android {
35// ---------------------------------------------------------------------------
36
37class SurfaceFlinger;
38class String8;
39
40// ---------------------------------------------------------------------------
41
42class EventThread : public Thread {
43    class Connection : public BnDisplayEventConnection {
44    public:
45        Connection(const sp<EventThread>& eventThread);
46        status_t postEvent(const DisplayEventReceiver::Event& event);
47
48        // count >= 1 : continuous event. count is the vsync rate
49        // count == 0 : one-shot event that has not fired
50        // count ==-1 : one-shot event that fired this round / disabled
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
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 type, nsecs_t timestamp);
81    void onHotplugReceived(int type, bool connected);
82
83    Vector< sp<EventThread::Connection> > waitForEvent(
84            DisplayEventReceiver::Event* event);
85
86    void dump(String8& result) const;
87
88private:
89    virtual bool        threadLoop();
90    virtual void        onFirstRef();
91
92    void removeDisplayEventConnection(const wp<Connection>& connection);
93    void enableVSyncLocked();
94    void disableVSyncLocked();
95
96    // constants
97    sp<SurfaceFlinger> mFlinger;
98    PowerHAL mPowerHAL;
99
100    mutable Mutex mLock;
101    mutable Condition mCondition;
102
103    // protected by mLock
104    SortedVector< wp<Connection> > mDisplayEventConnections;
105    Vector< DisplayEventReceiver::Event > mPendingEvents;
106    DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
107    bool mUseSoftwareVSync;
108
109    // for debugging
110    bool mDebugVsyncEnabled;
111};
112
113// ---------------------------------------------------------------------------
114
115}; // namespace android
116
117// ---------------------------------------------------------------------------
118
119#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
120