blob: 9c13ed27556fdd8e9b7a1b64079c93cb9a8f1b80 [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>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070032#include <utils/SortedVector.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033
Jesse Hall9e663de2013-08-16 14:28:37 -070034#include "DisplayDevice.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070035
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038// ---------------------------------------------------------------------------
39
Lloyd Pique24b0a482018-03-09 18:52:26 -080040class EventThreadTest;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070042class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
44// ---------------------------------------------------------------------------
45
Lloyd Piquee83f9312018-02-01 12:53:17 -080046class VSyncSource {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070047public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080048 class Callback {
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;
Lloyd Piquee83f9312018-02-01 12:53:17 -080056 virtual void setCallback(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 Pique0fcde1b2017-12-20 16:50:21 -080060class EventThread {
61public:
62 virtual ~EventThread();
63
64 virtual sp<BnDisplayEventConnection> createEventConnection() const = 0;
65
66 // called before the screen is turned off from main thread
67 virtual void onScreenReleased() = 0;
68
69 // called after the screen is turned on from main thread
70 virtual void onScreenAcquired() = 0;
71
72 // called when receiving a hotplug event
73 virtual void onHotplugReceived(int type, bool connected) = 0;
74
75 virtual void dump(String8& result) const = 0;
76
77 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
78};
79
80namespace impl {
81
82class EventThread : public android::EventThread, private VSyncSource::Callback {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070083 class Connection : public BnDisplayEventConnection {
84 public:
Lloyd Piquee83f9312018-02-01 12:53:17 -080085 explicit Connection(EventThread* eventThread);
Lloyd Pique24b0a482018-03-09 18:52:26 -080086 virtual ~Connection();
87
88 virtual status_t postEvent(const DisplayEventReceiver::Event& event);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070089
90 // count >= 1 : continuous event. count is the vsync rate
91 // count == 0 : one-shot event that has not fired
92 // count ==-1 : one-shot event that fired this round / disabled
Mathias Agopiancb9732a2012-04-03 17:48:03 -070093 int32_t count;
94
95 private:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070096 virtual void onFirstRef();
Dan Stoza6b698e42017-04-03 13:09:08 -070097 status_t stealReceiveChannel(gui::BitTube* outChannel) override;
Dan Stozae1c599b2017-03-30 16:37:19 -070098 status_t setVsyncRate(uint32_t count) override;
Lloyd Pique78ce4182018-01-31 16:39:51 -080099 void requestNextVsync() override; // asynchronous
Lloyd Piquee83f9312018-02-01 12:53:17 -0800100 EventThread* const mEventThread;
Dan Stoza6b698e42017-04-03 13:09:08 -0700101 gui::BitTube mChannel;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700102 };
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800103
104public:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800105 using ResyncWithRateLimitCallback = std::function<void()>;
106 using InterceptVSyncsCallback = std::function<void(nsecs_t)>;
107
108 EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback,
109 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800110 ~EventThread();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800111
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800112 sp<BnDisplayEventConnection> createEventConnection() const override;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700113 status_t registerDisplayEventConnection(const sp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800114
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700115 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
116 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800117
Mathias Agopian22ffb112012-04-10 21:04:02 -0700118 // called before the screen is turned off from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800119 void onScreenReleased() override;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700120
121 // called after the screen is turned on from main thread
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800122 void onScreenAcquired() override;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800123
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700124 // called when receiving a hotplug event
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800125 void onHotplugReceived(int type, bool connected) override;
Mathias Agopian86303202012-07-24 22:46:10 -0700126
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800127 void dump(String8& result) const override;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800128
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800129 void setPhaseOffset(nsecs_t phaseOffset) override;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700130
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800131private:
Lloyd Pique24b0a482018-03-09 18:52:26 -0800132 friend EventThreadTest;
133
Lloyd Pique46a46b32018-01-31 19:01:18 -0800134 void threadMain();
135 Vector<sp<EventThread::Connection>> waitForEventLocked(std::unique_lock<std::mutex>* lock,
136 DisplayEventReceiver::Event* event)
137 REQUIRES(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700138
Lloyd Pique9cbe4da2018-02-13 18:51:55 -0800139 void removeDisplayEventConnectionLocked(const wp<Connection>& connection) REQUIRES(mMutex);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800140 void enableVSyncLocked() REQUIRES(mMutex);
141 void disableVSyncLocked() REQUIRES(mMutex);
142
143 // Implements VSyncSource::Callback
144 void onVSyncEvent(nsecs_t timestamp) override;
Mathias Agopian23748662011-12-05 14:33:34 -0800145
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800146 // constants
Lloyd Pique24b0a482018-03-09 18:52:26 -0800147 VSyncSource* const mVSyncSource GUARDED_BY(mMutex) = nullptr;
148 const ResyncWithRateLimitCallback mResyncWithRateLimitCallback;
149 const InterceptVSyncsCallback mInterceptVSyncsCallback;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800150
Lloyd Pique46a46b32018-01-31 19:01:18 -0800151 std::thread mThread;
152 mutable std::mutex mMutex;
153 mutable std::condition_variable mCondition;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800154
155 // protected by mLock
Lloyd Pique46a46b32018-01-31 19:01:18 -0800156 SortedVector<wp<Connection>> mDisplayEventConnections GUARDED_BY(mMutex);
157 Vector<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex);
158 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES] GUARDED_BY(
159 mMutex);
160 bool mUseSoftwareVSync GUARDED_BY(mMutex) = false;
161 bool mVsyncEnabled GUARDED_BY(mMutex) = false;
162 bool mKeepRunning GUARDED_BY(mMutex) = true;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700163
164 // for debugging
Lloyd Pique46a46b32018-01-31 19:01:18 -0800165 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800166};
167
168// ---------------------------------------------------------------------------
169
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800170} // namespace impl
171} // namespace android