[Vulkan] Basic Vulkan backend
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-command-pool.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // INTERNAL INCLUDES
19 #include <dali/graphics/vulkan/vulkan-graphics.h>
20 #include <dali/graphics/vulkan/vulkan-command-pool.h>
21 #include <dali/graphics/vulkan/vulkan-command-buffer.h>
22
23 namespace Dali
24 {
25 namespace Graphics
26 {
27 namespace Vulkan
28 {
29
30 /**
31  *
32  * Class: CommandPool::Impl
33  */
34
35 struct CommandPool::Impl
36 {
37   Impl( Graphics& graphics, CommandPool& interface, const vk::CommandPoolCreateInfo& createInfo )
38   : mGraphics( graphics ),
39     mInterface( interface ),
40     mCreateInfo( createInfo ),
41     mCommandPool( nullptr )
42   {
43   }
44
45   ~Impl()
46   {
47     if(mCommandPool)
48     {
49       mGraphics.GetDevice().destroyCommandPool( mCommandPool, mGraphics.GetAllocator());
50     }
51   }
52
53   bool Initialise()
54   {
55     mCommandPool = VkAssert(mGraphics.GetDevice().createCommandPool(mCreateInfo, mGraphics.GetAllocator()));
56     return true;
57   }
58
59   Handle<CommandBuffer> NewCommandBuffer( const vk::CommandBufferAllocateInfo& allocateInfo )
60   {
61     vk::CommandBufferAllocateInfo info( allocateInfo );
62     info.setCommandPool( mCommandPool );
63     info.setCommandBufferCount(1);
64     auto retval = VkAssert( mGraphics.GetDevice().allocateCommandBuffers( info ) );
65     mAllocatedCommandBuffers.emplace_back( new CommandBuffer( mInterface, info, retval[0]) );
66     return mAllocatedCommandBuffers.back();
67   }
68
69   Graphics& mGraphics;
70   CommandPool& mInterface;
71   vk::CommandPoolCreateInfo mCreateInfo;
72   vk::CommandPool mCommandPool;
73
74   std::vector<Handle<CommandBuffer>> mAllocatedCommandBuffers;
75 };
76
77 /**
78  *
79  * Class: CommandPool
80  */
81 CommandPoolHandle CommandPool::New( Graphics& graphics, const vk::CommandPoolCreateInfo& createInfo )
82 {
83   auto retval = Handle<CommandPool>( new CommandPool(graphics, createInfo) );
84
85   if(retval && retval->mImpl->Initialise())
86   {
87     graphics.AddCommandPool( retval );
88   }
89
90   return retval;
91 }
92
93 CommandPoolHandle CommandPool::New( Graphics& graphics )
94 {
95   return New( graphics, vk::CommandPoolCreateInfo{});
96 }
97
98 CommandPool::CommandPool() = default;
99
100 CommandPool::CommandPool(Graphics& graphics, const vk::CommandPoolCreateInfo& createInfo)
101 {
102   mImpl = MakeUnique<Impl>( graphics, *this, createInfo );
103 }
104
105 CommandPool::~CommandPool() = default;
106
107 vk::CommandPool CommandPool::GetPool() const
108 {
109   return mImpl->mCommandPool;
110 }
111
112 Graphics& CommandPool::GetGraphics() const
113 {
114   return mImpl->mGraphics;
115 }
116
117 bool CommandPool::OnDestroy()
118 {
119   return false;
120 }
121
122 Handle<CommandBuffer> CommandPool::NewCommandBuffer( const vk::CommandBufferAllocateInfo& allocateInfo )
123 {
124   return mImpl->NewCommandBuffer( allocateInfo );
125 }
126
127 Handle<CommandBuffer> CommandPool::NewCommandBuffer( bool isPrimary )
128 {
129   return mImpl->NewCommandBuffer( vk::CommandBufferAllocateInfo{}.setLevel(
130     isPrimary ? vk::CommandBufferLevel::ePrimary : vk::CommandBufferLevel::eSecondary
131   ) );
132 }
133
134
135 } // namespace Vulkan
136 } // namespace Graphics
137 } // namespace Dali
138