SurfaceFlinger: SW-based vsync events

This change adds the DispSync class, which models the hardware vsync event
times to allow vsync event callbacks to be done at an arbitrary phase offset
from the hardware vsync.  This can be used to reduce the minimum latency from
Choreographer wake-up to on-screen image presentation.

Bug: 10624956
Change-Id: I8c7a54ceacaa4d709726ed97b0dcae4093a7bdcf
diff --git a/services/surfaceflinger/EventThread.h b/services/surfaceflinger/EventThread.h
index 5e88693..f6ab4a7 100644
--- a/services/surfaceflinger/EventThread.h
+++ b/services/surfaceflinger/EventThread.h
@@ -39,7 +39,21 @@
 
 // ---------------------------------------------------------------------------
 
-class EventThread : public Thread {
+
+class VSyncSource : public virtual RefBase {
+public:
+    class Callback: public virtual RefBase {
+    public:
+        virtual ~Callback() {}
+        virtual void onVSyncEvent(nsecs_t when) = 0;
+    };
+
+    virtual ~VSyncSource() {}
+    virtual void setVSyncEnabled(bool enable) = 0;
+    virtual void setCallback(const sp<Callback>& callback) = 0;
+};
+
+class EventThread : public Thread, private VSyncSource::Callback {
     class Connection : public BnDisplayEventConnection {
     public:
         Connection(const sp<EventThread>& eventThread);
@@ -62,7 +76,7 @@
 
 public:
 
-    EventThread(const sp<SurfaceFlinger>& flinger);
+    EventThread(const sp<VSyncSource>& src);
 
     sp<Connection> createEventConnection() const;
     status_t registerDisplayEventConnection(const sp<Connection>& connection);
@@ -76,8 +90,7 @@
     // called after the screen is turned on from main thread
     void onScreenAcquired();
 
-    // called when receiving a vsync event
-    void onVSyncReceived(int type, nsecs_t timestamp);
+    // called when receiving a hotplug event
     void onHotplugReceived(int type, bool connected);
 
     Vector< sp<EventThread::Connection> > waitForEvent(
@@ -89,12 +102,14 @@
     virtual bool        threadLoop();
     virtual void        onFirstRef();
 
+    virtual void onVSyncEvent(nsecs_t timestamp);
+
     void removeDisplayEventConnection(const wp<Connection>& connection);
     void enableVSyncLocked();
     void disableVSyncLocked();
 
     // constants
-    sp<SurfaceFlinger> mFlinger;
+    sp<VSyncSource> mVSyncSource;
     PowerHAL mPowerHAL;
 
     mutable Mutex mLock;
@@ -105,6 +120,7 @@
     Vector< DisplayEventReceiver::Event > mPendingEvents;
     DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
     bool mUseSoftwareVSync;
+    bool mVsyncEnabled;
 
     // for debugging
     bool mDebugVsyncEnabled;