[Vulkan] Image and Swapchain upgrade
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-types.h
1 #ifndef DALI_GRAPHICS_VULKAN_TYPES
2 #define DALI_GRAPHICS_VULKAN_TYPES
3
4 /*
5  * Copyright (c) 2018 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 // EXTERNAL INCLUDES
22 #include <atomic>
23 #include <unordered_map>
24 #include <memory>
25
26 #include <dali/integration-api/graphics/vulkan/vulkan-hpp-wrapper.h>
27
28 namespace Dali
29 {
30 namespace Graphics
31 {
32
33 template< typename T, typename... Args >
34 std::unique_ptr< T > MakeUnique(Args&&... args)
35 {
36   return std::unique_ptr< T  >(new T(std::forward< Args >(args)...));
37 }
38
39 namespace Vulkan
40 {
41
42 /**
43  * Forward class declarations
44  */
45 class Graphics;
46 class Surface;
47 class Queue;
48
49 /**
50  * Unique pointers to Vulkan types
51  */
52 using UniqueQueue         = std::unique_ptr< Queue >;
53
54 /**
55  * Reference wrappers
56  */
57 using QueueRef         = std::reference_wrapper< Queue >;
58
59 template< typename T >
60 T VkAssert(const vk::ResultValue< T >& result, vk::Result expected = vk::Result::eSuccess)
61 {
62   assert(result.result == expected);
63   return result.value;
64 }
65
66 inline vk::Result VkAssert(vk::Result result, vk::Result expected = vk::Result::eSuccess)
67 {
68   assert(result == expected);
69   return result;
70 }
71
72 inline vk::Result VkTest(vk::Result result, vk::Result expected = vk::Result::eSuccess)
73 {
74   // todo: log if result different than expected?
75   return result;
76 }
77
78 template< typename T >
79 inline uint32_t U32(T value)
80 {
81   return static_cast< uint32_t >(value);
82 }
83
84 class Resource
85 {
86 public:
87   Resource() : mUserCount{0u} {}
88   virtual ~Resource() = default;
89
90   void IncreaseUserCount()
91   {
92     ++mUserCount;
93   }
94
95   void DecreaseUserCount()
96   {
97     --mUserCount;
98   }
99
100   uint32_t GetUserCount() const
101   {
102     return mUserCount;
103   }
104
105 private:
106
107   std::atomic<uint32_t> mUserCount;
108 };
109
110 /**
111  * Vulkan object handle
112  * @tparam T
113  */
114 template<class T>
115 class Handle
116 {
117 public:
118
119   Handle();
120   explicit Handle(T* object );
121   Handle( const Handle& handle);
122   Handle& operator=( const Handle& handle );
123   Handle& operator=( Handle&& handle );
124   Handle( Handle&& handle ) noexcept;
125   ~Handle();
126
127   operator bool() const;
128
129   T* operator->() const
130   {
131     return mObject;
132   }
133
134   uint32_t GetRefCount() const
135   {
136     return mObject->GetRefCount();
137   }
138
139   T& operator*() const
140   {
141     return *mObject;
142   }
143
144   template <class K>
145   Handle<K> StaticCast()
146   {
147     return Handle<K>(static_cast<K*>(mObject));
148   }
149
150   template<class K>
151   bool operator==( const Handle<K>& object ) const
152   {
153     return mObject == &*object;
154   }
155
156   template <class K>
157   Handle<K> DynamicCast();
158
159   void Reset()
160   {
161     if( mObject )
162     {
163       mObject->Release();
164       mObject = nullptr;
165     }
166   }
167
168 private:
169
170   T* mObject { nullptr };
171 };
172
173 template <class K, class T>
174 static Handle<K> VkTypeCast( const Handle<T>& inval )
175 {
176   return Handle<K>(static_cast<K*>(&*inval));
177 }
178
179 template<class T>
180 Handle<T>::Handle(T* object)
181   : mObject( object )
182 {
183   if(mObject)
184   {
185     mObject->Retain();
186   }
187 }
188
189 template<class T>
190 Handle<T>::Handle()
191   : mObject( nullptr )
192 {
193 }
194
195 template<class T>
196 Handle<T>::Handle(const Handle& handle)
197 {
198   mObject = handle.mObject;
199   if(mObject)
200   {
201     mObject->Retain();
202   }
203 }
204
205 template<class T>
206 Handle<T>::Handle( Handle&& handle ) noexcept
207 {
208   mObject = handle.mObject;
209   handle.mObject = nullptr;
210 }
211
212 template<class T>
213 Handle<T>::operator bool() const
214 {
215   return mObject != nullptr;
216 }
217
218 template<class T>
219 Handle<T>& Handle<T>::operator=( Handle&& handle )
220 {
221   mObject = handle.mObject;
222   handle.mObject = nullptr;
223   return *this;
224 }
225
226 template<class T>
227 Handle<T>& Handle<T>::operator=( const Handle<T>& handle )
228 {
229   mObject = handle.mObject;
230   if(mObject)
231   {
232     mObject->Retain();
233   }
234   return *this;
235 }
236
237 template<class T>
238 Handle<T>::~Handle()
239 {
240   if(mObject)
241   {
242     mObject->Release();
243   }
244 }
245
246 template<class T>
247 template<class K>
248 Handle<K> Handle<T>::DynamicCast()
249 {
250   auto val = dynamic_cast<K*>(mObject);
251   if(val)
252   {
253     return Handle<K>(val);
254   }
255   return Handle<K>();
256 }
257
258 template< typename T, typename... Args >
259 Handle< T > MakeRef(Args&&... args)
260 {
261   return Handle< T >(new T(std::forward< Args >(args)...));
262 }
263
264 template< typename T, typename... Args >
265 Handle< T > NewRef(Args&&... args)
266 {
267   return Handle< T >(T::New(std::forward< Args >(args)...));
268 }
269
270
271 template<class T>
272 typename T::Impl& GetImpl( Handle<T>& object )
273 {
274   return static_cast<typename T::Impl&>(*object->mImpl);
275 }
276
277 class VkManaged
278 {
279 public:
280
281   VkManaged() = default;
282   virtual ~VkManaged() = default;
283
284   void Release()
285   {
286     OnRelease(--mRefCount);
287     if(mRefCount == 0)
288     {
289       // orphaned
290       if(!Destroy())
291       {
292         delete this;
293       }
294     }
295   }
296
297   void Retain()
298   {
299     OnRetain(++mRefCount);
300   }
301
302   uint32_t GetRefCount()
303   {
304     return mRefCount;
305   }
306
307   bool Destroy()
308   {
309     return OnDestroy();
310   }
311
312   virtual void OnRetain( uint32_t refcount ) {};
313
314   virtual void OnRelease( uint32_t refcount ) {};
315
316   virtual bool OnDestroy() { return false; };
317
318 private:
319
320   std::atomic_uint mRefCount { 0u };
321 };
322
323 using FBID = int32_t;
324
325 #define NotImplemented() \
326 {\
327 printf("Function %s isn't implemented!\n", __FUNCTION__);\
328 assert( "Function no implemented" );\
329 }
330
331 /*
332  * Forward declarations of reference types
333  */
334 using ShaderRef = Handle<class Shader>;
335 using PipelineRef = Handle<class Pipeline>;
336 using FenceRef = Handle<class Fence>;
337 using BufferRef = Handle<class Buffer>;
338 using FramebufferRef = Handle<class Framebuffer>;
339 using ImageRef = Handle<class Image>;
340 using ImageViewRef = Handle<class ImageView>;
341 using DescriptorPoolRef = Handle<class DescriptorPool>;
342 using CommandPoolRef = Handle<class CommandPool>;
343 using CommandBufferRef = Handle<class CommandBuffer>;
344 using GpuMemoryBlockRef = Handle<class GpuMemoryBlock>;
345 using DescriptorSetRef = Handle<class DescriptorSet>;
346 using SwapchainRef = Handle<class Swapchain>;
347 using SurfaceRef = Handle<class Surface>;
348
349 /*
350 #pragma GCC diagnostic push
351 #pragma GCC diagnostic ignored "-Wframe-larger-than="
352 #pragma GCC diagnostic pop
353 */
354 } // namespace Vulkan
355 } // namespace Graphics
356 } // namespace Dali
357
358 #endif // DALI_GRAPHICS_VULKAN_TYPES