drm_hwcomposer: Initialize HwcDisplay using DrmDisplayPIpeline

HwcDisplay can now take all necessary objects from DrmDisplayPipeline.

Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
diff --git a/hwc2_device/HwcDisplay.cpp b/hwc2_device/HwcDisplay.cpp
index ea3957c..ac5d196 100644
--- a/hwc2_device/HwcDisplay.cpp
+++ b/hwc2_device/HwcDisplay.cpp
@@ -68,8 +68,12 @@
                              " VSync remains";
   }
 
+  std::string connector_name = IsInHeadlessMode()
+                                   ? "NULL-DISPLAY"
+                                   : GetPipe().connector->Get()->GetName();
+
   std::stringstream ss;
-  ss << "- Display on: " << connector_->GetName() << "\n"
+  ss << "- Display on: " << connector_name << "\n"
      << "  Flattening state: " << flattening_state_str << "\n"
      << "Statistics since system boot:\n"
      << DumpDelta(total_stats_) << "\n\n"
@@ -80,12 +84,12 @@
   return ss.str();
 }
 
-HwcDisplay::HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
-                       hwc2_display_t handle, HWC2::DisplayType type,
-                       DrmHwcTwo *hwc2)
+HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
+                       DrmDisplayPipeline *pipeline, hwc2_display_t handle,
+                       HWC2::DisplayType type, DrmHwcTwo *hwc2)
     : hwc2_(hwc2),
       resource_manager_(resource_manager),
-      drm_(drm),
+      pipeline_(pipeline),
       handle_(handle),
       type_(type),
       color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
@@ -104,43 +108,11 @@
   }
 
   AtomicCommitArgs a_args = {.clear_active_composition = true};
-  compositor_.ExecuteAtomicCommit(a_args);
+  GetPipe().compositor->ExecuteAtomicCommit(a_args);
 }
 
-HWC2::Error HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
-  int display = static_cast<int>(handle_);
-  int ret = compositor_.Init(resource_manager_, display);
-  if (ret) {
-    ALOGE("Failed display compositor init for display %d (%d)", display, ret);
-    return HWC2::Error::NoResources;
-  }
-
-  // Split up the given display planes into primary and overlay to properly
-  // interface with the composition
-  char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
-  property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
-               "1");
-  bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
-  for (auto &plane : *planes) {
-    if (plane->GetType() == DRM_PLANE_TYPE_PRIMARY)
-      primary_planes_.push_back(plane);
-    else if (use_overlay_planes && (plane)->GetType() == DRM_PLANE_TYPE_OVERLAY)
-      overlay_planes_.push_back(plane);
-  }
-
-  crtc_ = drm_->GetCrtcForDisplay(display);
-  if (!crtc_) {
-    ALOGE("Failed to get crtc for display %d", display);
-    return HWC2::Error::BadDisplay;
-  }
-
-  connector_ = drm_->GetConnectorForDisplay(display);
-  if (!connector_) {
-    ALOGE("Failed to get connector for display %d", display);
-    return HWC2::Error::BadDisplay;
-  }
-
-  ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
+HWC2::Error HwcDisplay::Init() {
+  int ret = vsync_worker_.Init(pipeline_, [this](int64_t timestamp) {
     const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
     /* vsync callback */
 #if PLATFORM_SDK_VERSION > 29
@@ -159,34 +131,30 @@
     }
   });
   if (ret) {
-    ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
+    ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
     return HWC2::Error::BadDisplay;
   }
 
-  ret = flattening_vsync_worker_
-            .Init(drm_, display, [this](int64_t /*timestamp*/) {
-              const std::lock_guard<std::mutex> lock(
-                  hwc2_->GetResMan().GetMainLock());
-              /* Frontend flattening */
-              if (flattenning_state_ >
-                      ClientFlattenningState::ClientRefreshRequested &&
-                  --flattenning_state_ ==
-                      ClientFlattenningState::ClientRefreshRequested &&
-                  hwc2_->refresh_callback_.first != nullptr &&
-                  hwc2_->refresh_callback_.second != nullptr) {
-                hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second,
-                                               handle_);
-                flattening_vsync_worker_.VSyncControl(false);
-              }
-            });
+  ret = flattening_vsync_worker_.Init(pipeline_, [this](int64_t /*timestamp*/) {
+    const std::lock_guard<std::mutex> lock(hwc2_->GetResMan().GetMainLock());
+    /* Frontend flattening */
+    if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
+        --flattenning_state_ ==
+            ClientFlattenningState::ClientRefreshRequested &&
+        hwc2_->refresh_callback_.first != nullptr &&
+        hwc2_->refresh_callback_.second != nullptr) {
+      hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
+      flattening_vsync_worker_.VSyncControl(false);
+    }
+  });
   if (ret) {
-    ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
+    ALOGE("Failed to create event worker for d=%d %d\n", int(handle_), ret);
     return HWC2::Error::BadDisplay;
   }
 
   ret = BackendManager::GetInstance().SetBackendForDisplay(this);
   if (ret) {
-    ALOGE("Failed to set backend for d=%d %d\n", display, ret);
+    ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
     return HWC2::Error::BadDisplay;
   }
 
@@ -196,7 +164,7 @@
 }
 
 HWC2::Error HwcDisplay::ChosePreferredConfig() {
-  HWC2::Error err = configs_.Update(*connector_);
+  HWC2::Error err = configs_.Update(*GetPipe().connector->Get());
   if (!IsInHeadlessMode() && err != HWC2::Error::None)
     return HWC2::Error::BadDisplay;
 
@@ -258,8 +226,8 @@
 HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
                                                int32_t /*format*/,
                                                int32_t dataspace) {
-  std::pair<uint32_t, uint32_t> min = drm_->GetMinResolution();
-  std::pair<uint32_t, uint32_t> max = drm_->GetMaxResolution();
+  std::pair<uint32_t, uint32_t> min = GetPipe().device->GetMinResolution();
+  std::pair<uint32_t, uint32_t> max = GetPipe().device->GetMaxResolution();
   if (IsInHeadlessMode()) {
     return HWC2::Error::None;
   }
@@ -363,7 +331,7 @@
 
 HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
   std::ostringstream stream;
-  stream << "display-" << connector_->GetId();
+  stream << "display-" << GetPipe().connector->Get()->GetId();
   std::string string = stream.str();
   size_t length = string.length();
   if (!name) {
@@ -471,7 +439,7 @@
   for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
     DrmHwcLayer layer;
     l.second->PopulateDrmLayer(&layer);
-    int ret = layer.ImportBuffer(drm_);
+    int ret = layer.ImportBuffer(GetPipe().device);
     if (ret) {
       ALOGE("Failed to import layer, ret=%d", ret);
       return HWC2::Error::NoResources;
@@ -479,7 +447,8 @@
     composition_layers.emplace_back(std::move(layer));
   }
 
-  auto composition = std::make_shared<DrmDisplayComposition>(crtc_);
+  auto composition = std::make_shared<DrmDisplayComposition>(
+      GetPipe().crtc->Get());
 
   // TODO(nobody): Don't always assume geometry changed
   int ret = composition->SetLayers(composition_layers.data(),
@@ -489,8 +458,12 @@
     return HWC2::Error::BadLayer;
   }
 
-  std::vector<DrmPlane *> primary_planes(primary_planes_);
-  std::vector<DrmPlane *> overlay_planes(overlay_planes_);
+  std::vector<DrmPlane *> primary_planes;
+  primary_planes.emplace_back(pipeline_->primary_plane->Get());
+  std::vector<DrmPlane *> overlay_planes;
+  for (const auto &owned_plane : pipeline_->overlay_planes) {
+    overlay_planes.emplace_back(owned_plane->Get());
+  }
   ret = composition->Plan(&primary_planes, &overlay_planes);
   if (ret) {
     ALOGV("Failed to plan the composition ret=%d", ret);
@@ -501,7 +474,7 @@
   if (staged_mode) {
     a_args.display_mode = *staged_mode;
   }
-  ret = compositor_.ExecuteAtomicCommit(a_args);
+  ret = GetPipe().compositor->ExecuteAtomicCommit(a_args);
 
   if (ret) {
     if (!a_args.test_only)
@@ -656,7 +629,7 @@
        * true, as the next composition frame will implicitly activate
        * the display
        */
-      return compositor_.ActivateDisplayUsingDPMS() == 0
+      return GetPipe().compositor->ActivateDisplayUsingDPMS() == 0
                  ? HWC2::Error::None
                  : HWC2::Error::BadParameter;
       break;
@@ -668,7 +641,7 @@
       return HWC2::Error::BadParameter;
   };
 
-  int err = compositor_.ExecuteAtomicCommit(a_args);
+  int err = GetPipe().compositor->ExecuteAtomicCommit(a_args);
   if (err) {
     ALOGE("Failed to apply the dpms composition err=%d", err);
     return HWC2::Error::BadParameter;
@@ -715,9 +688,9 @@
   /* Primary display should be always internal,
    * otherwise SF will be unhappy and will crash
    */
-  if (connector_->IsInternal() || handle_ == kPrimaryDisplay)
+  if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
     *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
-  else if (connector_->IsExternal())
+  else if (GetPipe().connector->Get()->IsExternal())
     *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
   else
     return HWC2::Error::BadConfig;
@@ -772,7 +745,7 @@
 HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
                                                      uint32_t *outDataSize,
                                                      uint8_t *outData) {
-  auto blob = connector_->GetEdidBlob();
+  auto blob = GetPipe().connector->Get()->GetEdidBlob();
 
   if (!blob) {
     ALOGE("Failed to get edid property value.");
@@ -785,7 +758,7 @@
   } else {
     *outDataSize = blob->length;
   }
-  *outPort = connector_->GetId();
+  *outPort = GetPipe().connector->Get()->GetId();
 
   return HWC2::Error::None;
 }