Add error handler for createOrGetGlablVkEmulation

Change-Id: Ib5d90d32f95b35b37dde747e6cb6b8f0480688f1
diff --git a/stream-servers/vulkan/VkCommonOperations.cpp b/stream-servers/vulkan/VkCommonOperations.cpp
index 6994f1f..73f67e2 100644
--- a/stream-servers/vulkan/VkCommonOperations.cpp
+++ b/stream-servers/vulkan/VkCommonOperations.cpp
@@ -446,13 +446,18 @@
 }
 
 VkEmulation* createOrGetGlobalVkEmulation(VulkanDispatch* vk) {
+#define VK_EMU_INIT_RETURN_ON_ERROR(...) \
+    do {                                 \
+        ERR(__VA_ARGS__);                \
+        return nullptr;                  \
+    } while (0)
+
     AutoLock lock(sVkEmulationLock);
 
     if (sVkEmulation) return sVkEmulation;
 
     if (!emugl::vkDispatchValid(vk)) {
-        fprintf(stderr, "%s: dispatch is invalid!\n", __func__);
-        return nullptr;
+        VK_EMU_INIT_RETURN_ON_ERROR("Dispatch is invalid.");
     }
 
     sVkEmulation = new VkEmulation;
@@ -546,8 +551,7 @@
     VkResult res = gvk->vkCreateInstance(&instCi, nullptr, &sVkEmulation->instance);
 
     if (res != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to create Vulkan instance.";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to create Vulkan instance.");
     }
 
     // Create instance level dispatch.
@@ -581,8 +585,7 @@
             VkResult res = gvk->vkCreateInstance(&instCi, nullptr, &sVkEmulation->instance);
 
             if (res != VK_SUCCESS) {
-                // LOG(ERROR) << "Failed to create Vulkan 1.1 instance.";
-                return sVkEmulation;
+                VK_EMU_INIT_RETURN_ON_ERROR("Failed to create Vulkan 1.1 instance.");
             }
 
             init_vulkan_dispatch_from_instance(
@@ -615,15 +618,13 @@
                         sVkEmulation->instance, "vkSetMTLTextureMVK"));
 
         if (!sVkEmulation->setMTLTextureFunc) {
-            // LOG(ERROR) << "Cannot find vkSetMTLTextureMVK";
-            return sVkEmulation;
+            VK_EMU_INIT_RETURN_ON_ERROR("Cannot find vkSetMTLTextureMVK.");
         }
        sVkEmulation->getMTLTextureFunc = reinterpret_cast<PFN_vkGetMTLTextureMVK>(
                 vk->vkGetInstanceProcAddr(
                         sVkEmulation->instance, "vkGetMTLTextureMVK"));
         if (!sVkEmulation->getMTLTextureFunc) {
-            // LOG(ERROR) << "Cannot find vkGetMTLTextureMVK"
-            return sVkEmulation;
+            VK_EMU_INIT_RETURN_ON_ERROR("Cannot find vkGetMTLTextureMVK.");
         }
         // LOG(VERBOSE) << "Instance supports VK_MVK_moltenvk.";
     }
@@ -638,8 +639,7 @@
     // LOG(VERBOSE) << "Found " << physdevCount << " Vulkan physical devices.";
 
     if (physdevCount == 0) {
-        // LOG(VERBOSE) << "No physical devices available.";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("No physical devices available.");
     }
 
     std::vector<VkEmulation::DeviceSupportInfo> deviceInfos(physdevCount);
@@ -797,8 +797,7 @@
     }
 
     if (!sVkEmulation->deviceInfo.hasGraphicsQueueFamily) {
-        // LOG(VERBOSE) << "No Vulkan devices with graphics queues found.";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("No Vulkan devices with graphics queues found.");
     }
 
     auto deviceVersion = sVkEmulation->deviceInfo.physdevProps.apiVersion;
@@ -850,8 +849,7 @@
                         &sVkEmulation->device);
 
     if (res != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to create Vulkan device.";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to create Vulkan device.");
     }
 
     // device created; populate dispatch table
@@ -877,16 +875,14 @@
                 dvk->vkGetDeviceProcAddr(
                     sVkEmulation->device, "vkGetImageMemoryRequirements2KHR"));
         if (!sVkEmulation->deviceInfo.getImageMemoryRequirements2Func) {
-            // LOG(ERROR) << "Cannot find vkGetImageMemoryRequirements2KHR";
-            return sVkEmulation;
+            VK_EMU_INIT_RETURN_ON_ERROR("Cannot find vkGetImageMemoryRequirements2KHR.");
         }
         sVkEmulation->deviceInfo.getBufferMemoryRequirements2Func =
             reinterpret_cast<PFN_vkGetBufferMemoryRequirements2KHR>(
                 dvk->vkGetDeviceProcAddr(
                     sVkEmulation->device, "vkGetBufferMemoryRequirements2KHR"));
         if (!sVkEmulation->deviceInfo.getBufferMemoryRequirements2Func) {
-            // LOG(ERROR) << "Cannot find vkGetBufferMemoryRequirements2KHR";
-            return sVkEmulation;
+            VK_EMU_INIT_RETURN_ON_ERROR("Cannot find vkGetBufferMemoryRequirements2KHR");
         }
 #ifdef _WIN32
         sVkEmulation->deviceInfo.getMemoryHandleFunc =
@@ -900,8 +896,7 @@
                                                 "vkGetMemoryFdKHR"));
 #endif
         if (!sVkEmulation->deviceInfo.getMemoryHandleFunc) {
-            // LOG(ERROR) << "Cannot find vkGetMemory(Fd|Win32Handle)KHR";
-            return sVkEmulation;
+            VK_EMU_INIT_RETURN_ON_ERROR("Cannot find vkGetMemory(Fd|Win32Handle)KHR");
         }
     }
 
@@ -931,8 +926,8 @@
             sVkEmulation->device, &poolCi, nullptr, &sVkEmulation->commandPool);
 
     if (poolCreateRes != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to create command pool. Error: " << poolCreateRes;
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to create command pool. Error: %s.",
+                                    string_VkResult(poolCreateRes));
     }
 
     VkCommandBufferAllocateInfo cbAi = {
@@ -944,8 +939,8 @@
             sVkEmulation->device, &cbAi, &sVkEmulation->commandBuffer);
 
     if (cbAllocRes != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to allocate command buffer. Error: " << cbAllocRes;
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to allocate command buffer. Error: %s.",
+                                    string_VkResult(cbAllocRes));
     }
 
     VkFenceCreateInfo fenceCi = {
@@ -957,8 +952,8 @@
         &sVkEmulation->commandBufferFence);
 
     if (fenceCreateRes != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to create fence for command buffer. Error: " << fenceCreateRes;
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to create fence for command buffer. Error: %s.",
+                                    string_VkResult(fenceCreateRes));
     }
 
     // At this point, the global emulation state's logical device can alloc
@@ -981,8 +976,8 @@
                                &sVkEmulation->staging.buffer);
 
     if (bufCreateRes != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to create staging buffer index";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to create staging buffer index. Error: %s.",
+                                    string_VkResult(bufCreateRes));
     }
 
     VkMemoryRequirements memReqs;
@@ -996,22 +991,19 @@
             &sVkEmulation->staging.memory.typeIndex);
 
     if (!gotStagingTypeIndex) {
-        // LOG(ERROR) << "Failed to determine staging memory type index";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to determine staging memory type index.");
     }
 
     if (!((1 << sVkEmulation->staging.memory.typeIndex) &
           memReqs.memoryTypeBits)) {
-        // LOG(ERROR) << "Failed: Inconsistent determination of memory type "
-                        "index for staging buffer";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR(
+            "Failed: Inconsistent determination of memory type index for staging buffer");
     }
 
     if (!allocExternalMemory(dvk, &sVkEmulation->staging.memory,
                              false /* not external */,
                              kNullopt /* deviceAlignment */)) {
-        // LOG(ERROR) << "Failed to allocate memory for staging buffer";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to allocate memory for staging buffer.");
     }
 
     VkResult stagingBufferBindRes = dvk->vkBindBufferMemory(
@@ -1020,8 +1012,7 @@
         sVkEmulation->staging.memory.memory, 0);
 
     if (stagingBufferBindRes != VK_SUCCESS) {
-        // LOG(ERROR) << "Failed to bind memory for staging buffer";
-        return sVkEmulation;
+        VK_EMU_INIT_RETURN_ON_ERROR("Failed to bind memory for staging buffer.");
     }
 
     // LOG(VERBOSE) << "Vulkan global emulation state successfully initialized.";