drm_hwcomposer: Allow accessing ResourceManager from DrmDevce

This is useful for accessing the main lock from drm / compositor
related code blocks.

Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
diff --git a/drm/DrmDevice.cpp b/drm/DrmDevice.cpp
index fd4589e..5e160d4 100644
--- a/drm/DrmDevice.cpp
+++ b/drm/DrmDevice.cpp
@@ -28,12 +28,29 @@
 
 #include "drm/DrmAtomicStateManager.h"
 #include "drm/DrmPlane.h"
+#include "drm/ResourceManager.h"
 #include "utils/log.h"
 #include "utils/properties.h"
 
 namespace android {
 
-DrmDevice::DrmDevice() {
+auto DrmDevice::CreateInstance(std::string const &path,
+                               ResourceManager *res_man)
+    -> std::unique_ptr<DrmDevice> {
+  if (!IsKMSDev(path.c_str())) {
+    return {};
+  }
+
+  auto device = std::unique_ptr<DrmDevice>(new DrmDevice(res_man));
+
+  if (device->Init(path.c_str()) != 0) {
+    return {};
+  }
+
+  return device;
+}
+
+DrmDevice::DrmDevice(ResourceManager *res_man) : res_man_(res_man) {
   drm_fb_importer_ = std::make_unique<DrmFbImporter>(*this);
 }
 
diff --git a/drm/DrmDevice.h b/drm/DrmDevice.h
index f2530ee..4cf0132 100644
--- a/drm/DrmDevice.h
+++ b/drm/DrmDevice.h
@@ -31,18 +31,23 @@
 
 class DrmFbImporter;
 class DrmPlane;
+class ResourceManager;
 
 class DrmDevice {
  public:
-  DrmDevice();
   ~DrmDevice() = default;
 
-  auto Init(const char *path) -> int;
+  static auto CreateInstance(std::string const &path, ResourceManager *res_man)
+      -> std::unique_ptr<DrmDevice>;
 
   auto GetFd() const {
     return fd_.Get();
   }
 
+  auto &GetResMan() {
+    return *res_man_;
+  }
+
   auto GetConnectors() -> const std::vector<std::unique_ptr<DrmConnector>> &;
   auto GetPlanes() -> const std::vector<std::unique_ptr<DrmPlane>> &;
   auto GetCrtcs() -> const std::vector<std::unique_ptr<DrmCrtc>> &;
@@ -69,8 +74,6 @@
     return *drm_fb_importer_;
   }
 
-  static auto IsKMSDev(const char *path) -> bool;
-
   auto FindCrtcById(uint32_t id) const -> DrmCrtc * {
     for (const auto &crtc : crtcs_) {
       if (crtc->GetId() == id) {
@@ -95,6 +98,11 @@
                   DrmProperty *property) const;
 
  private:
+  explicit DrmDevice(ResourceManager *res_man);
+  auto Init(const char *path) -> int;
+
+  static auto IsKMSDev(const char *path) -> bool;
+
   UniqueFd fd_;
 
   std::vector<std::unique_ptr<DrmConnector>> connectors_;
@@ -109,6 +117,8 @@
   bool HasAddFb2ModifiersSupport_{};
 
   std::unique_ptr<DrmFbImporter> drm_fb_importer_;
+
+  ResourceManager *const res_man_;
 };
 }  // namespace android
 
diff --git a/drm/ResourceManager.cpp b/drm/ResourceManager.cpp
index ddf59dd..53e98dc 100644
--- a/drm/ResourceManager.cpp
+++ b/drm/ResourceManager.cpp
@@ -58,7 +58,10 @@
   int path_len = property_get("vendor.hwc.drm.device", path_pattern,
                               "/dev/dri/card%");
   if (path_pattern[path_len - 1] != '%') {
-    AddDrmDevice(std::string(path_pattern));
+    auto dev = DrmDevice::CreateInstance(path_pattern, this);
+    if (dev) {
+      drms_.emplace_back(std::move(dev));
+    }
   } else {
     path_pattern[path_len - 1] = '\0';
     for (int idx = 0;; ++idx) {
@@ -69,8 +72,9 @@
       if (stat(path.str().c_str(), &buf) != 0)
         break;
 
-      if (DrmDevice::IsKMSDev(path.str().c_str())) {
-        AddDrmDevice(path.str());
+      auto dev = DrmDevice::CreateInstance(path.str(), this);
+      if (dev) {
+        drms_.emplace_back(std::move(dev));
       }
     }
   }
@@ -108,13 +112,6 @@
   initialized_ = false;
 }
 
-int ResourceManager::AddDrmDevice(const std::string &path) {
-  auto drm = std::make_unique<DrmDevice>();
-  int ret = drm->Init(path.c_str());
-  drms_.push_back(std::move(drm));
-  return ret;
-}
-
 auto ResourceManager::GetTimeMonotonicNs() -> int64_t {
   struct timespec ts {};
   clock_gettime(CLOCK_MONOTONIC, &ts);
diff --git a/drm/ResourceManager.h b/drm/ResourceManager.h
index 88ba878..144d00e 100644
--- a/drm/ResourceManager.h
+++ b/drm/ResourceManager.h
@@ -59,7 +59,6 @@
   static auto GetTimeMonotonicNs() -> int64_t;
 
  private:
-  auto AddDrmDevice(std::string const &path) -> int;
   auto GetOrderedConnectors() -> std::vector<DrmConnector *>;
   void UpdateFrontendDisplays();
   void DetachAllFrontendDisplays();