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