c39bfd60c11844b939bcc350b64cb24eef2ae2b5
[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 namespace Dali
30 {
31 namespace Graphics
32 {
33 using SurfaceFactory = Dali::Integration::Graphics::SurfaceFactory;
34 namespace API
35 {
36 class Controller;
37 }
38 namespace Vulkan
39 {
40
41 enum class Platform
42 {
43   UNDEFINED,
44   XLIB,
45   XCB,
46   WAYLAND,
47 };
48
49 class Buffer;
50 class Image;
51 class Pipeline;
52 class Shader;
53 class Framebuffer;
54 class Surface;
55 class CommandPool;
56 class DescriptorPool;
57 class GpuMemoryManager;
58 class PipelineCache;
59
60 struct SwapchainSurfacePair
61 {
62   SwapchainRef swapchain;
63   SurfaceRef surface;
64 };
65
66 class Graphics
67 {
68
69 public:
70   Graphics();
71   Graphics(std::unique_ptr< SurfaceFactory > surfaceFactory);
72   Graphics(const Graphics&) = delete;
73   Graphics& operator=(const Graphics&) = delete;
74   ~Graphics();
75
76   void Create();
77
78   // new way
79   FBID CreateSurface(std::unique_ptr< SurfaceFactory > surfaceFactory);
80
81   SwapchainRef CreateSwapchainForSurface( SurfaceRef surface );
82
83   SurfaceRef GetSurface( FBID surfaceId );
84
85   SwapchainRef GetSwapchainForSurface( SurfaceRef surface );
86
87   SwapchainRef GetSwapchainForFBID( FBID surfaceId );
88
89   void CreateDevice();
90
91   /** Creates new command pool */
92   CommandPoolRef CreateCommandPool(const vk::CommandPoolCreateInfo& info);
93
94   vk::Device GetDevice() const;
95
96   vk::PhysicalDevice GetPhysicalDevice() const;
97
98   vk::Instance GetInstance() const;
99
100   const vk::AllocationCallbacks& GetAllocator() const;
101
102   GpuMemoryManager& GetDeviceMemoryManager() const
103   {
104     return *mDeviceMemoryManager;
105   }
106
107   const vk::PhysicalDeviceMemoryProperties& GetMemoryProperties() const
108   {
109     return *mPhysicalDeviceMemoryProperties;
110   }
111
112   Queue& GetGraphicsQueue(uint32_t index = 0u) const;
113   Queue& GetTransferQueue(uint32_t index = 0u) const;
114   Queue& GetComputeQueue(uint32_t index = 0u) const;
115   Queue& GetPresentQueue() const;
116
117   Platform GetDefaultPlatform() const;
118
119   Dali::Graphics::API::Controller& GetController();
120
121 private:
122
123   void                                     CreateInstance( const std::vector<const char*>& extensions, const std::vector<const char*>& validationLayers );
124   void                                     DestroyInstance();
125   void                                     PreparePhysicalDevice();
126   void                                     GetPhysicalDeviceProperties();
127   void                                     GetQueueFamilyProperties();
128   std::vector< vk::DeviceQueueCreateInfo > GetQueueCreateInfos();
129   std::vector<const char*>                 PrepareDefaultInstanceExtensions();
130
131 private:
132
133   std::unique_ptr<GpuMemoryManager> mDeviceMemoryManager;
134
135   vk::Instance             mInstance;
136   std::unique_ptr<vk::AllocationCallbacks> mAllocator{nullptr};
137
138   // physical device
139   vk::PhysicalDevice mPhysicalDevice;
140
141   // logical device
142   vk::Device mDevice;
143
144   // physical device properites
145   std::unique_ptr< vk::PhysicalDeviceProperties >       mPhysicalDeviceProperties;
146   std::unique_ptr< vk::PhysicalDeviceMemoryProperties > mPhysicalDeviceMemoryProperties;
147   std::unique_ptr< vk::PhysicalDeviceFeatures >         mPhysicalDeviceFeatures;
148
149   // queue family properties
150   std::vector< vk::QueueFamilyProperties > mQueueFamilyProperties;
151
152   std::unordered_map< FBID, SwapchainSurfacePair > mSurfaceFBIDMap;
153   FBID mBaseFBID{0u};
154
155   // Sets of queues
156   std::vector< std::unique_ptr<Queue> >  mGraphicsQueues;
157   std::vector< std::unique_ptr<Queue> >  mTransferQueues;
158   std::vector< std::unique_ptr<Queue> >  mComputeQueues;
159   //std::unique_ptr< Queue > mPresentQueue;
160
161   Platform                               mPlatform  { Platform::UNDEFINED };
162
163 public:
164   // TODO: all this stuff should go into some vulkan cache
165
166   void AddBuffer( BufferRef buffer );
167   void AddImage( ImageRef image );
168   void AddPipeline( PipelineRef pipeline );
169   void AddShader( ShaderRef shader );
170   void AddCommandPool( CommandPoolRef pool );
171   void AddDescriptorPool( DescriptorPoolRef pool );
172   void AddFramebuffer( FramebufferRef framebuffer );
173
174   ShaderRef FindShader( vk::ShaderModule shaderModule );
175   ImageRef FindImage( vk::Image image );
176
177   void RemoveBuffer( Buffer& buffer );
178   void RemoveShader( Shader& shader );
179   void RemoveCommandPool( CommandPool& commandPool );
180   void RemoveDescriptorPool( std::unique_ptr<DescriptorPool> pool );
181
182 private:
183   std::vector<BufferRef>                mBuffersCache;
184   std::vector<ImageRef>                 mImageCache;
185   std::vector<PipelineRef>              mPipelineCache;
186   std::vector<ShaderRef>                mShaderCache;
187   std::vector<CommandPoolRef>           mCommandPoolCache;
188   std::vector<DescriptorPoolRef>        mDescriptorPoolCache;
189   std::vector<FramebufferRef>           mFramebufferCache;
190
191 private:
192   std::unique_ptr<Dali::Graphics::API::Controller>           mGfxController;
193
194   // TODO: rename
195   std::unique_ptr<PipelineCache>        mPipelineDatabase;
196
197 public:
198   PipelineCache& GetPipelineCache()
199   {
200     return *mPipelineDatabase.get();
201   }
202 };
203
204 } // namespace Vulkan
205 } // namespace Graphics
206 } // namespace Dali
207
208 #endif // DALI_GRAPHICS_VULKAN_GRAPHICS