drm_hwcomposer: Use shared pointer type for the pipeline_ variable

The change will help with resource management in further patches.

Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
diff --git a/drm/ResourceManager.cpp b/drm/ResourceManager.cpp
index 577d86c..634ccb7 100644
--- a/drm/ResourceManager.cpp
+++ b/drm/ResourceManager.cpp
@@ -137,14 +137,16 @@
             conn->GetName().c_str());
 
       if (connected) {
-        auto pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
+        std::shared_ptr<DrmDisplayPipeline>
+            pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
+
         if (pipeline) {
-          frontend_interface_->BindDisplay(pipeline.get());
+          frontend_interface_->BindDisplay(pipeline);
           attached_pipelines_[conn] = std::move(pipeline);
         }
       } else {
         auto &pipeline = attached_pipelines_[conn];
-        frontend_interface_->UnbindDisplay(pipeline.get());
+        frontend_interface_->UnbindDisplay(pipeline);
         attached_pipelines_.erase(conn);
       }
     }
@@ -154,7 +156,7 @@
 
 void ResourceManager::DetachAllFrontendDisplays() {
   for (auto &p : attached_pipelines_) {
-    frontend_interface_->UnbindDisplay(p.second.get());
+    frontend_interface_->UnbindDisplay(p.second);
   }
   attached_pipelines_.clear();
   frontend_interface_->FinalizeDisplayBinding();
diff --git a/drm/ResourceManager.h b/drm/ResourceManager.h
index 7fa3fc6..72ee3e2 100644
--- a/drm/ResourceManager.h
+++ b/drm/ResourceManager.h
@@ -34,8 +34,8 @@
 class PipelineToFrontendBindingInterface {
  public:
   virtual ~PipelineToFrontendBindingInterface() = default;
-  virtual bool BindDisplay(DrmDisplayPipeline *);
-  virtual bool UnbindDisplay(DrmDisplayPipeline *);
+  virtual bool BindDisplay(std::shared_ptr<DrmDisplayPipeline>);
+  virtual bool UnbindDisplay(std::shared_ptr<DrmDisplayPipeline>);
   virtual void FinalizeDisplayBinding();
 };
 
@@ -82,7 +82,7 @@
 
   std::recursive_mutex main_lock_;
 
-  std::map<DrmConnector *, std::unique_ptr<DrmDisplayPipeline>>
+  std::map<DrmConnector *, std::shared_ptr<DrmDisplayPipeline>>
       attached_pipelines_;
 
   PipelineToFrontendBindingInterface *const frontend_interface_;
diff --git a/drm/VSyncWorker.cpp b/drm/VSyncWorker.cpp
index 8a251c7..10d48e3 100644
--- a/drm/VSyncWorker.cpp
+++ b/drm/VSyncWorker.cpp
@@ -30,14 +30,14 @@
 
 namespace android {
 
-auto VSyncWorker::CreateInstance(DrmDisplayPipeline *pipe,
+auto VSyncWorker::CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe,
                                  VSyncWorkerCallbacks &callbacks)
     -> std::shared_ptr<VSyncWorker> {
   auto vsw = std::shared_ptr<VSyncWorker>(new VSyncWorker());
 
   vsw->callbacks_ = callbacks;
 
-  if (pipe != nullptr) {
+  if (pipe) {
     vsw->high_crtc_ = pipe->crtc->Get()->GetIndexInResArray()
                       << DRM_VBLANK_HIGH_CRTC_SHIFT;
     vsw->drm_fd_ = pipe->device->GetFd();
diff --git a/drm/VSyncWorker.h b/drm/VSyncWorker.h
index 031a561..2a4c7c8 100644
--- a/drm/VSyncWorker.h
+++ b/drm/VSyncWorker.h
@@ -35,7 +35,7 @@
  public:
   ~VSyncWorker() = default;
 
-  auto static CreateInstance(DrmDisplayPipeline *pipe,
+  auto static CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe,
                              VSyncWorkerCallbacks &callbacks)
       -> std::shared_ptr<VSyncWorker>;
 
diff --git a/hwc2_device/DrmHwcTwo.cpp b/hwc2_device/DrmHwcTwo.cpp
index 64755c3..99756eb 100644
--- a/hwc2_device/DrmHwcTwo.cpp
+++ b/hwc2_device/DrmHwcTwo.cpp
@@ -35,13 +35,13 @@
     displays_[kPrimaryDisplay] = std::make_unique<
         HwcDisplay>(kPrimaryDisplay, HWC2::DisplayType::Physical, this);
     /* Initializes null-display */
-    displays_[kPrimaryDisplay]->SetPipeline(nullptr);
+    displays_[kPrimaryDisplay]->SetPipeline({});
   }
 
   if (displays_[kPrimaryDisplay]->IsInHeadlessMode() &&
       !display_handles_.empty()) {
     /* Reattach first secondary display to take place of the primary */
-    auto *pipe = display_handles_.begin()->first;
+    auto pipe = display_handles_.begin()->first;
     ALOGI("Primary display was disconnected, reattaching '%s' as new primary",
           pipe->connector->Get()->GetName().c_str());
     UnbindDisplay(pipe);
@@ -66,10 +66,10 @@
   }
 }
 
-bool DrmHwcTwo::BindDisplay(DrmDisplayPipeline *pipeline) {
+bool DrmHwcTwo::BindDisplay(std::shared_ptr<DrmDisplayPipeline> pipeline) {
   if (display_handles_.count(pipeline) != 0) {
     ALOGE("%s, pipeline is already used by another display, FIXME!!!: %p",
-          __func__, pipeline);
+          __func__, pipeline.get());
     return false;
   }
 
@@ -96,9 +96,9 @@
   return true;
 }
 
-bool DrmHwcTwo::UnbindDisplay(DrmDisplayPipeline *pipeline) {
+bool DrmHwcTwo::UnbindDisplay(std::shared_ptr<DrmDisplayPipeline> pipeline) {
   if (display_handles_.count(pipeline) == 0) {
-    ALOGE("%s, can't find the display, pipeline: %p", __func__, pipeline);
+    ALOGE("%s, can't find the display, pipeline: %p", __func__, pipeline.get());
     return false;
   }
   auto handle = display_handles_[pipeline];
@@ -112,7 +112,7 @@
     ALOGE("%s, can't find the display, handle: %" PRIu64, __func__, handle);
     return false;
   }
-  displays_[handle]->SetPipeline(nullptr);
+  displays_[handle]->SetPipeline({});
 
   /* We must defer display disposal and removal, since it may still have pending
    * HWC_API calls scheduled and waiting until ueventlistener thread releases
diff --git a/hwc2_device/DrmHwcTwo.h b/hwc2_device/DrmHwcTwo.h
index 81c5155..8701feb 100644
--- a/hwc2_device/DrmHwcTwo.h
+++ b/hwc2_device/DrmHwcTwo.h
@@ -61,8 +61,8 @@
   }
 
   // PipelineToFrontendBindingInterface
-  bool BindDisplay(DrmDisplayPipeline *pipeline) override;
-  bool UnbindDisplay(DrmDisplayPipeline *pipeline) override;
+  bool BindDisplay(std::shared_ptr<DrmDisplayPipeline> pipeline) override;
+  bool UnbindDisplay(std::shared_ptr<DrmDisplayPipeline> pipeline) override;
   void FinalizeDisplayBinding() override;
 
   void SendVsyncEventToClient(hwc2_display_t displayid, int64_t timestamp,
@@ -75,7 +75,8 @@
 
   ResourceManager resource_manager_;
   std::map<hwc2_display_t, std::unique_ptr<HwcDisplay>> displays_;
-  std::map<DrmDisplayPipeline *, hwc2_display_t> display_handles_;
+  std::map<std::shared_ptr<DrmDisplayPipeline>, hwc2_display_t>
+      display_handles_;
 
   std::string mDumpString;
 
diff --git a/hwc2_device/HwcDisplay.cpp b/hwc2_device/HwcDisplay.cpp
index efd8c14..4ef98c3 100644
--- a/hwc2_device/HwcDisplay.cpp
+++ b/hwc2_device/HwcDisplay.cpp
@@ -83,12 +83,12 @@
 
 HwcDisplay::~HwcDisplay() = default;
 
-void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
+void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
   Deinit();
 
-  pipeline_ = pipeline;
+  pipeline_ = std::move(pipeline);
 
-  if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
+  if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
     Init();
     hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
   } else {
diff --git a/hwc2_device/HwcDisplay.h b/hwc2_device/HwcDisplay.h
index bf95c3e..d9dc4eb 100644
--- a/hwc2_device/HwcDisplay.h
+++ b/hwc2_device/HwcDisplay.h
@@ -44,7 +44,7 @@
   ~HwcDisplay();
 
   /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
-  void SetPipeline(DrmDisplayPipeline *pipeline);
+  void SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline);
 
   HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
   std::vector<HwcLayer *> GetOrderLayersByZPos();
@@ -193,7 +193,7 @@
   int64_t staged_mode_change_time_{};
   uint32_t staged_mode_config_id_{};
 
-  DrmDisplayPipeline *pipeline_{};
+  std::shared_ptr<DrmDisplayPipeline> pipeline_;
 
   std::unique_ptr<Backend> backend_;
   std::shared_ptr<FlatteningController> flatcon_;