Fix usage of HWC_DISPLAY_PRIMARY in vr flinger

The hardware composer functions require display ids obtained from the
onHotplug() composer callback. Our vr flinger code was supplying
HWC_DISPLAY_PRIMARY as the primary display id, which is incorrect. The
HWC_DISPLAY_* values are used for representing the display type, not the
display id. The code worked anyway because most hardware composers
always use 0 as the primary display id, which happens to be the same
value as HWC_DISPLAY_PRIMARY. The emulator uses 1 as the primary display
id though, which exposed the bug.

This CL changes the vr flinger code to get the display id from the
onHotplug() composer callback, and uses that value when talking to
hardware composer, instead of HWC_DISPLAY_PRIMARY. This matches the
behavior of surface flinger.

Bug: 69631196

Test: Verified the vr flinger code path works as expected on phones and
standalones.

Change-Id: Ia691941d0eafaa1f89e0ee81a4ae27fdef5a57cf
diff --git a/libs/vr/libvrflinger/hardware_composer.h b/libs/vr/libvrflinger/hardware_composer.h
index 7010db9..9ed4b22 100644
--- a/libs/vr/libvrflinger/hardware_composer.h
+++ b/libs/vr/libvrflinger/hardware_composer.h
@@ -152,6 +152,11 @@
     display_metrics_ = display_metrics;
   }
 
+  // Sets the display id used by all Layer instances.
+  static void SetDisplayId(hwc2_display_t display_id) {
+    display_id_ = display_id;
+  }
+
  private:
   void CommonLayerSetup();
 
@@ -180,6 +185,11 @@
   // thereafter.
   static HWCDisplayMetrics display_metrics_;
 
+  // Id of the primary display. Shared by all instances of Layer. This must be
+  // set whenever the primary display id changes. This can be left unset as long
+  // as there are no instances of Layer that might need to use it.
+  static hwc2_display_t display_id_;
+
   // The hardware composer layer and metrics to use during the prepare cycle.
   hwc2_layer_t hardware_composer_layer_ = 0;
 
@@ -298,13 +308,14 @@
 class HardwareComposer {
  public:
   // Type for vsync callback.
-  using VSyncCallback = std::function<void(int, int64_t, int64_t, uint32_t)>;
+  using VSyncCallback = std::function<void(int64_t, int64_t, uint32_t)>;
   using RequestDisplayCallback = std::function<void(bool)>;
 
   HardwareComposer();
   ~HardwareComposer();
 
   bool Initialize(Hwc2::Composer* composer,
+                  hwc2_display_t primary_display_id,
                   RequestDisplayCallback request_display_callback);
 
   bool IsInitialized() const { return initialized_; }
@@ -362,16 +373,16 @@
     hardware::Return<void> onVsync(Hwc2::Display display,
                                    int64_t timestamp) override;
 
-    pdx::Status<int64_t> GetVsyncTime(Hwc2::Display display);
+    bool HasDisplayId() { return has_display_id_; }
+    hwc2_display_t GetDisplayId() { return display_id_; }
+    pdx::Status<int64_t> GetVsyncTime();
 
    private:
     std::mutex vsync_mutex_;
-
-    struct Display {
-      pdx::LocalHandle driver_vsync_event_fd;
-      int64_t callback_vsync_timestamp{0};
-    };
-    std::array<Display, HWC_NUM_PHYSICAL_DISPLAY_TYPES> displays_;
+    bool has_display_id_ = false;
+    hwc2_display_t display_id_;
+    pdx::LocalHandle driver_vsync_event_fd_;
+    int64_t callback_vsync_timestamp_{0};
   };
 
   HWC::Error Validate(hwc2_display_t display);
@@ -412,7 +423,6 @@
   // kPostThreadInterrupted.
   int ReadWaitPPState();
   pdx::Status<int64_t> WaitForVSync();
-  pdx::Status<int64_t> GetVSyncTime();
   int SleepUntil(int64_t wakeup_timestamp);
 
   // Reconfigures the layer stack if the display surfaces changed since the last