drm_hwcomposer: Dynamic DrmDisplayPipeline to HwcDisplay bindings

The following use scenarios are now possible:

1. When no display connected, primary HwcDisplay is created in headless
   mode.

2. When user connects first display, it binds to primary slot, replacing
   headless HwcDisplay.

3. When user connects another display it binds to the new HwcDisplay
   slot, creating new display for the framework.

4. When user disconnects first (Primary) display, drm_hwc detaches
   second display and attaches it to the Primary slot. In this case
   framework is notified as Primary display resolution updated
   (Plugged->Plugged transition). And second display is disconnected
   (Plugged->Unplugged transition).

DrmDisplayPipeline is now created on demand (after hotplug event).

HwcDisplay class is now destructed on connector unplug, which will give
us ability to destroy any resource caches (will be required for FB
caching logic).

Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
diff --git a/hwc2_device/HwcDisplay.h b/hwc2_device/HwcDisplay.h
index b73404a..42b000a 100644
--- a/hwc2_device/HwcDisplay.h
+++ b/hwc2_device/HwcDisplay.h
@@ -37,10 +37,10 @@
 
 class HwcDisplay {
  public:
-  HwcDisplay(ResourceManager *resource_manager, DrmDisplayPipeline *pipeline,
-             hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2);
+  HwcDisplay(DrmDisplayPipeline *pipeline, hwc2_display_t handle,
+             HWC2::DisplayType type, DrmHwcTwo *hwc2);
   HwcDisplay(const HwcDisplay &) = delete;
-  HWC2::Error Init();
+  ~HwcDisplay();
 
   HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
   std::vector<HwcLayer *> GetOrderLayersByZPos();
@@ -144,6 +144,10 @@
   const Backend *backend() const;
   void set_backend(std::unique_ptr<Backend> backend);
 
+  auto GetHwc2() {
+    return hwc2_;
+  }
+
   std::map<hwc2_layer_t, HwcLayer> &layers() {
     return layers_;
   }
@@ -152,10 +156,6 @@
     return *pipeline_;
   }
 
-  ResourceManager *resource_manager() const {
-    return resource_manager_;
-  }
-
   android_color_transform_t &color_transform_hint() {
     return color_transform_hint_;
   }
@@ -193,8 +193,7 @@
    * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
    */
   bool IsInHeadlessMode() {
-    return handle_ == kPrimaryDisplay &&
-           !GetPipe().connector->Get()->IsConnected();
+    return !pipeline_;
   }
 
  private:
@@ -213,20 +212,22 @@
 
   HwcDisplayConfigs configs_;
 
-  DrmHwcTwo *hwc2_;
+  DrmHwcTwo *const hwc2_;
 
   std::optional<DrmMode> staged_mode;
 
-  ResourceManager *resource_manager_;
-
   DrmDisplayPipeline *const pipeline_;
 
   std::unique_ptr<Backend> backend_;
 
   VSyncWorker vsync_worker_;
-  hwc2_display_t handle_;
+
+  const hwc2_display_t handle_;
   HWC2::DisplayType type_;
-  uint32_t layer_idx_ = 0;
+
+  // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
+  static uint32_t layer_idx_;
+
   std::map<hwc2_layer_t, HwcLayer> layers_;
   HwcLayer client_layer_;
   int32_t color_mode_{};
@@ -237,6 +238,8 @@
   Stats total_stats_;
   Stats prev_stats_;
   std::string DumpDelta(HwcDisplay::Stats delta);
+
+  HWC2::Error Init();
 };
 
 }  // namespace android