Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 23 | #include <gui/DisplayEventReceiver.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 24 | #include <gui/IDisplayEventConnection.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 25 | #include <private/gui/BitTube.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 26 | |
| 27 | #include <utils/Errors.h> |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 28 | #include <utils/SortedVector.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 29 | #include <utils/threads.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | |
Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 31 | #include "DisplayDevice.h" |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 33 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 34 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | class SurfaceFlinger; |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 38 | class String8; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 39 | |
| 40 | // --------------------------------------------------------------------------- |
| 41 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 42 | class VSyncSource : public virtual RefBase { |
| 43 | public: |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 44 | class Callback : public virtual RefBase { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 45 | 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 Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 53 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class EventThread : public Thread, private VSyncSource::Callback { |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 57 | class Connection : public BnDisplayEventConnection { |
| 58 | public: |
Chih-Hung Hsieh | 342b760 | 2016-09-01 11:34:16 -0700 | [diff] [blame] | 59 | explicit Connection(const sp<EventThread>& eventThread); |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 60 | 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 Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 65 | int32_t count; |
| 66 | |
| 67 | private: |
| 68 | virtual ~Connection(); |
| 69 | virtual void onFirstRef(); |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 70 | status_t stealReceiveChannel(gui::BitTube* outChannel) override; |
Dan Stoza | e1c599b | 2017-03-30 16:37:19 -0700 | [diff] [blame] | 71 | status_t setVsyncRate(uint32_t count) override; |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 72 | void requestNextVsync() override; // asynchronous |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 73 | sp<EventThread> const mEventThread; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 74 | gui::BitTube mChannel; |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 75 | }; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 76 | |
| 77 | public: |
Irvel | ab04685 | 2016-07-28 11:23:08 -0700 | [diff] [blame] | 78 | EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger, bool interceptVSyncs); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 79 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 80 | sp<Connection> createEventConnection() const; |
| 81 | status_t registerDisplayEventConnection(const sp<Connection>& connection); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 82 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 83 | void setVsyncRate(uint32_t count, const sp<Connection>& connection); |
| 84 | void requestNextVsync(const sp<Connection>& connection); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 85 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 86 | // 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 Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 91 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 92 | // called when receiving a hotplug event |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 93 | void onHotplugReceived(int type, bool connected); |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 94 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 95 | Vector<sp<EventThread::Connection> > waitForEvent(DisplayEventReceiver::Event* event); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 96 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 97 | void dump(String8& result) const; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 98 | |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 99 | void setPhaseOffset(nsecs_t phaseOffset); |
| 100 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 101 | private: |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 102 | virtual bool threadLoop(); |
| 103 | virtual void onFirstRef(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 104 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 105 | virtual void onVSyncEvent(nsecs_t timestamp); |
| 106 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 107 | void removeDisplayEventConnection(const wp<Connection>& connection); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 108 | void enableVSyncLocked(); |
| 109 | void disableVSyncLocked(); |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 110 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 111 | // constants |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 112 | sp<VSyncSource> mVSyncSource; |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 113 | SurfaceFlinger& mFlinger; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 114 | |
| 115 | mutable Mutex mLock; |
| 116 | mutable Condition mCondition; |
| 117 | |
| 118 | // protected by mLock |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 119 | SortedVector<wp<Connection> > mDisplayEventConnections; |
| 120 | Vector<DisplayEventReceiver::Event> mPendingEvents; |
Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 121 | DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES]; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 122 | bool mUseSoftwareVSync; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 123 | bool mVsyncEnabled; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 124 | |
| 125 | // for debugging |
| 126 | bool mDebugVsyncEnabled; |
Ruchi Kandoi | ef472ec | 2014-04-02 12:50:06 -0700 | [diff] [blame] | 127 | |
Irvel | ab04685 | 2016-07-28 11:23:08 -0700 | [diff] [blame] | 128 | const bool mInterceptVSyncs; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // --------------------------------------------------------------------------- |
| 132 | |
| 133 | }; // namespace android |
| 134 | |
| 135 | // --------------------------------------------------------------------------- |
| 136 | |
| 137 | #endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */ |