Merge "DrmClient: check card0 then card1 for virtio-gpu" into main
diff --git a/system/hwc3/DrmClient.cpp b/system/hwc3/DrmClient.cpp
index 618fe58..1a3e25e 100644
--- a/system/hwc3/DrmClient.cpp
+++ b/system/hwc3/DrmClient.cpp
@@ -30,28 +30,46 @@
     }
 }
 
+::android::base::unique_fd DrmClient::OpenVirtioGpuDrmFd() {
+  for (int i = 0; i < 10; i++) {
+    const std::string path = "/dev/dri/card" + std::to_string(i);
+    DEBUG_LOG("%s: trying to open DRM device at %s",
+              __FUNCTION__, path.c_str());
+
+    ::android::base::unique_fd fd(open(path.c_str(), O_RDWR | O_CLOEXEC));
+
+    if (fd < 0) {
+      ALOGE("%s: failed to open drm device %s: %s",
+            __FUNCTION__, path.c_str(), strerror(errno));
+        continue;
+    }
+
+    auto version = drmGetVersion(fd.get());
+    const std::string name = version->name;
+    drmFreeVersion(version);
+
+    DEBUG_LOG("%s: The DRM device at %s is \"%s\"",
+              __FUNCTION__, path.c_str(), name.c_str());
+    if (name.find("virtio") != std::string::npos) {
+      return fd;
+    }
+  }
+
+  ALOGE("Failed to find virtio-gpu DRM node. Ranchu HWComposer "
+        "is only expected to be used with \"virtio_gpu\"");
+
+  return ::android::base::unique_fd(-1);
+}
+
 HWC3::Error DrmClient::init() {
     DEBUG_LOG("%s", __FUNCTION__);
 
-    mFd = ::android::base::unique_fd(open("/dev/dri/card0", O_RDWR | O_CLOEXEC));
+    mFd = OpenVirtioGpuDrmFd();
     if (mFd < 0) {
         ALOGE("%s: failed to open drm device: %s", __FUNCTION__, strerror(errno));
         return HWC3::Error::NoResources;
     }
 
-    auto version = drmGetVersion(mFd.get());
-
-    const std::string deviceName = version->name;
-    DEBUG_LOG("%s: /dev/dri/card0 is %s", __FUNCTION__, deviceName.c_str());
-    if (deviceName.find("virtio") == std::string::npos) {
-        ALOGE("%s: found unexpected DRM device when opening /dev/dri/card0: \"%s\". "
-              "Ranchu HWComposer is only expected to be used with \"virtio_gpu\".",
-              __FUNCTION__, deviceName.c_str());
-        return HWC3::Error::NoResources;
-    }
-
-    drmFreeVersion(version);
-
     int ret = drmSetClientCap(mFd.get(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
     if (ret) {
         ALOGE("%s: failed to set cap universal plane %s\n", __FUNCTION__, strerror(errno));
diff --git a/system/hwc3/DrmClient.h b/system/hwc3/DrmClient.h
index ad0d6d0..91dddb0 100644
--- a/system/hwc3/DrmClient.h
+++ b/system/hwc3/DrmClient.h
@@ -49,6 +49,8 @@
     DrmClient(DrmClient&&) = delete;
     DrmClient& operator=(DrmClient&&) = delete;
 
+    ::android::base::unique_fd OpenVirtioGpuDrmFd();
+
     HWC3::Error init();
 
     struct DisplayConfig {