Vulkan: Use Scoped resource init helper.

In a few places we initialize resource on the stack in the Vulkan
back-end. When we do this we should use a RAII wrapper so that if we
generate an error and unwind the stack we can auto-release the object.

This fixes a bug that was being triggered by an unexpected failure on
Present with the Vulkan back-end on Intel.

There are several other places in the code that should be modified to
use the new wrapper.

Bug: angleproject:2690
Change-Id: I49a1c5516756f8b7dba833aca65926aa7b8bd5c6
Reviewed-on: https://chromium-review.googlesource.com/1118610
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/renderer/vulkan/RendererVk.cpp b/src/libANGLE/renderer/vulkan/RendererVk.cpp
index 0a54c6c..be91ede 100644
--- a/src/libANGLE/renderer/vulkan/RendererVk.cpp
+++ b/src/libANGLE/renderer/vulkan/RendererVk.cpp
@@ -180,6 +180,12 @@
     return *this;
 }
 
+void RendererVk::CommandBatch::destroy(VkDevice device)
+{
+    commandPool.destroy(device);
+    fence.destroy(device);
+}
+
 // RendererVk implementation.
 RendererVk::RendererVk()
     : mCapsInitialized(false),
@@ -737,7 +743,8 @@
     fenceInfo.pNext = nullptr;
     fenceInfo.flags = 0;
 
-    CommandBatch batch;
+    vk::Scoped<CommandBatch> scopedBatch(mDevice);
+    CommandBatch &batch = scopedBatch.get();
     ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
 
     ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
@@ -746,7 +753,7 @@
     batch.commandPool = std::move(mCommandPool);
     batch.serial      = mCurrentQueueSerial;
 
-    mInFlightCommands.emplace_back(std::move(batch));
+    mInFlightCommands.emplace_back(scopedBatch.release());
 
     // Sanity check.
     ASSERT(mInFlightCommands.size() < 1000u);
@@ -818,8 +825,8 @@
                             const vk::Semaphore &waitSemaphore,
                             const vk::Semaphore &signalSemaphore)
 {
-    vk::CommandBuffer commandBatch;
-    ANGLE_TRY(flushCommandGraph(context, &commandBatch));
+    vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
+    ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
 
     VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
 
@@ -830,11 +837,11 @@
     submitInfo.pWaitSemaphores      = waitSemaphore.ptr();
     submitInfo.pWaitDstStageMask    = &waitStageMask;
     submitInfo.commandBufferCount   = 1;
-    submitInfo.pCommandBuffers      = commandBatch.ptr();
+    submitInfo.pCommandBuffers      = commandBatch.get().ptr();
     submitInfo.signalSemaphoreCount = 1;
     submitInfo.pSignalSemaphores    = signalSemaphore.ptr();
 
-    ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
+    ANGLE_TRY(submitFrame(submitInfo, commandBatch.release()));
     return vk::NoError();
 }