blob: b4d718cd1716338e9ffbc58f07ce603140d67bd3 [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>
Lloyd Pique78ce4182018-01-31 16:39:51 -080025#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080026
27#include <utils/Errors.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070028#include <utils/SortedVector.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080029#include <utils/threads.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030
Jesse Hall9e663de2013-08-16 14:28:37 -070031#include "DisplayDevice.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 -070042class VSyncSource : public virtual RefBase {
43public:
Lloyd Pique78ce4182018-01-31 16:39:51 -080044 class Callback : public virtual RefBase {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070045 public:
46 virtual ~Callback() {}
47 virtual void onVSyncEvent(nsecs_t when) = 0;
48 };
49
50 virtual ~VSyncSource() {}
51 virtual void setVSyncEnabled(bool enable) = 0;
52 virtual void setCallback(const sp<Callback>& callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070053 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070054};
55
56class EventThread : public Thread, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070057 class Connection : public BnDisplayEventConnection {
58 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070059 explicit Connection(const sp<EventThread>& eventThread);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070060 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();
Dan Stoza6b698e42017-04-03 13:09:08 -070070 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dan Stozae1c599b2017-03-30 16:37:19 -070071 status_t setVsyncRate(uint32_t count) override;
Lloyd Pique78ce4182018-01-31 16:39:51 -080072 void requestNextVsync() override; // asynchronous
Mathias Agopiancb9732a2012-04-03 17:48:03 -070073 sp<EventThread> const mEventThread;
Dan Stoza6b698e42017-04-03 13:09:08 -070074 gui::BitTube mChannel;
Mathias Agopiancb9732a2012-04-03 17:48:03 -070075 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080076
77public:
Irvelab046852016-07-28 11:23:08 -070078 EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger, bool interceptVSyncs);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080079
Mathias Agopiancb9732a2012-04-03 17:48:03 -070080 sp<Connection> createEventConnection() const;
81 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080082
Mathias Agopiancb9732a2012-04-03 17:48:03 -070083 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
84 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080085
Mathias Agopian22ffb112012-04-10 21:04:02 -070086 // called before the screen is turned off from main thread
87 void onScreenReleased();
88
89 // called after the screen is turned on from main thread
90 void onScreenAcquired();
Mathias Agopian8aedd472012-01-24 16:39:14 -080091
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092 // called when receiving a hotplug event
Mathias Agopian148994e2012-09-19 17:31:36 -070093 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -070094
Lloyd Pique78ce4182018-01-31 16:39:51 -080095 Vector<sp<EventThread::Connection> > waitForEvent(DisplayEventReceiver::Event* event);
Mathias Agopianf6bbd442012-08-21 15:47:28 -070096
Mathias Agopian74d211a2013-04-22 16:55:35 +020097 void dump(String8& result) const;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080098
Dan Stozadb4ac3c2015-04-14 11:34:01 -070099 void setPhaseOffset(nsecs_t phaseOffset);
100
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800101private:
Lloyd Pique78ce4182018-01-31 16:39:51 -0800102 virtual bool threadLoop();
103 virtual void onFirstRef();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800104
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700105 virtual void onVSyncEvent(nsecs_t timestamp);
106
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700107 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700108 void enableVSyncLocked();
109 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -0800110
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800111 // constants
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700112 sp<VSyncSource> mVSyncSource;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000113 SurfaceFlinger& mFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800114
115 mutable Mutex mLock;
116 mutable Condition mCondition;
117
118 // protected by mLock
Lloyd Pique78ce4182018-01-31 16:39:51 -0800119 SortedVector<wp<Connection> > mDisplayEventConnections;
120 Vector<DisplayEventReceiver::Event> mPendingEvents;
Jesse Hall9e663de2013-08-16 14:28:37 -0700121 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700122 bool mUseSoftwareVSync;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700123 bool mVsyncEnabled;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700124
125 // for debugging
126 bool mDebugVsyncEnabled;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700127
Irvelab046852016-07-28 11:23:08 -0700128 const bool mInterceptVSyncs;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800129};
130
131// ---------------------------------------------------------------------------
132
133}; // namespace android
134
135// ---------------------------------------------------------------------------
136
137#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */