blob: 9ba179ad7be009ecfd2dec33a7a546a8a1590a09 [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;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070054 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070055};
56
57class EventThread : public Thread, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070058 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
Mathias Agopiancb9732a2012-04-03 17:48:03 -070066 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 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080077
78public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070079
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070080 EventThread(const sp<VSyncSource>& src);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080081
Mathias Agopiancb9732a2012-04-03 17:48:03 -070082 sp<Connection> createEventConnection() const;
83 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080084
Mathias Agopiancb9732a2012-04-03 17:48:03 -070085 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
86 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080087
Mathias Agopian22ffb112012-04-10 21:04:02 -070088 // 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();
Mathias Agopian8aedd472012-01-24 16:39:14 -080093
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094 // called when receiving a hotplug event
Mathias Agopian148994e2012-09-19 17:31:36 -070095 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -070096
Mathias Agopianf6bbd442012-08-21 15:47:28 -070097 Vector< sp<EventThread::Connection> > waitForEvent(
98 DisplayEventReceiver::Event* event);
99
Mathias Agopian74d211a2013-04-22 16:55:35 +0200100 void dump(String8& result) const;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700101 void sendVsyncHintOff();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800102
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700103 void setPhaseOffset(nsecs_t phaseOffset);
104
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800105private:
106 virtual bool threadLoop();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800107 virtual void onFirstRef();
108
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700109 virtual void onVSyncEvent(nsecs_t timestamp);
110
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700111 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700112 void enableVSyncLocked();
113 void disableVSyncLocked();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700114 void sendVsyncHintOnLocked();
Mathias Agopian23748662011-12-05 14:33:34 -0800115
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800116 // constants
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700117 sp<VSyncSource> mVSyncSource;
Mathias Agopian86303202012-07-24 22:46:10 -0700118 PowerHAL mPowerHAL;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800119
120 mutable Mutex mLock;
121 mutable Condition mCondition;
122
123 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700124 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian148994e2012-09-19 17:31:36 -0700125 Vector< DisplayEventReceiver::Event > mPendingEvents;
Jesse Hall9e663de2013-08-16 14:28:37 -0700126 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700127 bool mUseSoftwareVSync;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700128 bool mVsyncEnabled;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700129
130 // for debugging
131 bool mDebugVsyncEnabled;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700132
133 bool mVsyncHintSent;
134 timer_t mTimerId;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800135};
136
137// ---------------------------------------------------------------------------
138
139}; // namespace android
140
141// ---------------------------------------------------------------------------
142
143#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */