EventThread.h revision 74d211ae26a0257c6075a823812e40b55aa1e653
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 <hardware/hwcomposer_defs.h>
27
28#include <utils/Errors.h>
29#include <utils/threads.h>
30#include <utils/SortedVector.h>
31
32#include "DisplayHardware/PowerHAL.h"
33
34// ---------------------------------------------------------------------------
35namespace android {
36// ---------------------------------------------------------------------------
37
38class SurfaceFlinger;
39class String8;
40
41// ---------------------------------------------------------------------------
42
43class EventThread : public Thread {
44    class Connection : public BnDisplayEventConnection {
45    public:
46        Connection(const sp<EventThread>& eventThread);
47        status_t postEvent(const DisplayEventReceiver::Event& event);
48
49        // count >= 1 : continuous event. count is the vsync rate
50        // count == 0 : one-shot event that has not fired
51        // count ==-1 : one-shot event that fired this round / disabled
52        int32_t count;
53
54    private:
55        virtual ~Connection();
56        virtual void onFirstRef();
57        virtual sp<BitTube> getDataChannel() const;
58        virtual void setVsyncRate(uint32_t count);
59        virtual void requestNextVsync();    // asynchronous
60        sp<EventThread> const mEventThread;
61        sp<BitTube> const mChannel;
62    };
63
64public:
65
66    EventThread(const sp<SurfaceFlinger>& flinger);
67
68    sp<Connection> createEventConnection() const;
69    status_t registerDisplayEventConnection(const sp<Connection>& connection);
70
71    void setVsyncRate(uint32_t count, const sp<Connection>& connection);
72    void requestNextVsync(const sp<Connection>& connection);
73
74    // called before the screen is turned off from main thread
75    void onScreenReleased();
76
77    // called after the screen is turned on from main thread
78    void onScreenAcquired();
79
80    // called when receiving a vsync event
81    void onVSyncReceived(int type, nsecs_t timestamp);
82    void onHotplugReceived(int type, bool connected);
83
84    Vector< sp<EventThread::Connection> > waitForEvent(
85            DisplayEventReceiver::Event* event);
86
87    void dump(String8& result) const;
88
89private:
90    virtual bool        threadLoop();
91    virtual void        onFirstRef();
92
93    void removeDisplayEventConnection(const wp<Connection>& connection);
94    void enableVSyncLocked();
95    void disableVSyncLocked();
96
97    // constants
98    sp<SurfaceFlinger> mFlinger;
99    PowerHAL mPowerHAL;
100
101    mutable Mutex mLock;
102    mutable Condition mCondition;
103
104    // protected by mLock
105    SortedVector< wp<Connection> > mDisplayEventConnections;
106    Vector< DisplayEventReceiver::Event > mPendingEvents;
107    DisplayEventReceiver::Event mVSyncEvent[HWC_DISPLAY_TYPES_SUPPORTED];
108    bool mUseSoftwareVSync;
109
110    // for debugging
111    bool mDebugVsyncEnabled;
112};
113
114// ---------------------------------------------------------------------------
115
116}; // namespace android
117
118// ---------------------------------------------------------------------------
119
120#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
121