blob: 6a59fbbf4270b142a245a259225792af5b6143b4 [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
Dan Stoza6b698e42017-04-03 13:09:08 -070023#include <private/gui/BitTube.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070024#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <gui/IDisplayEventConnection.h>
26
27#include <utils/Errors.h>
28#include <utils/threads.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070029#include <utils/SortedVector.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030
Jesse Hall9e663de2013-08-16 14:28:37 -070031#include "DisplayDevice.h"
Mathias Agopian86303202012-07-24 22:46:10 -070032#include "DisplayHardware/PowerHAL.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070033
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036// ---------------------------------------------------------------------------
37
38class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040
41// ---------------------------------------------------------------------------
42
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070043
44class VSyncSource : public virtual RefBase {
45public:
46 class Callback: public virtual RefBase {
47 public:
48 virtual ~Callback() {}
49 virtual void onVSyncEvent(nsecs_t when) = 0;
50 };
51
52 virtual ~VSyncSource() {}
53 virtual void setVSyncEnabled(bool enable) = 0;
54 virtual void setCallback(const sp<Callback>& callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070055 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070056};
57
58class EventThread : public Thread, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070059 class Connection : public BnDisplayEventConnection {
60 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070061 explicit Connection(const sp<EventThread>& eventThread);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070062 status_t postEvent(const DisplayEventReceiver::Event& event);
63
64 // count >= 1 : continuous event. count is the vsync rate
65 // count == 0 : one-shot event that has not fired
66 // count ==-1 : one-shot event that fired this round / disabled
Mathias Agopiancb9732a2012-04-03 17:48:03 -070067 int32_t count;
68
69 private:
70 virtual ~Connection();
71 virtual void onFirstRef();
Dan Stoza6b698e42017-04-03 13:09:08 -070072 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dan Stozae1c599b2017-03-30 16:37:19 -070073 status_t setVsyncRate(uint32_t count) override;
74 void requestNextVsync() override; // asynchronous
Mathias Agopiancb9732a2012-04-03 17:48:03 -070075 sp<EventThread> const mEventThread;
Dan Stoza6b698e42017-04-03 13:09:08 -070076 gui::BitTube mChannel;
Mathias Agopiancb9732a2012-04-03 17:48:03 -070077 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080078
79public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070080
Irvelab046852016-07-28 11:23:08 -070081 EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger, bool interceptVSyncs);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080082
Mathias Agopiancb9732a2012-04-03 17:48:03 -070083 sp<Connection> createEventConnection() const;
84 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080085
Mathias Agopiancb9732a2012-04-03 17:48:03 -070086 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
87 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080088
Mathias Agopian22ffb112012-04-10 21:04:02 -070089 // called before the screen is turned off from main thread
90 void onScreenReleased();
91
92 // called after the screen is turned on from main thread
93 void onScreenAcquired();
Mathias Agopian8aedd472012-01-24 16:39:14 -080094
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070095 // called when receiving a hotplug event
Mathias Agopian148994e2012-09-19 17:31:36 -070096 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -070097
Mathias Agopianf6bbd442012-08-21 15:47:28 -070098 Vector< sp<EventThread::Connection> > waitForEvent(
99 DisplayEventReceiver::Event* event);
100
Mathias Agopian74d211a2013-04-22 16:55:35 +0200101 void dump(String8& result) const;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700102 void sendVsyncHintOff();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800103
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700104 void setPhaseOffset(nsecs_t phaseOffset);
105
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800106private:
107 virtual bool threadLoop();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800108 virtual void onFirstRef();
109
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700110 virtual void onVSyncEvent(nsecs_t timestamp);
111
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700112 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700113 void enableVSyncLocked();
114 void disableVSyncLocked();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700115 void sendVsyncHintOnLocked();
Mathias Agopian23748662011-12-05 14:33:34 -0800116
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117 // constants
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700118 sp<VSyncSource> mVSyncSource;
Mathias Agopian86303202012-07-24 22:46:10 -0700119 PowerHAL mPowerHAL;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000120 SurfaceFlinger& mFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800121
122 mutable Mutex mLock;
123 mutable Condition mCondition;
124
125 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700126 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian148994e2012-09-19 17:31:36 -0700127 Vector< DisplayEventReceiver::Event > mPendingEvents;
Jesse Hall9e663de2013-08-16 14:28:37 -0700128 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700129 bool mUseSoftwareVSync;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700130 bool mVsyncEnabled;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700131
132 // for debugging
133 bool mDebugVsyncEnabled;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700134
135 bool mVsyncHintSent;
Irvelab046852016-07-28 11:23:08 -0700136 const bool mInterceptVSyncs;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700137 timer_t mTimerId;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800138};
139
140// ---------------------------------------------------------------------------
141
142}; // namespace android
143
144// ---------------------------------------------------------------------------
145
146#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */