blob: 0823839ee7649fe7c09396fa3ef881f71f4fb215 [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 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:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070060 explicit Connection(const sp<EventThread>& eventThread);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070061 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();
Dan Stoza6b698e42017-04-03 13:09:08 -070071 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dan Stozae1c599b2017-03-30 16:37:19 -070072 status_t setVsyncRate(uint32_t count) override;
73 void requestNextVsync() override; // asynchronous
Mathias Agopiancb9732a2012-04-03 17:48:03 -070074 sp<EventThread> const mEventThread;
Dan Stoza6b698e42017-04-03 13:09:08 -070075 gui::BitTube mChannel;
Mathias Agopiancb9732a2012-04-03 17:48:03 -070076 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080077
78public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070079
Irvelab046852016-07-28 11:23:08 -070080 EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger, bool interceptVSyncs);
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;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800101
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700102 void setPhaseOffset(nsecs_t phaseOffset);
103
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800104private:
105 virtual bool threadLoop();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800106 virtual void onFirstRef();
107
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700108 virtual void onVSyncEvent(nsecs_t timestamp);
109
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700110 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700111 void enableVSyncLocked();
112 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -0800113
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800114 // constants
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700115 sp<VSyncSource> mVSyncSource;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000116 SurfaceFlinger& mFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117
118 mutable Mutex mLock;
119 mutable Condition mCondition;
120
121 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700122 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian148994e2012-09-19 17:31:36 -0700123 Vector< DisplayEventReceiver::Event > mPendingEvents;
Jesse Hall9e663de2013-08-16 14:28:37 -0700124 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700125 bool mUseSoftwareVSync;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700126 bool mVsyncEnabled;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700127
128 // for debugging
129 bool mDebugVsyncEnabled;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700130
Irvelab046852016-07-28 11:23:08 -0700131 const bool mInterceptVSyncs;
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 */