blob: 65217cb72f64e3ace5cf032d5bd9b1f71458697d [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
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
Mathias Agopiancb9732a2012-04-03 17:48:03 -070023#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080024#include <gui/IDisplayEventConnection.h>
25
26#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070028#include <utils/SortedVector.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029
30// ---------------------------------------------------------------------------
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
37class DisplayHardware;
38
39// ---------------------------------------------------------------------------
40
41class EventThread : public Thread {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070042 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 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062
63public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065 EventThread(const sp<SurfaceFlinger>& flinger);
66
Mathias Agopiancb9732a2012-04-03 17:48:03 -070067 sp<Connection> createEventConnection() const;
68 status_t registerDisplayEventConnection(const sp<Connection>& connection);
69 status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080070
Mathias Agopiancb9732a2012-04-03 17:48:03 -070071 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
72 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080073
Mathias Agopian8aedd472012-01-24 16:39:14 -080074 nsecs_t getLastVSyncTimestamp() const;
Mathias Agopian8aedd472012-01-24 16:39:14 -080075 nsecs_t getVSyncPeriod() const;
76
Mathias Agopiand0566bc2011-11-17 17:49:17 -080077 void dump(String8& result, char* buffer, size_t SIZE) const;
78
79private:
80 virtual bool threadLoop();
81 virtual status_t readyToRun();
82 virtual void onFirstRef();
83
Mathias Agopiancb9732a2012-04-03 17:48:03 -070084 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian23748662011-12-05 14:33:34 -080085
Mathias Agopiand0566bc2011-11-17 17:49:17 -080086 // constants
87 sp<SurfaceFlinger> mFlinger;
88 const DisplayHardware& mHw;
89
90 mutable Mutex mLock;
91 mutable Condition mCondition;
92
93 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -070094 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian8aedd472012-01-24 16:39:14 -080095 nsecs_t mLastVSyncTimestamp;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080096
97 // main thread only
Mathias Agopiand0566bc2011-11-17 17:49:17 -080098 size_t mDeliveredEvents;
99};
100
101// ---------------------------------------------------------------------------
102
103}; // namespace android
104
105// ---------------------------------------------------------------------------
106
107#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */