| 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 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 17 | #pragma once | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 18 |  | 
 | 19 | #include <stdint.h> | 
 | 20 | #include <sys/types.h> | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 21 | #include <condition_variable> | 
 | 22 | #include <mutex> | 
 | 23 | #include <thread> | 
 | 24 |  | 
 | 25 | #include <android-base/thread_annotations.h> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 26 |  | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 27 | #include <gui/DisplayEventReceiver.h> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 28 | #include <gui/IDisplayEventConnection.h> | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 29 | #include <private/gui/BitTube.h> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 |  | 
 | 31 | #include <utils/Errors.h> | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 32 | #include <utils/SortedVector.h> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 33 |  | 
| Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 34 | #include "DisplayDevice.h" | 
| Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 35 |  | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 36 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 37 | namespace android { | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- | 
 | 39 |  | 
 | 40 | class SurfaceFlinger; | 
| Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 41 | class String8; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 42 |  | 
 | 43 | // --------------------------------------------------------------------------- | 
 | 44 |  | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 45 | class VSyncSource { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 46 | public: | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 47 |     class Callback { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 48 |     public: | 
 | 49 |         virtual ~Callback() {} | 
 | 50 |         virtual void onVSyncEvent(nsecs_t when) = 0; | 
 | 51 |     }; | 
 | 52 |  | 
 | 53 |     virtual ~VSyncSource() {} | 
 | 54 |     virtual void setVSyncEnabled(bool enable) = 0; | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 55 |     virtual void setCallback(Callback* callback) = 0; | 
| Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 56 |     virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 57 | }; | 
 | 58 |  | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 59 | class EventThread : private VSyncSource::Callback { | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 60 |     class Connection : public BnDisplayEventConnection { | 
 | 61 |     public: | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 62 |         explicit Connection(EventThread* eventThread); | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 63 |         status_t postEvent(const DisplayEventReceiver::Event& event); | 
 | 64 |  | 
 | 65 |         // count >= 1 : continuous event. count is the vsync rate | 
 | 66 |         // count == 0 : one-shot event that has not fired | 
 | 67 |         // count ==-1 : one-shot event that fired this round / disabled | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 68 |         int32_t count; | 
 | 69 |  | 
 | 70 |     private: | 
 | 71 |         virtual ~Connection(); | 
 | 72 |         virtual void onFirstRef(); | 
| Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 73 |         status_t stealReceiveChannel(gui::BitTube* outChannel) override; | 
| Dan Stoza | e1c599b | 2017-03-30 16:37:19 -0700 | [diff] [blame] | 74 |         status_t setVsyncRate(uint32_t count) override; | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 75 |         void requestNextVsync() override; // asynchronous | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 76 |         EventThread* const mEventThread; | 
| Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 77 |         gui::BitTube mChannel; | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 78 |     }; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 79 |  | 
 | 80 | public: | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 81 |     EventThread(VSyncSource* src, SurfaceFlinger& flinger, bool interceptVSyncs, | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 82 |                 const char* threadName); | 
 | 83 |     ~EventThread(); | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 84 |  | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 85 |     sp<Connection> createEventConnection() const; | 
 | 86 |     status_t registerDisplayEventConnection(const sp<Connection>& connection); | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 87 |  | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 88 |     void setVsyncRate(uint32_t count, const sp<Connection>& connection); | 
 | 89 |     void requestNextVsync(const sp<Connection>& connection); | 
| Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 90 |  | 
| Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 91 |     // called before the screen is turned off from main thread | 
 | 92 |     void onScreenReleased(); | 
 | 93 |  | 
 | 94 |     // called after the screen is turned on from main thread | 
 | 95 |     void onScreenAcquired(); | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 96 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 97 |     // called when receiving a hotplug event | 
| Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 98 |     void onHotplugReceived(int type, bool connected); | 
| Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 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: | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 105 |     void threadMain(); | 
 | 106 |     Vector<sp<EventThread::Connection>> waitForEventLocked(std::unique_lock<std::mutex>* lock, | 
 | 107 |                                                            DisplayEventReceiver::Event* event) | 
 | 108 |             REQUIRES(mMutex); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 109 |  | 
| Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 110 |     void removeDisplayEventConnection(const wp<Connection>& connection); | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 111 |     void enableVSyncLocked() REQUIRES(mMutex); | 
 | 112 |     void disableVSyncLocked() REQUIRES(mMutex); | 
 | 113 |  | 
 | 114 |     // Implements VSyncSource::Callback | 
 | 115 |     void onVSyncEvent(nsecs_t timestamp) override; | 
| Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 116 |  | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 117 |     // constants | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 118 |     VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 119 |     SurfaceFlinger& mFlinger; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 120 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 121 |     std::thread mThread; | 
 | 122 |     mutable std::mutex mMutex; | 
 | 123 |     mutable std::condition_variable mCondition; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 124 |  | 
 | 125 |     // protected by mLock | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 126 |     SortedVector<wp<Connection>> mDisplayEventConnections GUARDED_BY(mMutex); | 
 | 127 |     Vector<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); | 
 | 128 |     DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES] GUARDED_BY( | 
 | 129 |             mMutex); | 
 | 130 |     bool mUseSoftwareVSync GUARDED_BY(mMutex) = false; | 
 | 131 |     bool mVsyncEnabled GUARDED_BY(mMutex) = false; | 
 | 132 |     bool mKeepRunning GUARDED_BY(mMutex) = true; | 
| Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 133 |  | 
 | 134 |     // for debugging | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 135 |     bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false; | 
| Ruchi Kandoi | ef472ec | 2014-04-02 12:50:06 -0700 | [diff] [blame] | 136 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 137 |     const bool mInterceptVSyncs = false; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 138 | }; | 
 | 139 |  | 
 | 140 | // --------------------------------------------------------------------------- | 
 | 141 |  | 
 | 142 | }; // namespace android |