Pass onVsync() callbacks from vr flinger back to surface flinger

When vr flinger is active we weren't returning onVsync() callbacks to
surface flinger from vr_hwc, which caused surface flinger frame
scheduling to drift with respect to display vsync, causing system
latency and performance to be less predictable than expected. Since
actual display vsync periods are usually slightly different than the
reported vsync period from hardware composer, the lack of vsync feedback
would also cause surface flinger to occasionally either miss a frame or
produce an extra frame during a vsync period.

This CL adds a new vsync service in vr flinger. Vr_hwc registers to
receive vsync callbacks from that service, and it forwards the vsync
events on to surface flinger. I confirmed using systrace this fixes the
scheduling drift in surface flinger.

I also removed the old PDX vsync service, which exposed obsolete
information and is no longer used anywhere.

The DispSync code in surface flinger needed to be updated as
well. DispSync uses the hardware composer present fence timestamps to
make vsync predictions, and when the present fence-based prediction
is close enough to the actual vsync times, it turns off the vysnc
callbacks. However the present fences returned from vr_hwc are not
correlated in any way to vsync times, so I changed the code to ignore
present fences when vr flinger is active.

Bug: 72890037

Test: - Used systrace to confirm surface flinger scheduling no longer
drifts with respect to the real vsync.

- Added new traces to confirm the vsync callbacks don't take a lot of
  cpu time.

- Confirmed that hardware vsync events in surface flinger are turned off
  when DispSync's predictions align with the present fence timestamp, as
  was previously the case.

- Confirmed hardware vsync events in surface flinger are turned off when
  the display is turned off.

- Confirmed that hardware vsync events are turned off as normal even
  when the zero phase tracer is turned on in DispSync.cpp.

- Confirmed that when we enter vr flinger, we turn usage of the present
  fence off in DispSync, and when we exit vr flinger, we turn usage of
  the present fence back on.

- Confirmed that I can't bind to the new vsync service from a normal
  Android application, and system processes (other than vr_hwc) are
  prevented from connecting by selinux.

- All tests mentioned above were done on a Pixel 2 (non-XL).

Change-Id: Ie009040e125f4d31958a1575b2e2bbe3e601a0f4
diff --git a/libs/vr/libvrflinger/hardware_composer.h b/libs/vr/libvrflinger/hardware_composer.h
index 1d8d463..80fa7ac 100644
--- a/libs/vr/libvrflinger/hardware_composer.h
+++ b/libs/vr/libvrflinger/hardware_composer.h
@@ -24,6 +24,7 @@
 #include <pdx/rpc/variant.h>
 #include <private/dvr/buffer_hub_client.h>
 #include <private/dvr/shared_buffer_helpers.h>
+#include <private/dvr/vsync_service.h>
 
 #include "acquired_buffer.h"
 #include "display_surface.h"
@@ -300,8 +301,6 @@
 // will access the state and whether it needs to be synchronized.
 class HardwareComposer {
  public:
-  // Type for vsync callback.
-  using VSyncCallback = std::function<void(int64_t, int64_t, uint32_t)>;
   using RequestDisplayCallback = std::function<void(bool)>;
 
   HardwareComposer();
@@ -325,8 +324,6 @@
 
   std::string Dump();
 
-  void SetVSyncCallback(VSyncCallback callback);
-
   const DisplayParams& GetPrimaryDisplayParams() const {
     return primary_display_;
   }
@@ -350,6 +347,18 @@
   // on/off. Returns true on success, false on failure.
   bool EnableDisplay(const DisplayParams& display, bool enabled);
 
+  class VsyncService : public BnVsyncService {
+   public:
+    status_t registerCallback(const sp<IVsyncCallback> callback) override;
+    status_t unregisterCallback(const sp<IVsyncCallback> callback) override;
+    void OnVsync(int64_t vsync_timestamp);
+   private:
+    std::vector<sp<IVsyncCallback>>::const_iterator FindCallback(
+        const sp<IVsyncCallback>& callback) const;
+    std::mutex mutex_;
+    std::vector<sp<IVsyncCallback>> callbacks_;
+  };
+
   class ComposerCallback : public Hwc2::IComposerCallback {
    public:
     ComposerCallback() = default;
@@ -360,6 +369,7 @@
                                    int64_t timestamp) override;
 
     bool GotFirstHotplug() { return got_first_hotplug_; }
+    void SetVsyncService(const sp<VsyncService>& vsync_service);
 
     struct Displays {
       hwc2_display_t primary_display = 0;
@@ -385,6 +395,7 @@
     DisplayInfo primary_display_;
     std::optional<DisplayInfo> external_display_;
     bool external_display_was_hotplugged_ = false;
+    sp<VsyncService> vsync_service_;
   };
 
   HWC::Error Validate(hwc2_display_t display);
@@ -484,9 +495,6 @@
   // vector must be sorted by surface_id in ascending order.
   std::vector<Layer> layers_;
 
-  // Handler to hook vsync events outside of this class.
-  VSyncCallback vsync_callback_;
-
   // The layer posting thread. This thread wakes up a short time before vsync to
   // hand buffers to hardware composer.
   std::thread post_thread_;
@@ -534,6 +542,9 @@
   DvrConfig post_thread_config_;
   std::mutex shared_config_mutex_;
 
+  bool vsync_trace_parity_ = false;
+  sp<VsyncService> vsync_service_;
+
   static constexpr int kPostThreadInterrupted = 1;
 
   HardwareComposer(const HardwareComposer&) = delete;