• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/vulkan/vulkan_command_buffer.h"
6 
7 #include "flutter/vulkan/vulkan_proc_table.h"
8 
9 namespace vulkan {
10 
VulkanCommandBuffer(const VulkanProcTable & p_vk,const VulkanHandle<VkDevice> & device,const VulkanHandle<VkCommandPool> & pool)11 VulkanCommandBuffer::VulkanCommandBuffer(
12     const VulkanProcTable& p_vk,
13     const VulkanHandle<VkDevice>& device,
14     const VulkanHandle<VkCommandPool>& pool)
15     : vk(p_vk), device_(device), pool_(pool), valid_(false) {
16   const VkCommandBufferAllocateInfo allocate_info = {
17       .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
18       .pNext = nullptr,
19       .commandPool = pool_,
20       .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
21       .commandBufferCount = 1,
22   };
23 
24   VkCommandBuffer buffer = VK_NULL_HANDLE;
25 
26   if (VK_CALL_LOG_ERROR(vk.AllocateCommandBuffers(device_, &allocate_info,
27                                                   &buffer)) != VK_SUCCESS) {
28     FML_DLOG(INFO) << "Could not allocate command buffers.";
29     return;
30   }
31 
32   auto buffer_collect = [this](VkCommandBuffer buffer) {
33     vk.FreeCommandBuffers(device_, pool_, 1, &buffer);
34   };
35 
36   handle_ = {buffer, buffer_collect};
37 
38   valid_ = true;
39 }
40 
41 VulkanCommandBuffer::~VulkanCommandBuffer() = default;
42 
IsValid() const43 bool VulkanCommandBuffer::IsValid() const {
44   return valid_;
45 }
46 
Handle() const47 VkCommandBuffer VulkanCommandBuffer::Handle() const {
48   return handle_;
49 }
50 
Begin() const51 bool VulkanCommandBuffer::Begin() const {
52   const VkCommandBufferBeginInfo info{
53       .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
54       .pNext = nullptr,
55       .flags = 0,
56       .pInheritanceInfo = nullptr,
57   };
58 
59   return VK_CALL_LOG_ERROR(vk.BeginCommandBuffer(handle_, &info)) == VK_SUCCESS;
60 }
61 
End() const62 bool VulkanCommandBuffer::End() const {
63   return VK_CALL_LOG_ERROR(vk.EndCommandBuffer(handle_)) == VK_SUCCESS;
64 }
65 
InsertPipelineBarrier(VkPipelineStageFlagBits src_stage_flags,VkPipelineStageFlagBits dest_stage_flags,uint32_t dependency_flags,uint32_t memory_barrier_count,const VkMemoryBarrier * memory_barriers,uint32_t buffer_memory_barrier_count,const VkBufferMemoryBarrier * buffer_memory_barriers,uint32_t image_memory_barrier_count,const VkImageMemoryBarrier * image_memory_barriers) const66 bool VulkanCommandBuffer::InsertPipelineBarrier(
67     VkPipelineStageFlagBits src_stage_flags,
68     VkPipelineStageFlagBits dest_stage_flags,
69     uint32_t /* mask of VkDependencyFlagBits */ dependency_flags,
70     uint32_t memory_barrier_count,
71     const VkMemoryBarrier* memory_barriers,
72     uint32_t buffer_memory_barrier_count,
73     const VkBufferMemoryBarrier* buffer_memory_barriers,
74     uint32_t image_memory_barrier_count,
75     const VkImageMemoryBarrier* image_memory_barriers) const {
76   vk.CmdPipelineBarrier(handle_, src_stage_flags, dest_stage_flags,
77                         dependency_flags, memory_barrier_count, memory_barriers,
78                         buffer_memory_barrier_count, buffer_memory_barriers,
79                         image_memory_barrier_count, image_memory_barriers);
80   return true;
81 }
82 
83 }  // namespace vulkan
84