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
42
43class VSyncSource : public virtual RefBase {
44public:
45    class Callback: public virtual RefBase {
46    public:
47        virtual ~Callback() {}
48        virtual void onVSyncEvent(nsecs_t when) = 0;
49    };
50
51    virtual ~VSyncSource() {}
52    virtual void setVSyncEnabled(bool enable) = 0;
53    virtual void setCallback(const sp<Callback>& callback) = 0;
54    virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
55};
56
57class EventThread : public Thread, private VSyncSource::Callback {
58    class Connection : public BnDisplayEventConnection {
59    public:
60        Connection(const sp<EventThread>& eventThread);
61        status_t postEvent(const DisplayEventReceiver::Event& event);
62
63        // count >= 1 : continuous event. count is the vsync rate
64        // count == 0 : one-shot event that has not fired
65        // count ==-1 : one-shot event that fired this round / disabled
66        int32_t count;
67
68    private:
69        virtual ~Connection();
70        virtual void onFirstRef();
71        virtual sp<BitTube> getDataChannel() const;
72        virtual void setVsyncRate(uint32_t count);
73        virtual void requestNextVsync();    // asynchronous
74        sp<EventThread> const mEventThread;
75        sp<BitTube> const mChannel;
76    };
77
78public:
79
80    EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger);
81
82    sp<Connection> createEventConnection() const;
83    status_t registerDisplayEventConnection(const sp<Connection>& connection);
84
85    void setVsyncRate(uint32_t count, const sp<Connection>& connection);
86    void requestNextVsync(const sp<Connection>& connection);
87
88    // called before the screen is turned off from main thread
89    void onScreenReleased();
90
91    // called after the screen is turned on from main thread
92    void onScreenAcquired();
93
94    // called when receiving a hotplug event
95    void onHotplugReceived(int type, bool connected);
96
97    Vector< sp<EventThread::Connection> > waitForEvent(
98            DisplayEventReceiver::Event* event);
99
100    void dump(String8& result) const;
101    void sendVsyncHintOff();
102
103    void setPhaseOffset(nsecs_t phaseOffset);
104
105private:
106    virtual bool        threadLoop();
107    virtual void        onFirstRef();
108
109    virtual void onVSyncEvent(nsecs_t timestamp);
110
111    void removeDisplayEventConnection(const wp<Connection>& connection);
112    void enableVSyncLocked();
113    void disableVSyncLocked();
114    void sendVsyncHintOnLocked();
115
116    // constants
117    sp<VSyncSource> mVSyncSource;
118    PowerHAL mPowerHAL;
119    SurfaceFlinger& mFlinger;
120
121    mutable Mutex mLock;
122    mutable Condition mCondition;
123
124    // protected by mLock
125    SortedVector< wp<Connection> > mDisplayEventConnections;
126    Vector< DisplayEventReceiver::Event > mPendingEvents;
127    DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
128    bool mUseSoftwareVSync;
129    bool mVsyncEnabled;
130
131    // for debugging
132    bool mDebugVsyncEnabled;
133
134    bool mVsyncHintSent;
135    timer_t mTimerId;
136};
137
138// ---------------------------------------------------------------------------
139
140}; // namespace android
141
142// ---------------------------------------------------------------------------
143
144#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
145