SF: Refactor HWComposer::onHotplug()

Currently HWComposer::onHotplug() contains logic that is
executed when hotplug disconnect or connect event occurs.
However reading what exactly is done during e.g. hotplug
connect is hard, because the logic is spread between two
if statements and the function HWComposer::onHotplugConnect().
This change moves all hotplug connect logic to
HWComposer::onHotplugConnect() and all disconnect logic
to a new function HWComposer::onHotplugDisconnect().

Additionally this CLs refactors onHotplugConnect() which
is currently responsible for multiple concerns, which is
making the code hard to understand and reuse.

 - checking if the event should be ignored is moved to
   a new function shouldIgnoreHotplugConnect()
 - creating the necessary state for the new display is
   moved to a new function allocatePhysicalDisplay()

Some of the newly created functions will be directly reused
in following CLs.

Bug: 143451809
Test: m surfaceflinger
Test: atest libsurfaceflinger_unittest
Merged-In: Ib4dae2ce81e8cac176a56b6de787a4f76d01c050
Change-Id: Ib4dae2ce81e8cac176a56b6de787a4f76d01c050
(cherry picked from commit 05f9398f9405587f8d1a5c5c17feffc89890ca2e)
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.h b/services/surfaceflinger/DisplayHardware/HWComposer.h
index 41db501..3cb40b1 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.h
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.h
@@ -77,6 +77,8 @@
     virtual std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
                                                             ui::PixelFormat* format) = 0;
 
+    virtual void allocatePhysicalDisplay(hwc2_display_t hwcDisplayId, DisplayId displayId) = 0;
+
     // Attempts to create a new layer on this display
     virtual HWC2::Layer* createLayer(DisplayId displayId) = 0;
     // Destroy a previously created layer
@@ -164,6 +166,7 @@
 
     // Returns stable display ID (and display name on connection of new or previously disconnected
     // display), or std::nullopt if hotplug event was ignored.
+    // This function is called from SurfaceFlinger.
     virtual std::optional<DisplayIdentificationInfo> onHotplug(hwc2_display_t hwcDisplayId,
                                                                HWC2::Connection connection) = 0;
 
@@ -238,6 +241,9 @@
     std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
                                                     ui::PixelFormat* format) override;
 
+    // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
+    void allocatePhysicalDisplay(hwc2_display_t hwcDisplayId, DisplayId displayId) override;
+
     // Attempts to create a new layer on this display
     HWC2::Layer* createLayer(DisplayId displayId) override;
     // Destroy a previously created layer
@@ -361,6 +367,10 @@
     friend TestableSurfaceFlinger;
 
     std::optional<DisplayIdentificationInfo> onHotplugConnect(hwc2_display_t hwcDisplayId);
+    std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hwc2_display_t hwcDisplayId);
+    bool shouldIgnoreHotplugConnect(hwc2_display_t hwcDisplayId,
+                                    bool hasDisplayIdentificationData) const;
+
     void loadCapabilities();
     void loadLayerMetadataSupport();
     uint32_t getMaxVirtualDisplayCount() const;