c45d2d5b1ad2aab4329168a909ddb0e81cb83767
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-surface.h
1 #ifndef DALI_GRAPHICS_VULKAN_SURFACE_H
2 #define DALI_GRAPHICS_VULKAN_SURFACE_H
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 // INTERNAL INCLUDES
22 #include <dali/graphics/vulkan/vulkan-types.h>
23
24 namespace Dali
25 {
26 namespace Graphics
27 {
28 namespace Vulkan
29 {
30
31 /**
32  * Vulkan surface is coupled with swapchain -> one swapchain per surface
33  * Swapchain won't exist until surface is used in a such way
34  *
35  */
36 class Graphics;
37 class CommandBuffer;
38 class CommandPool;
39 class Surface;
40
41 using UniqueSurface       = std::unique_ptr< Surface >;
42 using UniqueCommandBuffer = std::unique_ptr< CommandBuffer >;
43 using UniqueCommandPool   = std::unique_ptr< CommandPool >;
44
45 // simple structure describing single image of swapchain
46 // non-copyable, only movable
47 struct SwapchainImage
48 {
49   SwapchainImage();
50   ~SwapchainImage();
51   SwapchainImage(const SwapchainImage&) = delete;
52   SwapchainImage(SwapchainImage&&)      = default;
53   SwapchainImage& operator=(const SwapchainImage&) = delete;
54   SwapchainImage& operator=(SwapchainImage&&) = default;
55
56   UniqueImage     image;
57   UniqueImageView imageView;
58   vk::Framebuffer framebuffer;
59   vk::ImageLayout layout;
60   vk::Semaphore   acqSem;
61   vk::Semaphore   presentSem;
62
63   // layout transitions, prerecorded command buffers
64   UniqueCommandBuffer layoutToColorCmdBuf;
65   UniqueCommandBuffer mainCmdBuf;
66 };
67
68 class Surface
69 {
70 public:
71   Surface(Graphics& graphics, vk::SurfaceKHR surface, uint32_t bufferCount = 2,
72           bool hasDepthStencil = false);
73   ~Surface();
74
75   /**
76    * Prepares new swapchain image
77    */
78   void AcquireNextImage();
79
80   /**
81    * Presents image
82    */
83   void Present();
84
85   /**
86    *
87    * @return
88    */
89   vk::RenderPass GetRenderPass() const;
90
91   /**
92    *
93    * @param index
94    * @return
95    */
96   vk::Framebuffer GetFramebuffer(uint32_t index = -1u) const;
97
98   /**
99    *
100    * @param index
101    * @return
102    */
103   ImageView& GetImageView(uint32_t index = -1u) const;
104
105   /**
106    *
107    * @param index
108    * @return
109    */
110   Image& GetImage(uint32_t index = -1u) const;
111
112   /**
113    *
114    * @return
115    */
116   vk::SurfaceKHR GetSurfaceKHR() const;
117
118 private:
119   void CreateSwapchain();
120
121   void CreateVulkanSwapchain();
122
123   void CreateImageView(SwapchainImage& swapImage);
124   void CreateFramebuffer(SwapchainImage& swapImage);
125   void CreateSemaphores(SwapchainImage& swapImage);
126
127   void DestroySwapchain();
128
129   void InitialiseSwapchain();
130
131   void InitialiseRenderPass();
132
133   void AddSwapchainImage(vk::Image image, std::vector< SwapchainImage >& swapchainImages);
134
135   void CreateCommandBuffers();
136
137   void CreateDepthStencil();
138
139   void DestroyDepthStencil();
140
141   void BeginRenderPass();
142
143   void EndRenderPass();
144
145   Graphics&        mGraphics;
146   vk::SurfaceKHR   mSurface;
147   vk::SwapchainKHR mSwapchain;
148
149   vk::Format       mDepthStencilFormat{vk::Format::eD16UnormS8Uint};
150   vk::Image        mDepthStencilImage;
151   vk::ImageView    mDepthStencilImageView;
152   vk::DeviceMemory mDepthStencilMemory;
153
154   UniqueCommandPool mCommandPool;
155
156   vk::Format        mFormat;
157   vk::ColorSpaceKHR mColorSpace;
158   vk::Extent2D      mExtent;
159
160   std::vector< SwapchainImage >                 mSwapImages;
161   std::unique_ptr< vk::SurfaceCapabilitiesKHR > mCapabilities;
162
163   UniqueFence mFrameFence;
164
165   vk::RenderPass mDefaultRenderPass;
166   uint32_t       mBufferCount;
167   uint32_t       mCurrentBufferIndex;
168   bool           mHasDepthStencil;
169 };
170
171 } // namespace Vulkan
172 } // namespace Graphics
173 } // namespace Dali
174
175 #endif // DALI_GRAPHICS_VULKAN_SURFACE_H