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 | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 30 | #include "DisplayHardware/PowerHAL.h" |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 32 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 33 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | class SurfaceFlinger; |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 37 | class String8; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 38 | |
| 39 | // --------------------------------------------------------------------------- |
| 40 | |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 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 |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 50 | int32_t count; |
| 51 | |
| 52 | private: |
| 53 | virtual ~Connection(); |
| 54 | virtual void onFirstRef(); |
| 55 | virtual sp<BitTube> getDataChannel() const; |
| 56 | virtual void setVsyncRate(uint32_t count); |
| 57 | virtual void requestNextVsync(); // asynchronous |
| 58 | sp<EventThread> const mEventThread; |
| 59 | sp<BitTube> const mChannel; |
| 60 | }; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 61 | |
| 62 | public: |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 63 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 64 | EventThread(const sp<SurfaceFlinger>& flinger); |
| 65 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 66 | sp<Connection> createEventConnection() const; |
| 67 | status_t registerDisplayEventConnection(const sp<Connection>& connection); |
| 68 | status_t unregisterDisplayEventConnection(const wp<Connection>& connection); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 69 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 70 | void setVsyncRate(uint32_t count, const sp<Connection>& connection); |
| 71 | void requestNextVsync(const sp<Connection>& connection); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 72 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 73 | // called before the screen is turned off from main thread |
| 74 | void onScreenReleased(); |
| 75 | |
| 76 | // called after the screen is turned on from main thread |
| 77 | void onScreenAcquired(); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 78 | |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 79 | // called when receiving a vsync event |
| 80 | void onVSyncReceived(int display, nsecs_t timestamp); |
| 81 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 82 | void dump(String8& result, char* buffer, size_t SIZE) const; |
| 83 | |
| 84 | private: |
| 85 | virtual bool threadLoop(); |
| 86 | virtual status_t readyToRun(); |
| 87 | virtual void onFirstRef(); |
| 88 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 89 | void removeDisplayEventConnection(const wp<Connection>& connection); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 90 | void enableVSyncLocked(); |
| 91 | void disableVSyncLocked(); |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 92 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 93 | // constants |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 94 | sp<SurfaceFlinger> mFlinger; |
| 95 | PowerHAL mPowerHAL; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 96 | |
| 97 | mutable Mutex mLock; |
| 98 | mutable Condition mCondition; |
| 99 | |
| 100 | // protected by mLock |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 101 | SortedVector< wp<Connection> > mDisplayEventConnections; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 102 | nsecs_t mVSyncTimestamp; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 103 | bool mUseSoftwareVSync; |
Mathias Agopian | 10125f0 | 2012-08-17 15:06:02 -0700 | [diff] [blame] | 104 | size_t mVSyncCount; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 105 | |
| 106 | // for debugging |
| 107 | bool mDebugVsyncEnabled; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | // --------------------------------------------------------------------------- |
| 111 | |
| 112 | }; // namespace android |
| 113 | |
| 114 | // --------------------------------------------------------------------------- |
| 115 | |
| 116 | #endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */ |