blob: aa0ea7f3cd5509214aecce92068726f44230716f [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 Agopian86303202012-07-24 22:46:10 -070030#include "DisplayHardware/PowerHAL.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 Agopian86303202012-07-24 22:46:10 -070041class EventThread : public Thread {
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
Mathias Agopiancb9732a2012-04-03 17:48:03 -070050 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 Agopiand0566bc2011-11-17 17:49:17 -080061
62public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070063
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064 EventThread(const sp<SurfaceFlinger>& flinger);
65
Mathias Agopiancb9732a2012-04-03 17:48:03 -070066 sp<Connection> createEventConnection() const;
67 status_t registerDisplayEventConnection(const sp<Connection>& connection);
68 status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080069
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
71 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080072
Mathias Agopian22ffb112012-04-10 21:04:02 -070073 // 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 Agopian8aedd472012-01-24 16:39:14 -080078
Mathias Agopian86303202012-07-24 22:46:10 -070079 // called when receiving a vsync event
80 void onVSyncReceived(int display, nsecs_t timestamp);
81
Mathias Agopiand0566bc2011-11-17 17:49:17 -080082 void dump(String8& result, char* buffer, size_t SIZE) const;
83
84private:
85 virtual bool threadLoop();
86 virtual status_t readyToRun();
87 virtual void onFirstRef();
88
Mathias Agopiancb9732a2012-04-03 17:48:03 -070089 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -070090 void enableVSyncLocked();
91 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -080092
Mathias Agopiand0566bc2011-11-17 17:49:17 -080093 // constants
Mathias Agopian86303202012-07-24 22:46:10 -070094 sp<SurfaceFlinger> mFlinger;
95 PowerHAL mPowerHAL;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080096
97 mutable Mutex mLock;
98 mutable Condition mCondition;
99
100 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700101 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700102 nsecs_t mVSyncTimestamp;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700103 bool mUseSoftwareVSync;
Mathias Agopian10125f02012-08-17 15:06:02 -0700104 size_t mVSyncCount;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700105
106 // for debugging
107 bool mDebugVsyncEnabled;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800108};
109
110// ---------------------------------------------------------------------------
111
112}; // namespace android
113
114// ---------------------------------------------------------------------------
115
116#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */