EventThread.h revision 921e6ac4b7610a178285898d191eb0e3afe906c0
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.h"
31
32// ---------------------------------------------------------------------------
33namespace android {
34// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
37class String8;
38
39// ---------------------------------------------------------------------------
40
41class EventThread : public Thread, public DisplayHardware::VSyncHandler {
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        // count ==-2 : one-shot event that fired the round before
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    status_t unregisterDisplayEventConnection(const wp<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    void dump(String8& result, char* buffer, size_t SIZE) const;
81
82private:
83    virtual bool        threadLoop();
84    virtual status_t    readyToRun();
85    virtual void        onFirstRef();
86    virtual void        onVSyncReceived(int, nsecs_t timestamp);
87
88    void removeDisplayEventConnection(const wp<Connection>& connection);
89    void enableVSyncLocked();
90    void disableVSyncLocked();
91
92    // constants
93    DisplayHardware& mHw;
94
95    mutable Mutex mLock;
96    mutable Condition mCondition;
97
98    // protected by mLock
99    SortedVector< wp<Connection> > mDisplayEventConnections;
100    nsecs_t mLastVSyncTimestamp;
101    nsecs_t mVSyncTimestamp;
102    bool mUseSoftwareVSync;
103
104    // main thread only
105    size_t mDeliveredEvents;
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