blob: 5e88693ca75a24ca91e055ba5e850db1baa2bcb4 [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
Jesse Hall9e663de2013-08-16 14:28:37 -070030#include "DisplayDevice.h"
Mathias Agopian86303202012-07-24 22:46:10 -070031#include "DisplayHardware/PowerHAL.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070032
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035// ---------------------------------------------------------------------------
36
37class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039
40// ---------------------------------------------------------------------------
41
Mathias Agopian86303202012-07-24 22:46:10 -070042class EventThread : public Thread {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070043 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
Mathias Agopiancb9732a2012-04-03 17:48:03 -070051 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);
Mathias Agopian8aedd472012-01-24 16:39:14 -080069
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
71 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080072
Mathias Agopian22ffb112012-04-10 21:04:02 -070073 // 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();
Mathias Agopian8aedd472012-01-24 16:39:14 -080078
Mathias Agopian86303202012-07-24 22:46:10 -070079 // called when receiving a vsync event
Mathias Agopian148994e2012-09-19 17:31:36 -070080 void onVSyncReceived(int type, nsecs_t timestamp);
81 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -070082
Mathias Agopianf6bbd442012-08-21 15:47:28 -070083 Vector< sp<EventThread::Connection> > waitForEvent(
84 DisplayEventReceiver::Event* event);
85
Mathias Agopian74d211a2013-04-22 16:55:35 +020086 void dump(String8& result) const;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080087
88private:
89 virtual bool threadLoop();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080090 virtual void onFirstRef();
91
Mathias Agopiancb9732a2012-04-03 17:48:03 -070092 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -070093 void enableVSyncLocked();
94 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -080095
Mathias Agopiand0566bc2011-11-17 17:49:17 -080096 // constants
Mathias Agopian86303202012-07-24 22:46:10 -070097 sp<SurfaceFlinger> mFlinger;
98 PowerHAL mPowerHAL;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080099
100 mutable Mutex mLock;
101 mutable Condition mCondition;
102
103 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700104 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian148994e2012-09-19 17:31:36 -0700105 Vector< DisplayEventReceiver::Event > mPendingEvents;
Jesse Hall9e663de2013-08-16 14:28:37 -0700106 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700107 bool mUseSoftwareVSync;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700108
109 // for debugging
110 bool mDebugVsyncEnabled;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800111};
112
113// ---------------------------------------------------------------------------
114
115}; // namespace android
116
117// ---------------------------------------------------------------------------
118
119#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */