cros_gralloc: Check for init failure in allocator service

Prior to this change, the allocator service could be kept
alive in a bad state if the allocator failed to initialize
the underlying cros_gralloc_driver on the first attempt.

With this change, the allocator service will exit if the
allocator fails to initialize the underlying
cros_gralloc_driver which gives the service a chance to
restart and initialize again.

BUG=b:187802138
TEST=launch Cuttlefish

Change-Id: I2a0fd76cda8492632406597656a581325e69b18a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/minigbm/+/3015656
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org>
Tested-by: Jason Macnak <natsu@google.com>
Commit-Queue: Jason Macnak <natsu@google.com>
diff --git a/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc b/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc
index bb9ff97..65637bc 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc
+++ b/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc
@@ -24,6 +24,11 @@
 using BufferDescriptorInfo =
         android::hardware::graphics::mapper::V3_0::IMapper::BufferDescriptorInfo;
 
+Error CrosGralloc3Allocator::init() {
+    mDriver = cros_gralloc_driver::get_instance();
+    return mDriver ? Error::NONE : Error::NO_RESOURCES;
+}
+
 Error CrosGralloc3Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
                                       hidl_handle* outHandle) {
     if (!mDriver) {
diff --git a/cros_gralloc/gralloc3/CrosGralloc3Allocator.h b/cros_gralloc/gralloc3/CrosGralloc3Allocator.h
index 24a34bd..be5b5b9 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Allocator.h
+++ b/cros_gralloc/gralloc3/CrosGralloc3Allocator.h
@@ -17,6 +17,8 @@
             const android::hardware::hidl_vec<uint32_t>& descriptor, uint32_t count,
             allocate_cb hidl_cb) override;
 
+    android::hardware::graphics::mapper::V3_0::Error init();
+
     android::hardware::Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override;
 
   private:
@@ -25,5 +27,5 @@
                     description,
             uint32_t* outStride, android::hardware::hidl_handle* outHandle);
 
-    cros_gralloc_driver* mDriver = cros_gralloc_driver::get_instance();
+    cros_gralloc_driver* mDriver = nullptr;
 };
diff --git a/cros_gralloc/gralloc3/CrosGralloc3AllocatorService.cc b/cros_gralloc/gralloc3/CrosGralloc3AllocatorService.cc
index daab508..6bde1d6 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3AllocatorService.cc
+++ b/cros_gralloc/gralloc3/CrosGralloc3AllocatorService.cc
@@ -13,18 +13,22 @@
 using android::sp;
 using android::hardware::configureRpcThreadpool;
 using android::hardware::joinRpcThreadpool;
-using android::hardware::graphics::allocator::V3_0::IAllocator;
+using android::hardware::graphics::mapper::V3_0::Error;
 
 int main(int, char**) {
-    sp<IAllocator> allocator = new CrosGralloc3Allocator();
+    sp<CrosGralloc3Allocator> allocator = new CrosGralloc3Allocator();
+    if (allocator->init() != Error::NONE) {
+        ALOGE("Failed to initialize IAllocator 3.0 service.");
+        return -EINVAL;
+    }
     configureRpcThreadpool(4, true /* callerWillJoin */);
     if (allocator->registerAsService() != android::NO_ERROR) {
-        ALOGE("failed to register graphics IAllocator 3.0 service");
+        ALOGE("Failed to register graphics IAllocator 3.0 service.");
         return -EINVAL;
     }
 
-    ALOGI("graphics IAllocator 3.0 service is initialized");
+    ALOGI("IAllocator 3.0 service is initialized.");
     android::hardware::joinRpcThreadpool();
-    ALOGI("graphics IAllocator 3.0 service is terminating");
+    ALOGI("IAllocator 3.0 service is terminating.");
     return 0;
 }
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
index 200b939..3f77b54 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
@@ -23,6 +23,11 @@
 using BufferDescriptorInfo =
         android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
 
+Error CrosGralloc4Allocator::init() {
+    mDriver = cros_gralloc_driver::get_instance();
+    return mDriver ? Error::NONE : Error::NO_RESOURCES;
+}
+
 Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
                                       hidl_handle* outHandle) {
     if (!mDriver) {
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.h b/cros_gralloc/gralloc4/CrosGralloc4Allocator.h
index cb3b4b8..1555a61 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.h
+++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.h
@@ -16,11 +16,13 @@
     android::hardware::Return<void> allocate(const android::hardware::hidl_vec<uint8_t>& descriptor,
                                              uint32_t count, allocate_cb hidl_cb) override;
 
+    android::hardware::graphics::mapper::V4_0::Error init();
+
   private:
     android::hardware::graphics::mapper::V4_0::Error allocate(
             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo&
                     description,
             uint32_t* outStride, android::hardware::hidl_handle* outHandle);
 
-    cros_gralloc_driver* mDriver = cros_gralloc_driver::get_instance();
+    cros_gralloc_driver* mDriver = nullptr;
 };
diff --git a/cros_gralloc/gralloc4/CrosGralloc4AllocatorService.cc b/cros_gralloc/gralloc4/CrosGralloc4AllocatorService.cc
index 5b79860..99bc92e 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4AllocatorService.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4AllocatorService.cc
@@ -13,18 +13,23 @@
 using android::sp;
 using android::hardware::configureRpcThreadpool;
 using android::hardware::joinRpcThreadpool;
-using android::hardware::graphics::allocator::V4_0::IAllocator;
+using android::hardware::graphics::mapper::V4_0::Error;
 
 int main(int, char**) {
-    sp<IAllocator> allocator = new CrosGralloc4Allocator();
-    configureRpcThreadpool(4, true /* callerWillJoin */);
-    if (allocator->registerAsService() != android::NO_ERROR) {
-        ALOGE("failed to register graphics IAllocator 4.0 service");
+    sp<CrosGralloc4Allocator> allocator = new CrosGralloc4Allocator();
+    if (allocator->init() != Error::NONE) {
+        ALOGE("Failed to initialize IAllocator 4.0 service.");
         return -EINVAL;
     }
 
-    ALOGI("graphics IAllocator 4.0 service is initialized");
+    configureRpcThreadpool(4, true /* callerWillJoin */);
+    if (allocator->registerAsService() != android::NO_ERROR) {
+        ALOGE("Failed to register graphics IAllocator 4.0 service.");
+        return -EINVAL;
+    }
+
+    ALOGI("IAllocator 4.0 service is initialized.");
     android::hardware::joinRpcThreadpool();
-    ALOGI("graphics IAllocator 4.0 service is terminating");
+    ALOGI("IAllocator 4.0 service is terminating.");
     return 0;
 }