blob: d1c4fcd987aacf6713e7a7ffbab6eac986c76ade [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
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070042
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};
55
56class EventThread : public Thread, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070057 class Connection : public BnDisplayEventConnection {
58 public:
59 Connection(const sp<EventThread>& eventThread);
60 status_t postEvent(const DisplayEventReceiver::Event& event);
61
62 // count >= 1 : continuous event. count is the vsync rate
63 // count == 0 : one-shot event that has not fired
64 // count ==-1 : one-shot event that fired this round / disabled
Mathias Agopiancb9732a2012-04-03 17:48:03 -070065 int32_t count;
66
67 private:
68 virtual ~Connection();
69 virtual void onFirstRef();
70 virtual sp<BitTube> getDataChannel() const;
71 virtual void setVsyncRate(uint32_t count);
72 virtual void requestNextVsync(); // asynchronous
73 sp<EventThread> const mEventThread;
74 sp<BitTube> const mChannel;
75 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080076
77public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070078
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070079 EventThread(const sp<VSyncSource>& src);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080080
Mathias Agopiancb9732a2012-04-03 17:48:03 -070081 sp<Connection> createEventConnection() const;
82 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080083
Mathias Agopiancb9732a2012-04-03 17:48:03 -070084 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
85 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080086
Mathias Agopian22ffb112012-04-10 21:04:02 -070087 // called before the screen is turned off from main thread
88 void onScreenReleased();
89
90 // called after the screen is turned on from main thread
91 void onScreenAcquired();
Mathias Agopian8aedd472012-01-24 16:39:14 -080092
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093 // called when receiving a hotplug event
Mathias Agopian148994e2012-09-19 17:31:36 -070094 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -070095
Mathias Agopianf6bbd442012-08-21 15:47:28 -070096 Vector< sp<EventThread::Connection> > waitForEvent(
97 DisplayEventReceiver::Event* event);
98
Mathias Agopian74d211a2013-04-22 16:55:35 +020099 void dump(String8& result) const;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700100 void sendVsyncHintOff();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800101
102private:
103 virtual bool threadLoop();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800104 virtual void onFirstRef();
105
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700106 virtual void onVSyncEvent(nsecs_t timestamp);
107
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700108 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700109 void enableVSyncLocked();
110 void disableVSyncLocked();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700111 void sendVsyncHintOnLocked();
Mathias Agopian23748662011-12-05 14:33:34 -0800112
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800113 // constants
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700114 sp<VSyncSource> mVSyncSource;
Mathias Agopian86303202012-07-24 22:46:10 -0700115 PowerHAL mPowerHAL;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800116
117 mutable Mutex mLock;
118 mutable Condition mCondition;
119
120 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700121 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian148994e2012-09-19 17:31:36 -0700122 Vector< DisplayEventReceiver::Event > mPendingEvents;
Jesse Hall9e663de2013-08-16 14:28:37 -0700123 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700124 bool mUseSoftwareVSync;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700125 bool mVsyncEnabled;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700126
127 // for debugging
128 bool mDebugVsyncEnabled;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700129
130 bool mVsyncHintSent;
131 timer_t mTimerId;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800132};
133
134// ---------------------------------------------------------------------------
135
136}; // namespace android
137
138// ---------------------------------------------------------------------------
139
140#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */