[Vulkan] Managed command buffer pool
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-command-buffer.cpp
index d7ef0d1..db38136 100644 (file)
@@ -36,9 +36,11 @@ namespace Vulkan
 {
 struct CommandBuffer::Impl
 {
-  Impl( CommandPool& commandPool, const vk::CommandBufferAllocateInfo& allocateInfo, vk::CommandBuffer commandBuffer )
-  : mGraphics( commandPool.GetGraphics() ),
+  Impl( CommandBuffer& owner, CommandPool& commandPool, uint32_t poolIndex, const vk::CommandBufferAllocateInfo& allocateInfo, vk::CommandBuffer commandBuffer )
+  : mOwner( owner ),
+    mGraphics( commandPool.GetGraphics() ),
     mOwnerCommandPool( commandPool ),
+    mPoolAllocationIndex( poolIndex ),
     mAllocateInfo( allocateInfo ),
     mCommandBuffer( commandBuffer )
   {
@@ -46,11 +48,18 @@ struct CommandBuffer::Impl
 
   ~Impl()
   {
-    mResources.clear();
     mGraphics.GetDevice().freeCommandBuffers( mOwnerCommandPool.GetPool(),
                                               1, &mCommandBuffer );
   }
 
+  void ReleaseCommandBuffer()
+  {
+    mResources.clear();
+
+    // tell pool the buffer is not in use anymore
+    mOwnerCommandPool.ReleaseCommandBuffer(mOwner, false);
+  }
+
   bool Initialise()
   {
     return true;
@@ -320,9 +329,10 @@ struct CommandBuffer::Impl
          .setSubresourceRange( vk::ImageSubresourceRange{ aspectMask, 0, image->GetLevelCount(), 0, image->GetLayerCount() } );
   }
 
-
+  CommandBuffer&                mOwner;
   Graphics&                     mGraphics;
   CommandPool&                  mOwnerCommandPool;
+  uint32_t                      mPoolAllocationIndex;
   vk::CommandBufferAllocateInfo mAllocateInfo{};
 
   vk::CommandBuffer mCommandBuffer{};
@@ -350,10 +360,11 @@ struct CommandBuffer::Impl
  */
 
 CommandBuffer::CommandBuffer( CommandPool&                         commandPool,
+                              uint32_t                             poolIndex,
                               const vk::CommandBufferAllocateInfo& allocateInfo,
                               vk::CommandBuffer                    vkCommandBuffer )
 {
-  mImpl = MakeUnique<Impl>( commandPool, allocateInfo, vkCommandBuffer );
+  mImpl = MakeUnique<Impl>( *this, commandPool, poolIndex, allocateInfo, vkCommandBuffer );
 }
 
 CommandBuffer::~CommandBuffer()
@@ -384,11 +395,6 @@ void CommandBuffer::Free()
   mImpl->Free();
 }
 
-void CommandBuffer::OnRelease( uint32_t refcount )
-{
-  VkManaged::OnRelease( refcount );
-}
-
 /** Push wait semaphores */
 void CommandBuffer::PushWaitSemaphores( const std::vector<vk::Semaphore>&          semaphores,
                                         const std::vector<vk::PipelineStageFlags>& stages )
@@ -621,6 +627,17 @@ vk::ImageMemoryBarrier CommandBuffer::ImageLayoutTransitionBarrier( ImageRef ima
                                               aspectMask  );
 }
 
+uint32_t CommandBuffer::GetPoolAllocationIndex() const
+{
+  return mImpl->mPoolAllocationIndex;
+}
+
+bool CommandBuffer::OnDestroy()
+{
+  mImpl->ReleaseCommandBuffer();
+  return true;
+}
+
 } // namespace Vulkan
 } // namespace Graphics
 } // namespace Dali