blob: 1934f98fae2af65070f64ca922d813253ba20512 [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
Mathias Agopianff28e202012-09-20 23:24:19 -070026#include <hardware/hwcomposer_defs.h>
27
Mathias Agopiand0566bc2011-11-17 17:49:17 -080028#include <utils/Errors.h>
29#include <utils/threads.h>
Mathias Agopiancb9732a2012-04-03 17:48:03 -070030#include <utils/SortedVector.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031
Mathias Agopian86303202012-07-24 22:46:10 -070032#include "DisplayHardware/PowerHAL.h"
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070033
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036// ---------------------------------------------------------------------------
37
38class SurfaceFlinger;
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039class String8;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040
41// ---------------------------------------------------------------------------
42
Mathias Agopian86303202012-07-24 22:46:10 -070043class EventThread : public Thread {
Mathias Agopiancb9732a2012-04-03 17:48:03 -070044 class Connection : public BnDisplayEventConnection {
45 public:
46 Connection(const sp<EventThread>& eventThread);
47 status_t postEvent(const DisplayEventReceiver::Event& event);
48
49 // count >= 1 : continuous event. count is the vsync rate
50 // count == 0 : one-shot event that has not fired
51 // count ==-1 : one-shot event that fired this round / disabled
Mathias Agopiancb9732a2012-04-03 17:48:03 -070052 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);
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 Agopian86303202012-07-24 22:46:10 -070080 // called when receiving a vsync event
Mathias Agopian148994e2012-09-19 17:31:36 -070081 void onVSyncReceived(int type, nsecs_t timestamp);
82 void onHotplugReceived(int type, bool connected);
Mathias Agopian86303202012-07-24 22:46:10 -070083
Mathias Agopianf6bbd442012-08-21 15:47:28 -070084 Vector< sp<EventThread::Connection> > waitForEvent(
85 DisplayEventReceiver::Event* event);
86
Mathias Agopiand0566bc2011-11-17 17:49:17 -080087 void dump(String8& result, char* buffer, size_t SIZE) const;
88
89private:
90 virtual bool threadLoop();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080091 virtual void onFirstRef();
92
Mathias Agopiancb9732a2012-04-03 17:48:03 -070093 void removeDisplayEventConnection(const wp<Connection>& connection);
Mathias Agopian22ffb112012-04-10 21:04:02 -070094 void enableVSyncLocked();
95 void disableVSyncLocked();
Mathias Agopian23748662011-12-05 14:33:34 -080096
Mathias Agopiand0566bc2011-11-17 17:49:17 -080097 // constants
Mathias Agopian86303202012-07-24 22:46:10 -070098 sp<SurfaceFlinger> mFlinger;
99 PowerHAL mPowerHAL;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800100
101 mutable Mutex mLock;
102 mutable Condition mCondition;
103
104 // protected by mLock
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700105 SortedVector< wp<Connection> > mDisplayEventConnections;
Mathias Agopian148994e2012-09-19 17:31:36 -0700106 Vector< DisplayEventReceiver::Event > mPendingEvents;
Mathias Agopianff28e202012-09-20 23:24:19 -0700107 DisplayEventReceiver::Event mVSyncEvent[HWC_DISPLAY_TYPES_SUPPORTED];
Mathias Agopian22ffb112012-04-10 21:04:02 -0700108 bool mUseSoftwareVSync;
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700109
110 // for debugging
111 bool mDebugVsyncEnabled;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800112};
113
114// ---------------------------------------------------------------------------
115
116}; // namespace android
117
118// ---------------------------------------------------------------------------
119
120#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */