blob: 63cdb542f32746a2abe03576616a20cfb115a7f7 [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
Lloyd Pique46a46b32018-01-31 19:01:18 -080017#pragma once
Mathias Agopiand0566bc2011-11-17 17:49:17 -080018
19#include <stdint.h>
20#include <sys/types.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080021#include <condition_variable>
22#include <mutex>
23#include <thread>
24
25#include <android-base/thread_annotations.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080026
Mathias Agopiancb9732a2012-04-03 17:48:03 -070027#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080028#include <gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080029#include <private/gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030
31#include <utils/Errors.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080032#include <utils/RefBase.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070033#include <utils/SortedVector.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034
Jesse Hall9e663de2013-08-16 14:28:37 -070035#include "DisplayDevice.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070036
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039// ---------------------------------------------------------------------------
40
41class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070042class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
44// ---------------------------------------------------------------------------
45
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070046class VSyncSource : public virtual RefBase {
47public:
Lloyd Pique78ce4182018-01-31 16:39:51 -080048 class Callback : public virtual RefBase {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070049 public:
50 virtual ~Callback() {}
51 virtual void onVSyncEvent(nsecs_t when) = 0;
52 };
53
54 virtual ~VSyncSource() {}
55 virtual void setVSyncEnabled(bool enable) = 0;
56 virtual void setCallback(const sp<Callback>& callback) = 0;
Dan Stozadb4ac3c2015-04-14 11:34:01 -070057 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070058};
59
Lloyd Pique46a46b32018-01-31 19:01:18 -080060class EventThread : public virtual RefBase, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070061 class Connection : public BnDisplayEventConnection {
62 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070063 explicit Connection(const sp<EventThread>& eventThread);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064 status_t postEvent(const DisplayEventReceiver::Event& event);
65
66 // count >= 1 : continuous event. count is the vsync rate
67 // count == 0 : one-shot event that has not fired
68 // count ==-1 : one-shot event that fired this round / disabled
Mathias Agopiancb9732a2012-04-03 17:48:03 -070069 int32_t count;
70
71 private:
72 virtual ~Connection();
73 virtual void onFirstRef();
Dan Stoza6b698e42017-04-03 13:09:08 -070074 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dan Stozae1c599b2017-03-30 16:37:19 -070075 status_t setVsyncRate(uint32_t count) override;
Lloyd Pique78ce4182018-01-31 16:39:51 -080076 void requestNextVsync() override; // asynchronous
Mathias Agopiancb9732a2012-04-03 17:48:03 -070077 sp<EventThread> const mEventThread;
Dan Stoza6b698e42017-04-03 13:09:08 -070078 gui::BitTube mChannel;
Mathias Agopiancb9732a2012-04-03 17:48:03 -070079 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -080080
81public:
Lloyd Pique46a46b32018-01-31 19:01:18 -080082 EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger, bool interceptVSyncs,
83 const char* threadName);
84 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080085
Mathias Agopiancb9732a2012-04-03 17:48:03 -070086 sp<Connection> createEventConnection() const;
87 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080088
Mathias Agopiancb9732a2012-04-03 17:48:03 -070089 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
90 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080091
Mathias Agopian22ffb112012-04-10 21:04:02 -070092 // called before the screen is turned off from main thread
93 void onScreenReleased();
94
95 // called after the screen is turned on from main thread
96 void onScreenAcquired();
Mathias Agopian8aedd472012-01-24 16:39:14 -080097
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070098 // called when receiving a hotplug event
Mathias Agopian148994e2012-09-19 17:31:36 -070099 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -0700100
Mathias Agopian74d211a2013-04-22 16:55:35 +0200101 void dump(String8& result) const;
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:
Lloyd Pique46a46b32018-01-31 19:01:18 -0800106 void threadMain();
107 Vector<sp<EventThread::Connection>> waitForEventLocked(std::unique_lock<std::mutex>* lock,
108 DisplayEventReceiver::Event* event)
109 REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700110
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700111 void removeDisplayEventConnection(const wp<Connection>& connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800112 void enableVSyncLocked() REQUIRES(mMutex);
113 void disableVSyncLocked() REQUIRES(mMutex);
114
115 // Implements VSyncSource::Callback
116 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800117
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800118 // constants
Lloyd Pique46a46b32018-01-31 19:01:18 -0800119 sp<VSyncSource> mVSyncSource GUARDED_BY(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000120 SurfaceFlinger& mFlinger;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800121
Lloyd Pique46a46b32018-01-31 19:01:18 -0800122 std::thread mThread;
123 mutable std::mutex mMutex;
124 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800125
126 // protected by mLock
Lloyd Pique46a46b32018-01-31 19:01:18 -0800127 SortedVector<wp<Connection>> mDisplayEventConnections GUARDED_BY(mMutex);
128 Vector<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
129 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES] GUARDED_BY(
130 mMutex);
131 bool mUseSoftwareVSync GUARDED_BY(mMutex) = false;
132 bool mVsyncEnabled GUARDED_BY(mMutex) = false;
133 bool mKeepRunning GUARDED_BY(mMutex) = true;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700134
135 // for debugging
Lloyd Pique46a46b32018-01-31 19:01:18 -0800136 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700137
Lloyd Pique46a46b32018-01-31 19:01:18 -0800138 const bool mInterceptVSyncs = false;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800139};
140
141// ---------------------------------------------------------------------------
142
143}; // namespace android