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