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