Merge remote-tracking branch 'aosp/upstream-main' into 'aosp/master'

... to pull in crrev.com/c/3015656 to handle potential allocator
service startup failures for Cuttlefish.

Bug: b/187802138
Test: launch_cvd
Change-Id: Ibf547f86bda0046cafa6eb273c507a81954e9a88
diff --git a/cros_gralloc/cros_gralloc_types.h b/cros_gralloc/cros_gralloc_types.h
index 22f58e2..7c51778 100644
--- a/cros_gralloc/cros_gralloc_types.h
+++ b/cros_gralloc/cros_gralloc_types.h
@@ -9,6 +9,11 @@
 
 #include <string>
 
+// Reserve the GRALLOC_USAGE_PRIVATE_0 bit from hardware/gralloc.h for buffers
+// used for front rendering. minigbm backend later decides to use
+// BO_USE_FRONT_RENDERING or BO_USE_LINEAR upon buffer allocaton.
+#define BUFFER_USAGE_FRONT_RENDERING (1U << 28)
+
 struct cros_gralloc_buffer_descriptor {
 	uint32_t width;
 	uint32_t height;
diff --git a/cros_gralloc/gralloc0/gralloc0.cc b/cros_gralloc/gralloc0/gralloc0.cc
index 72c4d3b..afd40d4 100644
--- a/cros_gralloc/gralloc0/gralloc0.cc
+++ b/cros_gralloc/gralloc0/gralloc0.cc
@@ -71,11 +71,6 @@
 // to support allocating with AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER.
 #define BUFFER_USAGE_GPU_DATA_BUFFER (1 << 24)
 
-// Reserve the GRALLOC_USAGE_PRIVATE_0 bit for buffers used for front rendering.
-// minigbm backend later decides to use BO_USE_FRONT_RENDERING or BO_USE_LINEAR
-// upon buffer allocaton.
-#define BUFFER_USAGE_FRONT_RENDERING GRALLOC_USAGE_PRIVATE_0
-
 static uint64_t gralloc0_convert_usage(int usage)
 {
 	uint64_t use_flags = BO_USE_NONE;
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/gralloc3/CrosGralloc3Utils.cc b/cros_gralloc/gralloc3/CrosGralloc3Utils.cc
index f0ac207..d15c431 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Utils.cc
+++ b/cros_gralloc/gralloc3/CrosGralloc3Utils.cc
@@ -161,6 +161,10 @@
         usage &= ~static_cast<Underlying>(BufferUsage::GPU_DATA_BUFFER);
         usages.push_back("BufferUsage::GPU_DATA_BUFFER");
     }
+    if (usage & BUFFER_USAGE_FRONT_RENDERING) {
+        usage &= ~static_cast<Underlying>(BUFFER_USAGE_FRONT_RENDERING);
+        usages.push_back("BUFFER_USAGE_FRONT_RENDERING");
+    }
 
     if (usage) {
         usages.push_back(android::base::StringPrintf("UnknownUsageBits-%" PRIu64, usage));
@@ -308,6 +312,9 @@
     if (grallocUsage & BufferUsage::GPU_DATA_BUFFER) {
         bufferUsage |= BO_USE_GPU_DATA_BUFFER;
     }
+    if (grallocUsage & BUFFER_USAGE_FRONT_RENDERING) {
+        bufferUsage |= BO_USE_FRONT_RENDERING;
+    }
 
     *outBufferUsage = bufferUsage;
     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;
 }
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Utils.cc b/cros_gralloc/gralloc4/CrosGralloc4Utils.cc
index 55c0855..e2940cc 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Utils.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4Utils.cc
@@ -165,6 +165,10 @@
         usage &= ~static_cast<Underlying>(BufferUsage::GPU_DATA_BUFFER);
         usages.push_back("BufferUsage::GPU_DATA_BUFFER");
     }
+    if (usage & BUFFER_USAGE_FRONT_RENDERING) {
+        usage &= ~static_cast<Underlying>(BUFFER_USAGE_FRONT_RENDERING);
+        usages.push_back("BUFFER_USAGE_FRONT_RENDERING");
+    }
 
     if (usage) {
         usages.push_back(android::base::StringPrintf("UnknownUsageBits-%" PRIu64, usage));
@@ -312,6 +316,9 @@
     if (grallocUsage & BufferUsage::GPU_DATA_BUFFER) {
         bufferUsage |= BO_USE_GPU_DATA_BUFFER;
     }
+    if (grallocUsage & BUFFER_USAGE_FRONT_RENDERING) {
+        bufferUsage |= BO_USE_FRONT_RENDERING;
+    }
 
     *outBufferUsage = bufferUsage;
     return 0;