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 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 30 | #include "DisplayHardware/DisplayHardware.h" |
| 31 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 32 | // --------------------------------------------------------------------------- |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
| 38 | class SurfaceFlinger; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 39 | |
| 40 | // --------------------------------------------------------------------------- |
| 41 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 42 | class EventThread : public Thread, public DisplayHardware::VSyncHandler { |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 43 | class Connection : public BnDisplayEventConnection { |
| 44 | public: |
| 45 | Connection(const sp<EventThread>& eventThread); |
| 46 | status_t postEvent(const DisplayEventReceiver::Event& event); |
| 47 | |
| 48 | // count >= 1 : continuous event. count is the vsync rate |
| 49 | // count == 0 : one-shot event that has not fired |
| 50 | // count ==-1 : one-shot event that fired this round / disabled |
| 51 | // count ==-2 : one-shot event that fired the round before |
| 52 | int32_t count; |
| 53 | |
| 54 | private: |
| 55 | virtual ~Connection(); |
| 56 | virtual void onFirstRef(); |
| 57 | virtual sp<BitTube> getDataChannel() const; |
| 58 | virtual void setVsyncRate(uint32_t count); |
| 59 | virtual void requestNextVsync(); // asynchronous |
| 60 | sp<EventThread> const mEventThread; |
| 61 | sp<BitTube> const mChannel; |
| 62 | }; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 63 | |
| 64 | public: |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 65 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 66 | EventThread(const sp<SurfaceFlinger>& flinger); |
| 67 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 68 | sp<Connection> createEventConnection() const; |
| 69 | status_t registerDisplayEventConnection(const sp<Connection>& connection); |
| 70 | status_t unregisterDisplayEventConnection(const wp<Connection>& connection); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 71 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 72 | void setVsyncRate(uint32_t count, const sp<Connection>& connection); |
| 73 | void requestNextVsync(const sp<Connection>& connection); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 74 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 75 | nsecs_t getLastVSyncTimestamp() const; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 76 | nsecs_t getVSyncPeriod() const; |
| 77 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 78 | void dump(String8& result, char* buffer, size_t SIZE) const; |
| 79 | |
| 80 | private: |
| 81 | virtual bool threadLoop(); |
| 82 | virtual status_t readyToRun(); |
| 83 | virtual void onFirstRef(); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame^] | 84 | virtual void onVSyncReceived(int, nsecs_t timestamp); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 85 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 86 | void removeDisplayEventConnection(const wp<Connection>& connection); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame^] | 87 | void enableVSync(); |
| 88 | void disableVSync(); |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 89 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 90 | // constants |
| 91 | sp<SurfaceFlinger> mFlinger; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 92 | DisplayHardware& mHw; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 93 | |
| 94 | mutable Mutex mLock; |
| 95 | mutable Condition mCondition; |
| 96 | |
| 97 | // protected by mLock |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 98 | SortedVector< wp<Connection> > mDisplayEventConnections; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 99 | nsecs_t mLastVSyncTimestamp; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 100 | nsecs_t mVSyncTimestamp; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 101 | |
| 102 | // main thread only |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 103 | size_t mDeliveredEvents; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame^] | 104 | |
| 105 | // for debugging |
| 106 | bool mDebugVsyncEnabled; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // --------------------------------------------------------------------------- |
| 110 | |
| 111 | }; // namespace android |
| 112 | |
| 113 | // --------------------------------------------------------------------------- |
| 114 | |
| 115 | #endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */ |