blob: 92c92ded8f34e4c5b8feee7ca2d5c3d13d055275 [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
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 Agopiancb9732a2012-04-03 17:48:03 -070023#include <gui/DisplayEventReceiver.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080024#include <gui/IDisplayEventConnection.h>
25
26#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070028#include <utils/SortedVector.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029
Mathias Agopian1b031492012-06-20 17:51:20 -070030#include "DisplayHardware.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070031
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034// ---------------------------------------------------------------------------
35
36class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038
39// ---------------------------------------------------------------------------
40
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070041class EventThread : public Thread, public DisplayHardware::VSyncHandler {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070042 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 Agopiand0566bc2011-11-17 17:49:17 -080062
63public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065 EventThread(const sp<SurfaceFlinger>& flinger);
66
Mathias Agopiancb9732a2012-04-03 17:48:03 -070067 sp<Connection> createEventConnection() const;
68 status_t registerDisplayEventConnection(const sp<Connection>& connection);
69 status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080070
Mathias Agopiancb9732a2012-04-03 17:48:03 -070071 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
72 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080073
Mathias Agopian22ffb112012-04-10 21:04:02 -070074 // called before the screen is turned off from main thread
75 void onScreenReleased();
76
77 // called after the screen is turned on from main thread
78 void onScreenAcquired();
Mathias Agopian8aedd472012-01-24 16:39:14 -080079
Mathias Agopiand0566bc2011-11-17 17:49:17 -080080 void dump(String8& result, char* buffer, size_t SIZE) const;
81
82private:
83 virtual bool threadLoop();
84 virtual status_t readyToRun();
85 virtual void onFirstRef();
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070086 virtual void onVSyncReceived(int, nsecs_t timestamp);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080087
Mathias Agopiancb9732a2012-04-03 17:48:03 -070088 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -070089 void enableVSyncLocked();
90 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -080091
Mathias Agopiand0566bc2011-11-17 17:49:17 -080092 // constants
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070093 DisplayHardware& mHw;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080094
95 mutable Mutex mLock;
96 mutable Condition mCondition;
97
98 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -070099 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800100 nsecs_t mLastVSyncTimestamp;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700101 nsecs_t mVSyncTimestamp;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700102 bool mUseSoftwareVSync;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800103
104 // main thread only
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800105 size_t mDeliveredEvents;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700106
107 // for debugging
108 bool mDebugVsyncEnabled;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800109};
110
111// ---------------------------------------------------------------------------
112
113}; // namespace android
114
115// ---------------------------------------------------------------------------
116
117#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */