blob: 2f10cdb167b04668c608b55b709962fd2c540130 [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 Agopian86303202012-07-24 22:46:10 -070031#include "DisplayHardware/PowerHAL.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070032
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035// ---------------------------------------------------------------------------
36
37class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039
40// ---------------------------------------------------------------------------
41
Mathias Agopian86303202012-07-24 22:46:10 -070042class EventThread : public Thread {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070043 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 Agopiand0566bc2011-11-17 17:49:17 -080063
64public:
Mathias Agopiancb9732a2012-04-03 17:48:03 -070065
Mathias Agopiand0566bc2011-11-17 17:49:17 -080066 EventThread(const sp<SurfaceFlinger>& flinger);
67
Mathias Agopiancb9732a2012-04-03 17:48:03 -070068 sp<Connection> createEventConnection() const;
69 status_t registerDisplayEventConnection(const sp<Connection>& connection);
70 status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -080071
Mathias Agopiancb9732a2012-04-03 17:48:03 -070072 void setVsyncRate(uint32_t count, const sp<Connection>& connection);
73 void requestNextVsync(const sp<Connection>& connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080074
Mathias Agopian22ffb112012-04-10 21:04:02 -070075 // called before the screen is turned off from main thread
76 void onScreenReleased();
77
78 // called after the screen is turned on from main thread
79 void onScreenAcquired();
Mathias Agopian8aedd472012-01-24 16:39:14 -080080
Mathias Agopian86303202012-07-24 22:46:10 -070081 // called when receiving a vsync event
82 void onVSyncReceived(int display, nsecs_t timestamp);
83
Mathias Agopiand0566bc2011-11-17 17:49:17 -080084 void dump(String8& result, char* buffer, size_t SIZE) const;
85
86private:
87 virtual bool threadLoop();
88 virtual status_t readyToRun();
89 virtual void onFirstRef();
90
Mathias Agopiancb9732a2012-04-03 17:48:03 -070091 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -070092 void enableVSyncLocked();
93 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -080094
Mathias Agopiand0566bc2011-11-17 17:49:17 -080095 // constants
Mathias Agopian86303202012-07-24 22:46:10 -070096 sp<SurfaceFlinger> mFlinger;
97 PowerHAL mPowerHAL;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080098
99 mutable Mutex mLock;
100 mutable Condition mCondition;
101
102 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700103 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800104 nsecs_t mLastVSyncTimestamp;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700105 nsecs_t mVSyncTimestamp;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700106 bool mUseSoftwareVSync;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800107
108 // main thread only
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800109 size_t mDeliveredEvents;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700110
111 // for debugging
112 bool mDebugVsyncEnabled;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800113};
114
115// ---------------------------------------------------------------------------
116
117}; // namespace android
118
119// ---------------------------------------------------------------------------
120
121#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */