Vulkan: Refactor CommandBuffer helper class.

This makes the base vk::CommandBuffer class as close to the Vulkan
API as possible. It moves the extra functionality and state tracking
to a CommandBufferAndState helper class.

Also no longer stores a reference to a CommandPool in a CommandBuffer.
Eventually we won't need to free CommandBuffers explicitly, since they
can be freed en-masse by deallocating the CommandPool on a flush.

Bug: angleproject:2200
Change-Id: I5095fe89e8da935ff8273b1a402ccfd6a3ffe0df
Reviewed-on: https://chromium-review.googlesource.com/778184
Reviewed-by: Frank Henigman <fjhenigman@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/vulkan/RendererVk.cpp b/src/libANGLE/renderer/vulkan/RendererVk.cpp
index 1ec979b..7289f0d 100644
--- a/src/libANGLE/renderer/vulkan/RendererVk.cpp
+++ b/src/libANGLE/renderer/vulkan/RendererVk.cpp
@@ -120,7 +120,7 @@
 
     if (mCommandBuffer.valid())
     {
-        mCommandBuffer.destroy(mDevice);
+        mCommandBuffer.destroy(mDevice, mCommandPool);
     }
 
     if (mCommandPool.valid())
@@ -429,8 +429,6 @@
 
     ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
 
-    mCommandBuffer.setCommandPool(&mCommandPool);
-
     return vk::NoError();
 }
 
@@ -569,16 +567,16 @@
     return mNativeLimitations;
 }
 
-vk::Error RendererVk::getStartedCommandBuffer(vk::CommandBuffer **commandBufferOut)
+vk::Error RendererVk::getStartedCommandBuffer(vk::CommandBufferAndState **commandBufferOut)
 {
-    ANGLE_TRY(mCommandBuffer.begin(mDevice));
+    ANGLE_TRY(mCommandBuffer.ensureStarted(mDevice, mCommandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
     *commandBufferOut = &mCommandBuffer;
     return vk::NoError();
 }
 
-vk::Error RendererVk::submitCommandBuffer(vk::CommandBuffer *commandBuffer)
+vk::Error RendererVk::submitCommandBuffer(vk::CommandBufferAndState *commandBuffer)
 {
-    ANGLE_TRY(commandBuffer->end());
+    ANGLE_TRY(commandBuffer->ensureFinished());
 
     VkFenceCreateInfo fenceInfo;
     fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
@@ -602,7 +600,7 @@
     return vk::NoError();
 }
 
-vk::Error RendererVk::submitAndFinishCommandBuffer(vk::CommandBuffer *commandBuffer)
+vk::Error RendererVk::submitAndFinishCommandBuffer(vk::CommandBufferAndState *commandBuffer)
 {
     ANGLE_TRY(submitCommandBuffer(commandBuffer));
     ANGLE_TRY(finish());
@@ -610,7 +608,7 @@
     return vk::NoError();
 }
 
-vk::Error RendererVk::submitCommandsWithSync(vk::CommandBuffer *commandBuffer,
+vk::Error RendererVk::submitCommandsWithSync(vk::CommandBufferAndState *commandBuffer,
                                              const vk::Semaphore &waitSemaphore,
                                              const vk::Semaphore &signalSemaphore)
 {
@@ -647,13 +645,13 @@
 {
     for (auto &fence : mInFlightFences)
     {
-        fence.destroy(mDevice);
+        fence.get().destroy(mDevice);
     }
     mInFlightFences.clear();
 
     for (auto &command : mInFlightCommands)
     {
-        command.destroy(mDevice);
+        command.get().destroy(mDevice, mCommandPool);
     }
     mInFlightCommands.clear();
 
@@ -681,7 +679,7 @@
 
         // Release the fence handle.
         // TODO(jmadill): Re-use fences.
-        inFlightFence->destroy(mDevice);
+        inFlightFence->get().destroy(mDevice);
     }
 
     if (finishedIndex == 0)
@@ -698,7 +696,7 @@
             break;
 
         completedCBIndex = cbIndex + 1;
-        inFlightCB->destroy(mDevice);
+        inFlightCB->get().destroy(mDevice, mCommandPool);
     }
 
     if (completedCBIndex == 0)