display: hwc_buffer_allocator fixes

Fixes an issue where the HWC init randomly blocks on the
allocator init by doing a lazy initialization of the allocator
object. The allocator may not have fully initialized at HWC init.
Also, increases the allocator connection threadpool to 4.

Change-Id: I38454ee25aaf2a211125e55297300eac754fb5cf
CRs-Fixed: 2201202
diff --git a/gralloc/service.cpp b/gralloc/service.cpp
index 1136e2f..b19cc88 100644
--- a/gralloc/service.cpp
+++ b/gralloc/service.cpp
@@ -37,7 +37,7 @@
 
 int main(int, char **) {
   android::sp<IAllocator> service = new QtiAllocator();
-  configureRpcThreadpool(1, true /*callerWillJoin*/);
+  configureRpcThreadpool(4, true /*callerWillJoin*/);
   if (service->registerAsService() != android::OK) {
     ALOGE("Cannot register QTI Allocator service");
     return -EINVAL;
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.cpp b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
index 55988f1..dab9885 100644
--- a/sdm/libs/hwc2/hwc_buffer_allocator.cpp
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
@@ -47,7 +47,12 @@
 
 namespace sdm {
 
-DisplayError HWCBufferAllocator::Init() {
+DisplayError HWCBufferAllocator::GetGrallocInstance() {
+  // Lazy initialization of gralloc HALs
+  if (mapper_ != nullptr || allocator_ != nullptr) {
+    return kErrorNone;
+  }
+
   allocator_ = IAllocator::getService();
   mapper_ = IMapper::getService();
 
@@ -58,11 +63,11 @@
   return kErrorNone;
 }
 
-DisplayError HWCBufferAllocator::Deinit() {
-  return kErrorNone;
-}
-
 DisplayError HWCBufferAllocator::AllocateBuffer(BufferInfo *buffer_info) {
+  auto err = GetGrallocInstance();
+  if (err != kErrorNone) {
+    return err;
+  }
   const BufferConfig &buffer_config = buffer_info->buffer_config;
   AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
   int format;
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.h b/sdm/libs/hwc2/hwc_buffer_allocator.h
index 2846b89..d6f251f 100644
--- a/sdm/libs/hwc2/hwc_buffer_allocator.h
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.h
@@ -48,8 +48,6 @@
 
 class HWCBufferAllocator : public BufferAllocator {
  public:
-  DisplayError Init();
-  DisplayError Deinit();
   DisplayError AllocateBuffer(BufferInfo *buffer_info);
   DisplayError FreeBuffer(BufferInfo *buffer_info);
   uint32_t GetBufferSize(BufferInfo *buffer_info);
@@ -66,6 +64,7 @@
   DisplayError UnmapBuffer(const private_handle_t *handle, int *release_fence);
 
  private:
+  DisplayError GetGrallocInstance();
   android::sp<IMapper> mapper_;
   android::sp<IAllocator> allocator_;
 };
diff --git a/sdm/libs/hwc2/hwc_session.cpp b/sdm/libs/hwc2/hwc_session.cpp
index 02e2129..74ffe38 100644
--- a/sdm/libs/hwc2/hwc_session.cpp
+++ b/sdm/libs/hwc2/hwc_session.cpp
@@ -160,23 +160,10 @@
 
   StartServices();
 
-  DisplayError error = buffer_allocator_.Init();
-  if (error != kErrorNone) {
-    ALOGE("%s::%s: Buffer allocator initialization failed. Error = %d",
-          __CLASS__, __FUNCTION__, error);
-    return -EINVAL;
-  }
-
   g_hwc_uevent_.Register(this);
 
-  error = CoreInterface::CreateCore(HWCDebugHandler::Get(), &buffer_allocator_,
-                                    &buffer_sync_handler_, &socket_handler_, &core_intf_);
-  if (error != kErrorNone) {
-    buffer_allocator_.Deinit();
-    ALOGE("%s::%s: Display core initialization failed. Error = %d", __CLASS__, __FUNCTION__, error);
-    return -EINVAL;
-  }
-
+  auto error = CoreInterface::CreateCore(HWCDebugHandler::Get(), &buffer_allocator_,
+                                         &buffer_sync_handler_, &socket_handler_, &core_intf_);
 
   // If HDMI display is primary display, defer display creation until hotplug event is received.
   HWDisplayInterfaceInfo hw_disp_info = {};
@@ -184,7 +171,6 @@
   if (error != kErrorNone) {
     g_hwc_uevent_.Register(nullptr);
     CoreInterface::DestroyCore();
-    buffer_allocator_.Deinit();
     DLOGE("Primary display type not recognized. Error = %d", error);
     return -EINVAL;
   }
@@ -209,7 +195,6 @@
   if (status) {
     g_hwc_uevent_.Register(nullptr);
     CoreInterface::DestroyCore();
-    buffer_allocator_.Deinit();
     return status;
   }