4cb64d86844f8c40d00f47471c019e3eb48bf637
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-graphics.h
1 #ifndef DALI_GRAPHICS_VULKAN_GRAPHICS
2 #define DALI_GRAPHICS_VULKAN_GRAPHICS
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #ifndef VULKAN_HPP_NO_EXCEPTIONS
22 #define VULKAN_HPP_NO_EXCEPTIONS
23 #endif
24
25 // INTERNAL INCLUDES
26 #include <dali/graphics/vulkan/vulkan-types.h>
27 #include <dali/integration-api/graphics/surface-factory.h>
28 #include <dali/graphics/vulkan/vulkan-swapchain.h>
29 #include <thread>
30 #include <mutex>
31 #include <map>
32 #include <functional>
33
34 namespace Dali
35 {
36 namespace Graphics
37 {
38 namespace API
39 {
40 class Controller;
41 }
42
43 namespace VulkanAPI
44 {
45 class Controller;
46 }
47
48 namespace Vulkan
49 {
50
51 class Buffer;
52 class Image;
53 class Pipeline;
54 class Shader;
55 class Framebuffer;
56 class Surface;
57 class CommandPool;
58 class DescriptorPool;
59 class GpuMemoryManager;
60 class PipelineCache;
61 class ResourceCache;
62
63 using SurfaceFactory = Dali::Integration::Graphics::SurfaceFactory;
64
65 struct SwapchainSurfacePair
66 {
67   RefCountedSwapchain swapchain;
68   RefCountedSurface surface;
69 };
70
71 class Graphics
72 {
73
74 public:
75   Graphics();
76   Graphics(const Graphics&) = delete;
77   Graphics& operator=(const Graphics&) = delete;
78   ~Graphics();
79
80 public: // Create methods
81
82   void                                    Create();
83   void                                    CreateDevice();
84   FBID                                    CreateSurface(std::unique_ptr< SurfaceFactory > surfaceFactory);
85   RefCountedSwapchain                     CreateSwapchainForSurface( RefCountedSurface surface );
86   RefCountedShader                        CreateShader(); //will see if this will work
87   RefCountedPipeline                      CreatePipeline();
88   RefCountedFence                         CreateFence( const vk::FenceCreateInfo& fenceCreateInfo );
89   RefCountedBuffer                        CreateBuffer( size_t size, BufferType type );
90   RefCountedBuffer                        CreateBuffer( const vk::BufferCreateInfo& bufferCreateInfo );
91   RefCountedFramebuffer                   CreateFramebuffer();
92   RefCountedImage                         CreateImage();
93   RefCountedImageView                     CreateImageView(const vk::ImageViewCreateFlags& flags,
94                                                           const RefCountedImage& image,
95                                                           vk::ImageViewType viewType,
96                                                           vk::Format format,
97                                                           vk::ComponentMapping components,
98                                                           vk::ImageSubresourceRange subresourceRange);
99   RefCountedImageView                     CreateImageView(RefCountedImage image);
100   RefCountedDescriptorPool                CreateDescriptorPool();
101   RefCountedCommandPool                   CreateCommandPool(const vk::CommandPoolCreateInfo& info);
102   RefCountedCommandBuffer                 CreateCommandBuffer();
103   std::vector< RefCountedCommandBuffer >  CreateCommandBuffers();
104   RefCountedGpuMemoryBlock                CreateGpuMemoryBlock();
105   RefCountedDescriptorSet                 CreateDescriptorSet();
106   RefCountedSampler                       CreateSampler();
107
108 public: // Actions
109   vk::Result WaitForFence( RefCountedFence fence, uint32_t timeout = 0 );
110   vk::Result WaitForFences( const std::vector< RefCountedFence >& fences,
111                             bool waitAll = true,
112                             uint32_t timeout = std::numeric_limits< uint32_t >::max() );
113   vk::Result ResetFence( RefCountedFence fence );
114   vk::Result ResetFences( const std::vector< RefCountedFence >& fences );
115
116 public: // Getters
117   RefCountedSurface GetSurface( FBID surfaceId );
118
119   RefCountedSwapchain GetSwapchainForSurface( RefCountedSurface surface );
120
121   RefCountedSwapchain GetSwapchainForFBID( FBID surfaceId );
122
123   vk::Device GetDevice() const;
124
125   vk::PhysicalDevice GetPhysicalDevice() const;
126
127   vk::Instance GetInstance() const;
128
129   const vk::AllocationCallbacks& GetAllocator() const;
130
131   GpuMemoryManager& GetDeviceMemoryManager() const;
132
133   const vk::PhysicalDeviceMemoryProperties& GetMemoryProperties() const;
134
135   Queue& GetGraphicsQueue(uint32_t index = 0u) const;
136   Queue& GetTransferQueue(uint32_t index = 0u) const;
137   Queue& GetComputeQueue(uint32_t index = 0u) const;
138   Queue& GetPresentQueue() const;
139
140   Platform GetDefaultPlatform() const;
141
142   Dali::Graphics::API::Controller& GetController();
143
144   PipelineCache& GetPipelineCache();
145
146 public: //Cache management methods
147
148   void AddBuffer( RefCountedBuffer buffer );
149   void AddImage( RefCountedImage image );
150   void AddShader( RefCountedShader shader );
151   void AddCommandPool( RefCountedCommandPool pool );
152   void AddDescriptorPool( RefCountedDescriptorPool pool );
153   void AddFramebuffer( RefCountedFramebuffer framebuffer );
154
155   RefCountedShader FindShader( vk::ShaderModule shaderModule );
156   RefCountedImage FindImage( vk::Image image );
157
158   void RemoveBuffer( Buffer& buffer );
159   void RemoveShader( Shader& shader );
160   void RemoveCommandPool( CommandPool& commandPool );
161   void RemoveDescriptorPool( DescriptorPool& pool );
162   void RemoveFramebuffer( Framebuffer& framebuffer );
163   void RemoveSampler( Sampler& sampler );
164
165   void CollectGarbage();
166
167   void DiscardResource(std::function<void()> deleter);
168
169 private: // Methods
170
171   void                                      CreateInstance( const std::vector<const char*>& extensions,
172                                                             const std::vector<const char*>& validationLayers );
173   void                                      DestroyInstance();
174   void                                      PreparePhysicalDevice();
175   void                                      GetPhysicalDeviceProperties();
176   void                                      GetQueueFamilyProperties();
177   std::vector< vk::DeviceQueueCreateInfo >  GetQueueCreateInfos();
178   std::vector<const char*>                  PrepareDefaultInstanceExtensions();
179
180 private: // Members
181
182   std::unique_ptr<GpuMemoryManager>                           mDeviceMemoryManager;
183
184   vk::Instance                                                mInstance;
185   std::unique_ptr<vk::AllocationCallbacks>                    mAllocator{nullptr};
186
187   // physical device
188   vk::PhysicalDevice mPhysicalDevice;
189
190   // logical device
191   vk::Device                                                  mDevice;
192
193   // physical device properties
194   std::unique_ptr< vk::PhysicalDeviceProperties >             mPhysicalDeviceProperties;
195   std::unique_ptr< vk::PhysicalDeviceMemoryProperties >       mPhysicalDeviceMemoryProperties;
196   std::unique_ptr< vk::PhysicalDeviceFeatures >               mPhysicalDeviceFeatures;
197
198   // queue family properties
199   std::vector< vk::QueueFamilyProperties >                    mQueueFamilyProperties;
200
201   std::unordered_map< FBID, SwapchainSurfacePair >            mSurfaceFBIDMap;
202   FBID mBaseFBID{0u};
203
204   // Sets of queues
205   std::vector< std::unique_ptr<Queue> >                       mGraphicsQueues;
206   std::vector< std::unique_ptr<Queue> >                       mTransferQueues;
207   std::vector< std::unique_ptr<Queue> >                       mComputeQueues;
208   //std::unique_ptr< Queue > mPresentQueue;
209
210   Platform                                                    mPlatform  { Platform::UNDEFINED };
211
212   std::unique_ptr<Dali::Graphics::VulkanAPI::Controller>      mGfxController;
213
214   // TODO: rename
215   std::unique_ptr<PipelineCache>                              mPipelineDatabase;
216
217   std::mutex                                                  mMutex;
218   std::unique_ptr< ResourceCache >                            mResourceCache;
219
220 };
221
222 } // namespace Vulkan
223 } // namespace Graphics
224 } // namespace Dali
225
226 #endif // DALI_GRAPHICS_VULKAN_GRAPHICS