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> |
| 25 | |
| 26 | #include <utils/Errors.h> |
| 27 | #include <utils/threads.h> |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 28 | #include <utils/SortedVector.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 29 | |
Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 30 | #include "DisplayDevice.h" |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 31 | #include "DisplayHardware/PowerHAL.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; |
| 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: |
| 59 | Connection(const sp<EventThread>& eventThread); |
| 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(); |
| 70 | virtual sp<BitTube> getDataChannel() const; |
| 71 | virtual void setVsyncRate(uint32_t count); |
| 72 | virtual void requestNextVsync(); // asynchronous |
| 73 | sp<EventThread> const mEventThread; |
| 74 | sp<BitTube> const mChannel; |
| 75 | }; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 76 | |
| 77 | public: |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 78 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame^] | 79 | EventThread(const sp<VSyncSource>& src); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 80 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 81 | sp<Connection> createEventConnection() const; |
| 82 | status_t registerDisplayEventConnection(const sp<Connection>& connection); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 83 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 84 | void setVsyncRate(uint32_t count, const sp<Connection>& connection); |
| 85 | void requestNextVsync(const sp<Connection>& connection); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 86 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 87 | // called before the screen is turned off from main thread |
| 88 | void onScreenReleased(); |
| 89 | |
| 90 | // called after the screen is turned on from main thread |
| 91 | void onScreenAcquired(); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 92 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame^] | 93 | // called when receiving a hotplug event |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 94 | void onHotplugReceived(int type, bool connected); |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 95 | |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 96 | Vector< sp<EventThread::Connection> > waitForEvent( |
| 97 | DisplayEventReceiver::Event* event); |
| 98 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 99 | void dump(String8& result) const; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | virtual bool threadLoop(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 103 | virtual void onFirstRef(); |
| 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; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 113 | PowerHAL mPowerHAL; |
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 |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 119 | SortedVector< wp<Connection> > mDisplayEventConnections; |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 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; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // --------------------------------------------------------------------------- |
| 130 | |
| 131 | }; // namespace android |
| 132 | |
| 133 | // --------------------------------------------------------------------------- |
| 134 | |
| 135 | #endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */ |